Make builtin-branch.c use parse_options.
[git/git.git] / builtin-describe.c
CommitLineData
908e5310
LT
1#include "cache.h"
2#include "commit.h"
635d4134 3#include "tag.h"
908e5310 4#include "refs.h"
9a0eaf83 5#include "builtin.h"
23615708 6#include "exec_cmd.h"
908e5310 7
8713ab30
SP
8#define SEEN (1u<<0)
9#define MAX_TAGS (FLAG_BITS - 1)
10
2d9e7c9f
JH
11static const char describe_usage[] =
12"git-describe [--all] [--tags] [--abbrev=<n>] <committish>*";
908e5310 13
8713ab30 14static int debug; /* Display lots of verbose info */
96f1e58f
DR
15static int all; /* Default to annotated tags only */
16static int tags; /* But allow any tags if --tags is specified */
2d9e7c9f 17static int abbrev = DEFAULT_ABBREV;
8713ab30 18static int max_candidates = 10;
908e5310 19
e7eb5034 20struct commit_name {
64deb858 21 int prio; /* annotated tag = 2, tag = 1, head = 0 */
5a2282de 22 char path[FLEX_ARRAY]; /* more */
e7eb5034 23};
cf69fd49
SP
24static const char *prio_names[] = {
25 "head", "lightweight", "annotated",
26};
908e5310 27
64deb858 28static void add_to_known_names(const char *path,
80dbae03 29 struct commit *commit,
64deb858 30 int prio)
908e5310 31{
e7eb5034
SP
32 struct commit_name *e = commit->util;
33 if (!e || e->prio < prio) {
34 size_t len = strlen(path)+1;
35 free(e);
36 e = xmalloc(sizeof(struct commit_name) + len);
37 e->prio = prio;
38 memcpy(e->path, path, len);
39 commit->util = e;
908e5310 40 }
908e5310
LT
41}
42
8da19775 43static int get_name(const char *path, const unsigned char *sha1, int flag, void *cb_data)
908e5310
LT
44{
45 struct commit *commit = lookup_commit_reference_gently(sha1, 1);
64deb858
JH
46 struct object *object;
47 int prio;
48
908e5310
LT
49 if (!commit)
50 return 0;
64deb858 51 object = parse_object(sha1);
2d9e7c9f
JH
52 /* If --all, then any refs are used.
53 * If --tags, then any tags are used.
54 * Otherwise only annotated tags are used.
55 */
cc44c765 56 if (!prefixcmp(path, "refs/tags/")) {
1974632c 57 if (object->type == OBJ_TAG)
64deb858
JH
58 prio = 2;
59 else
60 prio = 1;
61 }
62 else
63 prio = 0;
64
635d4134 65 if (!all) {
64deb858
JH
66 if (!prio)
67 return 0;
68 if (!tags && prio < 2)
635d4134 69 return 0;
635d4134 70 }
64deb858 71 add_to_known_names(all ? path + 5 : path + 10, commit, prio);
908e5310
LT
72 return 0;
73}
74
80dbae03 75struct possible_tag {
80dbae03 76 struct commit_name *name;
cf69fd49
SP
77 int depth;
78 int found_order;
8713ab30 79 unsigned flag_within;
80dbae03
SP
80};
81
cf69fd49
SP
82static int compare_pt(const void *a_, const void *b_)
83{
84 struct possible_tag *a = (struct possible_tag *)a_;
85 struct possible_tag *b = (struct possible_tag *)b_;
86 if (a->name->prio != b->name->prio)
87 return b->name->prio - a->name->prio;
88 if (a->depth != b->depth)
89 return a->depth - b->depth;
90 if (a->found_order != b->found_order)
91 return a->found_order - b->found_order;
92 return 0;
93}
94
1b600e65
SP
95static unsigned long finish_depth_computation(
96 struct commit_list **list,
97 struct possible_tag *best)
98{
99 unsigned long seen_commits = 0;
100 while (*list) {
101 struct commit *c = pop_commit(list);
102 struct commit_list *parents = c->parents;
103 seen_commits++;
104 if (c->object.flags & best->flag_within) {
105 struct commit_list *a = *list;
106 while (a) {
107 struct commit *i = a->item;
108 if (!(i->object.flags & best->flag_within))
109 break;
110 a = a->next;
111 }
112 if (!a)
113 break;
114 } else
115 best->depth++;
116 while (parents) {
117 struct commit *p = parents->item;
118 parse_commit(p);
119 if (!(p->object.flags & SEEN))
120 insert_by_date(p, list);
121 p->object.flags |= c->object.flags;
122 parents = parents->next;
123 }
124 }
125 return seen_commits;
126}
127
554fe20d 128static void describe(const char *arg, int last_one)
908e5310 129{
4c34a2c5 130 unsigned char sha1[20];
8713ab30 131 struct commit *cmit, *gave_up_on = NULL;
908e5310
LT
132 struct commit_list *list;
133 static int initialized = 0;
134 struct commit_name *n;
cf69fd49 135 struct possible_tag all_matches[MAX_TAGS];
8713ab30
SP
136 unsigned int match_cnt = 0, annotated_cnt = 0, cur_match;
137 unsigned long seen_commits = 0;
908e5310 138
31fff305
DL
139 if (get_sha1(arg, sha1))
140 die("Not a valid object name %s", arg);
4c34a2c5
JH
141 cmit = lookup_commit_reference(sha1);
142 if (!cmit)
31fff305 143 die("%s is not a valid '%s' object", arg, commit_type);
4c34a2c5 144
908e5310
LT
145 if (!initialized) {
146 initialized = 1;
cb5d709f 147 for_each_ref(get_name, NULL);
908e5310
LT
148 }
149
e7eb5034 150 n = cmit->util;
908e5310
LT
151 if (n) {
152 printf("%s\n", n->path);
153 return;
154 }
155
8713ab30
SP
156 if (debug)
157 fprintf(stderr, "searching to describe %s\n", arg);
158
908e5310 159 list = NULL;
8713ab30 160 cmit->object.flags = SEEN;
908e5310
LT
161 commit_list_insert(cmit, &list);
162 while (list) {
80dbae03 163 struct commit *c = pop_commit(&list);
dccd0c2a 164 struct commit_list *parents = c->parents;
8713ab30 165 seen_commits++;
e7eb5034 166 n = c->util;
908e5310 167 if (n) {
8713ab30
SP
168 if (match_cnt < max_candidates) {
169 struct possible_tag *t = &all_matches[match_cnt++];
170 t->name = n;
171 t->depth = seen_commits - 1;
172 t->flag_within = 1u << match_cnt;
8a8169c0 173 t->found_order = match_cnt;
8713ab30
SP
174 c->object.flags |= t->flag_within;
175 if (n->prio == 2)
176 annotated_cnt++;
177 }
178 else {
179 gave_up_on = c;
180 break;
181 }
182 }
183 for (cur_match = 0; cur_match < match_cnt; cur_match++) {
184 struct possible_tag *t = &all_matches[cur_match];
185 if (!(c->object.flags & t->flag_within))
186 t->depth++;
187 }
188 if (annotated_cnt && !list) {
189 if (debug)
190 fprintf(stderr, "finished search at %s\n",
191 sha1_to_hex(c->object.sha1));
192 break;
dccd0c2a
SP
193 }
194 while (parents) {
195 struct commit *p = parents->item;
196 parse_commit(p);
8713ab30 197 if (!(p->object.flags & SEEN))
dccd0c2a 198 insert_by_date(p, &list);
8713ab30 199 p->object.flags |= c->object.flags;
dccd0c2a 200 parents = parents->next;
80dbae03
SP
201 }
202 }
203
8713ab30 204 if (!match_cnt)
80dbae03
SP
205 die("cannot describe '%s'", sha1_to_hex(cmit->object.sha1));
206
cf69fd49 207 qsort(all_matches, match_cnt, sizeof(all_matches[0]), compare_pt);
1b600e65
SP
208
209 if (gave_up_on) {
210 insert_by_date(gave_up_on, &list);
211 seen_commits--;
212 }
213 seen_commits += finish_depth_computation(&list, &all_matches[0]);
214 free_commit_list(list);
215
8713ab30
SP
216 if (debug) {
217 for (cur_match = 0; cur_match < match_cnt; cur_match++) {
218 struct possible_tag *t = &all_matches[cur_match];
cf69fd49
SP
219 fprintf(stderr, " %-11s %8d %s\n",
220 prio_names[t->name->prio],
8713ab30
SP
221 t->depth, t->name->path);
222 }
223 fprintf(stderr, "traversed %lu commits\n", seen_commits);
224 if (gave_up_on) {
225 fprintf(stderr,
226 "more than %i tags found; listed %i most recent\n"
227 "gave up search at %s\n",
228 max_candidates, max_candidates,
229 sha1_to_hex(gave_up_on->object.sha1));
230 }
80dbae03 231 }
f127404c
AP
232 if (abbrev == 0)
233 printf("%s\n", all_matches[0].name->path );
234 else
237fb6ca
SP
235 printf("%s-%d-g%s\n", all_matches[0].name->path,
236 all_matches[0].depth,
237 find_unique_abbrev(cmit->object.sha1, abbrev));
80dbae03 238
8713ab30
SP
239 if (!last_one)
240 clear_commit_marks(cmit, -1);
908e5310
LT
241}
242
9a0eaf83 243int cmd_describe(int argc, const char **argv, const char *prefix)
908e5310
LT
244{
245 int i;
23615708 246 int contains = 0;
908e5310
LT
247
248 for (i = 1; i < argc; i++) {
249 const char *arg = argv[i];
908e5310 250
4c34a2c5
JH
251 if (*arg != '-')
252 break;
23615708
SP
253 else if (!strcmp(arg, "--contains"))
254 contains = 1;
8713ab30
SP
255 else if (!strcmp(arg, "--debug"))
256 debug = 1;
4c34a2c5 257 else if (!strcmp(arg, "--all"))
908e5310 258 all = 1;
4c34a2c5 259 else if (!strcmp(arg, "--tags"))
2d9e7c9f 260 tags = 1;
cc44c765 261 else if (!prefixcmp(arg, "--abbrev=")) {
2d9e7c9f 262 abbrev = strtoul(arg + 9, NULL, 10);
f127404c 263 if (abbrev != 0 && (abbrev < MINIMUM_ABBREV || 40 < abbrev))
2d9e7c9f 264 abbrev = DEFAULT_ABBREV;
2d9e7c9f 265 }
cc44c765 266 else if (!prefixcmp(arg, "--candidates=")) {
8713ab30
SP
267 max_candidates = strtoul(arg + 13, NULL, 10);
268 if (max_candidates < 1)
269 max_candidates = 1;
270 else if (max_candidates > MAX_TAGS)
271 max_candidates = MAX_TAGS;
272 }
4c34a2c5 273 else
908e5310 274 usage(describe_usage);
908e5310 275 }
4c34a2c5 276
8c599c74 277 save_commit_buffer = 0;
8112894d 278
23615708
SP
279 if (contains) {
280 const char **args = xmalloc((4 + argc - i) * sizeof(char*));
281 args[0] = "name-rev";
282 args[1] = "--name-only";
283 args[2] = "--tags";
284 memcpy(args + 3, argv + i, (argc - i) * sizeof(char*));
285 args[3 + argc - i] = NULL;
286 return cmd_name_rev(3 + argc - i, args, prefix);
287 }
288
5b6df8e4 289 if (argc <= i)
fec9ebf1 290 describe("HEAD", 1);
4c34a2c5 291 else
fec9ebf1
JH
292 while (i < argc) {
293 describe(argv[i], (i == argc - 1));
294 i++;
295 }
4c34a2c5 296
908e5310
LT
297 return 0;
298}