blame: report correct number of lines in progress when using ranges

When using ranges, use the range sizes as the limit for progress instead of the size of the full file. Before: $ git blame --progress builtin/blame.c > /dev/null Blaming lines: 100% (1210/1210), done. $ git blame --progress -L 100,120 -L 200,300 builtin/blame.c > /dev/null Blaming lines: 10% (122/1210), done. $ After: $ ./git blame --progress builtin/blame.c > /dev/null Blaming lines: 100% (1210/1210), done. $ ./git blame --progress -L 100,120 -L 200,300 builtin/blame.c > /dev/null Blaming lines: 100% (122/122), done. $ Signed-off-by: Edmundo Carmona Antoranz <eantoranz@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Edmundo Carmona Antoranz committed Apr 6, 2022 at 20:13 UTC e5f5d7d42effb1d0d404897ac782783f742e9daa
2 files changed +45 -1
builtin/blame.c
+5 -1
@@ -897,6 +897,7 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
897 unsigned int range_i;
898 long anchor;
899 const int hexsz = the_hash_algo->hexsz;
900 + long num_lines = 0;
901
902 setup_default_color_by_age();
903 git_config(git_blame_config, &output_option);
@@ -1127,7 +1128,10 @@ parse_done:
1128 for (range_i = ranges.nr; range_i > 0; --range_i) {
1129 const struct range *r = &ranges.ranges[range_i - 1];
1130 ent = blame_entry_prepend(ent, r->start, r->end, o);
1131 + num_lines += (r->end - r->start);
1132 }
1133 + if (!num_lines)
1134 + num_lines = sb.num_lines;
1135
1136 o->suspects = ent;
1137 prio_queue_put(&sb.commits, o->commit);
@@ -1156,7 +1160,7 @@ parse_done:
1160 sb.found_guilty_entry = &found_guilty_entry;
1161 sb.found_guilty_entry_data = &pi;
1162 if (show_progress)
1159 - pi.progress = start_delayed_progress(_("Blaming lines"), sb.num_lines);
1163 + pi.progress = start_delayed_progress(_("Blaming lines"), num_lines);
1164
1165 assign_blame(&sb, opt);
1166
t/annotate-tests.sh
+40
@@ -56,6 +56,10 @@ check_count () {
56 ' "$@" <actual
57 }
58
59 +get_progress_result () {
60 + tr '\015' '\012' | tail -n 1
61 +}
62 +
63 test_expect_success 'setup A lines' '
64 echo "1A quick brown fox jumps over the" >file &&
65 echo "lazy dog" >>file &&
@@ -604,3 +608,39 @@ test_expect_success 'blame -L X,-N (non-numeric N)' '
608 test_expect_success 'blame -L ,^/RE/' '
609 test_must_fail $PROG -L1,^/99/ file
610 '
611 +
612 +test_expect_success 'blame progress on a full file' '
613 + cat >expect <<-\EOF &&
614 + Blaming lines: 100% (10/10), done.
615 + EOF
616 +
617 + GIT_PROGRESS_DELAY=0 \
618 + git blame --progress hello.c 2>stderr &&
619 +
620 + get_progress_result <stderr >actual &&
621 + test_cmp expect actual
622 +'
623 +
624 +test_expect_success 'blame progress on a single range' '
625 + cat >expect <<-\EOF &&
626 + Blaming lines: 100% (4/4), done.
627 + EOF
628 +
629 + GIT_PROGRESS_DELAY=0 \
630 + git blame --progress -L 3,6 hello.c 2>stderr &&
631 +
632 + get_progress_result <stderr >actual &&
633 + test_cmp expect actual
634 +'
635 +
636 +test_expect_success 'blame progress on multiple ranges' '
637 + cat >expect <<-\EOF &&
638 + Blaming lines: 100% (7/7), done.
639 + EOF
640 +
641 + GIT_PROGRESS_DELAY=0 \
642 + git blame --progress -L 3,6 -L 8,10 hello.c 2>stderr &&
643 +
644 + get_progress_result <stderr >actual &&
645 + test_cmp expect actual
646 +'