blob: 66fc291c8a81de71dee7b597c9ab0dc9d70e8e29 [file] [log] [blame]
Linus Torvalds908e5312005-12-24 13:50:45 -08001#include "cache.h"
2#include "commit.h"
Junio C Hamano635d4132005-12-27 14:36:49 -08003#include "tag.h"
Linus Torvalds908e5312005-12-24 13:50:45 -08004#include "refs.h"
Shawn O. Pearce9a0eaf82007-01-10 06:36:36 -05005#include "builtin.h"
Shawn O. Pearce23615702007-05-21 03:20:25 -04006#include "exec_cmd.h"
Pierre Habouzit166185b2007-10-07 20:54:08 +02007#include "parse-options.h"
Jean Privat9f67d2e2009-10-21 09:35:22 -04008#include "diff.h"
Anders Kaseorg3cfa4db2010-12-09 01:46:08 -05009#include "hash.h"
Linus Torvalds908e5312005-12-24 13:50:45 -080010
Shawn O. Pearce8713ab32007-01-13 17:30:53 -050011#define SEEN (1u<<0)
12#define MAX_TAGS (FLAG_BITS - 1)
13
Pierre Habouzit166185b2007-10-07 20:54:08 +020014static const char * const describe_usage[] = {
Stephan Beyer1b1dd232008-07-13 15:36:15 +020015 "git describe [options] <committish>*",
Jean Privat9f67d2e2009-10-21 09:35:22 -040016 "git describe [options] --dirty",
Pierre Habouzit166185b2007-10-07 20:54:08 +020017 NULL
18};
Linus Torvalds908e5312005-12-24 13:50:45 -080019
Shawn O. Pearce8713ab32007-01-13 17:30:53 -050020static int debug; /* Display lots of verbose info */
Shawn O. Pearce7e425c42008-10-13 07:39:46 -070021static int all; /* Any valid ref can be used */
22static int tags; /* Allow lightweight tags */
Santi Béjar518120e2008-02-25 10:43:33 +010023static int longformat;
Linus Torvaldsdce96482010-10-28 11:28:04 -070024static int abbrev = -1; /* unspecified */
Shawn O. Pearce8713ab32007-01-13 17:30:53 -050025static int max_candidates = 10;
Anders Kaseorg3cfa4db2010-12-09 01:46:08 -050026static struct hash_table names;
Anders Kaseorgd1645d02010-12-09 01:47:29 -050027static int have_util;
Nanako Shiraishicb2a1cc2008-07-16 19:42:14 +090028static const char *pattern;
Junio C Hamanoda2478d2008-03-02 08:51:57 -080029static int always;
Jean Privat9f67d2e2009-10-21 09:35:22 -040030static 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
Linus Torvalds908e5312005-12-24 13:50:45 -080037
Shawn O. Pearcee7eb5032007-01-14 22:16:55 -050038struct commit_name {
Anders Kaseorg3cfa4db2010-12-09 01:46:08 -050039 struct commit_name *next;
40 unsigned char peeled[20];
Shawn O. Pearce212945d2008-02-28 01:22:36 -050041 struct tag *tag;
Shawn O. Pearce03e8b542010-04-12 16:25:29 -070042 unsigned prio:2; /* annotated tag = 2, tag = 1, head = 0 */
43 unsigned name_checked:1;
Shawn O. Pearce212945d2008-02-28 01:22:36 -050044 unsigned char sha1[20];
Anders Kaseorg1e1ade12010-12-09 01:43:32 -050045 const char *path;
Shawn O. Pearcee7eb5032007-01-14 22:16:55 -050046};
Shawn O. Pearcecf69fd42007-01-14 04:37:44 -050047static const char *prio_names[] = {
48 "head", "lightweight", "annotated",
49};
Linus Torvalds908e5312005-12-24 13:50:45 -080050
Anders Kaseorg3cfa4db2010-12-09 01:46:08 -050051static 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
Linus Torvalds11f944d2011-02-18 19:55:19 -080066static int set_util(void *chain, void *data)
Anders Kaseorgd1645d02010-12-09 01:47:29 -050067{
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
Shawn O. Pearce03e8b542010-04-12 16:25:29 -070077static 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
Junio C Hamano64deb852005-12-27 16:09:37 -0800110static void add_to_known_names(const char *path,
Anders Kaseorgd1645d02010-12-09 01:47:29 -0500111 const unsigned char *peeled,
Shawn O. Pearce212945d2008-02-28 01:22:36 -0500112 int prio,
113 const unsigned char *sha1)
Linus Torvalds908e5312005-12-24 13:50:45 -0800114{
Anders Kaseorg3cfa4db2010-12-09 01:46:08 -0500115 struct commit_name *e = find_commit_name(peeled);
Shawn O. Pearce03e8b542010-04-12 16:25:29 -0700116 struct tag *tag = NULL;
117 if (replace_name(e, prio, sha1, &tag)) {
Anders Kaseorg1e1ade12010-12-09 01:43:32 -0500118 if (!e) {
Anders Kaseorg3cfa4db2010-12-09 01:46:08 -0500119 void **pos;
Anders Kaseorg1e1ade12010-12-09 01:43:32 -0500120 e = xmalloc(sizeof(struct commit_name));
Anders Kaseorg3cfa4db2010-12-09 01:46:08 -0500121 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 }
Anders Kaseorg1e1ade12010-12-09 01:43:32 -0500129 }
Shawn O. Pearce03e8b542010-04-12 16:25:29 -0700130 e->tag = tag;
Shawn O. Pearcee7eb5032007-01-14 22:16:55 -0500131 e->prio = prio;
Shawn O. Pearce03e8b542010-04-12 16:25:29 -0700132 e->name_checked = 0;
Shawn O. Pearce212945d2008-02-28 01:22:36 -0500133 hashcpy(e->sha1, sha1);
Anders Kaseorg1e1ade12010-12-09 01:43:32 -0500134 e->path = path;
Linus Torvalds908e5312005-12-24 13:50:45 -0800135 }
Linus Torvalds908e5312005-12-24 13:50:45 -0800136}
137
Junio C Hamano8da19772006-09-20 22:02:01 -0700138static int get_name(const char *path, const unsigned char *sha1, int flag, void *cb_data)
Linus Torvalds908e5312005-12-24 13:50:45 -0800139{
Shawn O. Pearce8a5a1882008-02-24 03:07:28 -0500140 int might_be_tag = !prefixcmp(path, "refs/tags/");
Shawn O. Pearcefeededd2008-02-24 03:07:25 -0500141 unsigned char peeled[20];
142 int is_tag, prio;
Junio C Hamano64deb852005-12-27 16:09:37 -0800143
Shawn O. Pearce8a5a1882008-02-24 03:07:28 -0500144 if (!all && !might_be_tag)
145 return 0;
146
Shawn O. Pearcefeededd2008-02-24 03:07:25 -0500147 if (!peel_ref(path, peeled) && !is_null_sha1(peeled)) {
Anders Kaseorgd1645d02010-12-09 01:47:29 -0500148 is_tag = !!hashcmp(sha1, peeled);
Shawn O. Pearcefeededd2008-02-24 03:07:25 -0500149 } else {
Anders Kaseorgd1645d02010-12-09 01:47:29 -0500150 hashcpy(peeled, sha1);
151 is_tag = 0;
Shawn O. Pearcefeededd2008-02-24 03:07:25 -0500152 }
153
Junio C Hamano2d9e7c92005-12-27 14:40:17 -0800154 /* If --all, then any refs are used.
155 * If --tags, then any tags are used.
156 * Otherwise only annotated tags are used.
157 */
Shawn O. Pearce8a5a1882008-02-24 03:07:28 -0500158 if (might_be_tag) {
Michael Dressel4ed19a32008-06-04 21:06:31 +0200159 if (is_tag)
Junio C Hamano64deb852005-12-27 16:09:37 -0800160 prio = 2;
Michael Dressel4ed19a32008-06-04 21:06:31 +0200161 else
Junio C Hamano64deb852005-12-27 16:09:37 -0800162 prio = 1;
Michael Dressel4ed19a32008-06-04 21:06:31 +0200163
164 if (pattern && fnmatch(pattern, path + 10, 0))
165 prio = 0;
Junio C Hamano635d4132005-12-27 14:36:49 -0800166 }
Junio C Hamano64deb852005-12-27 16:09:37 -0800167 else
168 prio = 0;
169
170 if (!all) {
171 if (!prio)
172 return 0;
Junio C Hamano64deb852005-12-27 16:09:37 -0800173 }
Anders Kaseorgd1645d02010-12-09 01:47:29 -0500174 add_to_known_names(all ? path + 5 : path + 10, peeled, prio, sha1);
Linus Torvalds908e5312005-12-24 13:50:45 -0800175 return 0;
176}
177
Shawn O. Pearce80dbae02007-01-10 06:39:47 -0500178struct possible_tag {
Shawn O. Pearce80dbae02007-01-10 06:39:47 -0500179 struct commit_name *name;
Shawn O. Pearcecf69fd42007-01-14 04:37:44 -0500180 int depth;
181 int found_order;
Shawn O. Pearce8713ab32007-01-13 17:30:53 -0500182 unsigned flag_within;
Shawn O. Pearce80dbae02007-01-10 06:39:47 -0500183};
184
Shawn O. Pearcecf69fd42007-01-14 04:37:44 -0500185static 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_;
Shawn O. Pearcecf69fd42007-01-14 04:37:44 -0500189 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
Shawn O. Pearce1b600e62007-01-27 01:54:21 -0500196static 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))
Thiago Farina47e44ed2010-11-26 23:58:14 -0200221 commit_list_insert_by_date(p, list);
Shawn O. Pearce1b600e62007-01-27 01:54:21 -0500222 p->object.flags |= c->object.flags;
223 parents = parents->next;
224 }
225 }
226 return seen_commits;
227}
228
Shawn O. Pearce212945d2008-02-28 01:22:36 -0500229static void display_name(struct commit_name *n)
230{
231 if (n->prio == 2 && !n->tag) {
232 n->tag = lookup_tag(n->sha1);
Shawn O. Pearce03e8b542010-04-12 16:25:29 -0700233 if (!n->tag || parse_tag(n->tag))
Ævar Arnfjörð Bjarmasone41f1cb2011-02-22 23:42:23 +0000234 die(_("annotated tag %s not available"), n->path);
Shawn O. Pearce03e8b542010-04-12 16:25:29 -0700235 }
236 if (n->tag && !n->name_checked) {
237 if (!n->tag->tag)
Ævar Arnfjörð Bjarmasone41f1cb2011-02-22 23:42:23 +0000238 die(_("annotated tag %s has no embedded name"), n->path);
Shawn O. Pearce81dc2232008-12-26 14:02:01 -0800239 if (strcmp(n->tag->tag, all ? n->path + 5 : n->path))
Ævar Arnfjörð Bjarmasone41f1cb2011-02-22 23:42:23 +0000240 warning(_("tag '%s' is really '%s' here"), n->tag->tag, n->path);
Shawn O. Pearce03e8b542010-04-12 16:25:29 -0700241 n->name_checked = 1;
Shawn O. Pearce212945d2008-02-28 01:22:36 -0500242 }
243
244 if (n->tag)
245 printf("%s", n->tag->tag);
246 else
247 printf("%s", n->path);
Junio C Hamano870cf7d2008-03-03 13:08:26 -0800248}
249
250static void show_suffix(int depth, const unsigned char *sha1)
251{
252 printf("-%d-g%s", depth, find_unique_abbrev(sha1, abbrev));
Shawn O. Pearce212945d2008-02-28 01:22:36 -0500253}
254
Timo Hirvonen554fe202006-06-28 12:04:39 +0300255static void describe(const char *arg, int last_one)
Linus Torvalds908e5312005-12-24 13:50:45 -0800256{
Junio C Hamano4c34a2c2006-01-11 13:57:42 -0800257 unsigned char sha1[20];
Shawn O. Pearce8713ab32007-01-13 17:30:53 -0500258 struct commit *cmit, *gave_up_on = NULL;
Linus Torvalds908e5312005-12-24 13:50:45 -0800259 struct commit_list *list;
Linus Torvalds908e5312005-12-24 13:50:45 -0800260 struct commit_name *n;
Shawn O. Pearcecf69fd42007-01-14 04:37:44 -0500261 struct possible_tag all_matches[MAX_TAGS];
Shawn O. Pearce8713ab32007-01-13 17:30:53 -0500262 unsigned int match_cnt = 0, annotated_cnt = 0, cur_match;
263 unsigned long seen_commits = 0;
Thomas Rast4d236602009-10-28 23:10:06 +0100264 unsigned int unannotated_cnt = 0;
Linus Torvalds908e5312005-12-24 13:50:45 -0800265
Dmitry V. Levin31fff302006-05-09 01:43:38 +0400266 if (get_sha1(arg, sha1))
Ævar Arnfjörð Bjarmasone41f1cb2011-02-22 23:42:23 +0000267 die(_("Not a valid object name %s"), arg);
Junio C Hamano4c34a2c2006-01-11 13:57:42 -0800268 cmit = lookup_commit_reference(sha1);
269 if (!cmit)
Ævar Arnfjörð Bjarmasone41f1cb2011-02-22 23:42:23 +0000270 die(_("%s is not a valid '%s' object"), arg, commit_type);
Junio C Hamano4c34a2c2006-01-11 13:57:42 -0800271
Anders Kaseorg3cfa4db2010-12-09 01:46:08 -0500272 n = find_commit_name(cmit->object.sha1);
Thomas Rast7a0d61b2009-11-18 14:32:26 +0100273 if (n && (tags || all || n->prio == 2)) {
Junio C Hamano870cf7d2008-03-03 13:08:26 -0800274 /*
275 * Exact match to an existing ref.
276 */
Shawn O. Pearce212945d2008-02-28 01:22:36 -0500277 display_name(n);
Junio C Hamano870cf7d2008-03-03 13:08:26 -0800278 if (longformat)
Shawn O. Pearce14d46422008-07-03 02:32:45 +0000279 show_suffix(0, n->tag ? n->tag->tagged->sha1 : sha1);
Jean Privat9f67d2e2009-10-21 09:35:22 -0400280 if (dirty)
281 printf("%s", dirty);
Shawn O. Pearce212945d2008-02-28 01:22:36 -0500282 printf("\n");
Linus Torvalds908e5312005-12-24 13:50:45 -0800283 return;
284 }
285
Shawn O. Pearce2c33f752008-02-24 03:07:31 -0500286 if (!max_candidates)
Ævar Arnfjörð Bjarmasone41f1cb2011-02-22 23:42:23 +0000287 die(_("no tag exactly matches '%s'"), sha1_to_hex(cmit->object.sha1));
Shawn O. Pearce8713ab32007-01-13 17:30:53 -0500288 if (debug)
Ævar Arnfjörð Bjarmasone41f1cb2011-02-22 23:42:23 +0000289 fprintf(stderr, _("searching to describe %s\n"), arg);
Shawn O. Pearce8713ab32007-01-13 17:30:53 -0500290
Anders Kaseorgd1645d02010-12-09 01:47:29 -0500291 if (!have_util) {
Linus Torvalds11f944d2011-02-18 19:55:19 -0800292 for_each_hash(&names, set_util, NULL);
Anders Kaseorgd1645d02010-12-09 01:47:29 -0500293 have_util = 1;
294 }
295
Linus Torvalds908e5312005-12-24 13:50:45 -0800296 list = NULL;
Shawn O. Pearce8713ab32007-01-13 17:30:53 -0500297 cmit->object.flags = SEEN;
Linus Torvalds908e5312005-12-24 13:50:45 -0800298 commit_list_insert(cmit, &list);
299 while (list) {
Shawn O. Pearce80dbae02007-01-10 06:39:47 -0500300 struct commit *c = pop_commit(&list);
Shawn O. Pearcedccd0c22007-01-13 17:27:52 -0500301 struct commit_list *parents = c->parents;
Shawn O. Pearce8713ab32007-01-13 17:30:53 -0500302 seen_commits++;
Shawn O. Pearcee7eb5032007-01-14 22:16:55 -0500303 n = c->util;
Linus Torvalds908e5312005-12-24 13:50:45 -0800304 if (n) {
Thomas Rast4d236602009-10-28 23:10:06 +0100305 if (!tags && !all && n->prio < 2) {
306 unannotated_cnt++;
307 } else if (match_cnt < max_candidates) {
Shawn O. Pearce8713ab32007-01-13 17:30:53 -0500308 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;
Shawn O. Pearce8a8169c2007-01-25 12:40:03 -0500312 t->found_order = match_cnt;
Shawn O. Pearce8713ab32007-01-13 17:30:53 -0500313 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)
Ævar Arnfjörð Bjarmasone41f1cb2011-02-22 23:42:23 +0000329 fprintf(stderr, _("finished search at %s\n"),
Shawn O. Pearce8713ab32007-01-13 17:30:53 -0500330 sha1_to_hex(c->object.sha1));
331 break;
Shawn O. Pearcedccd0c22007-01-13 17:27:52 -0500332 }
333 while (parents) {
334 struct commit *p = parents->item;
335 parse_commit(p);
Shawn O. Pearce8713ab32007-01-13 17:30:53 -0500336 if (!(p->object.flags & SEEN))
Thiago Farina47e44ed2010-11-26 23:58:14 -0200337 commit_list_insert_by_date(p, &list);
Shawn O. Pearce8713ab32007-01-13 17:30:53 -0500338 p->object.flags |= c->object.flags;
Shawn O. Pearcedccd0c22007-01-13 17:27:52 -0500339 parents = parents->next;
Linus Torvalds908e5312005-12-24 13:50:45 -0800340 }
341 }
Shawn O. Pearce80dbae02007-01-10 06:39:47 -0500342
Junio C Hamanoda2478d2008-03-02 08:51:57 -0800343 if (!match_cnt) {
344 const unsigned char *sha1 = cmit->object.sha1;
345 if (always) {
Jean Privat9f67d2e2009-10-21 09:35:22 -0400346 printf("%s", find_unique_abbrev(sha1, abbrev));
347 if (dirty)
348 printf("%s", dirty);
349 printf("\n");
Junio C Hamanoda2478d2008-03-02 08:51:57 -0800350 return;
351 }
Thomas Rast4d236602009-10-28 23:10:06 +0100352 if (unannotated_cnt)
Ævar Arnfjörð Bjarmasone41f1cb2011-02-22 23:42:23 +0000353 die(_("No annotated tags can describe '%s'.\n"
354 "However, there were unannotated tags: try --tags."),
Thomas Rast4d236602009-10-28 23:10:06 +0100355 sha1_to_hex(sha1));
356 else
Ævar Arnfjörð Bjarmasone41f1cb2011-02-22 23:42:23 +0000357 die(_("No tags can describe '%s'.\n"
358 "Try --always, or create some tags."),
Thomas Rast4d236602009-10-28 23:10:06 +0100359 sha1_to_hex(sha1));
Junio C Hamanoda2478d2008-03-02 08:51:57 -0800360 }
Shawn O. Pearce80dbae02007-01-10 06:39:47 -0500361
Shawn O. Pearcecf69fd42007-01-14 04:37:44 -0500362 qsort(all_matches, match_cnt, sizeof(all_matches[0]), compare_pt);
Shawn O. Pearce1b600e62007-01-27 01:54:21 -0500363
364 if (gave_up_on) {
Thiago Farina47e44ed2010-11-26 23:58:14 -0200365 commit_list_insert_by_date(gave_up_on, &list);
Shawn O. Pearce1b600e62007-01-27 01:54:21 -0500366 seen_commits--;
367 }
368 seen_commits += finish_depth_computation(&list, &all_matches[0]);
369 free_commit_list(list);
370
Shawn O. Pearce8713ab32007-01-13 17:30:53 -0500371 if (debug) {
372 for (cur_match = 0; cur_match < match_cnt; cur_match++) {
373 struct possible_tag *t = &all_matches[cur_match];
Shawn O. Pearcecf69fd42007-01-14 04:37:44 -0500374 fprintf(stderr, " %-11s %8d %s\n",
375 prio_names[t->name->prio],
Shawn O. Pearce8713ab32007-01-13 17:30:53 -0500376 t->depth, t->name->path);
377 }
Ævar Arnfjörð Bjarmasone41f1cb2011-02-22 23:42:23 +0000378 fprintf(stderr, _("traversed %lu commits\n"), seen_commits);
Shawn O. Pearce8713ab32007-01-13 17:30:53 -0500379 if (gave_up_on) {
380 fprintf(stderr,
Ævar Arnfjörð Bjarmasone41f1cb2011-02-22 23:42:23 +0000381 _("more than %i tags found; listed %i most recent\n"
382 "gave up search at %s\n"),
Shawn O. Pearce8713ab32007-01-13 17:30:53 -0500383 max_candidates, max_candidates,
384 sha1_to_hex(gave_up_on->object.sha1));
385 }
Shawn O. Pearce80dbae02007-01-10 06:39:47 -0500386 }
Shawn O. Pearce212945d2008-02-28 01:22:36 -0500387
388 display_name(all_matches[0].name);
389 if (abbrev)
Junio C Hamano870cf7d2008-03-03 13:08:26 -0800390 show_suffix(all_matches[0].depth, cmit->object.sha1);
Jean Privat9f67d2e2009-10-21 09:35:22 -0400391 if (dirty)
392 printf("%s", dirty);
Shawn O. Pearce212945d2008-02-28 01:22:36 -0500393 printf("\n");
Shawn O. Pearce80dbae02007-01-10 06:39:47 -0500394
Shawn O. Pearce8713ab32007-01-13 17:30:53 -0500395 if (!last_one)
396 clear_commit_marks(cmit, -1);
Linus Torvalds908e5312005-12-24 13:50:45 -0800397}
398
Shawn O. Pearce9a0eaf82007-01-10 06:36:36 -0500399int cmd_describe(int argc, const char **argv, const char *prefix)
Linus Torvalds908e5312005-12-24 13:50:45 -0800400{
Shawn O. Pearce23615702007-05-21 03:20:25 -0400401 int contains = 0;
Pierre Habouzit166185b2007-10-07 20:54:08 +0200402 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"),
Santi Béjar518120e2008-02-25 10:43:33 +0100407 OPT_BOOLEAN(0, "long", &longformat, "always use long format"),
Pierre Habouzit166185b2007-10-07 20:54:08 +0200408 OPT__ABBREV(&abbrev),
Shawn O. Pearce2c33f752008-02-24 03:07:31 -0500409 OPT_SET_INT(0, "exact-match", &max_candidates,
410 "only output exact matches", 0),
Pierre Habouzit166185b2007-10-07 20:54:08 +0200411 OPT_INTEGER(0, "candidates", &max_candidates,
Pierre Habouzit30ffa602007-12-21 22:49:54 +0100412 "consider <n> most recent tags (default: 10)"),
413 OPT_STRING(0, "match", &pattern, "pattern",
414 "only consider tags matching <pattern>"),
Junio C Hamanoda2478d2008-03-02 08:51:57 -0800415 OPT_BOOLEAN(0, "always", &always,
416 "show abbreviated commit object as fallback"),
Jean Privat9f67d2e2009-10-21 09:35:22 -0400417 {OPTION_STRING, 0, "dirty", &dirty, "mark",
418 "append <mark> on dirty working tree (default: \"-dirty\")",
419 PARSE_OPT_OPTARG, NULL, (intptr_t) "-dirty"},
Pierre Habouzit166185b2007-10-07 20:54:08 +0200420 OPT_END(),
421 };
Linus Torvalds908e5312005-12-24 13:50:45 -0800422
Linus Torvaldsdce96482010-10-28 11:28:04 -0700423 git_config(git_default_config, NULL);
Stephen Boyd37782922009-05-23 11:53:12 -0700424 argc = parse_options(argc, argv, prefix, options, describe_usage, 0);
Linus Torvaldsdce96482010-10-28 11:28:04 -0700425 if (abbrev < 0)
426 abbrev = DEFAULT_ABBREV;
427
Shawn O. Pearce2c33f752008-02-24 03:07:31 -0500428 if (max_candidates < 0)
429 max_candidates = 0;
Pierre Habouzit166185b2007-10-07 20:54:08 +0200430 else if (max_candidates > MAX_TAGS)
431 max_candidates = MAX_TAGS;
Junio C Hamano4c34a2c2006-01-11 13:57:42 -0800432
Shawn O. Pearce8c599c72007-01-10 06:36:29 -0500433 save_commit_buffer = 0;
Dmitry V. Levin81128942006-09-14 05:03:59 +0400434
Santi Béjar518120e2008-02-25 10:43:33 +0100435 if (longformat && abbrev == 0)
Ævar Arnfjörð Bjarmasone41f1cb2011-02-22 23:42:23 +0000436 die(_("--long is incompatible with --abbrev=0"));
Santi Béjar518120e2008-02-25 10:43:33 +0100437
Shawn O. Pearce23615702007-05-21 03:20:25 -0400438 if (contains) {
Felipe Contreras4b25d092009-05-01 12:06:36 +0300439 const char **args = xmalloc((7 + argc) * sizeof(char *));
Nicolas Pitre3f7701a2007-12-19 12:53:16 -0500440 int i = 0;
441 args[i++] = "name-rev";
442 args[i++] = "--name-only";
Pierre Habouzita2cf9f42007-12-24 12:18:22 +0100443 args[i++] = "--no-undefined";
Junio C Hamanoda2478d2008-03-02 08:51:57 -0800444 if (always)
445 args[i++] = "--always";
Pierre Habouzit30ffa602007-12-21 22:49:54 +0100446 if (!all) {
Nicolas Pitre3f7701a2007-12-19 12:53:16 -0500447 args[i++] = "--tags";
Pierre Habouzit30ffa602007-12-21 22:49:54 +0100448 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 }
Felipe Contreras4b25d092009-05-01 12:06:36 +0300454 memcpy(args + i, argv, argc * sizeof(char *));
Nicolas Pitre3f7701a2007-12-19 12:53:16 -0500455 args[i + argc] = NULL;
456 return cmd_name_rev(i + argc, args, prefix);
Shawn O. Pearce23615702007-05-21 03:20:25 -0400457 }
458
Anders Kaseorg3cfa4db2010-12-09 01:46:08 -0500459 init_hash(&names);
Anders Kaseorg56a5f3a2010-12-09 01:42:25 -0500460 for_each_rawref(get_name, NULL);
Anders Kaseorg3cfa4db2010-12-09 01:46:08 -0500461 if (!names.nr && !always)
Ævar Arnfjörð Bjarmasone41f1cb2011-02-22 23:42:23 +0000462 die(_("No names found, cannot describe anything."));
René Scharfefb423da2009-10-17 18:30:48 +0200463
Pierre Habouzit166185b2007-10-07 20:54:08 +0200464 if (argc == 0) {
Jean Privat9f67d2e2009-10-21 09:35:22 -0400465 if (dirty && !cmd_diff_index(ARRAY_SIZE(diff_index_args) - 1, diff_index_args, prefix))
466 dirty = NULL;
Junio C Hamanofec9ebf2006-01-15 22:25:35 -0800467 describe("HEAD", 1);
Jean Privat9f67d2e2009-10-21 09:35:22 -0400468 } else if (dirty) {
Ævar Arnfjörð Bjarmasone41f1cb2011-02-22 23:42:23 +0000469 die(_("--dirty is incompatible with committishes"));
Pierre Habouzit166185b2007-10-07 20:54:08 +0200470 } else {
471 while (argc-- > 0) {
472 describe(*argv++, argc == 0);
Junio C Hamanofec9ebf2006-01-15 22:25:35 -0800473 }
Pierre Habouzit166185b2007-10-07 20:54:08 +0200474 }
Linus Torvalds908e5312005-12-24 13:50:45 -0800475 return 0;
476}