@matheusbsbs / matheus-bagliano / commits / a45d34691e
Fork 0

rev-parse: --abbrev-ref option to shorten ref name

This applies the shorten_unambiguous_ref function to the object name. Default mode is controlled by core.warnAmbiguousRefs. Else it is given as optional argument to --abbrev-ref={strict|loose}. This should be faster than 'git for-each-ref --format="%(refname:short)" <ref>' for single refs. Signed-off-by: Bert Wesarg <bert.wesarg@googlemail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Bert Wesarg committed Apr 13, 2009 at 13:20 UTC a45d34691ea624e93863e95706eeb1b1909304f3
2 files changed +26 -2
Documentation/git-rev-parse.txt
+5
@@ -84,6 +84,11 @@ OPTIONS
84 unfortunately named tag "master"), and show them as full
85 refnames (e.g. "refs/heads/master").
86
87 +--abbrev-ref[={strict|loose}]::
88 + A non-ambiguous short name of the objects name.
89 + The option core.warnAmbiguousRefs is used to select the strict
90 + abbreviation mode.
91 +
92 --all::
93 Show all refs found in `$GIT_DIR/refs`.
94
builtin-rev-parse.c
+21 -2
@@ -26,6 +26,8 @@ static int show_type = NORMAL;
26 #define SHOW_SYMBOLIC_FULL 2
27 static int symbolic;
28 static int abbrev;
29 +static int abbrev_ref;
30 +static int abbrev_ref_strict;
31 static int output_sq;
32
33 /*
@@ -109,8 +111,8 @@ static void show_rev(int type, const unsigned char *sha1, const char *name)
111 return;
112 def = NULL;
113
112 - if (symbolic && name) {
113 - if (symbolic == SHOW_SYMBOLIC_FULL) {
114 + if ((symbolic || abbrev_ref) && name) {
115 + if (symbolic == SHOW_SYMBOLIC_FULL || abbrev_ref) {
116 unsigned char discard[20];
117 char *full;
118
@@ -125,6 +127,9 @@ static void show_rev(int type, const unsigned char *sha1, const char *name)
127 */
128 break;
129 case 1: /* happy */
130 + if (abbrev_ref)
131 + full = shorten_unambiguous_ref(full,
132 + abbrev_ref_strict);
133 show_with_type(type, full);
134 break;
135 default: /* ambiguous */
@@ -506,6 +511,20 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
511 symbolic = SHOW_SYMBOLIC_FULL;
512 continue;
513 }
514 + if (!prefixcmp(arg, "--abbrev-ref") &&
515 + (!arg[12] || arg[12] == '=')) {
516 + abbrev_ref = 1;
517 + abbrev_ref_strict = warn_ambiguous_refs;
518 + if (arg[12] == '=') {
519 + if (!strcmp(arg + 13, "strict"))
520 + abbrev_ref_strict = 1;
521 + else if (!strcmp(arg + 13, "loose"))
522 + abbrev_ref_strict = 0;
523 + else
524 + die("unknown mode for %s", arg);
525 + }
526 + continue;
527 + }
528 if (!strcmp(arg, "--all")) {
529 for_each_ref(show_reference, NULL);
530 continue;