8 static const char describe_usage[] =
9 "git-describe [--all] [--tags] [--abbrev=<n>] <committish>*";
11 static int all = 0; /* Default to annotated tags only */
12 static int tags = 0; /* But allow any tags if --tags is specified */
14 static int abbrev = DEFAULT_ABBREV;
16 static int names = 0, allocs = 0;
17 static struct commit_name {
18 const struct commit *commit;
19 int prio; /* annotated tag = 2, tag = 1, head = 0 */
20 char path[FLEX_ARRAY]; /* more */
21 } **name_array = NULL;
23 static struct commit_name *match(struct commit *cmit)
26 struct commit_name **p = name_array;
29 struct commit_name *n = *p++;
30 if (n->commit == cmit)
36 static void add_to_known_names(const char *path,
37 const struct commit *commit,
41 int len = strlen(path)+1;
42 struct commit_name *name = xmalloc(sizeof(struct commit_name) + len);
44 name->commit = commit;
46 memcpy(name->path, path, len);
49 allocs = (idx + 50) * 3 / 2;
50 name_array = xrealloc(name_array, allocs*sizeof(*name_array));
52 name_array[idx] = name;
56 static int get_name(const char *path, const unsigned char *sha1)
58 struct commit *commit = lookup_commit_reference_gently(sha1, 1);
59 struct object *object;
64 object = parse_object(sha1);
65 /* If --all, then any refs are used.
66 * If --tags, then any tags are used.
67 * Otherwise only annotated tags are used.
69 if (!strncmp(path, "refs/tags/", 10)) {
70 if (object->type == tag_type)
81 if (!tags && prio < 2)
84 add_to_known_names(all ? path + 5 : path + 10, commit, prio);
88 static int compare_names(const void *_a, const void *_b)
90 struct commit_name *a = *(struct commit_name **)_a;
91 struct commit_name *b = *(struct commit_name **)_b;
92 unsigned long a_date = a->commit->date;
93 unsigned long b_date = b->commit->date;
95 if (a->prio != b->prio)
96 return b->prio - a->prio;
97 return (a_date > b_date) ? -1 : (a_date == b_date) ? 0 : 1;
100 static void describe(char *arg, int last_one)
102 unsigned char sha1[20];
104 struct commit_list *list;
105 static int initialized = 0;
106 struct commit_name *n;
108 if (get_sha1(arg, sha1))
109 die("Not a valid object name %s", arg);
110 cmit = lookup_commit_reference(sha1);
112 die("%s is not a valid '%s' object", arg, commit_type);
116 for_each_ref(get_name);
117 qsort(name_array, names, sizeof(*name_array), compare_names);
122 printf("%s\n", n->path);
127 commit_list_insert(cmit, &list);
129 struct commit *c = pop_most_recent_commit(&list, SEEN);
132 printf("%s-g%s\n", n->path,
133 find_unique_abbrev(cmit->object.sha1, abbrev));
135 clear_commit_marks(cmit, SEEN);
139 die("cannot describe '%s'", sha1_to_hex(cmit->object.sha1));
142 int main(int argc, char **argv)
146 for (i = 1; i < argc; i++) {
147 const char *arg = argv[i];
151 else if (!strcmp(arg, "--all"))
153 else if (!strcmp(arg, "--tags"))
155 else if (!strncmp(arg, "--abbrev=", 9)) {
156 abbrev = strtoul(arg + 9, NULL, 10);
157 if (abbrev < MINIMUM_ABBREV || 40 <= abbrev)
158 abbrev = DEFAULT_ABBREV;
161 usage(describe_usage);
168 describe(argv[i], (i == argc - 1));