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