I want to switch my cloned repository to some specified version. When I run this code, its not working as I want. It realizes how many commits am I behind or above master, but it doesn't really switch the project.
For example, if I have version 1.0 with some txt document included, and version 1.1 without this txt document. Master is pointing to version 1.1
First I clone the whole repository, (destination folder doesn't contain txt document).
Then I perform this code and I want the txt document to appear in the destination folder.
When I try this:
Download a specific tag with Git
Its working and I want my code to do the same,
        git_libgit2_init();
        const char * REPO_PATH = path.c_str();
        git_repository * repo = nullptr;
        git_repository_open(&repo, REPO_PATH);
        git_reference *ref;
        git_reference_lookup(&ref, repo, "refs/heads/master");      // "refs/remotes/origin/HEAD"
        git_reference *new_ref;
        git_reference_lookup(&new_ref, repo, tag.c_str());
        git_revwalk *walker;
        git_revwalk_new(&walker, repo);
        git_revwalk_push_ref(walker, tag.c_str());
        git_oid id;
        git_revwalk_next(&id, walker);
        git_reference_set_target(&new_ref, ref, &id, NULL);
        if (0 != git_repository_set_head_detached(repo, &id)) cerr << "problem occured while detaching head" << endl;
        git_revwalk_free(walker);
        git_repository_free(repo);
        git_libgit2_shutdown();
I tried also something like this, but this fails at git_annotated_commit_from_ref()
There is my second implementation :
    git_libgit2_init();
    const char * REPO_PATH = path.c_str();
    git_repository * repo = nullptr;
    git_repository_open(&repo, REPO_PATH);
    git_reference *ref;
    git_reference_lookup(&ref, repo, tag.c_str());
    git_annotated_commit *out;
    if (0 != git_annotated_commit_from_ref(&out,repo,ref)) cerr << "error creating annotated commit" << endl;
    if (0 != git_repository_set_head_detached_from_annotated(repo, out)) cerr << "problem occured while detaching head" << endl;
    git_repository_free(repo);
    git_libgit2_shutdown();
 
     
    