1#include "cache.h"
2#include "commit.h"
3#include "tag.h"
4#include "refs.h"
5#include "builtin.h"
6#include "exec_cmd.h"
7#include "parse-options.h"
89
#define SEEN (1u<<0)
10#define MAX_TAGS (FLAG_BITS - 1)
1112
static const char * const describe_usage[] = {
13"git-describe [options] <committish>*",
14NULL
15};
1617
static int debug; /* Display lots of verbose info */
18static int all; /* Default to annotated tags only */
19static int tags; /* But allow any tags if --tags is specified */
20static int abbrev = DEFAULT_ABBREV;
21static int max_candidates = 10;
22const char *pattern = NULL;
2324
struct commit_name {
25int prio; /* annotated tag = 2, tag = 1, head = 0 */
26char path[FLEX_ARRAY]; /* more */
27};
28static const char *prio_names[] = {
29"head", "lightweight", "annotated",
30};
3132
static void add_to_known_names(const char *path,
33struct commit *commit,
34int prio)
35{
36struct commit_name *e = commit->util;
37if (!e || e->prio < prio) {
38size_t len = strlen(path)+1;
39free(e);
40e = xmalloc(sizeof(struct commit_name) + len);
41e->prio = prio;
42memcpy(e->path, path, len);
43commit->util = e;
44}
45}
4647
static int get_name(const char *path, const unsigned char *sha1, int flag, void *cb_data)
48{
49int might_be_tag = !prefixcmp(path, "refs/tags/");
50struct commit *commit;
51struct object *object;
52unsigned char peeled[20];
53int is_tag, prio;
5455
if (!all && !might_be_tag)
56return 0;
5758
if (!peel_ref(path, peeled) && !is_null_sha1(peeled)) {
59commit = lookup_commit_reference_gently(peeled, 1);
60if (!commit)
61return 0;
62is_tag = !!hashcmp(sha1, commit->object.sha1);
63} else {
64commit = lookup_commit_reference_gently(sha1, 1);
65object = parse_object(sha1);
66if (!commit || !object)
67return 0;
68is_tag = object->type == OBJ_TAG;
69}
7071
/* If --all, then any refs are used.
72* If --tags, then any tags are used.
73* Otherwise only annotated tags are used.
74*/
75if (might_be_tag) {
76if (is_tag) {
77prio = 2;
78if (pattern && fnmatch(pattern, path + 10, 0))
79prio = 0;
80} else
81prio = 1;
82}
83else
84prio = 0;
8586
if (!all) {
87if (!prio)
88return 0;
89if (!tags && prio < 2)
90return 0;
91}
92add_to_known_names(all ? path + 5 : path + 10, commit, prio);
93return 0;
94}
9596
struct possible_tag {
97struct commit_name *name;
98int depth;
99int found_order;
100unsigned flag_within;
101};
102103
static int compare_pt(const void *a_, const void *b_)
104{
105struct possible_tag *a = (struct possible_tag *)a_;
106struct possible_tag *b = (struct possible_tag *)b_;
107if (a->name->prio != b->name->prio)
108return b->name->prio - a->name->prio;
109if (a->depth != b->depth)
110return a->depth - b->depth;
111if (a->found_order != b->found_order)
112return a->found_order - b->found_order;
113return 0;
114}
115116
static unsigned long finish_depth_computation(
117struct commit_list **list,
118struct possible_tag *best)
119{
120unsigned long seen_commits = 0;
121while (*list) {
122struct commit *c = pop_commit(list);
123struct commit_list *parents = c->parents;
124seen_commits++;
125if (c->object.flags & best->flag_within) {
126struct commit_list *a = *list;
127while (a) {
128struct commit *i = a->item;
129if (!(i->object.flags & best->flag_within))
130break;
131a = a->next;
132}
133if (!a)
134break;
135} else
136best->depth++;
137while (parents) {
138struct commit *p = parents->item;
139parse_commit(p);
140if (!(p->object.flags & SEEN))
141insert_by_date(p, list);
142p->object.flags |= c->object.flags;
143parents = parents->next;
144}
145}
146return seen_commits;
147}
148149
static void describe(const char *arg, int last_one)
150{
151unsigned char sha1[20];
152struct commit *cmit, *gave_up_on = NULL;
153struct commit_list *list;
154static int initialized = 0;
155struct commit_name *n;
156struct possible_tag all_matches[MAX_TAGS];
157unsigned int match_cnt = 0, annotated_cnt = 0, cur_match;
158unsigned long seen_commits = 0;
159160
if (get_sha1(arg, sha1))
161die("Not a valid object name %s", arg);
162cmit = lookup_commit_reference(sha1);
163if (!cmit)
164die("%s is not a valid '%s' object", arg, commit_type);
165166
if (!initialized) {
167initialized = 1;
168for_each_ref(get_name, NULL);
169}
170171
n = cmit->util;
172if (n) {
173printf("%s\n", n->path);
174return;
175}
176177
if (!max_candidates)
178die("no tag exactly matches '%s'", sha1_to_hex(cmit->object.sha1));
179if (debug)
180fprintf(stderr, "searching to describe %s\n", arg);
181182
list = NULL;
183cmit->object.flags = SEEN;
184commit_list_insert(cmit, &list);
185while (list) {
186struct commit *c = pop_commit(&list);
187struct commit_list *parents = c->parents;
188seen_commits++;
189n = c->util;
190if (n) {
191if (match_cnt < max_candidates) {
192struct possible_tag *t = &all_matches[match_cnt++];
193t->name = n;
194t->depth = seen_commits - 1;
195t->flag_within = 1u << match_cnt;
196t->found_order = match_cnt;
197c->object.flags |= t->flag_within;
198if (n->prio == 2)
199annotated_cnt++;
200}
201else {
202gave_up_on = c;
203break;
204}
205}
206for (cur_match = 0; cur_match < match_cnt; cur_match++) {
207struct possible_tag *t = &all_matches[cur_match];
208if (!(c->object.flags & t->flag_within))
209t->depth++;
210}
211if (annotated_cnt && !list) {
212if (debug)
213fprintf(stderr, "finished search at %s\n",
214sha1_to_hex(c->object.sha1));
215break;
216}
217while (parents) {
218struct commit *p = parents->item;
219parse_commit(p);
220if (!(p->object.flags & SEEN))
221insert_by_date(p, &list);
222p->object.flags |= c->object.flags;
223parents = parents->next;
224}
225}
226227
if (!match_cnt)
228die("cannot describe '%s'", sha1_to_hex(cmit->object.sha1));
229230
qsort(all_matches, match_cnt, sizeof(all_matches[0]), compare_pt);
231232
if (gave_up_on) {
233insert_by_date(gave_up_on, &list);
234seen_commits--;
235}
236seen_commits += finish_depth_computation(&list, &all_matches[0]);
237free_commit_list(list);
238239
if (debug) {
240for (cur_match = 0; cur_match < match_cnt; cur_match++) {
241struct possible_tag *t = &all_matches[cur_match];
242fprintf(stderr, " %-11s %8d %s\n",
243prio_names[t->name->prio],
244t->depth, t->name->path);
245}
246fprintf(stderr, "traversed %lu commits\n", seen_commits);
247if (gave_up_on) {
248fprintf(stderr,
249"more than %i tags found; listed %i most recent\n"
250"gave up search at %s\n",
251max_candidates, max_candidates,
252sha1_to_hex(gave_up_on->object.sha1));
253}
254}
255if (abbrev == 0)
256printf("%s\n", all_matches[0].name->path );
257else
258printf("%s-%d-g%s\n", all_matches[0].name->path,
259all_matches[0].depth,
260find_unique_abbrev(cmit->object.sha1, abbrev));
261262
if (!last_one)
263clear_commit_marks(cmit, -1);
264}
265266
int cmd_describe(int argc, const char **argv, const char *prefix)
267{
268int contains = 0;
269struct option options[] = {
270OPT_BOOLEAN(0, "contains", &contains, "find the tag that comes after the commit"),
271OPT_BOOLEAN(0, "debug", &debug, "debug search strategy on stderr"),
272OPT_BOOLEAN(0, "all", &all, "use any ref in .git/refs"),
273OPT_BOOLEAN(0, "tags", &tags, "use any tag in .git/refs/tags"),
274OPT__ABBREV(&abbrev),
275OPT_SET_INT(0, "exact-match", &max_candidates,
276"only output exact matches", 0),
277OPT_INTEGER(0, "candidates", &max_candidates,
278"consider <n> most recent tags (default: 10)"),
279OPT_STRING(0, "match", &pattern, "pattern",
280"only consider tags matching <pattern>"),
281OPT_END(),
282};
283284
argc = parse_options(argc, argv, options, describe_usage, 0);
285if (max_candidates < 0)
286max_candidates = 0;
287else if (max_candidates > MAX_TAGS)
288max_candidates = MAX_TAGS;
289290
save_commit_buffer = 0;
291292
if (contains) {
293const char **args = xmalloc((6 + argc) * sizeof(char*));
294int i = 0;
295args[i++] = "name-rev";
296args[i++] = "--name-only";
297args[i++] = "--no-undefined";
298if (!all) {
299args[i++] = "--tags";
300if (pattern) {
301char *s = xmalloc(strlen("--refs=refs/tags/") + strlen(pattern) + 1);
302sprintf(s, "--refs=refs/tags/%s", pattern);
303args[i++] = s;
304}
305}
306memcpy(args + i, argv, argc * sizeof(char*));
307args[i + argc] = NULL;
308return cmd_name_rev(i + argc, args, prefix);
309}
310311
if (argc == 0) {
312describe("HEAD", 1);
313} else {
314while (argc-- > 0) {
315describe(*argv++, argc == 0);
316}
317}
318return 0;
319}