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