[PATCH 1/3] blame: handle the tail-match correctly in -C/-M

Subsystems: the rest

DORMANTno replies

3 messages, 1 author, 2016-06-15 · open the first message on its own page

[PATCH 1/3] blame: handle the tail-match correctly in -C/-M

From: Junio C Hamano <hidden>
Date: 2016-06-15 22:43:08

The -C/-M option to blame tries to find a section of a preimage
file by running diff against the lines whose origin is still
unknown, and excluding the different parts.  The code however
did not cover the case where the tail part of the section
matched, which we handle for the normal non-move/copy codepath.

This breakage was most visible when preimage file matches in its
entirety and failed to pass blame in such a case.

Signed-off-by: Junio C Hamano <redacted>
---
 builtin-blame.c |   52 ++++++++++++++++++++++++++++++++++++++++------------
 1 files changed, 40 insertions(+), 12 deletions(-)
diff --git a/builtin-blame.c b/builtin-blame.c
index 8919b02..f8843e6 100644
--- a/builtin-blame.c
+++ b/builtin-blame.c
@@ -891,6 +891,39 @@ static void copy_split_if_better(struct scoreboard *sb,
 }
 
 /*
+ * We are looking at a part of the final image represented by
+ * ent (tlno and same are offset by ent->s_lno).
+ * tlno is where we are looking at in the final image.
+ * up to (but not including) same match preimage.
+ * plno is where we are looking at in the preimage.
+ *
+ * <-------------- final image ---------------------->
+ *       <------ent------>
+ *         ^tlno ^same
+ *    <---------preimage----->
+ *         ^plno
+ *
+ * All line numbers are 0-based.
+ */
+static void handle_split(struct scoreboard *sb,
+			 struct blame_entry *ent,
+			 int tlno, int plno, int same,
+			 struct origin *parent,
+			 struct blame_entry *split)
+{
+	if (ent->num_lines <= tlno)
+		return;
+	if (tlno < same) {
+		struct blame_entry this[3];
+		tlno += ent->s_lno;
+		same += ent->s_lno;
+		split_overlap(this, ent, tlno, plno, same, parent);
+		copy_split_if_better(sb, split, this);
+		decref_split(this);
+	}
+}
+
+/*
  * Find the lines from parent that are the same as ent so that
  * we can pass blames to it.  file_p has the blob contents for
  * the parent.
@@ -922,26 +955,21 @@ static void find_copy_in_blob(struct scoreboard *sb,
 
 	patch = compare_buffer(file_p, &file_o, 1);
 
+	/*
+	 * file_o is a part of final image we are annotating.
+	 * file_p partially may match that image.
+	 */
 	memset(split, 0, sizeof(struct blame_entry [3]));
 	plno = tlno = 0;
 	for (i = 0; i < patch->num; i++) {
 		struct chunk *chunk = &patch->chunks[i];
 
-		/* tlno to chunk->same are the same as ent */
-		if (ent->num_lines <= tlno)
-			break;
-		if (tlno < chunk->same) {
-			struct blame_entry this[3];
-			split_overlap(this, ent,
-				      tlno + ent->s_lno, plno,
-				      chunk->same + ent->s_lno,
-				      parent);
-			copy_split_if_better(sb, split, this);
-			decref_split(this);
-		}
+		handle_split(sb, ent, tlno, plno, chunk->same, parent, split);
 		plno = chunk->p_next;
 		tlno = chunk->t_next;
 	}
+	/* remainder, if any, all match the preimage */
+	handle_split(sb, ent, tlno, plno, ent->num_lines, parent, split);
 	free_patch(patch);
 }
 
-- 
1.5.2.rc1.709.g9462

[PATCH 3/3] Add test for blame corner cases.

From: Junio C Hamano <hidden>
Date: 2016-06-15 22:43:08

Signed-off-by: Junio C Hamano <redacted>
---
 t/t8003-blame.sh |  132 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 132 insertions(+), 0 deletions(-)
 create mode 100755 t/t8003-blame.sh
