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

git-describe: --tags and --abbrev

With --tags, not just annontated tags, but also any ref under refs/tags/ are used to name the revision. The number of digits is configurable with the --abbrev=<n> option. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Dec 27, 2005 at 14:40 UTC 2d9e7c9f908d0a025970a5592d47def3d77e77a9
1 file changed +27 -6
describe.c
+27 -6
@@ -5,9 +5,14 @@
5
6 #define SEEN (1u << 0)
7
8 -static const char describe_usage[] = "git-describe [--all] <committish>*";
8 +static const char describe_usage[] =
9 +"git-describe [--all] [--tags] [--abbrev=<n>] <committish>*";
10
11 static int all = 0; /* Default to annotated tags only */
12 +static int tags = 0; /* But allow any tags if --tags is specified */
13 +
14 +#define DEFAULT_ABBREV 8 /* maybe too many */
15 +static int abbrev = DEFAULT_ABBREV;
16
17 static int names = 0, allocs = 0;
18 static struct commit_name {
@@ -50,13 +55,19 @@ static int get_name(const char *path, const unsigned char *sha1)
55 struct commit *commit = lookup_commit_reference_gently(sha1, 1);
56 if (!commit)
57 return 0;
58 + /* If --all, then any refs are used.
59 + * If --tags, then any tags are used.
60 + * Otherwise only annotated tags are used.
61 + */
62 if (!all) {
54 - struct object *object;
63 if (strncmp(path, "refs/tags/", 10))
64 return 0;
57 - object = parse_object(sha1);
58 - if (object->type != tag_type)
59 - return 0;
65 + if (!tags) {
66 + struct object *object;
67 + object = parse_object(sha1);
68 + if (object->type != tag_type)
69 + return 0;
70 + }
71 }
72 add_to_known_names(all ? path : path + 10, commit);
73 return 0;
@@ -96,7 +107,7 @@ static void describe(struct commit *cmit)
107 n = match(c);
108 if (n) {
109 printf("%s-g%s\n", n->path,
99 - find_unique_abbrev(cmit->object.sha1, 8));
110 + find_unique_abbrev(cmit->object.sha1, abbrev));
111 return;
112 }
113 }
@@ -115,6 +126,16 @@ int main(int argc, char **argv)
126 all = 1;
127 continue;
128 }
129 + if (!strcmp(arg, "--tags")) {
130 + tags = 1;
131 + continue;
132 + }
133 + if (!strncmp(arg, "--abbrev=", 9)) {
134 + abbrev = strtoul(arg + 9, NULL, 10);
135 + if (abbrev < 4 || 40 <= abbrev)
136 + abbrev = DEFAULT_ABBREV;
137 + continue;
138 + }
139 if (get_sha1(arg, sha1) < 0)
140 usage(describe_usage);
141 cmit = lookup_commit_reference(sha1);