如何使用libgit2中的mempack?

问题描述 投票:0回答:1

有一个函数

git_mempack_dump()
将一些内存中的对象放入缓冲区(来自代码注释):

packfile 的内容将存储在给定的缓冲区中。 调用者有责任确保生成的 packfile 可用于存储库

但我找不到任何微妙的方法来将缓冲区的内容放入存储库默认 ODB 内的磁盘 - 使用 libgit2 API。

我是否遗漏了什么,或者只是尚未实现?

c git libgit2
1个回答
0
投票

git_mempack_dump
准备一个可以用ODB写入的pack文件。 比如:

bool WriteMempack(git_repository* repo, git_odb_backend* mempacker)
{
    // Dump mepack into buffer
    git_buf buf = GIT_BUF_INIT;
    int error = git_mempack_dump(&buf, repo, mempacker);
    if (GIT_OK != error)
        return false;

    // Get repo's ODB
    git_odb* odb = nullptr;
    error = git_repository_odb(&odb, m_Repo);
    if (GIT_OK != error)
        return false;

    // Initialize the write pack operation
    git_odb_writepack* writepack = nullptr;
    error = git_odb_write_pack(&writepack, odb, nullptr, nullptr);
    if (GIT_OK != error)
    {
        git_odb_free(odb);
        return false;
    }

    // Write the buffer as a PACK file into the ODB
    git_indexer_progress progress;
    error = writepack->append(writepack, buf.ptr, buf.size, &progress);
    if (GIT_OK != error)
    {
        writepack->free(writepack);
        git_odb_free(odb);
        return false;
    }

    // Finalize the write pack operation
    error = writepack->commit(writepack, &progress);
    if (GIT_OK != error)
    {
        writepack->free(writepack);
        git_odb_free(odb);
        return false;
    }

    // Reset the mempacker
    error = git_mempack_reset(mempacker);

    // Cleanup
    writepack->free(writepack);
    git_odb_free(odb);

    return GIT_OK != error;
}
© www.soinside.com 2019 - 2024. All rights reserved.