Skip to content

Commit be941a2

Browse files
committed
Merge branch 'jk/rev-parse-double-dashes' into maint
"git rev-parse <revs> -- <paths>" did not implement the usual disambiguation rules the commands in the "git log" family used in the same way. * jk/rev-parse-double-dashes: rev-parse: be more careful with munging arguments rev-parse: correctly diagnose revision errors before "--"
2 parents 6845e8a + 62f162f commit be941a2

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

builtin/rev-parse.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ static int try_difference(const char *arg)
279279
exclude = n;
280280
}
281281
}
282+
*dotdot = '.';
282283
return 1;
283284
}
284285
*dotdot = '.';
@@ -302,8 +303,10 @@ static int try_parent_shorthands(const char *arg)
302303
return 0;
303304

304305
*dotdot = 0;
305-
if (get_sha1_committish(arg, sha1))
306+
if (get_sha1_committish(arg, sha1)) {
307+
*dotdot = '^';
306308
return 0;
309+
}
307310

308311
if (!parents_only)
309312
show_rev(NORMAL, sha1, arg);
@@ -312,6 +315,7 @@ static int try_parent_shorthands(const char *arg)
312315
show_rev(parents_only ? NORMAL : REVERSED,
313316
parents->item->object.sha1, arg);
314317

318+
*dotdot = '^';
315319
return 1;
316320
}
317321

@@ -476,6 +480,7 @@ N_("git rev-parse --parseopt [options] -- [<args>...]\n"
476480
int cmd_rev_parse(int argc, const char **argv, const char *prefix)
477481
{
478482
int i, as_is = 0, verify = 0, quiet = 0, revs_count = 0, type = 0;
483+
int has_dashdash = 0;
479484
int output_prefix = 0;
480485
unsigned char sha1[20];
481486
const char *name = NULL;
@@ -489,6 +494,13 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
489494
if (argc > 1 && !strcmp("-h", argv[1]))
490495
usage(builtin_rev_parse_usage);
491496

497+
for (i = 1; i < argc; i++) {
498+
if (!strcmp(argv[i], "--")) {
499+
has_dashdash = 1;
500+
break;
501+
}
502+
}
503+
492504
prefix = setup_git_directory();
493505
git_config(git_default_config, NULL);
494506
for (i = 1; i < argc; i++) {
@@ -765,6 +777,8 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
765777
}
766778
if (verify)
767779
die_no_single_rev(quiet);
780+
if (has_dashdash)
781+
die("bad revision '%s'", arg);
768782
as_is = 1;
769783
if (!show_file(arg, output_prefix))
770784
continue;

t/t1506-rev-parse-diagnosis.sh

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,4 +196,28 @@ test_expect_success 'dotdot is not an empty set' '
196196
test_cmp expect actual
197197
'
198198

199+
test_expect_success 'arg before dashdash must be a revision (missing)' '
200+
test_must_fail git rev-parse foobar -- 2>stderr &&
201+
test_i18ngrep "bad revision" stderr
202+
'
203+
204+
test_expect_success 'arg before dashdash must be a revision (file)' '
205+
>foobar &&
206+
test_must_fail git rev-parse foobar -- 2>stderr &&
207+
test_i18ngrep "bad revision" stderr
208+
'
209+
210+
test_expect_success 'arg before dashdash must be a revision (ambiguous)' '
211+
>foobar &&
212+
git update-ref refs/heads/foobar HEAD &&
213+
{
214+
# we do not want to use rev-parse here, because
215+
# we are testing it
216+
cat .git/refs/heads/foobar &&
217+
printf "%s\n" --
218+
} >expect &&
219+
git rev-parse foobar -- >actual &&
220+
test_cmp expect actual
221+
'
222+
199223
test_done

0 commit comments

Comments
 (0)