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