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