Merge branch 'jc/maint-log-merge-left-right'
[git/haiku.git] / builtin-describe.c
blob2342913df622f6759fd45b58e5ee2899ade9fbfb
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 longformat;
21 static int abbrev = DEFAULT_ABBREV;
22 static int max_candidates = 10;
23 const char *pattern = NULL;
25 struct commit_name {
26 int prio; /* annotated tag = 2, tag = 1, head = 0 */
27 char path[FLEX_ARRAY]; /* more */
29 static const char *prio_names[] = {
30 "head", "lightweight", "annotated",
33 static void add_to_known_names(const char *path,
34 struct commit *commit,
35 int prio)
37 struct commit_name *e = commit->util;
38 if (!e || e->prio < prio) {
39 size_t len = strlen(path)+1;
40 free(e);
41 e = xmalloc(sizeof(struct commit_name) + len);
42 e->prio = prio;
43 memcpy(e->path, path, len);
44 commit->util = e;
48 static int get_name(const char *path, const unsigned char *sha1, int flag, void *cb_data)
50 int might_be_tag = !prefixcmp(path, "refs/tags/");
51 struct commit *commit;
52 struct object *object;
53 unsigned char peeled[20];
54 int is_tag, prio;
56 if (!all && !might_be_tag)
57 return 0;
59 if (!peel_ref(path, peeled) && !is_null_sha1(peeled)) {
60 commit = lookup_commit_reference_gently(peeled, 1);
61 if (!commit)
62 return 0;
63 is_tag = !!hashcmp(sha1, commit->object.sha1);
64 } else {
65 commit = lookup_commit_reference_gently(sha1, 1);
66 object = parse_object(sha1);
67 if (!commit || !object)
68 return 0;
69 is_tag = object->type == OBJ_TAG;
72 /* If --all, then any refs are used.
73 * If --tags, then any tags are used.
74 * Otherwise only annotated tags are used.
76 if (might_be_tag) {
77 if (is_tag) {
78 prio = 2;
79 if (pattern && fnmatch(pattern, path + 10, 0))
80 prio = 0;
81 } else
82 prio = 1;
84 else
85 prio = 0;
87 if (!all) {
88 if (!prio)
89 return 0;
90 if (!tags && prio < 2)
91 return 0;
93 add_to_known_names(all ? path + 5 : path + 10, commit, prio);
94 return 0;
97 struct possible_tag {
98 struct commit_name *name;
99 int depth;
100 int found_order;
101 unsigned flag_within;
104 static int compare_pt(const void *a_, const void *b_)
106 struct possible_tag *a = (struct possible_tag *)a_;
107 struct possible_tag *b = (struct possible_tag *)b_;
108 if (a->name->prio != b->name->prio)
109 return b->name->prio - a->name->prio;
110 if (a->depth != b->depth)
111 return a->depth - b->depth;
112 if (a->found_order != b->found_order)
113 return a->found_order - b->found_order;
114 return 0;
117 static unsigned long finish_depth_computation(
118 struct commit_list **list,
119 struct possible_tag *best)
121 unsigned long seen_commits = 0;
122 while (*list) {
123 struct commit *c = pop_commit(list);
124 struct commit_list *parents = c->parents;
125 seen_commits++;
126 if (c->object.flags & best->flag_within) {
127 struct commit_list *a = *list;
128 while (a) {
129 struct commit *i = a->item;
130 if (!(i->object.flags & best->flag_within))
131 break;
132 a = a->next;
134 if (!a)
135 break;
136 } else
137 best->depth++;
138 while (parents) {
139 struct commit *p = parents->item;
140 parse_commit(p);
141 if (!(p->object.flags & SEEN))
142 insert_by_date(p, list);
143 p->object.flags |= c->object.flags;
144 parents = parents->next;
147 return seen_commits;
150 static void describe(const char *arg, int last_one)
152 unsigned char sha1[20];
153 struct commit *cmit, *gave_up_on = NULL;
154 struct commit_list *list;
155 static int initialized = 0;
156 struct commit_name *n;
157 struct possible_tag all_matches[MAX_TAGS];
158 unsigned int match_cnt = 0, annotated_cnt = 0, cur_match;
159 unsigned long seen_commits = 0;
161 if (get_sha1(arg, sha1))
162 die("Not a valid object name %s", arg);
163 cmit = lookup_commit_reference(sha1);
164 if (!cmit)
165 die("%s is not a valid '%s' object", arg, commit_type);
167 if (!initialized) {
168 initialized = 1;
169 for_each_ref(get_name, NULL);
172 n = cmit->util;
173 if (n) {
174 if (!longformat)
175 printf("%s\n", n->path);
176 else
177 printf("%s-0-g%s\n", n->path,
178 find_unique_abbrev(cmit->object.sha1, abbrev));
179 return;
182 if (!max_candidates)
183 die("no tag exactly matches '%s'", sha1_to_hex(cmit->object.sha1));
184 if (debug)
185 fprintf(stderr, "searching to describe %s\n", arg);
187 list = NULL;
188 cmit->object.flags = SEEN;
189 commit_list_insert(cmit, &list);
190 while (list) {
191 struct commit *c = pop_commit(&list);
192 struct commit_list *parents = c->parents;
193 seen_commits++;
194 n = c->util;
195 if (n) {
196 if (match_cnt < max_candidates) {
197 struct possible_tag *t = &all_matches[match_cnt++];
198 t->name = n;
199 t->depth = seen_commits - 1;
200 t->flag_within = 1u << match_cnt;
201 t->found_order = match_cnt;
202 c->object.flags |= t->flag_within;
203 if (n->prio == 2)
204 annotated_cnt++;
206 else {
207 gave_up_on = c;
208 break;
211 for (cur_match = 0; cur_match < match_cnt; cur_match++) {
212 struct possible_tag *t = &all_matches[cur_match];
213 if (!(c->object.flags & t->flag_within))
214 t->depth++;
216 if (annotated_cnt && !list) {
217 if (debug)
218 fprintf(stderr, "finished search at %s\n",
219 sha1_to_hex(c->object.sha1));
220 break;
222 while (parents) {
223 struct commit *p = parents->item;
224 parse_commit(p);
225 if (!(p->object.flags & SEEN))
226 insert_by_date(p, &list);
227 p->object.flags |= c->object.flags;
228 parents = parents->next;
232 if (!match_cnt)
233 die("cannot describe '%s'", sha1_to_hex(cmit->object.sha1));
235 qsort(all_matches, match_cnt, sizeof(all_matches[0]), compare_pt);
237 if (gave_up_on) {
238 insert_by_date(gave_up_on, &list);
239 seen_commits--;
241 seen_commits += finish_depth_computation(&list, &all_matches[0]);
242 free_commit_list(list);
244 if (debug) {
245 for (cur_match = 0; cur_match < match_cnt; cur_match++) {
246 struct possible_tag *t = &all_matches[cur_match];
247 fprintf(stderr, " %-11s %8d %s\n",
248 prio_names[t->name->prio],
249 t->depth, t->name->path);
251 fprintf(stderr, "traversed %lu commits\n", seen_commits);
252 if (gave_up_on) {
253 fprintf(stderr,
254 "more than %i tags found; listed %i most recent\n"
255 "gave up search at %s\n",
256 max_candidates, max_candidates,
257 sha1_to_hex(gave_up_on->object.sha1));
260 if (abbrev == 0)
261 printf("%s\n", all_matches[0].name->path );
262 else
263 printf("%s-%d-g%s\n", all_matches[0].name->path,
264 all_matches[0].depth,
265 find_unique_abbrev(cmit->object.sha1, abbrev));
267 if (!last_one)
268 clear_commit_marks(cmit, -1);
271 int cmd_describe(int argc, const char **argv, const char *prefix)
273 int contains = 0;
274 struct option options[] = {
275 OPT_BOOLEAN(0, "contains", &contains, "find the tag that comes after the commit"),
276 OPT_BOOLEAN(0, "debug", &debug, "debug search strategy on stderr"),
277 OPT_BOOLEAN(0, "all", &all, "use any ref in .git/refs"),
278 OPT_BOOLEAN(0, "tags", &tags, "use any tag in .git/refs/tags"),
279 OPT_BOOLEAN(0, "long", &longformat, "always use long format"),
280 OPT__ABBREV(&abbrev),
281 OPT_SET_INT(0, "exact-match", &max_candidates,
282 "only output exact matches", 0),
283 OPT_INTEGER(0, "candidates", &max_candidates,
284 "consider <n> most recent tags (default: 10)"),
285 OPT_STRING(0, "match", &pattern, "pattern",
286 "only consider tags matching <pattern>"),
287 OPT_END(),
290 argc = parse_options(argc, argv, options, describe_usage, 0);
291 if (max_candidates < 0)
292 max_candidates = 0;
293 else if (max_candidates > MAX_TAGS)
294 max_candidates = MAX_TAGS;
296 save_commit_buffer = 0;
298 if (longformat && abbrev == 0)
299 die("--long is incompatible with --abbrev=0");
301 if (contains) {
302 const char **args = xmalloc((6 + argc) * sizeof(char*));
303 int i = 0;
304 args[i++] = "name-rev";
305 args[i++] = "--name-only";
306 args[i++] = "--no-undefined";
307 if (!all) {
308 args[i++] = "--tags";
309 if (pattern) {
310 char *s = xmalloc(strlen("--refs=refs/tags/") + strlen(pattern) + 1);
311 sprintf(s, "--refs=refs/tags/%s", pattern);
312 args[i++] = s;
315 memcpy(args + i, argv, argc * sizeof(char*));
316 args[i + argc] = NULL;
317 return cmd_name_rev(i + argc, args, prefix);
320 if (argc == 0) {
321 describe("HEAD", 1);
322 } else {
323 while (argc-- > 0) {
324 describe(*argv++, argc == 0);
327 return 0;