Windows: Make 'git help -a' work.
[git/git-bigfiles.git] / builtin-rev-list.c
blob83a7b1349e06dbf1a355888272d9b13a7d4c22c4
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 " --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"
52 static struct rev_info revs;
54 static int bisect_list;
55 static int show_timestamp;
56 static int hdr_termination;
57 static const char *header_prefix;
59 static void finish_commit(struct commit *commit);
60 static void show_commit(struct commit *commit)
62 graph_show_commit(revs.graph);
64 if (show_timestamp)
65 printf("%lu ", commit->date);
66 if (header_prefix)
67 fputs(header_prefix, stdout);
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('>');
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;
93 show_decorations(commit);
94 if (revs.commit_format == CMIT_FMT_ONELINE)
95 putchar(' ');
96 else
97 putchar('\n');
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);
110 graph_show_commit_msg(revs.graph, &buf);
113 * Add a newline after the commit message.
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.
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.)
127 if (buf.len && buf.buf[buf.len - 1] == '\n')
128 graph_show_padding(revs.graph);
129 putchar('\n');
130 } else {
132 * If the message buffer is empty, just show
133 * the rest of the graph output for this
134 * commit.
136 if (graph_show_remainder(revs.graph))
137 putchar('\n');
139 } else {
140 if (buf.len)
141 printf("%s%c", buf.buf, hdr_termination);
143 strbuf_release(&buf);
144 } else {
145 if (graph_show_remainder(revs.graph))
146 putchar('\n');
148 maybe_flush_or_die(stdout, "stdout");
149 finish_commit(commit);
152 static void finish_commit(struct commit *commit)
154 if (commit->parents) {
155 free_commit_list(commit->parents);
156 commit->parents = NULL;
158 free(commit->buffer);
159 commit->buffer = NULL;
162 static void finish_object(struct object_array_entry *p)
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));
168 static void show_object(struct object_array_entry *p)
170 /* An object with name "foo\n0000000..." can be used to
171 * confuse downstream git-pack-objects very badly.
173 const char *ep = strchr(p->name, '\n');
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);
181 else
182 printf("%s %s\n", sha1_to_hex(p->item->sha1), p->name);
185 static void show_edge(struct commit *commit)
187 printf("-%s\n", sha1_to_hex(commit->object.sha1));
191 * This is a truly stupid algorithm, but it's only
192 * used for bisection, and we just don't care enough.
194 * We care just barely enough to avoid recursing for
195 * non-merge entries.
197 static int count_distance(struct commit_list *entry)
199 int nr = 0;
201 while (entry) {
202 struct commit *commit = entry->item;
203 struct commit_list *p;
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;
221 return nr;
224 static void clear_distance(struct commit_list *list)
226 while (list) {
227 struct commit *commit = list->item;
228 commit->object.flags &= ~COUNTED;
229 list = list->next;
233 #define DEBUG_BISECT 0
235 static inline int weight(struct commit_list *elem)
237 return *((int*)(elem->item->util));
240 static inline void weight_set(struct commit_list *elem, int weight)
242 *((int*)(elem->item->util)) = weight;
245 static int count_interesting_parents(struct commit *commit)
247 struct commit_list *p;
248 int count;
250 for (count = 0, p = commit->parents; p; p = p->next) {
251 if (p->item->object.flags & UNINTERESTING)
252 continue;
253 count++;
255 return count;
258 static inline int halfway(struct commit_list *p, int nr)
261 * Don't short-cut something we are not going to return!
263 if (p->item->object.flags & TREESAME)
264 return 0;
265 if (DEBUG_BISECT)
266 return 0;
268 * 2 and 3 are halfway of 5.
269 * 3 is halfway of 6 but 2 and 4 are not.
271 switch (2 * weight(p) - nr) {
272 case -1: case 0: case 1:
273 return 1;
274 default:
275 return 0;
279 #if !DEBUG_BISECT
280 #define show_list(a,b,c,d) do { ; } while (0)
281 #else
282 static void show_list(const char *debug, int counted, int nr,
283 struct commit_list *list)
285 struct commit_list *p;
287 fprintf(stderr, "%s (%d/%d)\n", debug, counted, nr);
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;
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));
311 sp = strstr(buf, "\n\n");
312 if (sp) {
313 sp += 2;
314 for (ep = sp; *ep && *ep != '\n'; ep++)
316 fprintf(stderr, " %.*s", (int)(ep - sp), sp);
318 fprintf(stderr, "\n");
321 #endif /* DEBUG_BISECT */
323 static struct commit_list *best_bisection(struct commit_list *list, int nr)
325 struct commit_list *p, *best;
326 int best_distance = -1;
328 best = list;
329 for (p = list; p; p = p->next) {
330 int distance;
331 unsigned flags = p->item->object.flags;
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;
344 return best;
347 struct commit_dist {
348 struct commit *commit;
349 int distance;
352 static int compare_commit_dist(const void *a_, const void *b_)
354 struct commit_dist *a, *b;
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);
363 static struct commit_list *best_bisection_sorted(struct commit_list *list, int nr)
365 struct commit_list *p;
366 struct commit_dist *array = xcalloc(nr, sizeof(*array));
367 int cnt, i;
369 for (p = list, cnt = 0; p; p = p->next) {
370 int distance;
371 unsigned flags = p->item->object.flags;
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++;
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);
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;
392 if (p)
393 p->next = NULL;
394 free(array);
395 return list;
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).
404 * weight = -1 means it has one parent and its distance is yet to
405 * be computed.
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.
411 static struct commit_list *do_find_bisection(struct commit_list *list,
412 int nr, int *weights,
413 int find_all)
415 int n, counted;
416 struct commit_list *p;
418 counted = 0;
420 for (n = 0, p = list; p; p = p->next) {
421 struct commit *commit = p->item;
422 unsigned flags = commit->object.flags;
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);
434 * otherwise, it is known not to reach any
435 * tree-changing commit and gets weight 0.
437 break;
438 case 1:
439 weight_set(p, -1);
440 break;
441 default:
442 weight_set(p, -2);
443 break;
447 show_list("bisection 2 initialize", counted, nr, list);
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.
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.
460 * So we will first count distance of merges the usual
461 * way, and then fill the blanks using cheaper algorithm.
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);
471 /* Does it happen to be at exactly half-way? */
472 if (!find_all && halfway(p, nr))
473 return p;
474 counted++;
477 show_list("bisection 2 count_distance", counted, nr, list);
479 while (counted < nr) {
480 for (p = list; p; p = p->next) {
481 struct commit_list *q;
482 unsigned flags = p->item->object.flags;
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;
492 if (!q)
493 continue;
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.
500 if (!(flags & TREESAME)) {
501 weight_set(p, weight(q)+1);
502 counted++;
503 show_list("bisection 2 count one",
504 counted, nr, list);
506 else
507 weight_set(p, weight(q));
509 /* Does it happen to be at exactly half-way? */
510 if (!find_all && halfway(p, nr))
511 return p;
515 show_list("bisection 2 counted all", counted, nr, list);
517 if (!find_all)
518 return best_bisection(list, nr);
519 else
520 return best_bisection_sorted(list, nr);
523 static struct commit_list *find_bisection(struct commit_list *list,
524 int *reaches, int *all,
525 int find_all)
527 int nr, on_list;
528 struct commit_list *p, *best, *next, *last;
529 int *weights;
531 show_list("bisection 2 entry", 0, 0, list);
534 * Count the number of total and tree-changing items on the
535 * list, while reversing the list.
537 for (nr = on_list = 0, last = NULL, p = list;
539 p = next) {
540 unsigned flags = p->item->object.flags;
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++;
551 list = last;
552 show_list("bisection 2 sorted", 0, nr, list);
554 *all = nr;
555 weights = xcalloc(on_list, sizeof(*weights));
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);
564 free(weights);
565 return best;
568 static void read_revisions_from_stdin(struct rev_info *revs)
570 char line[1000];
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);
585 int cmd_rev_list(int argc, const char **argv, const char *prefix)
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;
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);
600 for (i = 1 ; i < argc; i++) {
601 const char *arg = argv[i];
603 if (!strcmp(arg, "--header")) {
604 revs.verbose_header = 1;
605 continue;
607 if (!strcmp(arg, "--timestamp")) {
608 show_timestamp = 1;
609 continue;
611 if (!strcmp(arg, "--bisect")) {
612 bisect_list = 1;
613 continue;
615 if (!strcmp(arg, "--bisect-all")) {
616 bisect_list = 1;
617 bisect_find_all = 1;
618 continue;
620 if (!strcmp(arg, "--bisect-vars")) {
621 bisect_list = 1;
622 bisect_show_vars = 1;
623 continue;
625 if (!strcmp(arg, "--stdin")) {
626 if (read_from_stdin++)
627 die("--stdin given twice?");
628 read_revisions_from_stdin(&revs);
629 continue;
631 if (!strcmp(arg, "--quiet")) {
632 quiet = 1;
633 continue;
635 usage(rev_list_usage);
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 ";
646 else if (revs.verbose_header)
647 /* Only --header was specified */
648 revs.commit_format = CMIT_FMT_RAW;
650 list = revs.commits;
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);
658 save_commit_buffer = revs.verbose_header || revs.grep_filter;
659 if (bisect_list)
660 revs.limited = 1;
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);
667 if (bisect_list) {
668 int reaches = reaches, all = all;
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;
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.
686 cnt = all - reaches;
687 if (cnt < reaches)
688 cnt = reaches;
689 strcpy(hex, sha1_to_hex(revs.commits->item->object.sha1));
691 if (bisect_find_all) {
692 traverse_commit_list(&revs, show_commit, show_object);
693 printf("------\n");
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;
710 traverse_commit_list(&revs,
711 quiet ? finish_commit : show_commit,
712 quiet ? finish_object : show_object);
714 return 0;