@matheusbsbs / matheus-bagliano / commits / 2eaf22242f
Fork 0

show-ref --hash=len, --abbrev=len, and --abbrev

This teaches show-ref to abbreviate the object name. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Oct 1, 2006 at 00:27 UTC 2eaf22242f61b13c38c87cbb0e84c84974c52d66
2 files changed +39 -8
Documentation/git-show-ref.txt
+8 -4
@@ -9,7 +9,7 @@ SYNOPSIS
9 --------
10 [verse]
11 'git-show-ref' [-q|--quiet] [--verify] [-h|--head] [-d|--dereference]
12 - [-s|--hash] [--tags] [--heads] [--] <pattern>...
12 + [-s|--hash] [--abbrev] [--tags] [--heads] [--] <pattern>...
13
14 DESCRIPTION
15 -----------
@@ -37,14 +37,13 @@ OPTIONS
37
38 -d, --dereference::
39
40 - Dereference tags into object IDs. They will be shown with "^{}"
40 + Dereference tags into object IDs as well. They will be shown with "^{}"
41 appended.
42
43 -s, --hash::
44
45 Only show the SHA1 hash, not the reference name. When also using
46 - --dereference the dereferenced tag will still be shown after the SHA1,
47 - this maybe a bug.
46 + --dereference the dereferenced tag will still be shown after the SHA1.
47
48 --verify::
49
@@ -52,6 +51,11 @@ OPTIONS
51 Aside from returning an error code of 1, it will also print an error
52 message if '--quiet' was not specified.
53
54 +--abbrev, --abbrev=len::
55 +
56 + Abbreviate the object name. When using `--hash`, you do
57 + not have to say `--hash --abbrev`; `--hash=len` would do.
58 +
59 -q, --quiet::
60
61 Do not print any results to stdout. When combined with '--verify' this
builtin-show-ref.c
+31 -4
@@ -6,12 +6,13 @@
6 static const char show_ref_usage[] = "git show-ref [-q|--quiet] [--verify] [-h|--head] [-d|--dereference] [-s|--hash] [--tags] [--heads] [--] [pattern*]";
7
8 static int deref_tags = 0, show_head = 0, tags_only = 0, heads_only = 0,
9 - found_match = 0, verify = 0, quiet = 0, hash_only = 0;
9 + found_match = 0, verify = 0, quiet = 0, hash_only = 0, abbrev = 0;
10 static const char **pattern;
11
12 static int show_ref(const char *refname, const unsigned char *sha1, int flag, void *cbdata)
13 {
14 struct object *obj;
15 + const char *hex;
16
17 if (tags_only || heads_only) {
18 int match;
@@ -51,13 +52,16 @@ match:
52 }
53 if (quiet)
54 return 0;
55 +
56 + hex = find_unique_abbrev(sha1, abbrev);
57 if (hash_only)
55 - printf("%s\n", sha1_to_hex(sha1));
58 + printf("%s\n", hex);
59 else
57 - printf("%s %s\n", sha1_to_hex(sha1), refname);
60 + printf("%s %s\n", hex, refname);
61 if (deref_tags && obj->type == OBJ_TAG) {
62 obj = deref_tag(obj, refname, 0);
60 - printf("%s %s^{}\n", sha1_to_hex(obj->sha1), refname);
63 + hex = find_unique_abbrev(obj->sha1, abbrev);
64 + printf("%s %s^{}\n", hex, refname);
65 }
66 return 0;
67 }
@@ -94,6 +98,29 @@ int cmd_show_ref(int argc, const char **argv, const char *prefix)
98 hash_only = 1;
99 continue;
100 }
101 + if (!strncmp(arg, "--hash=", 7) ||
102 + (!strncmp(arg, "--abbrev", 8) &&
103 + (arg[8] == '=' || arg[8] == '\0'))) {
104 + if (arg[3] != 'h' && !arg[8])
105 + /* --abbrev only */
106 + abbrev = DEFAULT_ABBREV;
107 + else {
108 + /* --hash= or --abbrev= */
109 + char *end;
110 + if (arg[3] == 'h') {
111 + hash_only = 1;
112 + arg += 7;
113 + }
114 + else
115 + arg += 9;
116 + abbrev = strtoul(arg, &end, 10);
117 + if (*end || abbrev > 40)
118 + usage(show_ref_usage);
119 + if (abbrev < MINIMUM_ABBREV)
120 + abbrev = MINIMUM_ABBREV;
121 + }
122 + continue;
123 + }
124 if (!strcmp(arg, "--verify")) {
125 verify = 1;
126 continue;