c++ - 如何将 HEAD check out 到 libgit2 中的指定引用或标记?

标签 c++ libgit2

我想将我的程序降级到以前的版本。是否可以在 libgit2 中执行此操作?

这是我的代码

string path = "C://Local//Path//to//my//repo";

string tag = "refs/tags/v0.0.4";

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");

git_reference *new_ref;

git_reference_symbolic_set_target(&new_ref, ref,tag.c_str(),"message");


git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;

// how to specify options to use ref or tag?

git_checkout_head(repo, &opts);

git_repository_free(repo);
git_libgit2_shutdown();

最佳答案

我觉得我明白了

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, "message");

     git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;

    if (0!=git_repository_set_head_detached(repo,&id)) cerr<<"problem occured while detaching head"<< endl;

    if (0 != git_checkout_head(repo, &opts)) cerr << "problem checkout head" << endl;

    git_revwalk_free(walker);
    git_repository_free(repo);
    git_libgit2_shutdown();

关于c++ - 如何将 HEAD check out 到 libgit2 中的指定引用或标记?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43203967/

相关文章:

c - 尝试使用 git_clone 函数导致段错误

c++ - 使用 Boost Graph 编写图的连通分量

C++方法参数按引用传递-内存问题

c++ - undefined symbol - 符号查找错误

c++ - 使用 git init、fetch 和 checkout 克隆一个 git 存储库

LibGit2Sharp 相当于 git diff --stat

c++ - 为什么 (1UL <<53) 加上 1.0 不等于它自己?

c++ - C++中的类和虚函数

libgit2 - 如何创建历史图表

libgit2 - 如何使用libgit2运行 "git log filename"?