Commit 84ba4944 by Vicent Martí

Merge pull request #1659 from arrbee/rename-cycle-fixes

Diff rename detection cycle fixes
parents ffb762fe e4acc3ba
......@@ -46,7 +46,7 @@ static char diff_pick_suffix(int mode)
{
if (S_ISDIR(mode))
return '/';
else if (mode & 0100) //-V536
else if (mode & 0100) /* -V536 */
/* in git, modes are very regular, so we must have 0100755 mode */
return '*';
else
......@@ -162,7 +162,7 @@ static int diff_print_one_raw(
if (delta->similarity > 0)
git_buf_printf(out, "%03u", delta->similarity);
if (delta->status == GIT_DELTA_RENAMED || delta->status == GIT_DELTA_COPIED)
if (delta->old_file.path != delta->new_file.path)
git_buf_printf(
out, "\t%s %s\n", delta->old_file.path, delta->new_file.path);
else
......
......@@ -650,6 +650,59 @@ void test_diff_rename__file_exchange(void)
git_buf_free(&c2);
}
void test_diff_rename__file_exchange_three(void)
{
git_buf c1 = GIT_BUF_INIT, c2 = GIT_BUF_INIT, c3 = GIT_BUF_INIT;
git_index *index;
git_tree *tree;
git_diff_list *diff;
git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT;
git_diff_find_options opts = GIT_DIFF_FIND_OPTIONS_INIT;
diff_expects exp;
cl_git_pass(git_futils_readbuffer(&c1, "renames/untimely.txt"));
cl_git_pass(git_futils_readbuffer(&c2, "renames/songof7cities.txt"));
cl_git_pass(git_futils_readbuffer(&c3, "renames/ikeepsix.txt"));
cl_git_pass(git_futils_writebuffer(&c1, "renames/ikeepsix.txt", 0, 0));
cl_git_pass(git_futils_writebuffer(&c2, "renames/untimely.txt", 0, 0));
cl_git_pass(git_futils_writebuffer(&c3, "renames/songof7cities.txt", 0, 0));
cl_git_pass(
git_revparse_single((git_object **)&tree, g_repo, "HEAD^{tree}"));
cl_git_pass(git_repository_index(&index, g_repo));
cl_git_pass(git_index_read_tree(index, tree));
cl_git_pass(git_index_add_bypath(index, "songof7cities.txt"));
cl_git_pass(git_index_add_bypath(index, "untimely.txt"));
cl_git_pass(git_index_add_bypath(index, "ikeepsix.txt"));
cl_git_pass(git_diff_tree_to_index(&diff, g_repo, tree, index, &diffopts));
memset(&exp, 0, sizeof(exp));
cl_git_pass(git_diff_foreach(
diff, diff_file_cb, diff_hunk_cb, diff_line_cb, &exp));
cl_assert_equal_i(3, exp.files);
cl_assert_equal_i(3, exp.file_status[GIT_DELTA_MODIFIED]);
opts.flags = GIT_DIFF_FIND_ALL;
cl_git_pass(git_diff_find_similar(diff, &opts));
memset(&exp, 0, sizeof(exp));
cl_git_pass(git_diff_foreach(
diff, diff_file_cb, diff_hunk_cb, diff_line_cb, &exp));
cl_assert_equal_i(3, exp.files);
cl_assert_equal_i(3, exp.file_status[GIT_DELTA_RENAMED]);
git_diff_list_free(diff);
git_tree_free(tree);
git_index_free(index);
git_buf_free(&c1);
git_buf_free(&c2);
git_buf_free(&c3);
}
void test_diff_rename__file_partial_exchange(void)
{
git_buf c1 = GIT_BUF_INIT, c2 = GIT_BUF_INIT;
......@@ -702,7 +755,7 @@ void test_diff_rename__file_partial_exchange(void)
git_buf_free(&c2);
}
void test_diff_rename__file_split(void)
void test_diff_rename__rename_and_copy_from_same_source(void)
{
git_buf c1 = GIT_BUF_INIT, c2 = GIT_BUF_INIT;
git_index *index;
......@@ -894,6 +947,7 @@ void test_diff_rename__rejected_match_can_match_others(void)
cl_git_pass(
git_diff_tree_to_index(&diff, g_repo, tree, index, &diffopts));
cl_git_pass(git_diff_find_similar(diff, &findopts));
cl_git_pass(
......@@ -908,6 +962,77 @@ void test_diff_rename__rejected_match_can_match_others(void)
git_buf_free(&two);
}
static void write_similarity_file_two(const char *filename, size_t b_lines)
{
git_buf contents = GIT_BUF_INIT;
size_t i;
for (i = 0; i < b_lines; i++)
git_buf_printf(&contents, "%0.2d - bbbbb\r\n", (int)(i+1));
for (i = b_lines; i < 50; i++)
git_buf_printf(&contents, "%0.2d - aaaaa%s", (int)(i+1), (i == 49 ? "" : "\r\n"));
cl_git_pass(
git_futils_writebuffer(&contents, filename, O_RDWR|O_CREAT, 0777));
git_buf_free(&contents);
}
void test_diff_rename__rejected_match_can_match_others_two(void)
{
git_reference *head, *selfsimilar;
git_index *index;
git_tree *tree;
git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
git_diff_list *diff;
git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT;
git_diff_find_options findopts = GIT_DIFF_FIND_OPTIONS_INIT;
const char *sources[] = { "a.txt", "b.txt" };
const char *targets[] = { "c.txt", "d.txt" };
struct rename_expected expect = { 2, sources, targets };
opts.checkout_strategy = GIT_CHECKOUT_FORCE;
cl_git_pass(git_reference_lookup(&head, g_repo, "HEAD"));
cl_git_pass(git_reference_symbolic_set_target(
&selfsimilar, head, "refs/heads/renames_similar_two"));
cl_git_pass(git_checkout_head(g_repo, &opts));
cl_git_pass(git_repository_index(&index, g_repo));
cl_git_pass(p_unlink("renames/a.txt"));
cl_git_pass(p_unlink("renames/b.txt"));
cl_git_pass(git_index_remove_bypath(index, "a.txt"));
cl_git_pass(git_index_remove_bypath(index, "b.txt"));
write_similarity_file_two("renames/c.txt", 7);
write_similarity_file_two("renames/d.txt", 8);
cl_git_pass(git_index_add_bypath(index, "c.txt"));
cl_git_pass(git_index_add_bypath(index, "d.txt"));
cl_git_pass(git_index_write(index));
cl_git_pass(
git_revparse_single((git_object **)&tree, g_repo, "HEAD^{tree}"));
cl_git_pass(
git_diff_tree_to_index(&diff, g_repo, tree, index, &diffopts));
cl_git_pass(git_diff_find_similar(diff, &findopts));
cl_git_pass(
git_diff_foreach(diff, test_names_expected, NULL, NULL, &expect));
cl_assert(expect.idx > 0);
git_diff_list_free(diff);
git_tree_free(tree);
git_index_free(index);
git_reference_free(head);
git_reference_free(selfsimilar);
}
void test_diff_rename__case_changes_are_split(void)
{
git_index *index;
......
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