diff --git a/t/t8003-blame.sh b/t/t8003-blame.sh
new file mode 100755
index 0000000..db51b3a
--- /dev/null
+++ b/t/t8003-blame.sh
@@ -0,0 +1,132 @@
+#!/bin/sh
+
+test_description='git blame corner cases'
+. ./test-lib.sh
+
+pick_fc='s/^[0-9a-f^]* *\([^ ]*\) *(\([^ ]*\) .*/\1-\2/'
+
+test_expect_success setup '
+
+	echo A A A A A >one &&
+	echo B B B B B >two &&
+	echo C C C C C >tres &&
+	echo ABC >mouse &&
+	git add one two tres mouse &&
+	test_tick &&
+	GIT_AUTHOR_NAME=Initial git commit -m Initial &&
+
+	cat one >uno &&
+	mv two dos &&
+	cat one >>tres &&
+	echo DEF >>mouse
+	git add uno dos tres mouse &&
+	test_tick &&
+	GIT_AUTHOR_NAME=Second git commit -a -m Second &&
+
+	echo GHIJK >>mouse &&
+	git add mouse &&
+	test_tick &&
+	GIT_AUTHOR_NAME=Third git commit -m Third &&
+
+	cat mouse >cow &&
+	git add cow &&
+	test_tick &&
+	GIT_AUTHOR_NAME=Fourth git commit -m Fourth &&
+
+	{
+		echo ABC
+		echo DEF
+		echo XXXX
+		echo GHIJK
+	} >cow &&
+	git add cow &&
+	test_tick &&
+	GIT_AUTHOR_NAME=Fifth git commit -m Fifth
+'
+
+test_expect_success 'straight copy without -C' '
+
+	git blame uno | grep Second
+
+'
+
+test_expect_success 'straight move without -C' '
+
+	git blame dos | grep Initial
+
+'
+
+test_expect_success 'straight copy with -C' '
+
+	git blame -C1 uno | grep Second
+
+'
+
+test_expect_success 'straight move with -C' '
+
+	git blame -C1 dos | grep Initial
+
+'
+
+test_expect_success 'straight copy with -C -C' '
+
+	git blame -C -C1 uno | grep Initial
+
+'
+
+test_expect_success 'straight move with -C -C' '
+
+	git blame -C -C1 dos | grep Initial
+
+'
+
+test_expect_success 'append without -C' '
+
+	git blame -L2 tres | grep Second
+
+'
+
+test_expect_success 'append with -C' '
+
+	git blame -L2 -C1 tres | grep Second
+
+'
+
+test_expect_success 'append with -C -C' '
+
+	git blame -L2 -C -C1 tres | grep Second
+
+'
+
+test_expect_success 'append with -C -C -C' '
+
+	git blame -L2 -C -C -C1 tres | grep Initial
+
+'
+
+test_expect_success 'blame wholesale copy' '
+
+	git blame -f -C -C1 HEAD^ -- cow | sed -e "$pick_fc" >current &&
+	{
+		echo mouse-Initial
+		echo mouse-Second
+		echo mouse-Third
+	} >expected &&
+	diff -u expected current
+
+'
+
+test_expect_success 'blame wholesale copy and more' '
+
+	git blame -f -C -C1 HEAD -- cow | sed -e "$pick_fc" >current &&
+	{
+		echo mouse-Initial
+		echo mouse-Second
+		echo cow-Fifth
+		echo mouse-Third
+	} >expected &&
+	diff -u expected current
+
+'
+
+test_done
-- 
1.5.2.rc1.709.g9462

[PATCH 2/3] blame: -C -C -C

From: Junio C Hamano <hidden>
Date: 2016-06-15 22:43:08

Existing "blame -C -C" would not find that the latter half of
the file2 came from the existing file1:

	... both file1 and file2 are tracked ...
	$ cat file1 >>file2
	$ git add file1 file2
	$ git commit

This is because we avoid the expensive find-copies-harder code
that makes unchanged file (in this case, file1) as a candidate
for copy & paste source when annotating an existing file
(file2).  The third -C now allows it.  However, this obviously
makes the process very expensive.  We've actually seen this
patch before, but I dismissed it because it covers such a narrow
(and arguably stupid) corner case.

Signed-off-by: Junio C Hamano <redacted>
---
 builtin-blame.c |   15 +++++++++++++--
 1 files changed, 13 insertions(+), 2 deletions(-)
diff --git a/builtin-blame.c b/builtin-blame.c
index f8843e6..65d029a 100644
--- a/builtin-blame.c
+++ b/builtin-blame.c
@@ -55,6 +55,7 @@ static int num_commits;
 #define PICKAXE_BLAME_MOVE		01
 #define PICKAXE_BLAME_COPY		02
 #define PICKAXE_BLAME_COPY_HARDER	04
+#define PICKAXE_BLAME_COPY_HARDEST	010
 
 /*
  * blame for a blame_entry with score lower than these thresholds
@@ -1079,8 +1080,9 @@ static int find_copy_in_parent(struct scoreboard *sb,
 	 * and this code needs to be after diff_setup_done(), which
 	 * usually makes find-copies-harder imply copy detection.
 	 */
-	if ((opt & PICKAXE_BLAME_COPY_HARDER) &&
-	    (!porigin || strcmp(target->path, porigin->path)))
+	if ((opt & PICKAXE_BLAME_COPY_HARDEST)
+	    || ((opt & PICKAXE_BLAME_COPY_HARDER)
+		&& (!porigin || strcmp(target->path, porigin->path))))
 		diff_opts.find_copies_harder = 1;
 
 	if (is_null_sha1(target->commit->object.sha1))
@@ -2127,6 +2129,15 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
 			blame_move_score = parse_score(arg+2);
 		}
 		else if (!prefixcmp(arg, "-C")) {
+			/*
+			 * -C enables copy from removed files;
+			 * -C -C enables copy from existing files, but only
+			 *       when blaming a new file;
+			 * -C -C -C enables copy from existing files for
+			 *          everybody
+			 */
+			if (opt & PICKAXE_BLAME_COPY_HARDER)
+				opt |= PICKAXE_BLAME_COPY_HARDEST;
 			if (opt & PICKAXE_BLAME_COPY)
 				opt |= PICKAXE_BLAME_COPY_HARDER;
 			opt |= PICKAXE_BLAME_COPY | PICKAXE_BLAME_MOVE;
-- 
1.5.2.rc1.709.g9462
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help