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