Skip to content

Commit d7236c4

Browse files
moygitster
authored andcommitted
sha1_name: do not trigger detailed diagnosis for file arguments
diagnose_invalid_sha1_path() is meant to be called to diagnose a misspelt <treeish>:<pathname> when <pathname> does not exist in <treeish>. However, the code may call it if <treeish>:<pathname> is invalid (which triggers another call with only_to_die == 1), but for another reason. This happens when calling e.g. git log existing-file HEAD:existing-file because existing-file is a path and not a revision, the code verifies that the arguments that follow to be paths. This leads to an incorrect message like "existing-file does not exist in HEAD", even though the path exists in HEAD. Check that the search for <pathname> in <treeish> fails before triggering the diagnosis. Signed-off-by: Matthieu Moy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 828ea97 commit d7236c4

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

sha1_name.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1115,7 +1115,7 @@ int get_sha1_with_context_1(const char *name, unsigned char *sha1,
11151115
if (new_filename)
11161116
filename = new_filename;
11171117
ret = get_tree_entry(tree_sha1, filename, sha1, &oc->mode);
1118-
if (only_to_die) {
1118+
if (ret && only_to_die) {
11191119
diagnose_invalid_sha1_path(prefix, filename,
11201120
tree_sha1, object_name);
11211121
free(object_name);

t/t1506-rev-parse-diagnosis.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,4 +171,15 @@ test_expect_success 'relative path when startup_info is NULL' '
171171
grep "BUG: startup_info struct is not initialized." error
172172
'
173173

174+
test_expect_success '<commit>:file correctly diagnosed after a pathname' '
175+
test_must_fail git rev-parse file.txt HEAD:file.txt 1>actual 2>error &&
176+
test_i18ngrep ! "exists on disk" error &&
177+
test_i18ngrep "unknown revision or path not in the working tree" error &&
178+
cat >expect <<-\EOF &&
179+
file.txt
180+
HEAD:file.txt
181+
EOF
182+
test_cmp expect actual
183+
'
184+
174185
test_done

0 commit comments

Comments
 (0)