fix blame bug where lines got annotated with wrong commit
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
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"