]>
Commit | Line | Data |
---|---|---|
d6b64ed0 | 1 | #include "builtin.h" |
bd321bcc | 2 | #include "cache.h" |
109cd76d | 3 | #include "repository.h" |
b2141fc1 | 4 | #include "config.h" |
bd321bcc JS |
5 | #include "commit.h" |
6 | #include "tag.h" | |
7 | #include "refs.h" | |
edefb1a2 | 8 | #include "parse-options.h" |
49f7a2fd | 9 | #include "prio-queue.h" |
bc626927 | 10 | #include "hash-lookup.h" |
8fd79a73 | 11 | #include "commit-slab.h" |
2e8ea40f | 12 | #include "commit-graph.h" |
bd321bcc | 13 | |
2e09c012 SG |
14 | /* |
15 | * One day. See the 'name a rev shortly after epoch' test in t6120 when | |
16 | * changing this value | |
17 | */ | |
18 | #define CUTOFF_DATE_SLOP 86400 | |
c075aea5 | 19 | |
71620ca8 | 20 | struct rev_name { |
45a14f57 | 21 | const char *tip_name; |
dddbad72 | 22 | timestamp_t taggerdate; |
bd321bcc | 23 | int generation; |
ac076c29 | 24 | int distance; |
ef1e7406 | 25 | int from_tag; |
71620ca8 | 26 | }; |
bd321bcc | 27 | |
f13ca7ce | 28 | define_commit_slab(commit_rev_name, struct rev_name); |
8fd79a73 | 29 | |
2e8ea40f | 30 | static timestamp_t generation_cutoff = GENERATION_NUMBER_INFINITY; |
5589e87f | 31 | static timestamp_t cutoff = TIME_MAX; |
8fd79a73 | 32 | static struct commit_rev_name rev_names; |
bd321bcc | 33 | |
2e8ea40f JK |
34 | /* Disable the cutoff checks entirely */ |
35 | static void disable_cutoff(void) | |
36 | { | |
37 | generation_cutoff = 0; | |
38 | cutoff = 0; | |
39 | } | |
40 | ||
41 | /* Cutoff searching any commits older than this one */ | |
42 | static void set_commit_cutoff(struct commit *commit) | |
43 | { | |
44 | ||
45 | if (cutoff > commit->date) | |
46 | cutoff = commit->date; | |
47 | ||
48 | if (generation_cutoff) { | |
49 | timestamp_t generation = commit_graph_generation(commit); | |
50 | ||
51 | if (generation_cutoff > generation) | |
52 | generation_cutoff = generation; | |
53 | } | |
54 | } | |
55 | ||
56 | /* adjust the commit date cutoff with a slop to allow for slightly incorrect | |
57 | * commit timestamps in case of clock skew. | |
58 | */ | |
59 | static void adjust_cutoff_timestamp_for_slop(void) | |
60 | { | |
61 | if (cutoff) { | |
62 | /* check for undeflow */ | |
63 | if (cutoff > TIME_MIN + CUTOFF_DATE_SLOP) | |
64 | cutoff = cutoff - CUTOFF_DATE_SLOP; | |
65 | else | |
66 | cutoff = TIME_MIN; | |
67 | } | |
68 | } | |
69 | ||
70 | /* Check if a commit is before the cutoff. Prioritize generation numbers | |
71 | * first, but use the commit timestamp if we lack generation data. | |
72 | */ | |
73 | static int commit_is_before_cutoff(struct commit *commit) | |
74 | { | |
75 | if (generation_cutoff < GENERATION_NUMBER_INFINITY) | |
76 | return generation_cutoff && | |
77 | commit_graph_generation(commit) < generation_cutoff; | |
78 | ||
79 | return commit->date < cutoff; | |
80 | } | |
81 | ||
ac076c29 JS |
82 | /* How many generations are maximally preferred over _one_ merge traversal? */ |
83 | #define MERGE_TRAVERSAL_WEIGHT 65535 | |
84 | ||
f13ca7ce RS |
85 | static int is_valid_rev_name(const struct rev_name *name) |
86 | { | |
45a14f57 | 87 | return name && name->tip_name; |
f13ca7ce RS |
88 | } |
89 | ||
36d2419c | 90 | static struct rev_name *get_commit_rev_name(const struct commit *commit) |
8fd79a73 | 91 | { |
f13ca7ce | 92 | struct rev_name *name = commit_rev_name_peek(&rev_names, commit); |
8fd79a73 | 93 | |
f13ca7ce | 94 | return is_valid_rev_name(name) ? name : NULL; |
8fd79a73 NTND |
95 | } |
96 | ||
3656f842 EN |
97 | static int effective_distance(int distance, int generation) |
98 | { | |
99 | return distance + (generation > 0 ? MERGE_TRAVERSAL_WEIGHT : 0); | |
100 | } | |
101 | ||
0041bf65 | 102 | static int is_better_name(struct rev_name *name, |
78089b71 | 103 | timestamp_t taggerdate, |
3656f842 | 104 | int generation, |
ef1e7406 JH |
105 | int distance, |
106 | int from_tag) | |
0041bf65 | 107 | { |
3656f842 EN |
108 | int name_distance = effective_distance(name->distance, name->generation); |
109 | int new_distance = effective_distance(distance, generation); | |
110 | ||
ef1e7406 JH |
111 | /* |
112 | * When comparing names based on tags, prefer names | |
113 | * based on the older tag, even if it is farther away. | |
114 | */ | |
115 | if (from_tag && name->from_tag) | |
116 | return (name->taggerdate > taggerdate || | |
117 | (name->taggerdate == taggerdate && | |
3656f842 | 118 | name_distance > new_distance)); |
ef1e7406 JH |
119 | |
120 | /* | |
121 | * We know that at least one of them is a non-tag at this point. | |
122 | * favor a tag over a non-tag. | |
123 | */ | |
124 | if (name->from_tag != from_tag) | |
125 | return from_tag; | |
126 | ||
127 | /* | |
128 | * We are now looking at two non-tags. Tiebreak to favor | |
129 | * shorter hops. | |
130 | */ | |
3656f842 EN |
131 | if (name_distance != new_distance) |
132 | return name_distance > new_distance; | |
ef1e7406 JH |
133 | |
134 | /* ... or tiebreak to favor older date */ | |
135 | if (name->taggerdate != taggerdate) | |
136 | return name->taggerdate > taggerdate; | |
137 | ||
138 | /* keep the current one if we cannot decide */ | |
139 | return 0; | |
0041bf65 JH |
140 | } |
141 | ||
766f9e39 | 142 | static struct rev_name *create_or_update_name(struct commit *commit, |
766f9e39 SG |
143 | timestamp_t taggerdate, |
144 | int generation, int distance, | |
145 | int from_tag) | |
bd321bcc | 146 | { |
f13ca7ce | 147 | struct rev_name *name = commit_rev_name_at(&rev_names, commit); |
bd321bcc | 148 | |
45a14f57 RS |
149 | if (is_valid_rev_name(name) && |
150 | !is_better_name(name, taggerdate, generation, distance, from_tag)) | |
151 | return NULL; | |
3e2feb0d | 152 | |
3e2feb0d MÅ |
153 | name->taggerdate = taggerdate; |
154 | name->generation = generation; | |
155 | name->distance = distance; | |
156 | name->from_tag = from_tag; | |
157 | ||
158 | return name; | |
766f9e39 SG |
159 | } |
160 | ||
ddc42ec7 RS |
161 | static char *get_parent_name(const struct rev_name *name, int parent_number) |
162 | { | |
1c56fc20 | 163 | struct strbuf sb = STRBUF_INIT; |
ddc42ec7 RS |
164 | size_t len; |
165 | ||
166 | strip_suffix(name->tip_name, "^0", &len); | |
1c56fc20 RS |
167 | if (name->generation > 0) { |
168 | strbuf_grow(&sb, len + | |
169 | 1 + decimal_width(name->generation) + | |
170 | 1 + decimal_width(parent_number)); | |
171 | strbuf_addf(&sb, "%.*s~%d^%d", (int)len, name->tip_name, | |
172 | name->generation, parent_number); | |
173 | } else { | |
174 | strbuf_grow(&sb, len + | |
175 | 1 + decimal_width(parent_number)); | |
176 | strbuf_addf(&sb, "%.*s^%d", (int)len, name->tip_name, | |
177 | parent_number); | |
178 | } | |
179 | return strbuf_detach(&sb, NULL); | |
ddc42ec7 RS |
180 | } |
181 | ||
49f7a2fd | 182 | static void name_rev(struct commit *start_commit, |
dddbad72 | 183 | const char *tip_name, timestamp_t taggerdate, |
2866fd28 | 184 | int from_tag, int deref) |
bd321bcc | 185 | { |
49f7a2fd SG |
186 | struct prio_queue queue; |
187 | struct commit *commit; | |
188 | struct commit **parents_to_queue = NULL; | |
189 | size_t parents_to_queue_nr, parents_to_queue_alloc = 0; | |
977dc191 | 190 | struct rev_name *start_name; |
2866fd28 SG |
191 | |
192 | parse_commit(start_commit); | |
2e8ea40f | 193 | if (commit_is_before_cutoff(start_commit)) |
2866fd28 SG |
194 | return; |
195 | ||
977dc191 RS |
196 | start_name = create_or_update_name(start_commit, taggerdate, 0, 0, |
197 | from_tag); | |
198 | if (!start_name) | |
199 | return; | |
2866fd28 | 200 | if (deref) |
977dc191 | 201 | start_name->tip_name = xstrfmt("%s^0", tip_name); |
15a4205d | 202 | else |
977dc191 | 203 | start_name->tip_name = xstrdup(tip_name); |
bd321bcc | 204 | |
49f7a2fd SG |
205 | memset(&queue, 0, sizeof(queue)); /* Use the prio_queue as LIFO */ |
206 | prio_queue_put(&queue, start_commit); | |
207 | ||
208 | while ((commit = prio_queue_get(&queue))) { | |
209 | struct rev_name *name = get_commit_rev_name(commit); | |
210 | struct commit_list *parents; | |
211 | int parent_number = 1; | |
212 | ||
213 | parents_to_queue_nr = 0; | |
214 | ||
215 | for (parents = commit->parents; | |
216 | parents; | |
217 | parents = parents->next, parent_number++) { | |
218 | struct commit *parent = parents->item; | |
977dc191 | 219 | struct rev_name *parent_name; |
49f7a2fd SG |
220 | int generation, distance; |
221 | ||
222 | parse_commit(parent); | |
2e8ea40f | 223 | if (commit_is_before_cutoff(parent)) |
49f7a2fd SG |
224 | continue; |
225 | ||
226 | if (parent_number > 1) { | |
49f7a2fd SG |
227 | generation = 0; |
228 | distance = name->distance + MERGE_TRAVERSAL_WEIGHT; | |
229 | } else { | |
49f7a2fd SG |
230 | generation = name->generation + 1; |
231 | distance = name->distance + 1; | |
232 | } | |
233 | ||
977dc191 RS |
234 | parent_name = create_or_update_name(parent, taggerdate, |
235 | generation, | |
236 | distance, from_tag); | |
237 | if (parent_name) { | |
238 | if (parent_number > 1) | |
239 | parent_name->tip_name = | |
240 | get_parent_name(name, | |
241 | parent_number); | |
242 | else | |
243 | parent_name->tip_name = name->tip_name; | |
49f7a2fd SG |
244 | ALLOC_GROW(parents_to_queue, |
245 | parents_to_queue_nr + 1, | |
246 | parents_to_queue_alloc); | |
247 | parents_to_queue[parents_to_queue_nr] = parent; | |
248 | parents_to_queue_nr++; | |
249 | } | |
bd321bcc | 250 | } |
3a521503 | 251 | |
49f7a2fd SG |
252 | /* The first parent must come out first from the prio_queue */ |
253 | while (parents_to_queue_nr) | |
254 | prio_queue_put(&queue, | |
255 | parents_to_queue[--parents_to_queue_nr]); | |
bd321bcc | 256 | } |
49f7a2fd SG |
257 | |
258 | clear_prio_queue(&queue); | |
259 | free(parents_to_queue); | |
bd321bcc JS |
260 | } |
261 | ||
98c5c4ad NK |
262 | static int subpath_matches(const char *path, const char *filter) |
263 | { | |
264 | const char *subpath = path; | |
265 | ||
266 | while (subpath) { | |
55d34269 | 267 | if (!wildmatch(filter, subpath, 0)) |
98c5c4ad NK |
268 | return subpath - path; |
269 | subpath = strchr(subpath, '/'); | |
270 | if (subpath) | |
271 | subpath++; | |
272 | } | |
273 | return -1; | |
274 | } | |
275 | ||
9608a190 JH |
276 | static const char *name_ref_abbrev(const char *refname, int shorten_unambiguous) |
277 | { | |
278 | if (shorten_unambiguous) | |
279 | refname = shorten_unambiguous_ref(refname, 0); | |
2059e79c RS |
280 | else if (skip_prefix(refname, "refs/heads/", &refname)) |
281 | ; /* refname already advanced */ | |
282 | else | |
283 | skip_prefix(refname, "refs/", &refname); | |
9608a190 JH |
284 | return refname; |
285 | } | |
286 | ||
2afc29aa JS |
287 | struct name_ref_data { |
288 | int tags_only; | |
23615708 | 289 | int name_only; |
290be667 | 290 | struct string_list ref_filters; |
96415b49 | 291 | struct string_list exclude_filters; |
2afc29aa JS |
292 | }; |
293 | ||
b23e0b93 JH |
294 | static struct tip_table { |
295 | struct tip_table_entry { | |
511dca80 | 296 | struct object_id oid; |
b23e0b93 | 297 | const char *refname; |
079f9709 RS |
298 | struct commit *commit; |
299 | timestamp_t taggerdate; | |
300 | unsigned int from_tag:1; | |
301 | unsigned int deref:1; | |
b23e0b93 JH |
302 | } *table; |
303 | int nr; | |
304 | int alloc; | |
305 | int sorted; | |
306 | } tip_table; | |
307 | ||
511dca80 | 308 | static void add_to_tip_table(const struct object_id *oid, const char *refname, |
079f9709 RS |
309 | int shorten_unambiguous, struct commit *commit, |
310 | timestamp_t taggerdate, int from_tag, int deref) | |
b23e0b93 JH |
311 | { |
312 | refname = name_ref_abbrev(refname, shorten_unambiguous); | |
313 | ||
314 | ALLOC_GROW(tip_table.table, tip_table.nr + 1, tip_table.alloc); | |
511dca80 | 315 | oidcpy(&tip_table.table[tip_table.nr].oid, oid); |
b23e0b93 | 316 | tip_table.table[tip_table.nr].refname = xstrdup(refname); |
079f9709 RS |
317 | tip_table.table[tip_table.nr].commit = commit; |
318 | tip_table.table[tip_table.nr].taggerdate = taggerdate; | |
319 | tip_table.table[tip_table.nr].from_tag = from_tag; | |
320 | tip_table.table[tip_table.nr].deref = deref; | |
b23e0b93 JH |
321 | tip_table.nr++; |
322 | tip_table.sorted = 0; | |
323 | } | |
324 | ||
325 | static int tipcmp(const void *a_, const void *b_) | |
326 | { | |
327 | const struct tip_table_entry *a = a_, *b = b_; | |
511dca80 | 328 | return oidcmp(&a->oid, &b->oid); |
b23e0b93 JH |
329 | } |
330 | ||
079f9709 RS |
331 | static int cmp_by_tag_and_age(const void *a_, const void *b_) |
332 | { | |
333 | const struct tip_table_entry *a = a_, *b = b_; | |
334 | int cmp; | |
335 | ||
336 | /* Prefer tags. */ | |
337 | cmp = b->from_tag - a->from_tag; | |
338 | if (cmp) | |
339 | return cmp; | |
340 | ||
341 | /* Older is better. */ | |
342 | if (a->taggerdate < b->taggerdate) | |
343 | return -1; | |
344 | return a->taggerdate != b->taggerdate; | |
345 | } | |
346 | ||
63e14ee2 | 347 | static int name_ref(const char *path, const struct object_id *oid, |
5cf88fd8 | 348 | int flags UNUSED, void *cb_data) |
bd321bcc | 349 | { |
109cd76d | 350 | struct object *o = parse_object(the_repository, oid); |
2afc29aa | 351 | struct name_ref_data *data = cb_data; |
98c5c4ad | 352 | int can_abbreviate_output = data->tags_only && data->name_only; |
bd321bcc | 353 | int deref = 0; |
079f9709 RS |
354 | int from_tag = 0; |
355 | struct commit *commit = NULL; | |
dddbad72 | 356 | timestamp_t taggerdate = TIME_MAX; |
bd321bcc | 357 | |
59556548 | 358 | if (data->tags_only && !starts_with(path, "refs/tags/")) |
2afc29aa JS |
359 | return 0; |
360 | ||
96415b49 JK |
361 | if (data->exclude_filters.nr) { |
362 | struct string_list_item *item; | |
363 | ||
364 | for_each_string_list_item(item, &data->exclude_filters) { | |
365 | if (subpath_matches(path, item->string) >= 0) | |
366 | return 0; | |
367 | } | |
368 | } | |
369 | ||
290be667 JK |
370 | if (data->ref_filters.nr) { |
371 | struct string_list_item *item; | |
372 | int matched = 0; | |
373 | ||
374 | /* See if any of the patterns match. */ | |
375 | for_each_string_list_item(item, &data->ref_filters) { | |
376 | /* | |
377 | * Check all patterns even after finding a match, so | |
378 | * that we can see if a match with a subpath exists. | |
379 | * When a user asked for 'refs/tags/v*' and 'v1.*', | |
380 | * both of which match, the user is showing her | |
381 | * willingness to accept a shortened output by having | |
382 | * the 'v1.*' in the acceptable refnames, so we | |
383 | * shouldn't stop when seeing 'refs/tags/v1.4' matches | |
384 | * 'refs/tags/v*'. We should show it as 'v1.4'. | |
385 | */ | |
386 | switch (subpath_matches(path, item->string)) { | |
387 | case -1: /* did not match */ | |
388 | break; | |
389 | case 0: /* matched fully */ | |
390 | matched = 1; | |
391 | break; | |
392 | default: /* matched subpath */ | |
393 | matched = 1; | |
394 | can_abbreviate_output = 1; | |
395 | break; | |
396 | } | |
98c5c4ad | 397 | } |
290be667 JK |
398 | |
399 | /* If none of the patterns matched, stop now */ | |
400 | if (!matched) | |
401 | return 0; | |
98c5c4ad | 402 | } |
bd321bcc | 403 | |
1974632c | 404 | while (o && o->type == OBJ_TAG) { |
bd321bcc JS |
405 | struct tag *t = (struct tag *) o; |
406 | if (!t->tagged) | |
407 | break; /* broken repository */ | |
109cd76d | 408 | o = parse_object(the_repository, &t->tagged->oid); |
bd321bcc | 409 | deref = 1; |
75504248 | 410 | taggerdate = t->date; |
bd321bcc | 411 | } |
1974632c | 412 | if (o && o->type == OBJ_COMMIT) { |
079f9709 RS |
413 | commit = (struct commit *)o; |
414 | from_tag = starts_with(path, "refs/tags/"); | |
5554451d | 415 | if (taggerdate == TIME_MAX) |
e0c4da6f | 416 | taggerdate = commit->date; |
bd321bcc | 417 | } |
079f9709 RS |
418 | |
419 | add_to_tip_table(oid, path, can_abbreviate_output, commit, taggerdate, | |
420 | from_tag, deref); | |
bd321bcc JS |
421 | return 0; |
422 | } | |
423 | ||
079f9709 RS |
424 | static void name_tips(void) |
425 | { | |
426 | int i; | |
427 | ||
428 | /* | |
429 | * Try to set better names first, so that worse ones spread | |
430 | * less. | |
431 | */ | |
432 | QSORT(tip_table.table, tip_table.nr, cmp_by_tag_and_age); | |
433 | for (i = 0; i < tip_table.nr; i++) { | |
434 | struct tip_table_entry *e = &tip_table.table[i]; | |
435 | if (e->commit) { | |
436 | name_rev(e->commit, e->refname, e->taggerdate, | |
437 | e->from_tag, e->deref); | |
438 | } | |
439 | } | |
440 | } | |
441 | ||
8380dcd7 | 442 | static const struct object_id *nth_tip_table_ent(size_t ix, const void *table_) |
b23e0b93 | 443 | { |
8380dcd7 | 444 | const struct tip_table_entry *table = table_; |
45ee13b9 | 445 | return &table[ix].oid; |
b23e0b93 JH |
446 | } |
447 | ||
448 | static const char *get_exact_ref_match(const struct object *o) | |
449 | { | |
450 | int found; | |
451 | ||
452 | if (!tip_table.table || !tip_table.nr) | |
453 | return NULL; | |
454 | ||
455 | if (!tip_table.sorted) { | |
9ed0d8d6 | 456 | QSORT(tip_table.table, tip_table.nr, tipcmp); |
b23e0b93 JH |
457 | tip_table.sorted = 1; |
458 | } | |
459 | ||
45ee13b9 JK |
460 | found = oid_pos(&o->oid, tip_table.table, tip_table.nr, |
461 | nth_tip_table_ent); | |
b23e0b93 JH |
462 | if (0 <= found) |
463 | return tip_table.table[found].refname; | |
464 | return NULL; | |
465 | } | |
466 | ||
903fc7da JK |
467 | /* may return a constant string or use "buf" as scratch space */ |
468 | static const char *get_rev_name(const struct object *o, struct strbuf *buf) | |
bd321bcc | 469 | { |
d3ff6f55 | 470 | struct rev_name *n; |
36d2419c | 471 | const struct commit *c; |
d3ff6f55 | 472 | |
1974632c | 473 | if (o->type != OBJ_COMMIT) |
b23e0b93 | 474 | return get_exact_ref_match(o); |
36d2419c | 475 | c = (const struct commit *) o; |
8fd79a73 | 476 | n = get_commit_rev_name(c); |
bd321bcc | 477 | if (!n) |
a2cf9f44 | 478 | return NULL; |
bd321bcc JS |
479 | |
480 | if (!n->generation) | |
481 | return n->tip_name; | |
59d3f541 | 482 | else { |
903fc7da | 483 | strbuf_reset(buf); |
c3794d4c RS |
484 | strbuf_addstr(buf, n->tip_name); |
485 | strbuf_strip_suffix(buf, "^0"); | |
486 | strbuf_addf(buf, "~%d", n->generation); | |
903fc7da | 487 | return buf->buf; |
59d3f541 | 488 | } |
bd321bcc | 489 | } |
1f1e895f | 490 | |
da2478db JH |
491 | static void show_name(const struct object *obj, |
492 | const char *caller_name, | |
493 | int always, int allow_undefined, int name_only) | |
494 | { | |
495 | const char *name; | |
f2fd0760 | 496 | const struct object_id *oid = &obj->oid; |
903fc7da | 497 | struct strbuf buf = STRBUF_INIT; |
da2478db JH |
498 | |
499 | if (!name_only) | |
f2fd0760 | 500 | printf("%s ", caller_name ? caller_name : oid_to_hex(oid)); |
903fc7da | 501 | name = get_rev_name(obj, &buf); |
da2478db JH |
502 | if (name) |
503 | printf("%s\n", name); | |
504 | else if (allow_undefined) | |
505 | printf("undefined\n"); | |
506 | else if (always) | |
aab9583f | 507 | printf("%s\n", find_unique_abbrev(oid, DEFAULT_ABBREV)); |
da2478db | 508 | else |
f2fd0760 | 509 | die("cannot describe '%s'", oid_to_hex(oid)); |
903fc7da | 510 | strbuf_release(&buf); |
da2478db JH |
511 | } |
512 | ||
edefb1a2 | 513 | static char const * const name_rev_usage[] = { |
9c9b4f2f AH |
514 | N_("git name-rev [<options>] <commit>..."), |
515 | N_("git name-rev [<options>] --all"), | |
d271892f | 516 | N_("git name-rev [<options>] --annotate-stdin"), |
edefb1a2 PH |
517 | NULL |
518 | }; | |
519 | ||
e8b55fab JH |
520 | static void name_rev_line(char *p, struct name_ref_data *data) |
521 | { | |
903fc7da | 522 | struct strbuf buf = STRBUF_INIT; |
1c4675dc | 523 | int counter = 0; |
e8b55fab | 524 | char *p_start; |
1c4675dc | 525 | const unsigned hexsz = the_hash_algo->hexsz; |
526 | ||
e8b55fab JH |
527 | for (p_start = p; *p; p++) { |
528 | #define ishex(x) (isdigit((x)) || ((x) >= 'a' && (x) <= 'f')) | |
529 | if (!ishex(*p)) | |
1c4675dc | 530 | counter = 0; |
531 | else if (++counter == hexsz && | |
e8b55fab | 532 | !ishex(*(p+1))) { |
511dca80 | 533 | struct object_id oid; |
e8b55fab JH |
534 | const char *name = NULL; |
535 | char c = *(p+1); | |
7c5b1675 | 536 | int p_len = p - p_start + 1; |
e8b55fab | 537 | |
1c4675dc | 538 | counter = 0; |
e8b55fab JH |
539 | |
540 | *(p+1) = 0; | |
1c4675dc | 541 | if (!get_oid(p - (hexsz - 1), &oid)) { |
e8b55fab | 542 | struct object *o = |
d0229abd | 543 | lookup_object(the_repository, &oid); |
e8b55fab | 544 | if (o) |
903fc7da | 545 | name = get_rev_name(o, &buf); |
e8b55fab JH |
546 | } |
547 | *(p+1) = c; | |
548 | ||
549 | if (!name) | |
550 | continue; | |
551 | ||
7c5b1675 | 552 | if (data->name_only) |
1c4675dc | 553 | printf("%.*s%s", p_len - hexsz, p_start, name); |
7c5b1675 RS |
554 | else |
555 | printf("%.*s (%s)", p_len, p_start, name); | |
e8b55fab JH |
556 | p_start = p + 1; |
557 | } | |
558 | } | |
559 | ||
560 | /* flush */ | |
561 | if (p_start != p) | |
562 | fwrite(p_start, p - p_start, 1, stdout); | |
903fc7da JK |
563 | |
564 | strbuf_release(&buf); | |
e8b55fab JH |
565 | } |
566 | ||
d6b64ed0 | 567 | int cmd_name_rev(int argc, const char **argv, const char *prefix) |
bd321bcc | 568 | { |
3cd47459 | 569 | struct object_array revs = OBJECT_ARRAY_INIT; |
34ae3b70 | 570 | int all = 0, annotate_stdin = 0, transform_stdin = 0, allow_undefined = 1, always = 0, peel_tag = 0; |
96415b49 | 571 | struct name_ref_data data = { 0, 0, STRING_LIST_INIT_NODUP, STRING_LIST_INIT_NODUP }; |
edefb1a2 | 572 | struct option opts[] = { |
4279000d | 573 | OPT_BOOL(0, "name-only", &data.name_only, N_("print only ref-based names (no object names)")), |
d5d09d47 | 574 | OPT_BOOL(0, "tags", &data.tags_only, N_("only use tags to name the commits")), |
290be667 | 575 | OPT_STRING_LIST(0, "refs", &data.ref_filters, N_("pattern"), |
422cad0a | 576 | N_("only use refs matching <pattern>")), |
96415b49 JK |
577 | OPT_STRING_LIST(0, "exclude", &data.exclude_filters, N_("pattern"), |
578 | N_("ignore refs matching <pattern>")), | |
edefb1a2 | 579 | OPT_GROUP(""), |
d5d09d47 | 580 | OPT_BOOL(0, "all", &all, N_("list all commits reachable from all refs")), |
325240df | 581 | OPT_BOOL(0, "stdin", &transform_stdin, N_("deprecated: use --annotate-stdin instead")), |
34ae3b70 | 582 | OPT_BOOL(0, "annotate-stdin", &annotate_stdin, N_("annotate text from stdin")), |
d5d09d47 SB |
583 | OPT_BOOL(0, "undefined", &allow_undefined, N_("allow to print `undefined` names (default)")), |
584 | OPT_BOOL(0, "always", &always, | |
422cad0a | 585 | N_("show abbreviated commit object as fallback")), |
adfc1857 JH |
586 | { |
587 | /* A Hidden OPT_BOOL */ | |
588 | OPTION_SET_INT, 0, "peel-tag", &peel_tag, NULL, | |
589 | N_("dereference tags in the input (internal use)"), | |
590 | PARSE_OPT_NOARG | PARSE_OPT_HIDDEN, NULL, 1, | |
591 | }, | |
edefb1a2 PH |
592 | OPT_END(), |
593 | }; | |
bd321bcc | 594 | |
8fd79a73 | 595 | init_commit_rev_name(&rev_names); |
ef90d6d4 | 596 | git_config(git_default_config, NULL); |
37782920 | 597 | argc = parse_options(argc, argv, prefix, opts, name_rev_usage, 0); |
34ae3b70 JC |
598 | |
599 | if (transform_stdin) { | |
600 | warning("--stdin is deprecated. Please use --annotate-stdin instead, " | |
601 | "which is functionally equivalent.\n" | |
602 | "This option will be removed in a future release."); | |
603 | annotate_stdin = 1; | |
604 | } | |
605 | ||
606 | if (all + annotate_stdin + !!argc > 1) { | |
edefb1a2 PH |
607 | error("Specify either a list, or --all, not both!"); |
608 | usage_with_options(name_rev_usage, opts); | |
609 | } | |
34ae3b70 | 610 | if (all || annotate_stdin) |
2e8ea40f | 611 | disable_cutoff(); |
bd321bcc | 612 | |
edefb1a2 | 613 | for (; argc; argc--, argv++) { |
511dca80 | 614 | struct object_id oid; |
118aa4ac | 615 | struct object *object; |
bd321bcc JS |
616 | struct commit *commit; |
617 | ||
511dca80 | 618 | if (get_oid(*argv, &oid)) { |
bd321bcc JS |
619 | fprintf(stderr, "Could not get sha1 for %s. Skipping.\n", |
620 | *argv); | |
621 | continue; | |
622 | } | |
623 | ||
118aa4ac | 624 | commit = NULL; |
109cd76d | 625 | object = parse_object(the_repository, &oid); |
118aa4ac | 626 | if (object) { |
a74093da SB |
627 | struct object *peeled = deref_tag(the_repository, |
628 | object, *argv, 0); | |
118aa4ac JH |
629 | if (peeled && peeled->type == OBJ_COMMIT) |
630 | commit = (struct commit *)peeled; | |
631 | } | |
632 | ||
633 | if (!object) { | |
634 | fprintf(stderr, "Could not get object for %s. Skipping.\n", | |
bd321bcc JS |
635 | *argv); |
636 | continue; | |
637 | } | |
638 | ||
2e8ea40f JK |
639 | if (commit) |
640 | set_commit_cutoff(commit); | |
adfc1857 JH |
641 | |
642 | if (peel_tag) { | |
643 | if (!commit) { | |
644 | fprintf(stderr, "Could not get commit for %s. Skipping.\n", | |
645 | *argv); | |
646 | continue; | |
647 | } | |
648 | object = (struct object *)commit; | |
649 | } | |
118aa4ac | 650 | add_object_array(object, *argv, &revs); |
bd321bcc JS |
651 | } |
652 | ||
2e8ea40f JK |
653 | adjust_cutoff_timestamp_for_slop(); |
654 | ||
73868486 | 655 | for_each_ref(name_ref, &data); |
079f9709 | 656 | name_tips(); |
bd321bcc | 657 | |
34ae3b70 | 658 | if (annotate_stdin) { |
a2585719 | 659 | struct strbuf sb = STRBUF_INIT; |
bd321bcc | 660 | |
a2585719 JC |
661 | while (strbuf_getline(&sb, stdin) != EOF) { |
662 | strbuf_addch(&sb, '\n'); | |
663 | name_rev_line(sb.buf, &data); | |
bd321bcc | 664 | } |
a2585719 | 665 | strbuf_release(&sb); |
bd321bcc | 666 | } else if (all) { |
fc046a75 | 667 | int i, max; |
bd321bcc | 668 | |
fc046a75 | 669 | max = get_max_object_index(); |
a83123de BS |
670 | for (i = 0; i < max; i++) { |
671 | struct object *obj = get_indexed_object(i); | |
e8b14d7e | 672 | if (!obj || obj->type != OBJ_COMMIT) |
a83123de BS |
673 | continue; |
674 | show_name(obj, NULL, | |
da2478db | 675 | always, allow_undefined, data.name_only); |
a83123de | 676 | } |
1f1e895f LT |
677 | } else { |
678 | int i; | |
da2478db JH |
679 | for (i = 0; i < revs.nr; i++) |
680 | show_name(revs.objects[i].item, revs.objects[i].name, | |
681 | always, allow_undefined, data.name_only); | |
1f1e895f | 682 | } |
bd321bcc | 683 | |
886e1084 | 684 | UNLEAK(revs); |
bd321bcc JS |
685 | return 0; |
686 | } |