]> git.ipfire.org Git - thirdparty/git.git/blame - builtin-rev-list.c
deref_tag: handle tag->tagged = NULL
[thirdparty/git.git] / builtin-rev-list.c
CommitLineData
64745109 1#include "cache.h"
e091eb93 2#include "refs.h"
36f8d174 3#include "tag.h"
64745109 4#include "commit.h"
9de48752
LT
5#include "tree.h"
6#include "blob.h"
1b0c7174 7#include "tree-walk.h"
c4e05b1a 8#include "diff.h"
ae563542 9#include "revision.h"
c64ed70d 10#include "list-objects.h"
5fb61b8d 11#include "builtin.h"
50e62a8e 12#include "log-tree.h"
ae563542 13
1b65a5aa 14/* bits #0-15 in revision.h */
64745109 15
1b65a5aa 16#define COUNTED (1u<<16)
8906300f 17
a6f68d47 18static const char rev_list_usage[] =
69e0c256
JH
19"git-rev-list [OPTION] <commit-id>... [ -- paths... ]\n"
20" limiting output:\n"
21" --max-count=nr\n"
22" --max-age=epoch\n"
23" --min-age=epoch\n"
24" --sparse\n"
25" --no-merges\n"
93b74bca 26" --remove-empty\n"
69e0c256 27" --all\n"
42cabc34 28" --stdin\n"
27350891 29" --quiet\n"
69e0c256 30" ordering output:\n"
69e0c256 31" --topo-order\n"
4c8725f1 32" --date-order\n"
69e0c256
JH
33" formatting output:\n"
34" --parents\n"
c6496575 35" --objects | --objects-edge\n"
69e0c256
JH
36" --unpacked\n"
37" --header | --pretty\n"
9da5c2f0 38" --abbrev=nr | --no-abbrev\n"
5c51c985 39" --abbrev-commit\n"
b24bace5 40" --left-right\n"
69e0c256 41" special purpose:\n"
457f08a0 42" --bisect\n"
50e62a8e
CC
43" --bisect-vars\n"
44" --bisect-all"
69e0c256 45;
a6f68d47 46
5fb61b8d 47static struct rev_info revs;
ae563542 48
96f1e58f
DR
49static int bisect_list;
50static int show_timestamp;
51static int hdr_termination;
91539833 52static const char *header_prefix;
e646de0d 53
27350891 54static void finish_commit(struct commit *commit);
81f2bb1f
LT
55static void show_commit(struct commit *commit)
56{
dc68c4ff
JH
57 if (show_timestamp)
58 printf("%lu ", commit->date);
91539833
LT
59 if (header_prefix)
60 fputs(header_prefix, stdout);
384e99a4
JH
61 if (commit->object.flags & BOUNDARY)
62 putchar('-');
74bd9029 63 else if (revs.left_right) {
577ed5c2
JH
64 if (commit->object.flags & SYMMETRIC_LEFT)
65 putchar('<');
66 else
67 putchar('>');
68 }
7594c4b2
JH
69 if (revs.abbrev_commit && revs.abbrev)
70 fputs(find_unique_abbrev(commit->object.sha1, revs.abbrev),
71 stdout);
5c51c985
JH
72 else
73 fputs(sha1_to_hex(commit->object.sha1), stdout);
7b0c9966 74 if (revs.parents) {
81f2bb1f
LT
75 struct commit_list *parents = commit->parents;
76 while (parents) {
1ed84157 77 printf(" %s", sha1_to_hex(parents->item->object.sha1));
81f2bb1f
LT
78 parents = parents->next;
79 }
80 }
50e62a8e 81 show_decorations(commit);
7594c4b2 82 if (revs.commit_format == CMIT_FMT_ONELINE)
d87449c5
JH
83 putchar(' ');
84 else
85 putchar('\n');
86
7594c4b2 87 if (revs.verbose_header) {
674d1727
PH
88 struct strbuf buf;
89 strbuf_init(&buf, 0);
90 pretty_print_commit(revs.commit_format, commit,
4593fb84
JH
91 &buf, revs.abbrev, NULL, NULL,
92 revs.date_mode, 0);
cc61ae82
JH
93 if (buf.len)
94 printf("%s%c", buf.buf, hdr_termination);
674d1727 95 strbuf_release(&buf);
7620d39f 96 }
06f59e9f 97 maybe_flush_or_die(stdout, "stdout");
27350891
SP
98 finish_commit(commit);
99}
100
101static void finish_commit(struct commit *commit)
102{
cb115748
LT
103 if (commit->parents) {
104 free_commit_list(commit->parents);
105 commit->parents = NULL;
106 }
4cac42b1
JH
107 free(commit->buffer);
108 commit->buffer = NULL;
a3437b8c
JS
109}
110
27350891
SP
111static void finish_object(struct object_array_entry *p)
112{
113 if (p->item->type == OBJ_BLOB && !has_sha1_file(p->item->sha1))
114 die("missing blob object '%s'", sha1_to_hex(p->item->sha1));
115}
116
c64ed70d 117static void show_object(struct object_array_entry *p)
9de48752 118{
c64ed70d
JH
119 /* An object with name "foo\n0000000..." can be used to
120 * confuse downstream git-pack-objects very badly.
121 */
122 const char *ep = strchr(p->name, '\n');
b9849a1a 123
27350891 124 finish_object(p);
c64ed70d
JH
125 if (ep) {
126 printf("%s %.*s\n", sha1_to_hex(p->item->sha1),
127 (int) (ep - p->name),
128 p->name);
9de48752 129 }
c64ed70d
JH
130 else
131 printf("%s %s\n", sha1_to_hex(p->item->sha1), p->name);
9de48752
LT
132}
133
8d1d8f83
JH
134static void show_edge(struct commit *commit)
135{
136 printf("-%s\n", sha1_to_hex(commit->object.sha1));
137}
138
8b3a1e05
LT
139/*
140 * This is a truly stupid algorithm, but it's only
141 * used for bisection, and we just don't care enough.
142 *
143 * We care just barely enough to avoid recursing for
144 * non-merge entries.
145 */
146static int count_distance(struct commit_list *entry)
147{
148 int nr = 0;
149
150 while (entry) {
151 struct commit *commit = entry->item;
152 struct commit_list *p;
153
154 if (commit->object.flags & (UNINTERESTING | COUNTED))
155 break;
7dc0fe3b 156 if (!(commit->object.flags & TREESAME))
b3cfd939 157 nr++;
8b3a1e05
LT
158 commit->object.flags |= COUNTED;
159 p = commit->parents;
160 entry = p;
161 if (p) {
162 p = p->next;
163 while (p) {
164 nr += count_distance(p);
165 p = p->next;
166 }
167 }
168 }
b3cfd939 169
8b3a1e05
LT
170 return nr;
171}
172
3d958064 173static void clear_distance(struct commit_list *list)
8b3a1e05
LT
174{
175 while (list) {
176 struct commit *commit = list->item;
177 commit->object.flags &= ~COUNTED;
178 list = list->next;
179 }
180}
181
1daa09d9 182#define DEBUG_BISECT 0
1c4fea3a
JH
183
184static inline int weight(struct commit_list *elem)
185{
186 return *((int*)(elem->item->util));
187}
188
189static inline void weight_set(struct commit_list *elem, int weight)
190{
191 *((int*)(elem->item->util)) = weight;
192}
193
1daa09d9 194static int count_interesting_parents(struct commit *commit)
1c4fea3a 195{
1daa09d9
JH
196 struct commit_list *p;
197 int count;
198
199 for (count = 0, p = commit->parents; p; p = p->next) {
200 if (p->item->object.flags & UNINTERESTING)
201 continue;
202 count++;
1c4fea3a 203 }
1daa09d9 204 return count;
1c4fea3a
JH
205}
206
53271411 207static inline int halfway(struct commit_list *p, int nr)
2a464690
JH
208{
209 /*
210 * Don't short-cut something we are not going to return!
211 */
7dc0fe3b 212 if (p->item->object.flags & TREESAME)
2a464690 213 return 0;
1daa09d9
JH
214 if (DEBUG_BISECT)
215 return 0;
2a464690
JH
216 /*
217 * 2 and 3 are halfway of 5.
218 * 3 is halfway of 6 but 2 and 4 are not.
219 */
53271411 220 switch (2 * weight(p) - nr) {
2a464690
JH
221 case -1: case 0: case 1:
222 return 1;
223 default:
224 return 0;
225 }
226}
227
1daa09d9
JH
228#if !DEBUG_BISECT
229#define show_list(a,b,c,d) do { ; } while (0)
230#else
231static void show_list(const char *debug, int counted, int nr,
232 struct commit_list *list)
1c4fea3a 233{
1daa09d9
JH
234 struct commit_list *p;
235
236 fprintf(stderr, "%s (%d/%d)\n", debug, counted, nr);
237
238 for (p = list; p; p = p->next) {
239 struct commit_list *pp;
240 struct commit *commit = p->item;
241 unsigned flags = commit->object.flags;
242 enum object_type type;
243 unsigned long size;
244 char *buf = read_sha1_file(commit->object.sha1, &type, &size);
245 char *ep, *sp;
246
247 fprintf(stderr, "%c%c%c ",
7dc0fe3b 248 (flags & TREESAME) ? ' ' : 'T',
1daa09d9
JH
249 (flags & UNINTERESTING) ? 'U' : ' ',
250 (flags & COUNTED) ? 'C' : ' ');
251 if (commit->util)
252 fprintf(stderr, "%3d", weight(p));
253 else
254 fprintf(stderr, "---");
255 fprintf(stderr, " %.*s", 8, sha1_to_hex(commit->object.sha1));
256 for (pp = commit->parents; pp; pp = pp->next)
257 fprintf(stderr, " %.*s", 8,
258 sha1_to_hex(pp->item->object.sha1));
259
260 sp = strstr(buf, "\n\n");
261 if (sp) {
262 sp += 2;
263 for (ep = sp; *ep && *ep != '\n'; ep++)
264 ;
265 fprintf(stderr, " %.*s", (int)(ep - sp), sp);
266 }
267 fprintf(stderr, "\n");
268 }
269}
270#endif /* DEBUG_BISECT */
271
77c11e06
CC
272static struct commit_list *best_bisection(struct commit_list *list, int nr)
273{
274 struct commit_list *p, *best;
275 int best_distance = -1;
276
277 best = list;
278 for (p = list; p; p = p->next) {
279 int distance;
280 unsigned flags = p->item->object.flags;
281
7dc0fe3b 282 if (flags & TREESAME)
77c11e06
CC
283 continue;
284 distance = weight(p);
285 if (nr - distance < distance)
286 distance = nr - distance;
287 if (distance > best_distance) {
288 best = p;
289 best_distance = distance;
290 }
291 }
292
293 return best;
294}
295
50e62a8e
CC
296struct commit_dist {
297 struct commit *commit;
298 int distance;
299};
300
301static int compare_commit_dist(const void *a_, const void *b_)
302{
303 struct commit_dist *a, *b;
304
305 a = (struct commit_dist *)a_;
306 b = (struct commit_dist *)b_;
307 if (a->distance != b->distance)
308 return b->distance - a->distance; /* desc sort */
309 return hashcmp(a->commit->object.sha1, b->commit->object.sha1);
310}
311
312static struct commit_list *best_bisection_sorted(struct commit_list *list, int nr)
313{
314 struct commit_list *p;
315 struct commit_dist *array = xcalloc(nr, sizeof(*array));
316 int cnt, i;
317
318 for (p = list, cnt = 0; p; p = p->next) {
319 int distance;
320 unsigned flags = p->item->object.flags;
321
7dc0fe3b 322 if (flags & TREESAME)
50e62a8e
CC
323 continue;
324 distance = weight(p);
325 if (nr - distance < distance)
326 distance = nr - distance;
327 array[cnt].commit = p->item;
328 array[cnt].distance = distance;
329 cnt++;
330 }
331 qsort(array, cnt, sizeof(*array), compare_commit_dist);
332 for (p = list, i = 0; i < cnt; i++) {
333 struct name_decoration *r = xmalloc(sizeof(*r) + 100);
334 struct object *obj = &(array[i].commit->object);
335
336 sprintf(r->name, "dist=%d", array[i].distance);
337 r->next = add_decoration(&name_decoration, obj, r);
338 p->item = array[i].commit;
339 p = p->next;
340 }
341 if (p)
342 p->next = NULL;
343 free(array);
344 return list;
345}
346
1daa09d9
JH
347/*
348 * zero or positive weight is the number of interesting commits it can
349 * reach, including itself. Especially, weight = 0 means it does not
350 * reach any tree-changing commits (e.g. just above uninteresting one
351 * but traversal is with pathspec).
352 *
353 * weight = -1 means it has one parent and its distance is yet to
354 * be computed.
355 *
356 * weight = -2 means it has more than one parent and its distance is
357 * unknown. After running count_distance() first, they will get zero
358 * or positive distance.
359 */
ce0cbad7 360static struct commit_list *do_find_bisection(struct commit_list *list,
50e62a8e
CC
361 int nr, int *weights,
362 int find_all)
1daa09d9 363{
53271411 364 int n, counted;
77c11e06 365 struct commit_list *p;
1daa09d9 366
1c4fea3a
JH
367 counted = 0;
368
369 for (n = 0, p = list; p; p = p->next) {
1daa09d9
JH
370 struct commit *commit = p->item;
371 unsigned flags = commit->object.flags;
372
373 p->item->util = &weights[n++];
374 switch (count_interesting_parents(commit)) {
375 case 0:
7dc0fe3b 376 if (!(flags & TREESAME)) {
1c4fea3a
JH
377 weight_set(p, 1);
378 counted++;
1daa09d9
JH
379 show_list("bisection 2 count one",
380 counted, nr, list);
1c4fea3a 381 }
1daa09d9
JH
382 /*
383 * otherwise, it is known not to reach any
384 * tree-changing commit and gets weight 0.
385 */
386 break;
387 case 1:
388 weight_set(p, -1);
389 break;
390 default:
391 weight_set(p, -2);
392 break;
1c4fea3a
JH
393 }
394 }
395
1daa09d9
JH
396 show_list("bisection 2 initialize", counted, nr, list);
397
1c4fea3a
JH
398 /*
399 * If you have only one parent in the resulting set
400 * then you can reach one commit more than that parent
401 * can reach. So we do not have to run the expensive
402 * count_distance() for single strand of pearls.
403 *
404 * However, if you have more than one parents, you cannot
405 * just add their distance and one for yourself, since
406 * they usually reach the same ancestor and you would
407 * end up counting them twice that way.
408 *
409 * So we will first count distance of merges the usual
410 * way, and then fill the blanks using cheaper algorithm.
411 */
412 for (p = list; p; p = p->next) {
1daa09d9 413 if (p->item->object.flags & UNINTERESTING)
1c4fea3a 414 continue;
53271411 415 if (weight(p) != -2)
1c4fea3a 416 continue;
53271411 417 weight_set(p, count_distance(p));
1daa09d9 418 clear_distance(list);
1c4fea3a
JH
419
420 /* Does it happen to be at exactly half-way? */
50e62a8e 421 if (!find_all && halfway(p, nr))
1c4fea3a 422 return p;
1c4fea3a
JH
423 counted++;
424 }
425
1daa09d9
JH
426 show_list("bisection 2 count_distance", counted, nr, list);
427
1c4fea3a
JH
428 while (counted < nr) {
429 for (p = list; p; p = p->next) {
430 struct commit_list *q;
1daa09d9 431 unsigned flags = p->item->object.flags;
1c4fea3a 432
1daa09d9 433 if (0 <= weight(p))
1c4fea3a 434 continue;
1daa09d9
JH
435 for (q = p->item->parents; q; q = q->next) {
436 if (q->item->object.flags & UNINTERESTING)
437 continue;
438 if (0 <= weight(q))
1c4fea3a 439 break;
1daa09d9 440 }
1c4fea3a
JH
441 if (!q)
442 continue;
1daa09d9
JH
443
444 /*
445 * weight for p is unknown but q is known.
446 * add one for p itself if p is to be counted,
447 * otherwise inherit it from q directly.
448 */
7dc0fe3b 449 if (!(flags & TREESAME)) {
1daa09d9
JH
450 weight_set(p, weight(q)+1);
451 counted++;
452 show_list("bisection 2 count one",
453 counted, nr, list);
454 }
455 else
456 weight_set(p, weight(q));
1c4fea3a
JH
457
458 /* Does it happen to be at exactly half-way? */
50e62a8e 459 if (!find_all && halfway(p, nr))
1c4fea3a 460 return p;
1c4fea3a
JH
461 }
462 }
463
1daa09d9
JH
464 show_list("bisection 2 counted all", counted, nr, list);
465
50e62a8e
CC
466 if (!find_all)
467 return best_bisection(list, nr);
468 else
469 return best_bisection_sorted(list, nr);
ce0cbad7
CC
470}
471
472static struct commit_list *find_bisection(struct commit_list *list,
50e62a8e
CC
473 int *reaches, int *all,
474 int find_all)
ce0cbad7
CC
475{
476 int nr, on_list;
477 struct commit_list *p, *best, *next, *last;
478 int *weights;
479
480 show_list("bisection 2 entry", 0, 0, list);
481
482 /*
483 * Count the number of total and tree-changing items on the
484 * list, while reversing the list.
485 */
486 for (nr = on_list = 0, last = NULL, p = list;
487 p;
488 p = next) {
489 unsigned flags = p->item->object.flags;
490
491 next = p->next;
492 if (flags & UNINTERESTING)
493 continue;
494 p->next = last;
495 last = p;
7dc0fe3b 496 if (!(flags & TREESAME))
ce0cbad7
CC
497 nr++;
498 on_list++;
499 }
500 list = last;
501 show_list("bisection 2 sorted", 0, nr, list);
502
503 *all = nr;
504 weights = xcalloc(on_list, sizeof(*weights));
505
506 /* Do the real work of finding bisection commit. */
50e62a8e 507 best = do_find_bisection(list, nr, weights, find_all);
17ed1580 508 if (best) {
50e62a8e
CC
509 if (!find_all)
510 best->next = NULL;
17ed1580
CC
511 *reaches = weight(best);
512 }
1c4fea3a
JH
513 free(weights);
514 return best;
515}
516
42cabc34
JH
517static void read_revisions_from_stdin(struct rev_info *revs)
518{
519 char line[1000];
520
521 while (fgets(line, sizeof(line), stdin) != NULL) {
522 int len = strlen(line);
872c930d 523 if (len && line[len - 1] == '\n')
42cabc34
JH
524 line[--len] = 0;
525 if (!len)
526 break;
527 if (line[0] == '-')
528 die("options not supported in --stdin mode");
529 if (handle_revision_arg(line, revs, 0, 1))
530 die("bad revision '%s'", line);
531 }
532}
533
a633fca0 534int cmd_rev_list(int argc, const char **argv, const char *prefix)
64745109 535{
ae563542 536 struct commit_list *list;
d9a83684 537 int i;
42cabc34 538 int read_from_stdin = 0;
457f08a0 539 int bisect_show_vars = 0;
50e62a8e 540 int bisect_find_all = 0;
27350891 541 int quiet = 0;
64745109 542
256c3fe6 543 git_config(git_default_config);
a633fca0 544 init_revisions(&revs, prefix);
7594c4b2
JH
545 revs.abbrev = 0;
546 revs.commit_format = CMIT_FMT_UNSPECIFIED;
a4a88b2b 547 argc = setup_revisions(argc, argv, &revs, NULL);
ae563542 548
fcfda02b 549 for (i = 1 ; i < argc; i++) {
cf484544 550 const char *arg = argv[i];
fcfda02b 551
a6f68d47 552 if (!strcmp(arg, "--header")) {
7594c4b2 553 revs.verbose_header = 1;
9d97aa64
LT
554 continue;
555 }
dc68c4ff
JH
556 if (!strcmp(arg, "--timestamp")) {
557 show_timestamp = 1;
558 continue;
559 }
8b3a1e05
LT
560 if (!strcmp(arg, "--bisect")) {
561 bisect_list = 1;
562 continue;
563 }
50e62a8e
CC
564 if (!strcmp(arg, "--bisect-all")) {
565 bisect_list = 1;
566 bisect_find_all = 1;
567 continue;
568 }
457f08a0
JH
569 if (!strcmp(arg, "--bisect-vars")) {
570 bisect_list = 1;
571 bisect_show_vars = 1;
572 continue;
573 }
42cabc34
JH
574 if (!strcmp(arg, "--stdin")) {
575 if (read_from_stdin++)
576 die("--stdin given twice?");
577 read_revisions_from_stdin(&revs);
578 continue;
579 }
27350891
SP
580 if (!strcmp(arg, "--quiet")) {
581 quiet = 1;
582 continue;
583 }
ae563542 584 usage(rev_list_usage);
a6f68d47 585
fcfda02b 586 }
7594c4b2
JH
587 if (revs.commit_format != CMIT_FMT_UNSPECIFIED) {
588 /* The command line has a --pretty */
589 hdr_termination = '\n';
590 if (revs.commit_format == CMIT_FMT_ONELINE)
91539833 591 header_prefix = "";
7594c4b2 592 else
91539833 593 header_prefix = "commit ";
7594c4b2 594 }
db89665f
JH
595 else if (revs.verbose_header)
596 /* Only --header was specified */
597 revs.commit_format = CMIT_FMT_RAW;
fcfda02b 598
ae563542 599 list = revs.commits;
ae563542 600
8c1f0b44
JH
601 if ((!list &&
602 (!(revs.tag_objects||revs.tree_objects||revs.blob_objects) &&
1f1e895f 603 !revs.pending.nr)) ||
8c1f0b44 604 revs.diff)
7b34c2fa
LT
605 usage(rev_list_usage);
606
2d10c555 607 save_commit_buffer = revs.verbose_header || revs.grep_filter;
9181ca2c 608 track_object_refs = 0;
4e1dc640
JH
609 if (bisect_list)
610 revs.limited = 1;
9181ca2c 611
a4a88b2b
LT
612 prepare_revision_walk(&revs);
613 if (revs.tree_objects)
8d1d8f83 614 mark_edges_uninteresting(revs.commits, &revs, show_edge);
a4a88b2b 615
457f08a0
JH
616 if (bisect_list) {
617 int reaches = reaches, all = all;
618
50e62a8e
CC
619 revs.commits = find_bisection(revs.commits, &reaches, &all,
620 bisect_find_all);
457f08a0
JH
621 if (bisect_show_vars) {
622 int cnt;
50e62a8e 623 char hex[41];
457f08a0
JH
624 if (!revs.commits)
625 return 1;
626 /*
627 * revs.commits can reach "reaches" commits among
628 * "all" commits. If it is good, then there are
629 * (all-reaches) commits left to be bisected.
630 * On the other hand, if it is bad, then the set
631 * to bisect is "reaches".
632 * A bisect set of size N has (N-1) commits further
633 * to test, as we already know one bad one.
634 */
50e62a8e 635 cnt = all - reaches;
457f08a0
JH
636 if (cnt < reaches)
637 cnt = reaches;
50e62a8e
CC
638 strcpy(hex, sha1_to_hex(revs.commits->item->object.sha1));
639
640 if (bisect_find_all) {
641 traverse_commit_list(&revs, show_commit, show_object);
642 printf("------\n");
643 }
644
457f08a0
JH
645 printf("bisect_rev=%s\n"
646 "bisect_nr=%d\n"
647 "bisect_good=%d\n"
648 "bisect_bad=%d\n"
649 "bisect_all=%d\n",
50e62a8e 650 hex,
457f08a0
JH
651 cnt - 1,
652 all - reaches - 1,
653 reaches - 1,
654 all);
655 return 0;
656 }
657 }
7b34c2fa 658
27350891
SP
659 traverse_commit_list(&revs,
660 quiet ? finish_commit : show_commit,
661 quiet ? finish_object : show_object);
8906300f 662
64745109
LT
663 return 0;
664}