Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 88c401b

Browse files
author
Vicent Martí
committed
Merge pull request libgit2#1643 from ethomson/rename_source
Keep data about source of similarity
2 parents 93da7af + 690bf41 commit 88c401b

File tree

7 files changed

+117
-17
lines changed

7 files changed

+117
-17
lines changed

src/diff_tform.c

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -688,7 +688,7 @@ int git_diff_find_similar(
688688
git_diff_find_options opts;
689689
size_t num_rewrites = 0, num_updates = 0;
690690
void **cache; /* cache of similarity metric file signatures */
691-
diff_find_match *matches; /* cache of best matches */
691+
diff_find_match *match_sources, *match_targets; /* cache of best matches */
692692

693693
if ((error = normalize_find_opts(diff, &opts, given_opts)) < 0)
694694
return error;
@@ -701,16 +701,18 @@ int git_diff_find_similar(
701701
cache = git__calloc(cache_size, sizeof(void *));
702702
GITERR_CHECK_ALLOC(cache);
703703

704-
matches = git__calloc(diff->deltas.length, sizeof(diff_find_match));
705-
GITERR_CHECK_ALLOC(matches);
704+
match_sources = git__calloc(diff->deltas.length, sizeof(diff_find_match));
705+
match_targets = git__calloc(diff->deltas.length, sizeof(diff_find_match));
706+
GITERR_CHECK_ALLOC(match_sources);
707+
GITERR_CHECK_ALLOC(match_targets);
706708

707709
/* next find the most similar delta for each rename / copy candidate */
708710

709711
git_vector_foreach(&diff->deltas, i, to) {
710712
size_t tried_sources = 0;
711713

712-
matches[i].idx = i;
713-
matches[i].similarity = 0;
714+
match_targets[i].idx = i;
715+
match_targets[i].similarity = 0;
714716

715717
/* skip things that are not rename targets */
716718
if (!is_rename_target(diff, &opts, i, cache))
@@ -738,23 +740,26 @@ int git_diff_find_similar(
738740
continue;
739741
}
740742

741-
if (matches[i].similarity < (uint32_t)similarity) {
742-
matches[i].similarity = (uint32_t)similarity;
743-
matches[i].idx = j;
743+
if (match_targets[i].similarity < (uint32_t)similarity &&
744+
match_sources[j].similarity < (uint32_t)similarity) {
745+
match_targets[i].similarity = (uint32_t)similarity;
746+
match_sources[j].similarity = (uint32_t)similarity;
747+
match_targets[i].idx = j;
748+
match_sources[j].idx = i;
744749
}
745750
}
746751
}
747752

748753
/* next rewrite the diffs with renames / copies */
749754

750755
git_vector_foreach(&diff->deltas, i, to) {
751-
752-
/* check if this delta was matched to another one */
753-
if ((similarity = (int)matches[i].similarity) <= 0)
756+
/* check if this delta was the target of a similarity */
757+
if ((similarity = (int)match_targets[i].similarity) <= 0)
754758
continue;
759+
755760
assert(to && (to->flags & GIT_DIFF_FLAG__IS_RENAME_TARGET) != 0);
756761

757-
from = GIT_VECTOR_GET(&diff->deltas, matches[i].idx);
762+
from = GIT_VECTOR_GET(&diff->deltas, match_targets[i].idx);
758763
assert(from && (from->flags & GIT_DIFF_FLAG__IS_RENAME_SOURCE) != 0);
759764

760765
/* possible scenarios:
@@ -851,14 +856,14 @@ int git_diff_find_similar(
851856
/* in the off chance that we've just swapped the new
852857
* element into the correct place, clear the SPLIT flag
853858
*/
854-
if (matches[matches[i].idx].idx == i &&
855-
matches[matches[i].idx].similarity >
859+
if (match_targets[match_targets[i].idx].idx == i &&
860+
match_targets[match_targets[i].idx].similarity >
856861
opts.rename_from_rewrite_threshold) {
857862

858863
from->status = GIT_DELTA_RENAMED;
859864
from->similarity =
860-
(uint32_t)matches[matches[i].idx].similarity;
861-
matches[matches[i].idx].similarity = 0;
865+
(uint32_t)match_targets[match_targets[i].idx].similarity;
866+
match_targets[match_targets[i].idx].similarity = 0;
862867
from->flags &= ~GIT_DIFF_FLAG__TO_SPLIT;
863868
num_rewrites--;
864869
}
@@ -886,7 +891,8 @@ int git_diff_find_similar(
886891
FLAG_SET(&opts, GIT_DIFF_BREAK_REWRITES));
887892

888893
cleanup:
889-
git__free(matches);
894+
git__free(match_sources);
895+
git__free(match_targets);
890896

891897
for (i = 0; i < cache_size; ++i) {
892898
if (cache[i] != NULL)

tests-clar/diff/rename.c

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -811,3 +811,96 @@ void test_diff_rename__from_deleted_to_split(void)
811811

812812
git_buf_free(&c1);
813813
}
814+
815+
struct rename_expected
816+
{
817+
size_t len;
818+
const char **sources;
819+
const char **targets;
820+
821+
size_t idx;
822+
};
823+
824+
int test_names_expected(const git_diff_delta *delta, float progress, void *p)
825+
{
826+
struct rename_expected *expected = p;
827+
828+
cl_assert(expected->idx < expected->len);
829+
830+
cl_assert_equal_i(delta->status, GIT_DELTA_RENAMED);
831+
832+
cl_assert(git__strcmp(expected->sources[expected->idx],
833+
delta->old_file.path) == 0);
834+
cl_assert(git__strcmp(expected->targets[expected->idx],
835+
delta->new_file.path) == 0);
836+
837+
expected->idx++;
838+
839+
return 0;
840+
}
841+
842+
void test_diff_rename__rejected_match_can_match_others(void)
843+
{
844+
git_reference *head, *selfsimilar;
845+
git_index *index;
846+
git_tree *tree;
847+
git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
848+
git_diff_list *diff;
849+
git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT;
850+
git_diff_find_options findopts = GIT_DIFF_FIND_OPTIONS_INIT;
851+
git_buf one = GIT_BUF_INIT, two = GIT_BUF_INIT;
852+
const char *sources[] = { "Class1.cs", "Class2.cs" };
853+
const char *targets[] = { "ClassA.cs", "ClassB.cs" };
854+
struct rename_expected expect = { 2, sources, targets };
855+
char *ptr;
856+
857+
opts.checkout_strategy = GIT_CHECKOUT_FORCE;
858+
859+
cl_git_pass(git_reference_lookup(&head, g_repo, "HEAD"));
860+
cl_git_pass(git_reference_symbolic_set_target(
861+
&selfsimilar, head, "refs/heads/renames_similar"));
862+
cl_git_pass(git_checkout_head(g_repo, &opts));
863+
cl_git_pass(git_repository_index(&index, g_repo));
864+
865+
cl_git_pass(git_futils_readbuffer(&one, "renames/Class1.cs"));
866+
cl_git_pass(git_futils_readbuffer(&two, "renames/Class2.cs"));
867+
868+
cl_git_pass(p_unlink("renames/Class1.cs"));
869+
cl_git_pass(p_unlink("renames/Class2.cs"));
870+
871+
cl_git_pass(git_index_remove_bypath(index, "Class1.cs"));
872+
cl_git_pass(git_index_remove_bypath(index, "Class2.cs"));
873+
874+
cl_assert(ptr = strstr(one.ptr, "Class1"));
875+
ptr[5] = 'A';
876+
877+
cl_assert(ptr = strstr(two.ptr, "Class2"));
878+
ptr[5] = 'B';
879+
880+
cl_git_pass(
881+
git_futils_writebuffer(&one, "renames/ClassA.cs", O_RDWR|O_CREAT, 0777));
882+
cl_git_pass(
883+
git_futils_writebuffer(&two, "renames/ClassB.cs", O_RDWR|O_CREAT, 0777));
884+
885+
cl_git_pass(git_index_add_bypath(index, "ClassA.cs"));
886+
cl_git_pass(git_index_add_bypath(index, "ClassB.cs"));
887+
888+
cl_git_pass(git_index_write(index));
889+
890+
cl_git_pass(
891+
git_revparse_single((git_object **)&tree, g_repo, "HEAD^{tree}"));
892+
893+
cl_git_pass(
894+
git_diff_tree_to_index(&diff, g_repo, tree, index, &diffopts));
895+
cl_git_pass(git_diff_find_similar(diff, &findopts));
896+
897+
cl_git_pass(
898+
git_diff_foreach(diff, test_names_expected, NULL, NULL, &expect));
899+
900+
git_tree_free(tree);
901+
git_index_free(index);
902+
git_reference_free(head);
903+
git_reference_free(selfsimilar);
904+
git_buf_free(&one);
905+
git_buf_free(&two);
906+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
444a76ed3e45b183753f49376af30da8c3fe276a

0 commit comments

Comments
 (0)