]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/describe.c
t/README: A new section about test coverage
[thirdparty/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"
166185be 7#include "parse-options.h"
9f67d2e8 8#include "diff.h"
908e5310 9
8713ab30
SP
10#define SEEN (1u<<0)
11#define MAX_TAGS (FLAG_BITS - 1)
12
166185be 13static const char * const describe_usage[] = {
1b1dd23f 14 "git describe [options] <committish>*",
9f67d2e8 15 "git describe [options] --dirty",
166185be
PH
16 NULL
17};
908e5310 18
8713ab30 19static int debug; /* Display lots of verbose info */
7e425c4f
SP
20static int all; /* Any valid ref can be used */
21static int tags; /* Allow lightweight tags */
518120e3 22static int longformat;
2d9e7c9f 23static int abbrev = DEFAULT_ABBREV;
8713ab30 24static int max_candidates = 10;
d68dc34c 25static int found_names;
cb2a1cc2 26static const char *pattern;
da2478db 27static int always;
9f67d2e8
JP
28static const char *dirty;
29
30/* diff-index command arguments to check if working tree is dirty. */
31static const char *diff_index_args[] = {
32 "diff-index", "--quiet", "HEAD", "--", NULL
33};
34
908e5310 35
e7eb5034 36struct commit_name {
212945d4 37 struct tag *tag;
03e8b541
SP
38 unsigned prio:2; /* annotated tag = 2, tag = 1, head = 0 */
39 unsigned name_checked:1;
212945d4 40 unsigned char sha1[20];
5a2282de 41 char path[FLEX_ARRAY]; /* more */
e7eb5034 42};
cf69fd49
SP
43static const char *prio_names[] = {
44 "head", "lightweight", "annotated",
45};
908e5310 46
03e8b541
SP
47static int replace_name(struct commit_name *e,
48 int prio,
49 const unsigned char *sha1,
50 struct tag **tag)
51{
52 if (!e || e->prio < prio)
53 return 1;
54
55 if (e->prio == 2 && prio == 2) {
56 /* Multiple annotated tags point to the same commit.
57 * Select one to keep based upon their tagger date.
58 */
59 struct tag *t;
60
61 if (!e->tag) {
62 t = lookup_tag(e->sha1);
63 if (!t || parse_tag(t))
64 return 1;
65 e->tag = t;
66 }
67
68 t = lookup_tag(sha1);
69 if (!t || parse_tag(t))
70 return 0;
71 *tag = t;
72
73 if (e->tag->date < t->date)
74 return 1;
75 }
76
77 return 0;
78}
79
64deb858 80static void add_to_known_names(const char *path,
80dbae03 81 struct commit *commit,
212945d4
SP
82 int prio,
83 const unsigned char *sha1)
908e5310 84{
e7eb5034 85 struct commit_name *e = commit->util;
03e8b541
SP
86 struct tag *tag = NULL;
87 if (replace_name(e, prio, sha1, &tag)) {
e7eb5034
SP
88 size_t len = strlen(path)+1;
89 free(e);
90 e = xmalloc(sizeof(struct commit_name) + len);
03e8b541 91 e->tag = tag;
e7eb5034 92 e->prio = prio;
03e8b541 93 e->name_checked = 0;
212945d4 94 hashcpy(e->sha1, sha1);
e7eb5034
SP
95 memcpy(e->path, path, len);
96 commit->util = e;
908e5310 97 }
d68dc34c 98 found_names = 1;
908e5310
LT
99}
100
8da19775 101static int get_name(const char *path, const unsigned char *sha1, int flag, void *cb_data)
908e5310 102{
8a5a1884 103 int might_be_tag = !prefixcmp(path, "refs/tags/");
feededd0 104 struct commit *commit;
64deb858 105 struct object *object;
feededd0
SP
106 unsigned char peeled[20];
107 int is_tag, prio;
108
8a5a1884
SP
109 if (!all && !might_be_tag)
110 return 0;
111
feededd0
SP
112 if (!peel_ref(path, peeled) && !is_null_sha1(peeled)) {
113 commit = lookup_commit_reference_gently(peeled, 1);
114 if (!commit)
115 return 0;
116 is_tag = !!hashcmp(sha1, commit->object.sha1);
117 } else {
118 commit = lookup_commit_reference_gently(sha1, 1);
119 object = parse_object(sha1);
120 if (!commit || !object)
121 return 0;
122 is_tag = object->type == OBJ_TAG;
123 }
64deb858 124
2d9e7c9f
JH
125 /* If --all, then any refs are used.
126 * If --tags, then any tags are used.
127 * Otherwise only annotated tags are used.
128 */
8a5a1884 129 if (might_be_tag) {
4ed19a3c 130 if (is_tag)
64deb858 131 prio = 2;
4ed19a3c 132 else
64deb858 133 prio = 1;
4ed19a3c
MD
134
135 if (pattern && fnmatch(pattern, path + 10, 0))
136 prio = 0;
64deb858
JH
137 }
138 else
139 prio = 0;
140
635d4134 141 if (!all) {
64deb858
JH
142 if (!prio)
143 return 0;
635d4134 144 }
212945d4 145 add_to_known_names(all ? path + 5 : path + 10, commit, prio, sha1);
908e5310
LT
146 return 0;
147}
148
80dbae03 149struct possible_tag {
80dbae03 150 struct commit_name *name;
cf69fd49
SP
151 int depth;
152 int found_order;
8713ab30 153 unsigned flag_within;
80dbae03
SP
154};
155
cf69fd49
SP
156static int compare_pt(const void *a_, const void *b_)
157{
158 struct possible_tag *a = (struct possible_tag *)a_;
159 struct possible_tag *b = (struct possible_tag *)b_;
cf69fd49
SP
160 if (a->depth != b->depth)
161 return a->depth - b->depth;
162 if (a->found_order != b->found_order)
163 return a->found_order - b->found_order;
164 return 0;
165}
166
1b600e65
SP
167static unsigned long finish_depth_computation(
168 struct commit_list **list,
169 struct possible_tag *best)
170{
171 unsigned long seen_commits = 0;
172 while (*list) {
173 struct commit *c = pop_commit(list);
174 struct commit_list *parents = c->parents;
175 seen_commits++;
176 if (c->object.flags & best->flag_within) {
177 struct commit_list *a = *list;
178 while (a) {
179 struct commit *i = a->item;
180 if (!(i->object.flags & best->flag_within))
181 break;
182 a = a->next;
183 }
184 if (!a)
185 break;
186 } else
187 best->depth++;
188 while (parents) {
189 struct commit *p = parents->item;
190 parse_commit(p);
191 if (!(p->object.flags & SEEN))
192 insert_by_date(p, list);
193 p->object.flags |= c->object.flags;
194 parents = parents->next;
195 }
196 }
197 return seen_commits;
198}
199
212945d4
SP
200static void display_name(struct commit_name *n)
201{
202 if (n->prio == 2 && !n->tag) {
203 n->tag = lookup_tag(n->sha1);
03e8b541 204 if (!n->tag || parse_tag(n->tag))
212945d4 205 die("annotated tag %s not available", n->path);
03e8b541
SP
206 }
207 if (n->tag && !n->name_checked) {
208 if (!n->tag->tag)
209 die("annotated tag %s has no embedded name", n->path);
81dc223d 210 if (strcmp(n->tag->tag, all ? n->path + 5 : n->path))
212945d4 211 warning("tag '%s' is really '%s' here", n->tag->tag, n->path);
03e8b541 212 n->name_checked = 1;
212945d4
SP
213 }
214
215 if (n->tag)
216 printf("%s", n->tag->tag);
217 else
218 printf("%s", n->path);
870cf7d6
JH
219}
220
221static void show_suffix(int depth, const unsigned char *sha1)
222{
223 printf("-%d-g%s", depth, find_unique_abbrev(sha1, abbrev));
212945d4
SP
224}
225
554fe20d 226static void describe(const char *arg, int last_one)
908e5310 227{
4c34a2c5 228 unsigned char sha1[20];
8713ab30 229 struct commit *cmit, *gave_up_on = NULL;
908e5310 230 struct commit_list *list;
908e5310 231 struct commit_name *n;
cf69fd49 232 struct possible_tag all_matches[MAX_TAGS];
8713ab30
SP
233 unsigned int match_cnt = 0, annotated_cnt = 0, cur_match;
234 unsigned long seen_commits = 0;
4d23660e 235 unsigned int unannotated_cnt = 0;
908e5310 236
31fff305
DL
237 if (get_sha1(arg, sha1))
238 die("Not a valid object name %s", arg);
4c34a2c5
JH
239 cmit = lookup_commit_reference(sha1);
240 if (!cmit)
31fff305 241 die("%s is not a valid '%s' object", arg, commit_type);
4c34a2c5 242
e7eb5034 243 n = cmit->util;
7a0d61bb 244 if (n && (tags || all || n->prio == 2)) {
870cf7d6
JH
245 /*
246 * Exact match to an existing ref.
247 */
212945d4 248 display_name(n);
870cf7d6 249 if (longformat)
14d4642e 250 show_suffix(0, n->tag ? n->tag->tagged->sha1 : sha1);
9f67d2e8
JP
251 if (dirty)
252 printf("%s", dirty);
212945d4 253 printf("\n");
908e5310
LT
254 return;
255 }
256
2c33f757
SP
257 if (!max_candidates)
258 die("no tag exactly matches '%s'", sha1_to_hex(cmit->object.sha1));
8713ab30
SP
259 if (debug)
260 fprintf(stderr, "searching to describe %s\n", arg);
261
908e5310 262 list = NULL;
8713ab30 263 cmit->object.flags = SEEN;
908e5310
LT
264 commit_list_insert(cmit, &list);
265 while (list) {
80dbae03 266 struct commit *c = pop_commit(&list);
dccd0c2a 267 struct commit_list *parents = c->parents;
8713ab30 268 seen_commits++;
e7eb5034 269 n = c->util;
908e5310 270 if (n) {
4d23660e
TR
271 if (!tags && !all && n->prio < 2) {
272 unannotated_cnt++;
273 } else if (match_cnt < max_candidates) {
8713ab30
SP
274 struct possible_tag *t = &all_matches[match_cnt++];
275 t->name = n;
276 t->depth = seen_commits - 1;
277 t->flag_within = 1u << match_cnt;
8a8169c0 278 t->found_order = match_cnt;
8713ab30
SP
279 c->object.flags |= t->flag_within;
280 if (n->prio == 2)
281 annotated_cnt++;
282 }
283 else {
284 gave_up_on = c;
285 break;
286 }
287 }
288 for (cur_match = 0; cur_match < match_cnt; cur_match++) {
289 struct possible_tag *t = &all_matches[cur_match];
290 if (!(c->object.flags & t->flag_within))
291 t->depth++;
292 }
293 if (annotated_cnt && !list) {
294 if (debug)
295 fprintf(stderr, "finished search at %s\n",
296 sha1_to_hex(c->object.sha1));
297 break;
dccd0c2a
SP
298 }
299 while (parents) {
300 struct commit *p = parents->item;
301 parse_commit(p);
8713ab30 302 if (!(p->object.flags & SEEN))
dccd0c2a 303 insert_by_date(p, &list);
8713ab30 304 p->object.flags |= c->object.flags;
dccd0c2a 305 parents = parents->next;
80dbae03
SP
306 }
307 }
308
da2478db
JH
309 if (!match_cnt) {
310 const unsigned char *sha1 = cmit->object.sha1;
311 if (always) {
9f67d2e8
JP
312 printf("%s", find_unique_abbrev(sha1, abbrev));
313 if (dirty)
314 printf("%s", dirty);
315 printf("\n");
da2478db
JH
316 return;
317 }
4d23660e
TR
318 if (unannotated_cnt)
319 die("No annotated tags can describe '%s'.\n"
320 "However, there were unannotated tags: try --tags.",
321 sha1_to_hex(sha1));
322 else
323 die("No tags can describe '%s'.\n"
324 "Try --always, or create some tags.",
325 sha1_to_hex(sha1));
da2478db 326 }
80dbae03 327
cf69fd49 328 qsort(all_matches, match_cnt, sizeof(all_matches[0]), compare_pt);
1b600e65
SP
329
330 if (gave_up_on) {
331 insert_by_date(gave_up_on, &list);
332 seen_commits--;
333 }
334 seen_commits += finish_depth_computation(&list, &all_matches[0]);
335 free_commit_list(list);
336
8713ab30
SP
337 if (debug) {
338 for (cur_match = 0; cur_match < match_cnt; cur_match++) {
339 struct possible_tag *t = &all_matches[cur_match];
cf69fd49
SP
340 fprintf(stderr, " %-11s %8d %s\n",
341 prio_names[t->name->prio],
8713ab30
SP
342 t->depth, t->name->path);
343 }
344 fprintf(stderr, "traversed %lu commits\n", seen_commits);
345 if (gave_up_on) {
346 fprintf(stderr,
347 "more than %i tags found; listed %i most recent\n"
348 "gave up search at %s\n",
349 max_candidates, max_candidates,
350 sha1_to_hex(gave_up_on->object.sha1));
351 }
80dbae03 352 }
212945d4
SP
353
354 display_name(all_matches[0].name);
355 if (abbrev)
870cf7d6 356 show_suffix(all_matches[0].depth, cmit->object.sha1);
9f67d2e8
JP
357 if (dirty)
358 printf("%s", dirty);
212945d4 359 printf("\n");
80dbae03 360
8713ab30
SP
361 if (!last_one)
362 clear_commit_marks(cmit, -1);
908e5310
LT
363}
364
9a0eaf83 365int cmd_describe(int argc, const char **argv, const char *prefix)
908e5310 366{
23615708 367 int contains = 0;
166185be
PH
368 struct option options[] = {
369 OPT_BOOLEAN(0, "contains", &contains, "find the tag that comes after the commit"),
370 OPT_BOOLEAN(0, "debug", &debug, "debug search strategy on stderr"),
371 OPT_BOOLEAN(0, "all", &all, "use any ref in .git/refs"),
372 OPT_BOOLEAN(0, "tags", &tags, "use any tag in .git/refs/tags"),
518120e3 373 OPT_BOOLEAN(0, "long", &longformat, "always use long format"),
166185be 374 OPT__ABBREV(&abbrev),
2c33f757
SP
375 OPT_SET_INT(0, "exact-match", &max_candidates,
376 "only output exact matches", 0),
166185be 377 OPT_INTEGER(0, "candidates", &max_candidates,
30ffa603
PH
378 "consider <n> most recent tags (default: 10)"),
379 OPT_STRING(0, "match", &pattern, "pattern",
380 "only consider tags matching <pattern>"),
da2478db
JH
381 OPT_BOOLEAN(0, "always", &always,
382 "show abbreviated commit object as fallback"),
9f67d2e8
JP
383 {OPTION_STRING, 0, "dirty", &dirty, "mark",
384 "append <mark> on dirty working tree (default: \"-dirty\")",
385 PARSE_OPT_OPTARG, NULL, (intptr_t) "-dirty"},
166185be
PH
386 OPT_END(),
387 };
908e5310 388
37782920 389 argc = parse_options(argc, argv, prefix, options, describe_usage, 0);
2c33f757
SP
390 if (max_candidates < 0)
391 max_candidates = 0;
166185be
PH
392 else if (max_candidates > MAX_TAGS)
393 max_candidates = MAX_TAGS;
4c34a2c5 394
8c599c74 395 save_commit_buffer = 0;
8112894d 396
518120e3
SB
397 if (longformat && abbrev == 0)
398 die("--long is incompatible with --abbrev=0");
399
23615708 400 if (contains) {
4b25d091 401 const char **args = xmalloc((7 + argc) * sizeof(char *));
3f7701a4
NP
402 int i = 0;
403 args[i++] = "name-rev";
404 args[i++] = "--name-only";
a2cf9f44 405 args[i++] = "--no-undefined";
da2478db
JH
406 if (always)
407 args[i++] = "--always";
30ffa603 408 if (!all) {
3f7701a4 409 args[i++] = "--tags";
30ffa603
PH
410 if (pattern) {
411 char *s = xmalloc(strlen("--refs=refs/tags/") + strlen(pattern) + 1);
412 sprintf(s, "--refs=refs/tags/%s", pattern);
413 args[i++] = s;
414 }
415 }
4b25d091 416 memcpy(args + i, argv, argc * sizeof(char *));
3f7701a4
NP
417 args[i + argc] = NULL;
418 return cmd_name_rev(i + argc, args, prefix);
23615708
SP
419 }
420
fb423da0 421 for_each_ref(get_name, NULL);
02d56fab 422 if (!found_names && !always)
fb423da0
RS
423 die("No names found, cannot describe anything.");
424
166185be 425 if (argc == 0) {
9f67d2e8
JP
426 if (dirty && !cmd_diff_index(ARRAY_SIZE(diff_index_args) - 1, diff_index_args, prefix))
427 dirty = NULL;
fec9ebf1 428 describe("HEAD", 1);
9f67d2e8
JP
429 } else if (dirty) {
430 die("--dirty is incompatible with committishes");
166185be
PH
431 } else {
432 while (argc-- > 0) {
433 describe(*argv++, argc == 0);
fec9ebf1 434 }
166185be 435 }
908e5310
LT
436 return 0;
437}