使用git init,fetch和checkout克隆git存储库

时间:2019-05-27 05:56:01

标签: c++ libgit2

简介

我正在使用用C语言编写的流行libgit2进行实验。
我正在尝试做一种clone,但是使用了一种不常见的方式。依次使用git命令:

  1. git init
  2. git remote add origin https://repository.git
  3. git fetch origin
  4. git checkout master

通过使用git bash和以下命令,我可以获得一个包含所有历史记录的现有存储库。

问题

现在,让我们看看我当前的C ++实现。以下代码试图复制先前编写的git命令的行为。

#define url         "https://repository.git"
#define path        "./"
#define user        "user"
#define pass        "pass"

/** credential callback **/
int credentials(git_cred **cred, const char *, const char *, unsigned int, void *) {
    return git_cred_userpass_plaintext_new(cred, user, pass);
}

class Git {
public:
    Git() {
        git_libgit2_init();
    }

    ~Git() {
        git_repository_free(repository);
        git_libgit2_shutdown();
    }

    void update() {
        init();
        fetch();
        checkout();
    }

private:
    void init() {
        assertSuccess(git_repository_init(&repository, path, GIT_CVAR_FALSE));

        git_remote *remote = nullptr;
        git_remote_callbacks options = GIT_REMOTE_CALLBACKS_INIT;

        assertSuccess(git_remote_create(&remote, repository, "origin", url));

        options.credentials = credentials;
        git_remote_connect(remote, GIT_DIRECTION_FETCH, &options, nullptr, nullptr);
    }

    void fetch() {
        git_remote* remote = nullptr;
        assertSuccess(git_remote_lookup(&remote, repository, "origin"));

        git_fetch_options options = GIT_FETCH_OPTIONS_INIT;
        options.callbacks.credentials = credentials;
        assertSuccess(git_remote_fetch(remote, nullptr, &options, nullptr));
    }

    void checkout() {
        git_checkout_options options = GIT_CHECKOUT_OPTIONS_INIT;
        options.checkout_strategy = GIT_CHECKOUT_FORCE;

        assertSuccess(git_checkout_head(repository, &options));

        assertSuccess(git_checkout_index(repository, nullptr, &options));

        assertSuccess(git_repository_set_head(repository, "refs/heads/master"));
        git_object *treeish = nullptr;
        assertSuccess(git_revparse_single(&treeish, repository, "master"));
        assertSuccess(git_checkout_tree(repository, treeish, &options));
    }

    void assertSuccess(int error) {
        if (!error) return;

        const git_error *e = giterr_last();
        std::cout << "code: " << e->klass << " error: " << e->message << std::endl;
        exit(1);
    }

private:
    git_repository *repository = nullptr;
};

int main() {
    Git git;
    git.update();
    return 0;
}

显然,这不起作用。运行此程序(调用Git().update()),在结帐步骤中出现以下错误:

code: 4 error: reference 'refs/heads/master' not found

已经创建了git存储库,通过git bash,我可以看到已成功设置的远程源。我可以从git checkout master开始做手册git bash,所以我猜我目前对checkout的实现是失败的。

有人可以向我强调这个错误吗?我找不到足够的资源,也找不到互联网上所有找到的示例的支持。

编辑
由于测试我的代码可能会有所帮助,所以让我给我的CMakeLists.txt编译libgit2。 (源代码https://github.com/libgit2/libgit2

cmake_minimum_required(VERSION 3.13)
project(test)

include_directories(libgit/include)
LINK_DIRECTORIES(${LIBSSH2_LIBRARY_DIRS})
add_subdirectory(libgit)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_BUILD_TYPE Release)

add_executable(test src/Git.h)
target_link_libraries(test git2)

1 个答案:

答案 0 :(得分:0)

缺少的链接是,由于您是从头开始构建存储库,因此您的存储库仍未出生(即,其HEAD指向不存在的refs/heads/master引用)。最重要的是,checkout在libgit2-land中唯一关心的是从ODB中导出文件,它不编写或更新引用。

因此,您错过了git checkout极有可能使用git update-ref来使master指向origin/master的OID的步骤,您可以这样做通过git_reference_create and friends

类似于以下内容(经过脑部编译):

static int setup_tracking_branch(char *branch_name, git_reference *upstream)
{
    git_reference *tracking;
    git_oid up_oid = git_reference_target_peel(upstream);
    char *ui_name;

#if 0
    /* should be constructed from `upstream`. IIRC there are some
     * git_reference accessors that can help
     * (eg. `refs/remotes/origin/heads/master` is `origin/master`).
     */
#else
    ui_name = "origin/master";
#endif

    if (git_reference_create_matching(&tracking,
                                      git_reference_owner(upstream),
                                      branch_name, up_oid, 0, NULL, "branch: created from %s", ui_name) < 0 ||
        git_branch_set_upstream(tracking, git_reference_name(upstream)) < 0) {
        printf("failed to create remote-tracking branch\n");
        return -1;
    }

cleanup:
    git_reference_free(tracking);

    return 0;
}

这使用您希望新分支为(-b)和要跟踪的远程分支(-t)的名称,尽管它显然不是完整的实现,甚至是正确的,所以YMMV。