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 (debug)
178fprintf(stderr, "searching to describe %s\n", arg);
179180
list = NULL;
181cmit->object.flags = SEEN;
182commit_list_insert(cmit, &list);
183while (list) {
184struct commit *c = pop_commit(&list);
185struct commit_list *parents = c->parents;
186seen_commits++;
187n = c->util;
188if (n) {
189if (match_cnt < max_candidates) {
190struct possible_tag *t = &all_matches[match_cnt++];
191t->name = n;
192t->depth = seen_commits - 1;
193t->flag_within = 1u << match_cnt;
194t->found_order = match_cnt;
195c->object.flags |= t->flag_within;
196if (n->prio == 2)
197annotated_cnt++;
198}
199else {
200gave_up_on = c;
201break;
202}
203}
204for (cur_match = 0; cur_match < match_cnt; cur_match++) {
205struct possible_tag *t = &all_matches[cur_match];
206if (!(c->object.flags & t->flag_within))
207t->depth++;
208}
209if (annotated_cnt && !list) {
210if (debug)
211fprintf(stderr, "finished search at %s\n",
212sha1_to_hex(c->object.sha1));
213break;
214}
215while (parents) {
216struct commit *p = parents->item;
217parse_commit(p);
218if (!(p->object.flags & SEEN))
219insert_by_date(p, &list);
220p->object.flags |= c->object.flags;
221parents = parents->next;
222}
223}
224225
if (!match_cnt)
226die("cannot describe '%s'", sha1_to_hex(cmit->object.sha1));
227228
qsort(all_matches, match_cnt, sizeof(all_matches[0]), compare_pt);
229230
if (gave_up_on) {
231insert_by_date(gave_up_on, &list);
232seen_commits--;
233}
234seen_commits += finish_depth_computation(&list, &all_matches[0]);
235free_commit_list(list);
236237
if (debug) {
238for (cur_match = 0; cur_match < match_cnt; cur_match++) {
239struct possible_tag *t = &all_matches[cur_match];
240fprintf(stderr, " %-11s %8d %s\n",
241prio_names[t->name->prio],
242t->depth, t->name->path);
243}
244fprintf(stderr, "traversed %lu commits\n", seen_commits);
245if (gave_up_on) {
246fprintf(stderr,
247"more than %i tags found; listed %i most recent\n"
248"gave up search at %s\n",
249max_candidates, max_candidates,
250sha1_to_hex(gave_up_on->object.sha1));
251}
252}
253if (abbrev == 0)
254printf("%s\n", all_matches[0].name->path );
255else
256printf("%s-%d-g%s\n", all_matches[0].name->path,
257all_matches[0].depth,
258find_unique_abbrev(cmit->object.sha1, abbrev));
259260
if (!last_one)
261clear_commit_marks(cmit, -1);
262}
263264
int cmd_describe(int argc, const char **argv, const char *prefix)
265{
266int contains = 0;
267struct option options[] = {
268OPT_BOOLEAN(0, "contains", &contains, "find the tag that comes after the commit"),
269OPT_BOOLEAN(0, "debug", &debug, "debug search strategy on stderr"),
270OPT_BOOLEAN(0, "all", &all, "use any ref in .git/refs"),
271OPT_BOOLEAN(0, "tags", &tags, "use any tag in .git/refs/tags"),
272OPT__ABBREV(&abbrev),
273OPT_INTEGER(0, "candidates", &max_candidates,
274"consider <n> most recent tags (default: 10)"),
275OPT_STRING(0, "match", &pattern, "pattern",
276"only consider tags matching <pattern>"),
277OPT_END(),
278};
279280
argc = parse_options(argc, argv, options, describe_usage, 0);
281if (max_candidates < 1)
282max_candidates = 1;
283else if (max_candidates > MAX_TAGS)
284max_candidates = MAX_TAGS;
285286
save_commit_buffer = 0;
287288
if (contains) {
289const char **args = xmalloc((6 + argc) * sizeof(char*));
290int i = 0;
291args[i++] = "name-rev";
292args[i++] = "--name-only";
293args[i++] = "--no-undefined";
294if (!all) {
295args[i++] = "--tags";
296if (pattern) {
297char *s = xmalloc(strlen("--refs=refs/tags/") + strlen(pattern) + 1);
298sprintf(s, "--refs=refs/tags/%s", pattern);
299args[i++] = s;
300}
301}
302memcpy(args + i, argv, argc * sizeof(char*));
303args[i + argc] = NULL;
304return cmd_name_rev(i + argc, args, prefix);
305}
306307
if (argc == 0) {
308describe("HEAD", 1);
309} else {
310while (argc-- > 0) {
311describe(*argv++, argc == 0);
312}
313}
314return 0;
315}