]>
Commit | Line | Data |
---|---|---|
1 | #include "builtin.h" | |
2 | #include "cache.h" | |
3 | #include "repository.h" | |
4 | #include "config.h" | |
5 | #include "commit.h" | |
6 | #include "tag.h" | |
7 | #include "refs.h" | |
8 | #include "parse-options.h" | |
9 | #include "prio-queue.h" | |
10 | #include "sha1-lookup.h" | |
11 | #include "commit-slab.h" | |
12 | ||
13 | /* | |
14 | * One day. See the 'name a rev shortly after epoch' test in t6120 when | |
15 | * changing this value | |
16 | */ | |
17 | #define CUTOFF_DATE_SLOP 86400 | |
18 | ||
19 | typedef struct rev_name { | |
20 | const char *tip_name; | |
21 | timestamp_t taggerdate; | |
22 | int generation; | |
23 | int distance; | |
24 | int from_tag; | |
25 | } rev_name; | |
26 | ||
27 | define_commit_slab(commit_rev_name, struct rev_name *); | |
28 | ||
29 | static timestamp_t cutoff = TIME_MAX; | |
30 | static struct commit_rev_name rev_names; | |
31 | ||
32 | /* How many generations are maximally preferred over _one_ merge traversal? */ | |
33 | #define MERGE_TRAVERSAL_WEIGHT 65535 | |
34 | ||
35 | static struct rev_name *get_commit_rev_name(struct commit *commit) | |
36 | { | |
37 | struct rev_name **slot = commit_rev_name_peek(&rev_names, commit); | |
38 | ||
39 | return slot ? *slot : NULL; | |
40 | } | |
41 | ||
42 | static void set_commit_rev_name(struct commit *commit, struct rev_name *name) | |
43 | { | |
44 | *commit_rev_name_at(&rev_names, commit) = name; | |
45 | } | |
46 | ||
47 | static int is_better_name(struct rev_name *name, | |
48 | timestamp_t taggerdate, | |
49 | int distance, | |
50 | int from_tag) | |
51 | { | |
52 | /* | |
53 | * When comparing names based on tags, prefer names | |
54 | * based on the older tag, even if it is farther away. | |
55 | */ | |
56 | if (from_tag && name->from_tag) | |
57 | return (name->taggerdate > taggerdate || | |
58 | (name->taggerdate == taggerdate && | |
59 | name->distance > distance)); | |
60 | ||
61 | /* | |
62 | * We know that at least one of them is a non-tag at this point. | |
63 | * favor a tag over a non-tag. | |
64 | */ | |
65 | if (name->from_tag != from_tag) | |
66 | return from_tag; | |
67 | ||
68 | /* | |
69 | * We are now looking at two non-tags. Tiebreak to favor | |
70 | * shorter hops. | |
71 | */ | |
72 | if (name->distance != distance) | |
73 | return name->distance > distance; | |
74 | ||
75 | /* ... or tiebreak to favor older date */ | |
76 | if (name->taggerdate != taggerdate) | |
77 | return name->taggerdate > taggerdate; | |
78 | ||
79 | /* keep the current one if we cannot decide */ | |
80 | return 0; | |
81 | } | |
82 | ||
83 | static struct rev_name *create_or_update_name(struct commit *commit, | |
84 | const char *tip_name, | |
85 | timestamp_t taggerdate, | |
86 | int generation, int distance, | |
87 | int from_tag) | |
88 | { | |
89 | struct rev_name *name = get_commit_rev_name(commit); | |
90 | ||
91 | if (name == NULL) { | |
92 | name = xmalloc(sizeof(*name)); | |
93 | set_commit_rev_name(commit, name); | |
94 | goto copy_data; | |
95 | } else if (is_better_name(name, taggerdate, distance, from_tag)) { | |
96 | copy_data: | |
97 | name->tip_name = tip_name; | |
98 | name->taggerdate = taggerdate; | |
99 | name->generation = generation; | |
100 | name->distance = distance; | |
101 | name->from_tag = from_tag; | |
102 | ||
103 | return name; | |
104 | } else | |
105 | return NULL; | |
106 | } | |
107 | ||
108 | static void name_rev(struct commit *start_commit, | |
109 | const char *tip_name, timestamp_t taggerdate, | |
110 | int from_tag, int deref) | |
111 | { | |
112 | struct prio_queue queue; | |
113 | struct commit *commit; | |
114 | struct commit **parents_to_queue = NULL; | |
115 | size_t parents_to_queue_nr, parents_to_queue_alloc = 0; | |
116 | char *to_free = NULL; | |
117 | ||
118 | parse_commit(start_commit); | |
119 | if (start_commit->date < cutoff) | |
120 | return; | |
121 | ||
122 | if (deref) | |
123 | tip_name = to_free = xstrfmt("%s^0", tip_name); | |
124 | ||
125 | if (!create_or_update_name(start_commit, tip_name, taggerdate, 0, 0, | |
126 | from_tag)) { | |
127 | free(to_free); | |
128 | return; | |
129 | } | |
130 | ||
131 | memset(&queue, 0, sizeof(queue)); /* Use the prio_queue as LIFO */ | |
132 | prio_queue_put(&queue, start_commit); | |
133 | ||
134 | while ((commit = prio_queue_get(&queue))) { | |
135 | struct rev_name *name = get_commit_rev_name(commit); | |
136 | struct commit_list *parents; | |
137 | int parent_number = 1; | |
138 | ||
139 | parents_to_queue_nr = 0; | |
140 | ||
141 | for (parents = commit->parents; | |
142 | parents; | |
143 | parents = parents->next, parent_number++) { | |
144 | struct commit *parent = parents->item; | |
145 | const char *new_name; | |
146 | int generation, distance; | |
147 | ||
148 | parse_commit(parent); | |
149 | if (parent->date < cutoff) | |
150 | continue; | |
151 | ||
152 | if (parent_number > 1) { | |
153 | size_t len; | |
154 | ||
155 | strip_suffix(name->tip_name, "^0", &len); | |
156 | if (name->generation > 0) | |
157 | new_name = xstrfmt("%.*s~%d^%d", | |
158 | (int)len, | |
159 | name->tip_name, | |
160 | name->generation, | |
161 | parent_number); | |
162 | else | |
163 | new_name = xstrfmt("%.*s^%d", (int)len, | |
164 | name->tip_name, | |
165 | parent_number); | |
166 | generation = 0; | |
167 | distance = name->distance + MERGE_TRAVERSAL_WEIGHT; | |
168 | } else { | |
169 | new_name = name->tip_name; | |
170 | generation = name->generation + 1; | |
171 | distance = name->distance + 1; | |
172 | } | |
173 | ||
174 | if (create_or_update_name(parent, new_name, taggerdate, | |
175 | generation, distance, | |
176 | from_tag)) { | |
177 | ALLOC_GROW(parents_to_queue, | |
178 | parents_to_queue_nr + 1, | |
179 | parents_to_queue_alloc); | |
180 | parents_to_queue[parents_to_queue_nr] = parent; | |
181 | parents_to_queue_nr++; | |
182 | } | |
183 | } | |
184 | ||
185 | /* The first parent must come out first from the prio_queue */ | |
186 | while (parents_to_queue_nr) | |
187 | prio_queue_put(&queue, | |
188 | parents_to_queue[--parents_to_queue_nr]); | |
189 | } | |
190 | ||
191 | clear_prio_queue(&queue); | |
192 | free(parents_to_queue); | |
193 | } | |
194 | ||
195 | static int subpath_matches(const char *path, const char *filter) | |
196 | { | |
197 | const char *subpath = path; | |
198 | ||
199 | while (subpath) { | |
200 | if (!wildmatch(filter, subpath, 0)) | |
201 | return subpath - path; | |
202 | subpath = strchr(subpath, '/'); | |
203 | if (subpath) | |
204 | subpath++; | |
205 | } | |
206 | return -1; | |
207 | } | |
208 | ||
209 | static const char *name_ref_abbrev(const char *refname, int shorten_unambiguous) | |
210 | { | |
211 | if (shorten_unambiguous) | |
212 | refname = shorten_unambiguous_ref(refname, 0); | |
213 | else if (skip_prefix(refname, "refs/heads/", &refname)) | |
214 | ; /* refname already advanced */ | |
215 | else | |
216 | skip_prefix(refname, "refs/", &refname); | |
217 | return refname; | |
218 | } | |
219 | ||
220 | struct name_ref_data { | |
221 | int tags_only; | |
222 | int name_only; | |
223 | struct string_list ref_filters; | |
224 | struct string_list exclude_filters; | |
225 | }; | |
226 | ||
227 | static struct tip_table { | |
228 | struct tip_table_entry { | |
229 | struct object_id oid; | |
230 | const char *refname; | |
231 | } *table; | |
232 | int nr; | |
233 | int alloc; | |
234 | int sorted; | |
235 | } tip_table; | |
236 | ||
237 | static void add_to_tip_table(const struct object_id *oid, const char *refname, | |
238 | int shorten_unambiguous) | |
239 | { | |
240 | refname = name_ref_abbrev(refname, shorten_unambiguous); | |
241 | ||
242 | ALLOC_GROW(tip_table.table, tip_table.nr + 1, tip_table.alloc); | |
243 | oidcpy(&tip_table.table[tip_table.nr].oid, oid); | |
244 | tip_table.table[tip_table.nr].refname = xstrdup(refname); | |
245 | tip_table.nr++; | |
246 | tip_table.sorted = 0; | |
247 | } | |
248 | ||
249 | static int tipcmp(const void *a_, const void *b_) | |
250 | { | |
251 | const struct tip_table_entry *a = a_, *b = b_; | |
252 | return oidcmp(&a->oid, &b->oid); | |
253 | } | |
254 | ||
255 | static int name_ref(const char *path, const struct object_id *oid, int flags, void *cb_data) | |
256 | { | |
257 | struct object *o = parse_object(the_repository, oid); | |
258 | struct name_ref_data *data = cb_data; | |
259 | int can_abbreviate_output = data->tags_only && data->name_only; | |
260 | int deref = 0; | |
261 | timestamp_t taggerdate = TIME_MAX; | |
262 | ||
263 | if (data->tags_only && !starts_with(path, "refs/tags/")) | |
264 | return 0; | |
265 | ||
266 | if (data->exclude_filters.nr) { | |
267 | struct string_list_item *item; | |
268 | ||
269 | for_each_string_list_item(item, &data->exclude_filters) { | |
270 | if (subpath_matches(path, item->string) >= 0) | |
271 | return 0; | |
272 | } | |
273 | } | |
274 | ||
275 | if (data->ref_filters.nr) { | |
276 | struct string_list_item *item; | |
277 | int matched = 0; | |
278 | ||
279 | /* See if any of the patterns match. */ | |
280 | for_each_string_list_item(item, &data->ref_filters) { | |
281 | /* | |
282 | * Check all patterns even after finding a match, so | |
283 | * that we can see if a match with a subpath exists. | |
284 | * When a user asked for 'refs/tags/v*' and 'v1.*', | |
285 | * both of which match, the user is showing her | |
286 | * willingness to accept a shortened output by having | |
287 | * the 'v1.*' in the acceptable refnames, so we | |
288 | * shouldn't stop when seeing 'refs/tags/v1.4' matches | |
289 | * 'refs/tags/v*'. We should show it as 'v1.4'. | |
290 | */ | |
291 | switch (subpath_matches(path, item->string)) { | |
292 | case -1: /* did not match */ | |
293 | break; | |
294 | case 0: /* matched fully */ | |
295 | matched = 1; | |
296 | break; | |
297 | default: /* matched subpath */ | |
298 | matched = 1; | |
299 | can_abbreviate_output = 1; | |
300 | break; | |
301 | } | |
302 | } | |
303 | ||
304 | /* If none of the patterns matched, stop now */ | |
305 | if (!matched) | |
306 | return 0; | |
307 | } | |
308 | ||
309 | add_to_tip_table(oid, path, can_abbreviate_output); | |
310 | ||
311 | while (o && o->type == OBJ_TAG) { | |
312 | struct tag *t = (struct tag *) o; | |
313 | if (!t->tagged) | |
314 | break; /* broken repository */ | |
315 | o = parse_object(the_repository, &t->tagged->oid); | |
316 | deref = 1; | |
317 | taggerdate = t->date; | |
318 | } | |
319 | if (o && o->type == OBJ_COMMIT) { | |
320 | struct commit *commit = (struct commit *)o; | |
321 | int from_tag = starts_with(path, "refs/tags/"); | |
322 | ||
323 | if (taggerdate == TIME_MAX) | |
324 | taggerdate = commit->date; | |
325 | path = name_ref_abbrev(path, can_abbreviate_output); | |
326 | name_rev(commit, xstrdup(path), taggerdate, from_tag, deref); | |
327 | } | |
328 | return 0; | |
329 | } | |
330 | ||
331 | static const unsigned char *nth_tip_table_ent(size_t ix, void *table_) | |
332 | { | |
333 | struct tip_table_entry *table = table_; | |
334 | return table[ix].oid.hash; | |
335 | } | |
336 | ||
337 | static const char *get_exact_ref_match(const struct object *o) | |
338 | { | |
339 | int found; | |
340 | ||
341 | if (!tip_table.table || !tip_table.nr) | |
342 | return NULL; | |
343 | ||
344 | if (!tip_table.sorted) { | |
345 | QSORT(tip_table.table, tip_table.nr, tipcmp); | |
346 | tip_table.sorted = 1; | |
347 | } | |
348 | ||
349 | found = sha1_pos(o->oid.hash, tip_table.table, tip_table.nr, | |
350 | nth_tip_table_ent); | |
351 | if (0 <= found) | |
352 | return tip_table.table[found].refname; | |
353 | return NULL; | |
354 | } | |
355 | ||
356 | /* may return a constant string or use "buf" as scratch space */ | |
357 | static const char *get_rev_name(const struct object *o, struct strbuf *buf) | |
358 | { | |
359 | struct rev_name *n; | |
360 | struct commit *c; | |
361 | ||
362 | if (o->type != OBJ_COMMIT) | |
363 | return get_exact_ref_match(o); | |
364 | c = (struct commit *) o; | |
365 | n = get_commit_rev_name(c); | |
366 | if (!n) | |
367 | return NULL; | |
368 | ||
369 | if (!n->generation) | |
370 | return n->tip_name; | |
371 | else { | |
372 | strbuf_reset(buf); | |
373 | strbuf_addstr(buf, n->tip_name); | |
374 | strbuf_strip_suffix(buf, "^0"); | |
375 | strbuf_addf(buf, "~%d", n->generation); | |
376 | return buf->buf; | |
377 | } | |
378 | } | |
379 | ||
380 | static void show_name(const struct object *obj, | |
381 | const char *caller_name, | |
382 | int always, int allow_undefined, int name_only) | |
383 | { | |
384 | const char *name; | |
385 | const struct object_id *oid = &obj->oid; | |
386 | struct strbuf buf = STRBUF_INIT; | |
387 | ||
388 | if (!name_only) | |
389 | printf("%s ", caller_name ? caller_name : oid_to_hex(oid)); | |
390 | name = get_rev_name(obj, &buf); | |
391 | if (name) | |
392 | printf("%s\n", name); | |
393 | else if (allow_undefined) | |
394 | printf("undefined\n"); | |
395 | else if (always) | |
396 | printf("%s\n", find_unique_abbrev(oid, DEFAULT_ABBREV)); | |
397 | else | |
398 | die("cannot describe '%s'", oid_to_hex(oid)); | |
399 | strbuf_release(&buf); | |
400 | } | |
401 | ||
402 | static char const * const name_rev_usage[] = { | |
403 | N_("git name-rev [<options>] <commit>..."), | |
404 | N_("git name-rev [<options>] --all"), | |
405 | N_("git name-rev [<options>] --stdin"), | |
406 | NULL | |
407 | }; | |
408 | ||
409 | static void name_rev_line(char *p, struct name_ref_data *data) | |
410 | { | |
411 | struct strbuf buf = STRBUF_INIT; | |
412 | int counter = 0; | |
413 | char *p_start; | |
414 | const unsigned hexsz = the_hash_algo->hexsz; | |
415 | ||
416 | for (p_start = p; *p; p++) { | |
417 | #define ishex(x) (isdigit((x)) || ((x) >= 'a' && (x) <= 'f')) | |
418 | if (!ishex(*p)) | |
419 | counter = 0; | |
420 | else if (++counter == hexsz && | |
421 | !ishex(*(p+1))) { | |
422 | struct object_id oid; | |
423 | const char *name = NULL; | |
424 | char c = *(p+1); | |
425 | int p_len = p - p_start + 1; | |
426 | ||
427 | counter = 0; | |
428 | ||
429 | *(p+1) = 0; | |
430 | if (!get_oid(p - (hexsz - 1), &oid)) { | |
431 | struct object *o = | |
432 | lookup_object(the_repository, &oid); | |
433 | if (o) | |
434 | name = get_rev_name(o, &buf); | |
435 | } | |
436 | *(p+1) = c; | |
437 | ||
438 | if (!name) | |
439 | continue; | |
440 | ||
441 | if (data->name_only) | |
442 | printf("%.*s%s", p_len - hexsz, p_start, name); | |
443 | else | |
444 | printf("%.*s (%s)", p_len, p_start, name); | |
445 | p_start = p + 1; | |
446 | } | |
447 | } | |
448 | ||
449 | /* flush */ | |
450 | if (p_start != p) | |
451 | fwrite(p_start, p - p_start, 1, stdout); | |
452 | ||
453 | strbuf_release(&buf); | |
454 | } | |
455 | ||
456 | int cmd_name_rev(int argc, const char **argv, const char *prefix) | |
457 | { | |
458 | struct object_array revs = OBJECT_ARRAY_INIT; | |
459 | int all = 0, transform_stdin = 0, allow_undefined = 1, always = 0, peel_tag = 0; | |
460 | struct name_ref_data data = { 0, 0, STRING_LIST_INIT_NODUP, STRING_LIST_INIT_NODUP }; | |
461 | struct option opts[] = { | |
462 | OPT_BOOL(0, "name-only", &data.name_only, N_("print only names (no SHA-1)")), | |
463 | OPT_BOOL(0, "tags", &data.tags_only, N_("only use tags to name the commits")), | |
464 | OPT_STRING_LIST(0, "refs", &data.ref_filters, N_("pattern"), | |
465 | N_("only use refs matching <pattern>")), | |
466 | OPT_STRING_LIST(0, "exclude", &data.exclude_filters, N_("pattern"), | |
467 | N_("ignore refs matching <pattern>")), | |
468 | OPT_GROUP(""), | |
469 | OPT_BOOL(0, "all", &all, N_("list all commits reachable from all refs")), | |
470 | OPT_BOOL(0, "stdin", &transform_stdin, N_("read from stdin")), | |
471 | OPT_BOOL(0, "undefined", &allow_undefined, N_("allow to print `undefined` names (default)")), | |
472 | OPT_BOOL(0, "always", &always, | |
473 | N_("show abbreviated commit object as fallback")), | |
474 | { | |
475 | /* A Hidden OPT_BOOL */ | |
476 | OPTION_SET_INT, 0, "peel-tag", &peel_tag, NULL, | |
477 | N_("dereference tags in the input (internal use)"), | |
478 | PARSE_OPT_NOARG | PARSE_OPT_HIDDEN, NULL, 1, | |
479 | }, | |
480 | OPT_END(), | |
481 | }; | |
482 | ||
483 | init_commit_rev_name(&rev_names); | |
484 | git_config(git_default_config, NULL); | |
485 | argc = parse_options(argc, argv, prefix, opts, name_rev_usage, 0); | |
486 | if (all + transform_stdin + !!argc > 1) { | |
487 | error("Specify either a list, or --all, not both!"); | |
488 | usage_with_options(name_rev_usage, opts); | |
489 | } | |
490 | if (all || transform_stdin) | |
491 | cutoff = 0; | |
492 | ||
493 | for (; argc; argc--, argv++) { | |
494 | struct object_id oid; | |
495 | struct object *object; | |
496 | struct commit *commit; | |
497 | ||
498 | if (get_oid(*argv, &oid)) { | |
499 | fprintf(stderr, "Could not get sha1 for %s. Skipping.\n", | |
500 | *argv); | |
501 | continue; | |
502 | } | |
503 | ||
504 | commit = NULL; | |
505 | object = parse_object(the_repository, &oid); | |
506 | if (object) { | |
507 | struct object *peeled = deref_tag(the_repository, | |
508 | object, *argv, 0); | |
509 | if (peeled && peeled->type == OBJ_COMMIT) | |
510 | commit = (struct commit *)peeled; | |
511 | } | |
512 | ||
513 | if (!object) { | |
514 | fprintf(stderr, "Could not get object for %s. Skipping.\n", | |
515 | *argv); | |
516 | continue; | |
517 | } | |
518 | ||
519 | if (commit) { | |
520 | if (cutoff > commit->date) | |
521 | cutoff = commit->date; | |
522 | } | |
523 | ||
524 | if (peel_tag) { | |
525 | if (!commit) { | |
526 | fprintf(stderr, "Could not get commit for %s. Skipping.\n", | |
527 | *argv); | |
528 | continue; | |
529 | } | |
530 | object = (struct object *)commit; | |
531 | } | |
532 | add_object_array(object, *argv, &revs); | |
533 | } | |
534 | ||
535 | if (cutoff) { | |
536 | /* check for undeflow */ | |
537 | if (cutoff > TIME_MIN + CUTOFF_DATE_SLOP) | |
538 | cutoff = cutoff - CUTOFF_DATE_SLOP; | |
539 | else | |
540 | cutoff = TIME_MIN; | |
541 | } | |
542 | for_each_ref(name_ref, &data); | |
543 | ||
544 | if (transform_stdin) { | |
545 | char buffer[2048]; | |
546 | ||
547 | while (!feof(stdin)) { | |
548 | char *p = fgets(buffer, sizeof(buffer), stdin); | |
549 | if (!p) | |
550 | break; | |
551 | name_rev_line(p, &data); | |
552 | } | |
553 | } else if (all) { | |
554 | int i, max; | |
555 | ||
556 | max = get_max_object_index(); | |
557 | for (i = 0; i < max; i++) { | |
558 | struct object *obj = get_indexed_object(i); | |
559 | if (!obj || obj->type != OBJ_COMMIT) | |
560 | continue; | |
561 | show_name(obj, NULL, | |
562 | always, allow_undefined, data.name_only); | |
563 | } | |
564 | } else { | |
565 | int i; | |
566 | for (i = 0; i < revs.nr; i++) | |
567 | show_name(revs.objects[i].item, revs.objects[i].name, | |
568 | always, allow_undefined, data.name_only); | |
569 | } | |
570 | ||
571 | UNLEAK(revs); | |
572 | return 0; | |
573 | } |