Commit 7ef28ff8dd61cbf38f88784ea8c11e373757985f

Stefan Sperling 2019-08-14T23:10:00

make 'got blame' show line numbers

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="$?"