Skip to content

Commit 2122f8b

Browse files
dotdashgitster
authored andcommitted
rev-parse: Add support for the ^! and ^@ syntax
Those shorthands are explained in the rev-parse documentation but were not actually supported by rev-parse itself. gitk internally uses rev-parse to interpret its command line arguments, and being able to use these "limit with parents" syntax is handy there. Signed-off-by: Björn Steinbrink <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 47c6ef1 commit 2122f8b

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

builtin-rev-parse.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,36 @@ static int try_difference(const char *arg)
241241
return 0;
242242
}
243243

244+
static int try_parent_shorthands(const char *arg)
245+
{
246+
char *dotdot;
247+
unsigned char sha1[20];
248+
struct commit *commit;
249+
struct commit_list *parents;
250+
int parents_only;
251+
252+
if ((dotdot = strstr(arg, "^!")))
253+
parents_only = 0;
254+
else if ((dotdot = strstr(arg, "^@")))
255+
parents_only = 1;
256+
257+
if (!dotdot || dotdot[2])
258+
return 0;
259+
260+
*dotdot = 0;
261+
if (get_sha1(arg, sha1))
262+
return 0;
263+
264+
if (!parents_only)
265+
show_rev(NORMAL, sha1, arg);
266+
commit = lookup_commit_reference(sha1);
267+
for (parents = commit->parents; parents; parents = parents->next)
268+
show_rev(parents_only ? NORMAL : REVERSED,
269+
parents->item->object.sha1, arg);
270+
271+
return 1;
272+
}
273+
244274
static int parseopt_dump(const struct option *o, const char *arg, int unset)
245275
{
246276
struct strbuf *parsed = o->value;
@@ -573,6 +603,8 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
573603
/* Not a flag argument */
574604
if (try_difference(arg))
575605
continue;
606+
if (try_parent_shorthands(arg))
607+
continue;
576608
name = arg;
577609
type = NORMAL;
578610
if (*arg == '^') {

t/t6101-rev-parse-parents.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ test_expect_success 'final^1^2 != final^1^1' "test $(git rev-parse final^1^2) !=
2828
test_expect_success 'final^1^3 not valid' "if git rev-parse --verify final^1^3; then false; else :; fi"
2929
test_expect_success '--verify start2^1' 'test_must_fail git rev-parse --verify start2^1'
3030
test_expect_success '--verify start2^0' 'git rev-parse --verify start2^0'
31+
test_expect_success 'final^1^@ = final^1^1 final^1^2' "test \"$(git rev-parse final^1^@)\" = \"$(git rev-parse final^1^1 final^1^2)\""
32+
test_expect_success 'final^1^! = final^1 ^final^1^1 ^final^1^2' "test \"$(git rev-parse final^1^\!)\" = \"$(git rev-parse final^1 ^final^1^1 ^final^1^2)\""
3133

3234
test_expect_success 'repack for next test' 'git repack -a -d'
3335
test_expect_success 'short SHA-1 works' '

0 commit comments

Comments
 (0)