Commit 548237bca5af8d72f355b42d06bafc910b13cb06

Stefan Sperling 2019-08-19T11:29:01

fix blame bug where lines got annotated with wrong commit

diff --git a/lib/blame.c b/lib/blame.c
index df12bfb..bff96a6 100644
--- a/lib/blame.c
+++ b/lib/blame.c
@@ -125,11 +125,12 @@ get_blamed_line(struct got_blame_diff_offsets_list *diff_offsets_list,
     int lineno)
 {
 	struct got_blame_diff_offsets *diff_offsets;
+	int offset = 0;
 
 	SLIST_FOREACH(diff_offsets, diff_offsets_list, entry)
 		lineno = got_diffoffset_get(diff_offsets->chunks, lineno);
 
-	return lineno;
+	return lineno + offset;
 }
 
 static const struct got_error *
diff --git a/lib/diffoffset.c b/lib/diffoffset.c
index 6abefa6..9895705 100644
--- a/lib/diffoffset.c
+++ b/lib/diffoffset.c
@@ -105,16 +105,27 @@ got_diffoffset_add(struct got_diffoffset_chunks *chunks,
 	const struct got_error *err = NULL;
 	int offset;
 
-	offset = new_lineno - old_lineno;
-	if (offset != 0) {
-		err = add_chunk(chunks, old_lineno, offset);
-		if (err)
-			return err;
+	if (old_length != 0) {
+		offset = new_lineno - old_lineno;
+		if (offset != 0) {
+			err = add_chunk(chunks, old_lineno, offset);
+			if (err)
+				return err;
+		}
+	} else {
+		offset = new_length;
+		if (offset != 0) {
+			err = add_chunk(chunks, old_lineno, offset);
+			if (err)
+				return err;
+		}
+		if (old_lineno == new_lineno)
+			return NULL;
 	}
 
 	offset = new_lineno - old_lineno + new_length - old_length;
 	if (offset != 0)
-		err = add_chunk(chunks, old_lineno + old_length, offset);
+		err = add_chunk(chunks, old_lineno + new_length, offset);
 
 	return err;
 }
@@ -122,14 +133,14 @@ got_diffoffset_add(struct got_diffoffset_chunks *chunks,
 int
 got_diffoffset_get(struct got_diffoffset_chunks *chunks, int lineno)
 {
-	struct got_diffoffset_chunk *chunk, *prev;
+	struct got_diffoffset_chunk *chunk;
+	int offset = 0;
 
-	prev = SIMPLEQ_FIRST(chunks);
 	SIMPLEQ_FOREACH(chunk, chunks, entry) {
 		if (chunk->lineno > lineno)
 			break;
-		prev = chunk;
+		offset += chunk->offset;
 	}
 
-	return lineno + prev->offset;
+	return lineno + offset;
 }
diff --git a/regress/cmdline/blame.sh b/regress/cmdline/blame.sh
index 6601553..ac29d63 100755
--- a/regress/cmdline/blame.sh
+++ b/regress/cmdline/blame.sh
@@ -485,8 +485,8 @@ EOF
 	cmp -s $testroot/stdout.expected $testroot/stdout
 	ret="$?"
 	if [ "$ret" != "0" ]; then
-		#diff -u $testroot/stdout.expected $testroot/stdout
-		test_done "$testroot" "xfail: line 3 has wrong annotation"
+		diff -u $testroot/stdout.expected $testroot/stdout
+		test_done "$testroot" "$ret"
 		return 1
 	fi
 	test_done "$testroot" "$ret"