use build-time SHELL_PATH in test scripts
[tgit.git] / builtin-describe.c
blob05e309f5ad15f9a6f85df34322bb59152b4e37be
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"
9 #define SEEN (1u<<0)
10 #define MAX_TAGS (FLAG_BITS - 1)
12 static const char * const describe_usage[] = {
13 "git-describe [options] <committish>*",
14 NULL
17 static int debug; /* Display lots of verbose info */
18 static int all; /* Default to annotated tags only */
19 static int tags; /* But allow any tags if --tags is specified */
20 static int abbrev = DEFAULT_ABBREV;
21 static int max_candidates = 10;
22 const char *pattern = NULL;
24 struct commit_name {
25 int prio; /* annotated tag = 2, tag = 1, head = 0 */
26 char path[FLEX_ARRAY]; /* more */
28 static const char *prio_names[] = {
29 "head", "lightweight", "annotated",
32 static void add_to_known_names(const char *path,
33 struct commit *commit,
34 int prio)
36 struct commit_name *e = commit->util;
37 if (!e || e->prio < prio) {
38 size_t len = strlen(path)+1;
39 free(e);
40 e = xmalloc(sizeof(struct commit_name) + len);
41 e->prio = prio;
42 memcpy(e->path, path, len);
43 commit->util = e;
47 static int get_name(const char *path, const unsigned char *sha1, int flag, void *cb_data)
49 int might_be_tag = !prefixcmp(path, "refs/tags/");
50 struct commit *commit;
51 struct object *object;
52 unsigned char peeled[20];
53 int is_tag, prio;
55 if (!all && !might_be_tag)
56 return 0;
58 if (!peel_ref(path, peeled) && !is_null_sha1(peeled)) {
59 commit = lookup_commit_reference_gently(peeled, 1);
60 if (!commit)
61 return 0;
62 is_tag = !!hashcmp(sha1, commit->object.sha1);
63 } else {
64 commit = lookup_commit_reference_gently(sha1, 1);
65 object = parse_object(sha1);
66 if (!commit || !object)
67 return 0;
68 is_tag = object->type == OBJ_TAG;
71 /* If --all, then any refs are used.
72 * If --tags, then any tags are used.
73 * Otherwise only annotated tags are used.
75 if (might_be_tag) {
76 if (is_tag) {
77 prio = 2;
78 if (pattern && fnmatch(pattern, path + 10, 0))
79 prio = 0;
80 } else
81 prio = 1;
83 else
84 prio = 0;
86 if (!all) {
87 if (!prio)
88 return 0;
89 if (!tags && prio < 2)
90 return 0;
92 add_to_known_names(all ? path + 5 : path + 10, commit, prio);
93 return 0;
96 struct possible_tag {
97 struct commit_name *name;
98 int depth;
99 int found_order;
100 unsigned flag_within;
103 static int compare_pt(const void *a_, const void *b_)
105 struct possible_tag *a = (struct possible_tag *)a_;
106 struct possible_tag *b = (struct possible_tag *)b_;
107 if (a->name->prio != b->name->prio)
108 return b->name->prio - a->name->prio;
109 if (a->depth != b->depth)
110 return a->depth - b->depth;
111 if (a->found_order != b->found_order)
112 return a->found_order - b->found_order;
113 return 0;
116 static unsigned long finish_depth_computation(
117 struct commit_list **list,
118 struct possible_tag *best)
120 unsigned long seen_commits = 0;
121 while (*list) {
122 struct commit *c = pop_commit(list);
123 struct commit_list *parents = c->parents;
124 seen_commits++;
125 if (c->object.flags & best->flag_within) {
126 struct commit_list *a = *list;
127 while (a) {
128 struct commit *i = a->item;
129 if (!(i->object.flags & best->flag_within))
130 break;
131 a = a->next;
133 if (!a)
134 break;
135 } else
136 best->depth++;
137 while (parents) {
138 struct commit *p = parents->item;
139 parse_commit(p);
140 if (!(p->object.flags & SEEN))
141 insert_by_date(p, list);
142 p->object.flags |= c->object.flags;
143 parents = parents->next;
146 return seen_commits;
149 static void describe(const char *arg, int last_one)
151 unsigned char sha1[20];
152 struct commit *cmit, *gave_up_on = NULL;
153 struct commit_list *list;
154 static int initialized = 0;
155 struct commit_name *n;
156 struct possible_tag all_matches[MAX_TAGS];
157 unsigned int match_cnt = 0, annotated_cnt = 0, cur_match;
158 unsigned long seen_commits = 0;
160 if (get_sha1(arg, sha1))
161 die("Not a valid object name %s", arg);
162 cmit = lookup_commit_reference(sha1);
163 if (!cmit)
164 die("%s is not a valid '%s' object", arg, commit_type);
166 if (!initialized) {
167 initialized = 1;
168 for_each_ref(get_name, NULL);
171 n = cmit->util;
172 if (n) {
173 printf("%s\n", n->path);
174 return;
177 if (!max_candidates)
178 die("no tag exactly matches '%s'", sha1_to_hex(cmit->object.sha1));
179 if (debug)
180 fprintf(stderr, "searching to describe %s\n", arg);
182 list = NULL;
183 cmit->object.flags = SEEN;
184 commit_list_insert(cmit, &list);
185 while (list) {
186 struct commit *c = pop_commit(&list);
187 struct commit_list *parents = c->parents;
188 seen_commits++;
189 n = c->util;
190 if (n) {
191 if (match_cnt < max_candidates) {
192 struct possible_tag *t = &all_matches[match_cnt++];
193 t->name = n;
194 t->depth = seen_commits - 1;
195 t->flag_within = 1u << match_cnt;
196 t->found_order = match_cnt;
197 c->object.flags |= t->flag_within;
198 if (n->prio == 2)
199 annotated_cnt++;
201 else {
202 gave_up_on = c;
203 break;
206 for (cur_match = 0; cur_match < match_cnt; cur_match++) {
207 struct possible_tag *t = &all_matches[cur_match];
208 if (!(c->object.flags & t->flag_within))
209 t->depth++;
211 if (annotated_cnt && !list) {
212 if (debug)
213 fprintf(stderr, "finished search at %s\n",
214 sha1_to_hex(c->object.sha1));
215 break;
217 while (parents) {
218 struct commit *p = parents->item;
219 parse_commit(p);
220 if (!(p->object.flags & SEEN))
221 insert_by_date(p, &list);
222 p->object.flags |= c->object.flags;
223 parents = parents->next;
227 if (!match_cnt)
228 die("cannot describe '%s'", sha1_to_hex(cmit->object.sha1));
230 qsort(all_matches, match_cnt, sizeof(all_matches[0]), compare_pt);
232 if (gave_up_on) {
233 insert_by_date(gave_up_on, &list);
234 seen_commits--;
236 seen_commits += finish_depth_computation(&list, &all_matches[0]);
237 free_commit_list(list);
239 if (debug) {
240 for (cur_match = 0; cur_match < match_cnt; cur_match++) {
241 struct possible_tag *t = &all_matches[cur_match];
242 fprintf(stderr, " %-11s %8d %s\n",
243 prio_names[t->name->prio],
244 t->depth, t->name->path);
246 fprintf(stderr, "traversed %lu commits\n", seen_commits);
247 if (gave_up_on) {
248 fprintf(stderr,
249 "more than %i tags found; listed %i most recent\n"
250 "gave up search at %s\n",
251 max_candidates, max_candidates,
252 sha1_to_hex(gave_up_on->object.sha1));
255 if (abbrev == 0)
256 printf("%s\n", all_matches[0].name->path );
257 else
258 printf("%s-%d-g%s\n", all_matches[0].name->path,
259 all_matches[0].depth,
260 find_unique_abbrev(cmit->object.sha1, abbrev));
262 if (!last_one)
263 clear_commit_marks(cmit, -1);
266 int cmd_describe(int argc, const char **argv, const char *prefix)
268 int contains = 0;
269 struct option options[] = {
270 OPT_BOOLEAN(0, "contains", &contains, "find the tag that comes after the commit"),
271 OPT_BOOLEAN(0, "debug", &debug, "debug search strategy on stderr"),
272 OPT_BOOLEAN(0, "all", &all, "use any ref in .git/refs"),
273 OPT_BOOLEAN(0, "tags", &tags, "use any tag in .git/refs/tags"),
274 OPT__ABBREV(&abbrev),
275 OPT_SET_INT(0, "exact-match", &max_candidates,
276 "only output exact matches", 0),
277 OPT_INTEGER(0, "candidates", &max_candidates,
278 "consider <n> most recent tags (default: 10)"),
279 OPT_STRING(0, "match", &pattern, "pattern",
280 "only consider tags matching <pattern>"),
281 OPT_END(),
284 argc = parse_options(argc, argv, options, describe_usage, 0);
285 if (max_candidates < 0)
286 max_candidates = 0;
287 else if (max_candidates > MAX_TAGS)
288 max_candidates = MAX_TAGS;
290 save_commit_buffer = 0;
292 if (contains) {
293 const char **args = xmalloc((6 + argc) * sizeof(char*));
294 int i = 0;
295 args[i++] = "name-rev";
296 args[i++] = "--name-only";
297 args[i++] = "--no-undefined";
298 if (!all) {
299 args[i++] = "--tags";
300 if (pattern) {
301 char *s = xmalloc(strlen("--refs=refs/tags/") + strlen(pattern) + 1);
302 sprintf(s, "--refs=refs/tags/%s", pattern);
303 args[i++] = s;
306 memcpy(args + i, argv, argc * sizeof(char*));
307 args[i + argc] = NULL;
308 return cmd_name_rev(i + argc, args, prefix);
311 if (argc == 0) {
312 describe("HEAD", 1);
313 } else {
314 while (argc-- > 0) {
315 describe(*argv++, argc == 0);
318 return 0;