make 'got blame' show line numbers
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 91
diff --git a/got/got.c b/got/got.c
index 892916e..6aaab25 100644
--- a/got/got.c
+++ b/got/got.c
@@ -2162,6 +2162,7 @@ struct blame_line {
struct blame_cb_args {
struct blame_line *lines;
int nlines;
+ int nlines_prec;
int lineno_cur;
off_t *line_offsets;
FILE *f;
@@ -2212,7 +2213,8 @@ blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
nl = strchr(line, '\n');
if (nl)
*nl = '\0';
- printf("%.8s %s\n", bline->id_str, line);
+ printf("%.*d) %.8s %s\n", a->nlines_prec, a->lineno_cur,
+ bline->id_str, line);
a->lineno_cur++;
bline = &a->lines[a->lineno_cur - 1];
@@ -2363,7 +2365,7 @@ cmd_blame(int argc, char *argv[])
}
error = got_object_blob_dump_to_file(NULL, &bca.nlines,
&bca.line_offsets, bca.f, blob);
- if (error)
+ if (error || bca.nlines == 0)
goto done;
bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
@@ -2374,6 +2376,13 @@ cmd_blame(int argc, char *argv[])
bca.lineno_cur = 1;
+ bca.nlines_prec = 0;
+ i = bca.nlines;
+ while (i > 0) {
+ i /= 10;
+ bca.nlines_prec++;
+ }
+
error = got_blame_incremental(in_repo_path, commit_id, repo,
blame_cb, &bca);
if (error)
diff --git a/regress/cmdline/blame.sh b/regress/cmdline/blame.sh
index bd295cf..b3f34a6 100755
--- a/regress/cmdline/blame.sh
+++ b/regress/cmdline/blame.sh
@@ -44,9 +44,9 @@ function test_blame_basic {
local short_commit2=`trim_obj_id 32 $commit2`
local short_commit3=`trim_obj_id 32 $commit3`
- echo "$short_commit1 1" > $testroot/stdout.expected
- echo "$short_commit2 2" >> $testroot/stdout.expected
- echo "$short_commit3 3" >> $testroot/stdout.expected
+ echo "1) $short_commit1 1" > $testroot/stdout.expected
+ echo "2) $short_commit2 2" >> $testroot/stdout.expected
+ echo "3) $short_commit3 3" >> $testroot/stdout.expected
cmp -s $testroot/stdout.expected $testroot/stdout
ret="$?"
@@ -86,8 +86,8 @@ function test_blame_tag {
local short_commit1=`trim_obj_id 32 $commit1`
local short_commit2=`trim_obj_id 32 $commit2`
- echo "$short_commit1 1" > $testroot/stdout.expected
- echo "$short_commit2 2" >> $testroot/stdout.expected
+ echo "1) $short_commit1 1" > $testroot/stdout.expected
+ echo "2) $short_commit2 2" >> $testroot/stdout.expected
cmp -s $testroot/stdout.expected $testroot/stdout
ret="$?"
@@ -115,7 +115,7 @@ function test_blame_file_single_line {
local short_commit1=`trim_obj_id 32 $commit1`
- echo "$short_commit1 1" > $testroot/stdout.expected
+ echo "1) $short_commit1 1" > $testroot/stdout.expected
cmp -s $testroot/stdout.expected $testroot/stdout
ret="$?"
@@ -143,7 +143,7 @@ function test_blame_file_single_line_no_newline {
local short_commit1=`trim_obj_id 32 $commit1`
- echo "$short_commit1 1" > $testroot/stdout.expected
+ echo "1) $short_commit1 1" > $testroot/stdout.expected
cmp -s $testroot/stdout.expected $testroot/stdout
ret="$?"