Skip to content

Commit e9ecee0

Browse files
committed
Merge branch 'jk/rev-parse-double-dashes'
"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 7cdebd8 + 62f162f commit e9ecee0

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
@@ -286,6 +286,7 @@ static int try_difference(const char *arg)
286286
exclude = n;
287287
}
288288
}
289+
*dotdot = '.';
289290
return 1;
290291
}
291292
*dotdot = '.';
@@ -309,8 +310,10 @@ static int try_parent_shorthands(const char *arg)
309310
return 0;
310311

311312
*dotdot = 0;
312-
if (get_sha1_committish(arg, sha1))
313+
if (get_sha1_committish(arg, sha1)) {
314+
*dotdot = '^';
313315
return 0;
316+
}
314317

315318
if (!parents_only)
316319
show_rev(NORMAL, sha1, arg);
@@ -319,6 +322,7 @@ static int try_parent_shorthands(const char *arg)
319322
show_rev(parents_only ? NORMAL : REVERSED,
320323
parents->item->object.sha1, arg);
321324

325+
*dotdot = '^';
322326
return 1;
323327
}
324328

@@ -488,6 +492,7 @@ N_("git rev-parse --parseopt [options] -- [<args>...]\n"
488492
int cmd_rev_parse(int argc, const char **argv, const char *prefix)
489493
{
490494
int i, as_is = 0, verify = 0, quiet = 0, revs_count = 0, type = 0;
495+
int has_dashdash = 0;
491496
int output_prefix = 0;
492497
unsigned char sha1[20];
493498
const char *name = NULL;
@@ -501,6 +506,13 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
501506
if (argc > 1 && !strcmp("-h", argv[1]))
502507
usage(builtin_rev_parse_usage);
503508

509+
for (i = 1; i < argc; i++) {
510+
if (!strcmp(argv[i], "--")) {
511+
has_dashdash = 1;
512+
break;
513+
}
514+
}
515+
504516
prefix = setup_git_directory();
505517
git_config(git_default_config, NULL);
506518
for (i = 1; i < argc; i++) {
@@ -788,6 +800,8 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
788800
}
789801
if (verify)
790802
die_no_single_rev(quiet);
803+
if (has_dashdash)
804+
die("bad revision '%s'", arg);
791805
as_is = 1;
792806
if (!show_file(arg, output_prefix))
793807
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)