Commit cedf9ca9 by Vicent Martí

tree: Kill the `git_tree_diff` functions

These are deprecated and replaced with the diffing code in git2/diff.h
parent 9bd5a99f
......@@ -313,40 +313,6 @@ enum git_treewalk_mode {
*/
GIT_EXTERN(int) git_tree_walk(git_tree *tree, git_treewalk_cb callback, int mode, void *payload);
typedef enum {
GIT_STATUS_ADDED = 1,
GIT_STATUS_DELETED = 2,
GIT_STATUS_MODIFIED = 3,
} git_status_t;
typedef struct {
unsigned int old_attr;
unsigned int new_attr;
git_oid old_oid;
git_oid new_oid;
git_status_t status;
const char *path;
} git_tree_diff_data;
typedef int (*git_tree_diff_cb)(const git_tree_diff_data *ptr, void *data);
/**
* Diff two trees
*
* Compare two trees. For each difference in the trees, the callback
* will be called with a git_tree_diff_data filled with the relevant
* information.
*
* @param old the "old" tree
* @param newer the "newer" tree
* @param cb callback
* @param data data to give to the callback
* @return GIT_SUCCESS or an error code
*/
GIT_EXTERN(int) git_tree_diff(git_tree *old, git_tree *newer, git_tree_diff_cb cb, void *data);
GIT_EXTERN(int) git_tree_diff_index_recursive(git_tree *tree, git_index *index, git_tree_diff_cb cb, void *data);
/** @} */
GIT_END_DECL
......
......@@ -780,273 +780,3 @@ int git_tree_walk(git_tree *tree, git_treewalk_cb callback, int mode, void *payl
return error;
}
static int tree_entry_cmp(const git_tree_entry *a, const git_tree_entry *b)
{
int ret;
ret = a->attr - b->attr;
if (ret != 0)
return ret;
return git_oid_cmp(&a->oid, &b->oid);
}
static void mark_del(git_tree_diff_data *diff, git_tree_entry *entry)
{
diff->old_attr = entry->attr;
git_oid_cpy(&diff->old_oid, &entry->oid);
diff->path = entry->filename;
diff->status |= GIT_STATUS_DELETED;
}
static void mark_add(git_tree_diff_data *diff, git_tree_entry *entry)
{
diff->new_attr = entry->attr;
git_oid_cpy(&diff->new_oid, &entry->oid);
diff->path = entry->filename;
diff->status |= GIT_STATUS_ADDED;
}
static int signal_additions(git_tree *tree, int start, int end, git_tree_diff_cb cb, void *data)
{
git_tree_diff_data diff;
git_tree_entry *entry;
int i;
if (end < 0)
end = git_tree_entrycount(tree);
for (i = start; i < end; ++i) {
memset(&diff, 0x0, sizeof(git_tree_diff_data));
entry = git_vector_get(&tree->entries, i);
mark_add(&diff, entry);
if (cb(&diff, data) < 0)
return -1;
}
return 0;
}
static int signal_addition(git_tree_entry *entry, git_tree_diff_cb cb, void *data)
{
git_tree_diff_data diff;
memset(&diff, 0x0, sizeof(git_tree_diff_data));
mark_add(&diff, entry);
return cb(&diff, data);
}
static int signal_deletions(git_tree *tree, int start, int end, git_tree_diff_cb cb, void *data)
{
git_tree_diff_data diff;
git_tree_entry *entry;
int i;
if (end < 0)
end = git_tree_entrycount(tree);
for (i = start; i < end; ++i) {
memset(&diff, 0x0, sizeof(git_tree_diff_data));
entry = git_vector_get(&tree->entries, i);
mark_del(&diff, entry);
if (cb(&diff, data) < 0)
return -1;
}
return 0;
}
static int signal_deletion(git_tree_entry *entry, git_tree_diff_cb cb, void *data)
{
git_tree_diff_data diff;
memset(&diff, 0x0, sizeof(git_tree_diff_data));
mark_del(&diff, entry);
return cb(&diff, data);
}
static int signal_modification(git_tree_entry *a, git_tree_entry *b,
git_tree_diff_cb cb, void *data)
{
git_tree_diff_data diff;
memset(&diff, 0x0, sizeof(git_tree_diff_data));
mark_del(&diff, a);
mark_add(&diff, b);
return cb(&diff, data);
}
int git_tree_diff(git_tree *a, git_tree *b, git_tree_diff_cb cb, void *data)
{
unsigned int i_a = 0, i_b = 0; /* Counters for trees a and b */
git_tree_entry *entry_a = NULL, *entry_b = NULL;
git_tree_diff_data diff;
int cmp;
while (1) {
entry_a = a == NULL ? NULL : git_vector_get(&a->entries, i_a);
entry_b = b == NULL ? NULL : git_vector_get(&b->entries, i_b);
if (!entry_a && !entry_b)
return 0;
memset(&diff, 0x0, sizeof(git_tree_diff_data));
/*
* We've run out of tree on one side so the rest of the
* entries on the tree with remaining entries are all
* deletions or additions.
*/
if (entry_a && !entry_b)
return signal_deletions(a, i_a, -1, cb, data);
if (!entry_a && entry_b)
return signal_additions(b, i_b, -1, cb, data);
/*
* Both trees are sorted with git's almost-alphabetical
* sorting, so a comparison value < 0 means the entry was
* deleted in the right tree. > 0 means the entry was added.
*/
cmp = entry_sort_cmp(entry_a, entry_b);
if (cmp == 0) {
i_a++;
i_b++;
/* If everything's the same, jump to next pair */
if (!tree_entry_cmp(entry_a, entry_b))
continue;
/* If they're not both dirs or both files, it's add + del */
if (S_ISDIR(entry_a->attr) != S_ISDIR(entry_b->attr)) {
if (signal_addition(entry_a, cb, data) < 0)
return -1;
if (signal_deletion(entry_b, cb, data) < 0)
return -1;
}
/* Otherwise consider it a modification */
if (signal_modification(entry_a, entry_b, cb, data) < 0)
return -1;
} else if (cmp < 0) {
i_a++;
if (signal_deletion(entry_a, cb, data) < 0)
return -1;
} else if (cmp > 0) {
i_b++;
if (signal_addition(entry_b, cb, data) < 0)
return -1;
}
}
return 0;
}
struct diff_index_cbdata {
git_index *index;
unsigned int i;
git_tree_diff_cb cb;
void *data;
};
static int cmp_tentry_ientry(git_tree_entry *tentry, git_index_entry *ientry)
{
int cmp;
cmp = tentry->attr - ientry->mode;
if (cmp != 0)
return cmp;
return git_oid_cmp(&tentry->oid, &ientry->oid);
}
static void make_tentry(git_tree_entry *tentry, git_index_entry *ientry)
{
char *last_slash;
memset(tentry, 0x0, sizeof(git_tree_entry));
tentry->attr = ientry->mode;
last_slash = strrchr(ientry->path, '/');
if (last_slash)
last_slash++;
else
last_slash = ientry->path;
tentry->filename = last_slash;
git_oid_cpy(&tentry->oid, &ientry->oid);
tentry->filename_len = strlen(tentry->filename);
}
static int diff_index_cb(const char *root, git_tree_entry *tentry, void *data)
{
struct diff_index_cbdata *cbdata = (struct diff_index_cbdata *) data;
git_index_entry *ientry = git_index_get(cbdata->index, cbdata->i);
git_tree_entry fake_entry;
git_buf fn_buf = GIT_BUF_INIT;
int cmp;
if (entry_is_tree(tentry))
return 0;
if (!ientry)
return signal_deletion(tentry, cbdata->cb, cbdata->data);
git_buf_puts(&fn_buf, root);
git_buf_puts(&fn_buf, tentry->filename);
/* Like with 'git diff-index', the index is the right side*/
cmp = strcmp(git_buf_cstr(&fn_buf), ientry->path);
git_buf_free(&fn_buf);
if (cmp == 0) {
cbdata->i++;
if (!cmp_tentry_ientry(tentry, ientry))
return 0;
/* modification */
make_tentry(&fake_entry, ientry);
if (signal_modification(tentry, &fake_entry, cbdata->cb, cbdata->data) < 0)
return -1;
} else if (cmp < 0) {
/* deletion */
memcpy(&fake_entry, tentry, sizeof(git_tree_entry));
if (signal_deletion(tentry, cbdata->cb, cbdata->data) < 0)
return -1;
} else {
/* addition */
cbdata->i++;
make_tentry(&fake_entry, ientry);
if (signal_addition(&fake_entry, cbdata->cb, cbdata->data) < 0)
return -1;
/*
* The index has an addition. This means that we need to use
* the next entry in the index without advancing the tree
* walker, so call ourselves with the same tree state.
*/
if (diff_index_cb(root, tentry, data) < 0)
return -1;;
}
return 0;
}
int git_tree_diff_index_recursive(git_tree *tree, git_index *index, git_tree_diff_cb cb, void *data)
{
struct diff_index_cbdata cbdata;
git_buf dummy_path = GIT_BUF_INIT;
cbdata.index = index;
cbdata.i = 0;
cbdata.cb = cb;
cbdata.data = data;
return tree_walk_post(tree, diff_index_cb, &dummy_path, &cbdata);
}
#include "clar_libgit2.h"
#include "tree.h"
#include "repository.h"
static git_repository *repo;
static git_index *theindex;
static git_tree *atree, *btree;
static git_oid aoid, boid;
static void diff_cmp(const git_tree_diff_data *a, const git_tree_diff_data *b)
{
cl_assert(a->old_attr - b->old_attr == 0);
cl_assert(a->new_attr - b->new_attr == 0);
cl_assert(git_oid_cmp(&a->old_oid, &b->old_oid) == 0);
cl_assert(git_oid_cmp(&a->new_oid, &b->new_oid) == 0);
cl_assert(a->status - b->status == 0);
cl_assert_equal_s(a->path, b->path);
}
static int diff_cb(const git_tree_diff_data *diff, void *data)
{
diff_cmp(diff, data);
return 0;
}
static void test_diff(git_tree *a, git_tree *b, git_tree_diff_cb cb, void *data)
{
cl_must_pass(git_tree_diff(a, b, cb, data));
cl_git_pass(git_index_read_tree(theindex, b));
cl_git_pass(git_tree_diff_index_recursive(a, theindex, cb, data));
}
void test_object_tree_diff__initialize(void)
{
cl_git_pass(git_repository_open(&repo, cl_fixture("testrepo.git")));
cl_git_pass(git_repository_index(&theindex, repo));
}
void test_object_tree_diff__cleanup(void)
{
git_tree_free(atree);
git_tree_free(btree);
git_index_free(theindex);
git_repository_free(repo);
}
void test_object_tree_diff__addition(void)
{
char *astr = "181037049a54a1eb5fab404658a3a250b44335d7";
char *bstr = "f60079018b664e4e79329a7ef9559c8d9e0378d1";
git_tree_diff_data expect;
memset(&expect, 0x0, sizeof(git_tree_diff_data));
expect.old_attr = 0;
expect.new_attr = 0100644;
git_oid_fromstr(&expect.new_oid, "fa49b077972391ad58037050f2a75f74e3671e92");
expect.status = GIT_STATUS_ADDED;
expect.path = "new.txt";
cl_must_pass(git_oid_fromstr(&aoid, astr));
cl_must_pass(git_oid_fromstr(&boid, bstr));
cl_must_pass(git_tree_lookup(&atree, repo, &aoid));
cl_must_pass(git_tree_lookup(&btree, repo, &boid));
test_diff(atree, btree, diff_cb, &expect);
}
void test_object_tree_diff__deletion(void)
{
char *astr = "f60079018b664e4e79329a7ef9559c8d9e0378d1";
char *bstr = "181037049a54a1eb5fab404658a3a250b44335d7";
git_tree_diff_data expect;
memset(&expect, 0x0, sizeof(git_tree_diff_data));
expect.old_attr = 0100644;
expect.new_attr = 0;
git_oid_fromstr(&expect.old_oid, "fa49b077972391ad58037050f2a75f74e3671e92");
expect.status = GIT_STATUS_DELETED;
expect.path = "new.txt";
cl_must_pass(git_oid_fromstr(&aoid, astr));
cl_must_pass(git_oid_fromstr(&boid, bstr));
cl_must_pass(git_tree_lookup(&atree, repo, &aoid));
cl_must_pass(git_tree_lookup(&btree, repo, &boid));
test_diff(atree, btree, diff_cb, &expect);
}
void test_object_tree_diff__modification(void)
{
char *astr = "1810dff58d8a660512d4832e740f692884338ccd";
char *bstr = "944c0f6e4dfa41595e6eb3ceecdb14f50fe18162";
git_tree_diff_data expect;
expect.old_attr = 0100644;
expect.new_attr = 0100644;
git_oid_fromstr(&expect.old_oid, "45b983be36b73c0788dc9cbcb76cbb80fc7bb057");
git_oid_fromstr(&expect.new_oid, "3697d64be941a53d4ae8f6a271e4e3fa56b022cc");
expect.status = GIT_STATUS_MODIFIED;
expect.path = "branch_file.txt";
cl_must_pass(git_oid_fromstr(&aoid, astr));
cl_must_pass(git_oid_fromstr(&boid, bstr));
cl_must_pass(git_tree_lookup(&atree, repo, &aoid));
cl_must_pass(git_tree_lookup(&btree, repo, &boid));
test_diff(atree, btree, diff_cb, &expect);
}
struct diff_more_data {
git_tree_diff_data expect[3];
int expect_idx;
};
static int diff_more_cb(const git_tree_diff_data *diff, void *data)
{
struct diff_more_data *more_data = data;
diff_cmp(diff, &more_data->expect[more_data->expect_idx]);
more_data->expect_idx = (more_data->expect_idx + 1) % ARRAY_SIZE(more_data->expect);
return 0;
}
void test_object_tree_diff__more(void)
{
char *astr = "814889a078c031f61ed08ab5fa863aea9314344d";
char *bstr = "75057dd4114e74cca1d750d0aee1647c903cb60a";
struct diff_more_data more_data;
git_tree_diff_data *expect = more_data.expect;
memset(&more_data, 0x0, sizeof(struct diff_more_data));
/* M README */
expect[0].old_attr = 0100644;
expect[0].new_attr = 0100644;
git_oid_fromstr(&expect[0].old_oid, "a8233120f6ad708f843d861ce2b7228ec4e3dec6");
git_oid_fromstr(&expect[0].new_oid, "1385f264afb75a56a5bec74243be9b367ba4ca08");
expect[0].status = GIT_STATUS_MODIFIED;
expect[0].path = "README";
/* A branch_file.txt */
expect[1].old_attr = 0;
expect[1].new_attr = 0100644;
git_oid_fromstr(&expect[1].new_oid, "45b983be36b73c0788dc9cbcb76cbb80fc7bb057");
expect[1].status = GIT_STATUS_ADDED;
expect[1].path = "branch_file.txt";
/* M new.txt */
expect[2].old_attr = 0100644;
expect[2].new_attr = 0100644;
git_oid_fromstr(&expect[2].old_oid, "a71586c1dfe8a71c6cbf6c129f404c5642ff31bd");
git_oid_fromstr(&expect[2].new_oid, "fa49b077972391ad58037050f2a75f74e3671e92");
expect[2].status = GIT_STATUS_MODIFIED;
expect[2].path = "new.txt";
cl_must_pass(git_oid_fromstr(&aoid, astr));
cl_must_pass(git_oid_fromstr(&boid, bstr));
cl_must_pass(git_tree_lookup(&atree, repo, &aoid));
cl_must_pass(git_tree_lookup(&btree, repo, &boid));
test_diff(atree, btree, diff_more_cb, &more_data);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment