libgit2 - 需要帮助才能理解git_diff_foreach的工作原理

时间:2015-04-30 07:36:51

标签: libgit2

我使用的是libgit2 0.22.2,由mysql支持的odb / refdb。我正在测试git_diff_foreach函数,但不确定它是如何工作的,请指教!

下面是我的测试数据和代码,我把一个blob' README.md'在odb和commit中,内容是“承诺”。然后我把另一个同名的blob放入odb并提交,这次内容是“提交测试'

我试图将2树差异化以在3个回调(文件,块,行)中打印差异增量。但只有文件回调打印了一些东西

更新

我调试了foreach函数,在diff.h git_diff_file__resolve_zero_size函数中出现了错误。返回语句错误后弹出:

运行时检查失败#2 - 围绕变量' len'已经腐败了。

代码:

得到差异:

int error;
git_object *commit;
git_commit *parent = NULL;
error = git_revparse_single(&commit, repo, "HEAD^{commit}");
error = git_commit_parent(&parent, (git_commit *)commit, 0);

git_tree *commit_tree = NULL, *parent_tree = NULL;
error = git_commit_tree(&commit_tree, (git_commit *)commit);
error = git_commit_tree(&parent_tree, parent);

git_diff *diff = NULL;
error = git_diff_tree_to_tree(&diff, repo, commit_tree, parent_tree, NULL);

git_mysql_tree_diff(mysql, repo, diff);

git_object_free(commit);
git_commit_free(parent);
git_tree_free(commit_tree);
git_tree_free(parent_tree);
git_diff_free(diff);

迭代差异:

int each_file_cb(const git_diff_delta *delta,float progress,void *payload)
{
    printf("new file:%s \n", delta->new_file.path);
    printf("old file:%s \n", delta->old_file.path);

    return 0;
}

int each_hunk_cb(
    const git_diff_delta *delta,
    const git_diff_hunk *hunk,
    void *payload)
{

    printf("hunk header:%s \n", hunk->header);

    return 0;
}

int each_line_cb(
    const git_diff_delta *delta,
    const git_diff_hunk *hunk,
    const git_diff_line *line,
    void *payload)
{
    printf("line content:%s \n", line->content);

    return 0;
}

int git_mysql_tree_diff(git_diff *diff){
    int error;

    error = git_diff_foreach(diff,
        each_file_cb,
        each_hunk_cb,
        each_line_cb,
        NULL);

    return error;
}

数据:

# oid,                                     type, size, data
'92168310a2a8e8d6577028d97931fd104965f6c5', '2', '37', 100644 README.md �����؊�J��B���f�N
'9f4ee02f459f6a28fa7a129fde6653d971429336', '1', '248', tree 92168310a2a8e8d6577028d97931fd104965f6c5 parent ad6aa3d0fcecefa5e9ea250a069da4d3be1f8e3d author Jerry Jin <jerry.yang.jin@gmail.com> 1430376253 +0800 committer Jerry Jin <jerry.yang.jin@gmail.com> 1430376253 +0800 encoding UTF-8 update readme
'ad6aa3d0fcecefa5e9ea250a069da4d3be1f8e3d', '1', '201', tree af0f65dba4ddecf5f4faf889cbd2852c341958c7 author Jerry Jin <jerry.yang.jin@gmail.com> 1430376223 +0800 committer Jerry Jin <jerry.yang.jin@gmail.com> 1430376223 +0800 encoding UTF-8 Initial Commit
'af0f65dba4ddecf5f4faf889cbd2852c341958c7', '2', '37', 100644 README.md ��vX"��}���lW�`@�#
'f8a612e3f687d88a984acaf41142ba85a3668d4e', '3', '11', commit test
'fcad765822a69c7de41bb1db186c57b160409923', '3', '6', commit

输出:

file progress:0.000000

1 个答案:

答案 0 :(得分:0)

修复内存问题后,差异打印在下面

new file:README.md
old file:README.md

hunk header:@@ -1 +1 @@

line content:commit testd
line content:
\ No newline at end of file

line content:commit;
line content:
\ No newline at end of file