blame: use different author name for fake commit generated by --contents

When the --contents option is used with git blame, and the contents of the file have lines which can't be annotated by the history being blamed, the user will see an author of "Not Committed Yet". This is similar to the way blame handles working tree contents when blaming without a revision. This is slightly confusing since this data isn't the working copy and while it is technically "not committed yet", its also coming from an external file. Replace this author name with "External file (--contents)" to better differentiate such lines from actual working copy lines. Suggested-by: Junio C Hamano <gitster@pobox.com> Suggested-by: Glen Choo <chooglen@google.com> Signed-off-by: Jacob Keller <jacob.keller@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jacob Keller committed Apr 24, 2023 at 12:35 UTC 603d0fdce2ca84d4dc7b26e46430778fe9c4cb72
3 files changed +18 -9
Documentation/blame-options.txt
+3 -5
@@ -64,11 +64,9 @@ include::line-range-format.txt[]
64 manual page.
65
66 --contents <file>::
67 - Pretend the file being annotated has a commit with the
68 - contents from the named file and a parent of <rev>,
69 - defaulting to HEAD when no <rev> is specified. You may
70 - specify '-' to make the command read from the standard
71 - input for the file contents.
67 + Annotate using the contents from the named file, starting from <rev>
68 + if it is specified, and HEAD otherwise. You may specify '-' to make
69 + the command read from the standard input for the file contents.
70
71 --date <format>::
72 Specifies the format used to output dates. If --date is not
blame.c
+6 -2
@@ -206,8 +206,12 @@ static struct commit *fake_working_tree_commit(struct repository *r,
206
207 origin = make_origin(commit, path);
208
209 - ident = fmt_ident("Not Committed Yet", "not.committed.yet",
210 - WANT_BLANK_IDENT, NULL, 0);
209 + if (contents_from)
210 + ident = fmt_ident("External file (--contents)", "external.file",
211 + WANT_BLANK_IDENT, NULL, 0);
212 + else
213 + ident = fmt_ident("Not Committed Yet", "not.committed.yet",
214 + WANT_BLANK_IDENT, NULL, 0);
215 strbuf_addstr(&msg, "tree 0000000000000000000000000000000000000000\n");
216 for (parent = commit->parents; parent; parent = parent->next)
217 strbuf_addf(&msg, "parent %s\n",
t/annotate-tests.sh
+9 -2
@@ -72,6 +72,13 @@ test_expect_success 'blame 1 author' '
72 check_count A 2
73 '
74
75 +test_expect_success 'blame working copy' '
76 + test_when_finished "git restore file" &&
77 + echo "1A quick brown fox jumps over the" >file &&
78 + echo "another lazy dog" >>file &&
79 + check_count A 1 "Not Committed Yet" 1
80 +'
81 +
82 test_expect_success 'blame with --contents' '
83 check_count --contents=file A 2
84 '
@@ -79,7 +86,7 @@ test_expect_success 'blame with --contents' '
86 test_expect_success 'blame with --contents changed' '
87 echo "1A quick brown fox jumps over the" >contents &&
88 echo "another lazy dog" >>contents &&
82 - check_count --contents=contents A 1 "Not Committed Yet" 1
89 + check_count --contents=contents A 1 "External file (--contents)" 1
90 '
91
92 test_expect_success 'blame in a bare repo without starting commit' '
@@ -109,7 +116,7 @@ test_expect_success 'blame 2 authors' '
116 '
117
118 test_expect_success 'blame with --contents and revision' '
112 - check_count -h testTag --contents=file A 2 "Not Committed Yet" 2
119 + check_count -h testTag --contents=file A 2 "External file (--contents)" 2
120 '
121
122 test_expect_success 'setup B1 lines (branch1)' '