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