Merge commit 'junio/next' into next
[git/platforms/storm.git] / builtin-rev-list.c
bloba9652946989539e0f0a34af37a57d3b56f279103
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"
15 /* bits #0-15 in revision.h */
17 #define COUNTED (1u<<16)
19 static 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 " --children\n"
41 " --objects | --objects-edge\n"
42 " --unpacked\n"
43 " --header | --pretty\n"
44 " --abbrev=nr | --no-abbrev\n"
45 " --abbrev-commit\n"
46 " --left-right\n"
47 " special purpose:\n"
48 " --bisect\n"
49 " --bisect-vars\n"
50 " --bisect-all"
53 static struct rev_info revs;
55 static int bisect_list;
56 static int show_timestamp;
57 static int hdr_termination;
58 static const char *header_prefix;
60 static void finish_commit(struct commit *commit);
61 static void show_commit(struct commit *commit)
63 graph_show_commit(revs.graph);
65 if (show_timestamp)
66 printf("%lu ", commit->date);
67 if (header_prefix)
68 fputs(header_prefix, stdout);
69 if (commit->object.flags & BOUNDARY)
70 putchar('-');
71 else if (commit->object.flags & UNINTERESTING)
72 putchar('^');
73 else if (revs.left_right) {
74 if (commit->object.flags & SYMMETRIC_LEFT)
75 putchar('<');
76 else
77 putchar('>');
79 if (revs.abbrev_commit && revs.abbrev)
80 fputs(find_unique_abbrev(commit->object.sha1, revs.abbrev),
81 stdout);
82 else
83 fputs(sha1_to_hex(commit->object.sha1), stdout);
84 if (revs.print_parents) {
85 struct commit_list *parents = commit->parents;
86 while (parents) {
87 printf(" %s", sha1_to_hex(parents->item->object.sha1));
88 parents = parents->next;
91 if (revs.children.name) {
92 struct commit_list *children;
94 children = lookup_decoration(&revs.children, &commit->object);
95 while (children) {
96 printf(" %s", sha1_to_hex(children->item->object.sha1));
97 children = children->next;
100 show_decorations(commit);
101 if (revs.commit_format == CMIT_FMT_ONELINE)
102 putchar(' ');
103 else
104 putchar('\n');
106 if (revs.verbose_header && commit->buffer) {
107 struct strbuf buf;
108 strbuf_init(&buf, 0);
109 pretty_print_commit(revs.commit_format, commit,
110 &buf, revs.abbrev, NULL, NULL,
111 revs.date_mode, 0);
112 if (revs.graph) {
113 if (buf.len) {
114 if (revs.commit_format != CMIT_FMT_ONELINE)
115 graph_show_oneline(revs.graph);
117 graph_show_commit_msg(revs.graph, &buf);
120 * Add a newline after the commit message.
122 * Usually, this newline produces a blank
123 * padding line between entries, in which case
124 * we need to add graph padding on this line.
126 * However, the commit message may not end in a
127 * newline. In this case the newline simply
128 * ends the last line of the commit message,
129 * and we don't need any graph output. (This
130 * always happens with CMIT_FMT_ONELINE, and it
131 * happens with CMIT_FMT_USERFORMAT when the
132 * format doesn't explicitly end in a newline.)
134 if (buf.len && buf.buf[buf.len - 1] == '\n')
135 graph_show_padding(revs.graph);
136 putchar('\n');
137 } else {
139 * If the message buffer is empty, just show
140 * the rest of the graph output for this
141 * commit.
143 if (graph_show_remainder(revs.graph))
144 putchar('\n');
146 } else {
147 if (buf.len)
148 printf("%s%c", buf.buf, hdr_termination);
150 strbuf_release(&buf);
151 } else {
152 if (graph_show_remainder(revs.graph))
153 putchar('\n');
155 maybe_flush_or_die(stdout, "stdout");
156 finish_commit(commit);
159 static void finish_commit(struct commit *commit)
161 if (commit->parents) {
162 free_commit_list(commit->parents);
163 commit->parents = NULL;
165 free(commit->buffer);
166 commit->buffer = NULL;
169 static void finish_object(struct object_array_entry *p)
171 if (p->item->type == OBJ_BLOB && !has_sha1_file(p->item->sha1))
172 die("missing blob object '%s'", sha1_to_hex(p->item->sha1));
175 static void show_object(struct object_array_entry *p)
177 /* An object with name "foo\n0000000..." can be used to
178 * confuse downstream git-pack-objects very badly.
180 const char *ep = strchr(p->name, '\n');
182 finish_object(p);
183 if (ep) {
184 printf("%s %.*s\n", sha1_to_hex(p->item->sha1),
185 (int) (ep - p->name),
186 p->name);
188 else
189 printf("%s %s\n", sha1_to_hex(p->item->sha1), p->name);
192 static void show_edge(struct commit *commit)
194 printf("-%s\n", sha1_to_hex(commit->object.sha1));
198 * This is a truly stupid algorithm, but it's only
199 * used for bisection, and we just don't care enough.
201 * We care just barely enough to avoid recursing for
202 * non-merge entries.
204 static int count_distance(struct commit_list *entry)
206 int nr = 0;
208 while (entry) {
209 struct commit *commit = entry->item;
210 struct commit_list *p;
212 if (commit->object.flags & (UNINTERESTING | COUNTED))
213 break;
214 if (!(commit->object.flags & TREESAME))
215 nr++;
216 commit->object.flags |= COUNTED;
217 p = commit->parents;
218 entry = p;
219 if (p) {
220 p = p->next;
221 while (p) {
222 nr += count_distance(p);
223 p = p->next;
228 return nr;
231 static void clear_distance(struct commit_list *list)
233 while (list) {
234 struct commit *commit = list->item;
235 commit->object.flags &= ~COUNTED;
236 list = list->next;
240 #define DEBUG_BISECT 0
242 static inline int weight(struct commit_list *elem)
244 return *((int*)(elem->item->util));
247 static inline void weight_set(struct commit_list *elem, int weight)
249 *((int*)(elem->item->util)) = weight;
252 static int count_interesting_parents(struct commit *commit)
254 struct commit_list *p;
255 int count;
257 for (count = 0, p = commit->parents; p; p = p->next) {
258 if (p->item->object.flags & UNINTERESTING)
259 continue;
260 count++;
262 return count;
265 static inline int halfway(struct commit_list *p, int nr)
268 * Don't short-cut something we are not going to return!
270 if (p->item->object.flags & TREESAME)
271 return 0;
272 if (DEBUG_BISECT)
273 return 0;
275 * 2 and 3 are halfway of 5.
276 * 3 is halfway of 6 but 2 and 4 are not.
278 switch (2 * weight(p) - nr) {
279 case -1: case 0: case 1:
280 return 1;
281 default:
282 return 0;
286 #if !DEBUG_BISECT
287 #define show_list(a,b,c,d) do { ; } while (0)
288 #else
289 static void show_list(const char *debug, int counted, int nr,
290 struct commit_list *list)
292 struct commit_list *p;
294 fprintf(stderr, "%s (%d/%d)\n", debug, counted, nr);
296 for (p = list; p; p = p->next) {
297 struct commit_list *pp;
298 struct commit *commit = p->item;
299 unsigned flags = commit->object.flags;
300 enum object_type type;
301 unsigned long size;
302 char *buf = read_sha1_file(commit->object.sha1, &type, &size);
303 char *ep, *sp;
305 fprintf(stderr, "%c%c%c ",
306 (flags & TREESAME) ? ' ' : 'T',
307 (flags & UNINTERESTING) ? 'U' : ' ',
308 (flags & COUNTED) ? 'C' : ' ');
309 if (commit->util)
310 fprintf(stderr, "%3d", weight(p));
311 else
312 fprintf(stderr, "---");
313 fprintf(stderr, " %.*s", 8, sha1_to_hex(commit->object.sha1));
314 for (pp = commit->parents; pp; pp = pp->next)
315 fprintf(stderr, " %.*s", 8,
316 sha1_to_hex(pp->item->object.sha1));
318 sp = strstr(buf, "\n\n");
319 if (sp) {
320 sp += 2;
321 for (ep = sp; *ep && *ep != '\n'; ep++)
323 fprintf(stderr, " %.*s", (int)(ep - sp), sp);
325 fprintf(stderr, "\n");
328 #endif /* DEBUG_BISECT */
330 static struct commit_list *best_bisection(struct commit_list *list, int nr)
332 struct commit_list *p, *best;
333 int best_distance = -1;
335 best = list;
336 for (p = list; p; p = p->next) {
337 int distance;
338 unsigned flags = p->item->object.flags;
340 if (flags & TREESAME)
341 continue;
342 distance = weight(p);
343 if (nr - distance < distance)
344 distance = nr - distance;
345 if (distance > best_distance) {
346 best = p;
347 best_distance = distance;
351 return best;
354 struct commit_dist {
355 struct commit *commit;
356 int distance;
359 static int compare_commit_dist(const void *a_, const void *b_)
361 struct commit_dist *a, *b;
363 a = (struct commit_dist *)a_;
364 b = (struct commit_dist *)b_;
365 if (a->distance != b->distance)
366 return b->distance - a->distance; /* desc sort */
367 return hashcmp(a->commit->object.sha1, b->commit->object.sha1);
370 static struct commit_list *best_bisection_sorted(struct commit_list *list, int nr)
372 struct commit_list *p;
373 struct commit_dist *array = xcalloc(nr, sizeof(*array));
374 int cnt, i;
376 for (p = list, cnt = 0; p; p = p->next) {
377 int distance;
378 unsigned flags = p->item->object.flags;
380 if (flags & TREESAME)
381 continue;
382 distance = weight(p);
383 if (nr - distance < distance)
384 distance = nr - distance;
385 array[cnt].commit = p->item;
386 array[cnt].distance = distance;
387 cnt++;
389 qsort(array, cnt, sizeof(*array), compare_commit_dist);
390 for (p = list, i = 0; i < cnt; i++) {
391 struct name_decoration *r = xmalloc(sizeof(*r) + 100);
392 struct object *obj = &(array[i].commit->object);
394 sprintf(r->name, "dist=%d", array[i].distance);
395 r->next = add_decoration(&name_decoration, obj, r);
396 p->item = array[i].commit;
397 p = p->next;
399 if (p)
400 p->next = NULL;
401 free(array);
402 return list;
406 * zero or positive weight is the number of interesting commits it can
407 * reach, including itself. Especially, weight = 0 means it does not
408 * reach any tree-changing commits (e.g. just above uninteresting one
409 * but traversal is with pathspec).
411 * weight = -1 means it has one parent and its distance is yet to
412 * be computed.
414 * weight = -2 means it has more than one parent and its distance is
415 * unknown. After running count_distance() first, they will get zero
416 * or positive distance.
418 static struct commit_list *do_find_bisection(struct commit_list *list,
419 int nr, int *weights,
420 int find_all)
422 int n, counted;
423 struct commit_list *p;
425 counted = 0;
427 for (n = 0, p = list; p; p = p->next) {
428 struct commit *commit = p->item;
429 unsigned flags = commit->object.flags;
431 p->item->util = &weights[n++];
432 switch (count_interesting_parents(commit)) {
433 case 0:
434 if (!(flags & TREESAME)) {
435 weight_set(p, 1);
436 counted++;
437 show_list("bisection 2 count one",
438 counted, nr, list);
441 * otherwise, it is known not to reach any
442 * tree-changing commit and gets weight 0.
444 break;
445 case 1:
446 weight_set(p, -1);
447 break;
448 default:
449 weight_set(p, -2);
450 break;
454 show_list("bisection 2 initialize", counted, nr, list);
457 * If you have only one parent in the resulting set
458 * then you can reach one commit more than that parent
459 * can reach. So we do not have to run the expensive
460 * count_distance() for single strand of pearls.
462 * However, if you have more than one parents, you cannot
463 * just add their distance and one for yourself, since
464 * they usually reach the same ancestor and you would
465 * end up counting them twice that way.
467 * So we will first count distance of merges the usual
468 * way, and then fill the blanks using cheaper algorithm.
470 for (p = list; p; p = p->next) {
471 if (p->item->object.flags & UNINTERESTING)
472 continue;
473 if (weight(p) != -2)
474 continue;
475 weight_set(p, count_distance(p));
476 clear_distance(list);
478 /* Does it happen to be at exactly half-way? */
479 if (!find_all && halfway(p, nr))
480 return p;
481 counted++;
484 show_list("bisection 2 count_distance", counted, nr, list);
486 while (counted < nr) {
487 for (p = list; p; p = p->next) {
488 struct commit_list *q;
489 unsigned flags = p->item->object.flags;
491 if (0 <= weight(p))
492 continue;
493 for (q = p->item->parents; q; q = q->next) {
494 if (q->item->object.flags & UNINTERESTING)
495 continue;
496 if (0 <= weight(q))
497 break;
499 if (!q)
500 continue;
503 * weight for p is unknown but q is known.
504 * add one for p itself if p is to be counted,
505 * otherwise inherit it from q directly.
507 if (!(flags & TREESAME)) {
508 weight_set(p, weight(q)+1);
509 counted++;
510 show_list("bisection 2 count one",
511 counted, nr, list);
513 else
514 weight_set(p, weight(q));
516 /* Does it happen to be at exactly half-way? */
517 if (!find_all && halfway(p, nr))
518 return p;
522 show_list("bisection 2 counted all", counted, nr, list);
524 if (!find_all)
525 return best_bisection(list, nr);
526 else
527 return best_bisection_sorted(list, nr);
530 static struct commit_list *find_bisection(struct commit_list *list,
531 int *reaches, int *all,
532 int find_all)
534 int nr, on_list;
535 struct commit_list *p, *best, *next, *last;
536 int *weights;
538 show_list("bisection 2 entry", 0, 0, list);
541 * Count the number of total and tree-changing items on the
542 * list, while reversing the list.
544 for (nr = on_list = 0, last = NULL, p = list;
546 p = next) {
547 unsigned flags = p->item->object.flags;
549 next = p->next;
550 if (flags & UNINTERESTING)
551 continue;
552 p->next = last;
553 last = p;
554 if (!(flags & TREESAME))
555 nr++;
556 on_list++;
558 list = last;
559 show_list("bisection 2 sorted", 0, nr, list);
561 *all = nr;
562 weights = xcalloc(on_list, sizeof(*weights));
564 /* Do the real work of finding bisection commit. */
565 best = do_find_bisection(list, nr, weights, find_all);
566 if (best) {
567 if (!find_all)
568 best->next = NULL;
569 *reaches = weight(best);
571 free(weights);
572 return best;
575 static void read_revisions_from_stdin(struct rev_info *revs)
577 char line[1000];
579 while (fgets(line, sizeof(line), stdin) != NULL) {
580 int len = strlen(line);
581 if (len && line[len - 1] == '\n')
582 line[--len] = 0;
583 if (!len)
584 break;
585 if (line[0] == '-')
586 die("options not supported in --stdin mode");
587 if (handle_revision_arg(line, revs, 0, 1))
588 die("bad revision '%s'", line);
592 int cmd_rev_list(int argc, const char **argv, const char *prefix)
594 struct commit_list *list;
595 int i;
596 int read_from_stdin = 0;
597 int bisect_show_vars = 0;
598 int bisect_find_all = 0;
599 int quiet = 0;
601 git_config(git_default_config);
602 init_revisions(&revs, prefix);
603 revs.abbrev = 0;
604 revs.commit_format = CMIT_FMT_UNSPECIFIED;
605 argc = setup_revisions(argc, argv, &revs, NULL);
607 for (i = 1 ; i < argc; i++) {
608 const char *arg = argv[i];
610 if (!strcmp(arg, "--header")) {
611 revs.verbose_header = 1;
612 continue;
614 if (!strcmp(arg, "--timestamp")) {
615 show_timestamp = 1;
616 continue;
618 if (!strcmp(arg, "--bisect")) {
619 bisect_list = 1;
620 continue;
622 if (!strcmp(arg, "--bisect-all")) {
623 bisect_list = 1;
624 bisect_find_all = 1;
625 continue;
627 if (!strcmp(arg, "--bisect-vars")) {
628 bisect_list = 1;
629 bisect_show_vars = 1;
630 continue;
632 if (!strcmp(arg, "--stdin")) {
633 if (read_from_stdin++)
634 die("--stdin given twice?");
635 read_revisions_from_stdin(&revs);
636 continue;
638 if (!strcmp(arg, "--quiet")) {
639 quiet = 1;
640 continue;
642 usage(rev_list_usage);
645 if (revs.commit_format != CMIT_FMT_UNSPECIFIED) {
646 /* The command line has a --pretty */
647 hdr_termination = '\n';
648 if (revs.commit_format == CMIT_FMT_ONELINE)
649 header_prefix = "";
650 else
651 header_prefix = "commit ";
653 else if (revs.verbose_header)
654 /* Only --header was specified */
655 revs.commit_format = CMIT_FMT_RAW;
657 list = revs.commits;
659 if ((!list &&
660 (!(revs.tag_objects||revs.tree_objects||revs.blob_objects) &&
661 !revs.pending.nr)) ||
662 revs.diff)
663 usage(rev_list_usage);
665 save_commit_buffer = revs.verbose_header || revs.grep_filter;
666 if (bisect_list)
667 revs.limited = 1;
669 if (prepare_revision_walk(&revs))
670 die("revision walk setup failed");
671 if (revs.tree_objects)
672 mark_edges_uninteresting(revs.commits, &revs, show_edge);
674 if (bisect_list) {
675 int reaches = reaches, all = all;
677 revs.commits = find_bisection(revs.commits, &reaches, &all,
678 bisect_find_all);
679 if (bisect_show_vars) {
680 int cnt;
681 char hex[41];
682 if (!revs.commits)
683 return 1;
685 * revs.commits can reach "reaches" commits among
686 * "all" commits. If it is good, then there are
687 * (all-reaches) commits left to be bisected.
688 * On the other hand, if it is bad, then the set
689 * to bisect is "reaches".
690 * A bisect set of size N has (N-1) commits further
691 * to test, as we already know one bad one.
693 cnt = all - reaches;
694 if (cnt < reaches)
695 cnt = reaches;
696 strcpy(hex, sha1_to_hex(revs.commits->item->object.sha1));
698 if (bisect_find_all) {
699 traverse_commit_list(&revs, show_commit, show_object);
700 printf("------\n");
703 printf("bisect_rev=%s\n"
704 "bisect_nr=%d\n"
705 "bisect_good=%d\n"
706 "bisect_bad=%d\n"
707 "bisect_all=%d\n",
708 hex,
709 cnt - 1,
710 all - reaches - 1,
711 reaches - 1,
712 all);
713 return 0;
717 traverse_commit_list(&revs,
718 quiet ? finish_commit : show_commit,
719 quiet ? finish_object : show_object);
721 return 0;