@matheusbsbs / matheus-bagliano / commits / 2122f8b963
Fork 0

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 <B.Steinbrink@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Björn Steinbrink committed Jul 26, 2008 at 18:37 UTC 2122f8b963d49a59762e121c2da571c2348dcce3
2 files changed +34
builtin-rev-parse.c
+32
@@ -241,6 +241,36 @@ static int try_difference(const char *arg)
241 return 0;
242 }
243
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 +
274 static int parseopt_dump(const struct option *o, const char *arg, int unset)
275 {
276 struct strbuf *parsed = o->value;
@@ -573,6 +603,8 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
603 /* Not a flag argument */
604 if (try_difference(arg))
605 continue;
606 + if (try_parent_shorthands(arg))
607 + continue;
608 name = arg;
609 type = NORMAL;
610 if (*arg == '^') {
t/t6101-rev-parse-parents.sh
+2
@@ -28,6 +28,8 @@ test_expect_success 'final^1^2 != final^1^1' "test $(git rev-parse final^1^2) !=
28 test_expect_success 'final^1^3 not valid' "if git rev-parse --verify final^1^3; then false; else :; fi"
29 test_expect_success '--verify start2^1' 'test_must_fail git rev-parse --verify start2^1'
30 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)\""
33
34 test_expect_success 'repack for next test' 'git repack -a -d'
35 test_expect_success 'short SHA-1 works' '