Skip to content

Commit aac4fac

Browse files
peffgitster
authored andcommitted
get_sha1: don't die() on bogus search strings
The get_sha1() function generally returns an error code rather than dying, and we sometimes speculatively call it with something that may be a revision or a pathspec, in order to see which one it might be. If it sees a bogus ":/" search string, though, it complains, without giving the caller the opportunity to recover. We can demonstrate this in t6133 by looking for ":/*.t", which should mean "*.t at the root of the tree", but instead dies because of the invalid regex (the "*" has nothing to operate on). We can fix this by returning an error rather than calling die(). Unfortunately, the tradeoff is that the error message is slightly worse in cases where we _do_ know we have a rev. E.g., running "git log ':/*.t' --" before yielded: fatal: Invalid search pattern: *.t and now we get only: fatal: bad revision ':/*.t' There's not a simple way to fix this short of passing a "quiet" flag all the way through the get_sha1() stack. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent df714f8 commit aac4fac

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

sha1_name.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -858,12 +858,12 @@ static int get_sha1_oneline(const char *prefix, unsigned char *sha1,
858858

859859
if (prefix[0] == '!') {
860860
if (prefix[1] != '!')
861-
die ("Invalid search pattern: %s", prefix);
861+
return -1;
862862
prefix++;
863863
}
864864

865865
if (regcomp(&regex, prefix, REG_EXTENDED))
866-
die("Invalid search pattern: %s", prefix);
866+
return -1;
867867

868868
for (l = list; l; l = l->next) {
869869
l->item->object.flags |= ONELINE_SEEN;

t/t6133-pathspec-rev-dwim.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,14 @@ test_expect_success '@{foo} with metacharacters dwims to rev' '
3535
test_cmp expect actual
3636
'
3737

38+
test_expect_success ':/*.t from a subdir dwims to a pathspec' '
39+
mkdir subdir &&
40+
(
41+
cd subdir &&
42+
git log -- ":/*.t" >expect &&
43+
git log ":/*.t" >actual &&
44+
test_cmp expect actual
45+
)
46+
'
47+
3848
test_done

0 commit comments

Comments
 (0)