commit: -F overrides -t
[git/dscho.git] / builtin-rev-list.c
blob0af7cd94f99728e08379441061098e1966e36b73
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);
70 if (!revs.graph) {
71 if (commit->object.flags & BOUNDARY)
72 putchar('-');
73 else if (commit->object.flags & UNINTERESTING)
74 putchar('^');
75 else if (revs.left_right) {
76 if (commit->object.flags & SYMMETRIC_LEFT)
77 putchar('<');
78 else
79 putchar('>');
82 if (revs.abbrev_commit && revs.abbrev)
83 fputs(find_unique_abbrev(commit->object.sha1, revs.abbrev),
84 stdout);
85 else
86 fputs(sha1_to_hex(commit->object.sha1), stdout);
87 if (revs.print_parents) {
88 struct commit_list *parents = commit->parents;
89 while (parents) {
90 printf(" %s", sha1_to_hex(parents->item->object.sha1));
91 parents = parents->next;
94 if (revs.children.name) {
95 struct commit_list *children;
97 children = lookup_decoration(&revs.children, &commit->object);
98 while (children) {
99 printf(" %s", sha1_to_hex(children->item->object.sha1));
100 children = children->next;
103 show_decorations(&revs, commit);
104 if (revs.commit_format == CMIT_FMT_ONELINE)
105 putchar(' ');
106 else
107 putchar('\n');
109 if (revs.verbose_header && commit->buffer) {
110 struct strbuf buf = STRBUF_INIT;
111 pretty_print_commit(revs.commit_format, commit,
112 &buf, revs.abbrev, NULL, NULL,
113 revs.date_mode, 0);
114 if (revs.graph) {
115 if (buf.len) {
116 if (revs.commit_format != CMIT_FMT_ONELINE)
117 graph_show_oneline(revs.graph);
119 graph_show_commit_msg(revs.graph, &buf);
122 * Add a newline after the commit message.
124 * Usually, this newline produces a blank
125 * padding line between entries, in which case
126 * we need to add graph padding on this line.
128 * However, the commit message may not end in a
129 * newline. In this case the newline simply
130 * ends the last line of the commit message,
131 * and we don't need any graph output. (This
132 * always happens with CMIT_FMT_ONELINE, and it
133 * happens with CMIT_FMT_USERFORMAT when the
134 * format doesn't explicitly end in a newline.)
136 if (buf.len && buf.buf[buf.len - 1] == '\n')
137 graph_show_padding(revs.graph);
138 putchar('\n');
139 } else {
141 * If the message buffer is empty, just show
142 * the rest of the graph output for this
143 * commit.
145 if (graph_show_remainder(revs.graph))
146 putchar('\n');
148 } else {
149 if (buf.len)
150 printf("%s%c", buf.buf, hdr_termination);
152 strbuf_release(&buf);
153 } else {
154 if (graph_show_remainder(revs.graph))
155 putchar('\n');
157 maybe_flush_or_die(stdout, "stdout");
158 finish_commit(commit);
161 static void finish_commit(struct commit *commit)
163 if (commit->parents) {
164 free_commit_list(commit->parents);
165 commit->parents = NULL;
167 free(commit->buffer);
168 commit->buffer = NULL;
171 static void finish_object(struct object *obj, const struct name_path *path, const char *name)
173 if (obj->type == OBJ_BLOB && !has_sha1_file(obj->sha1))
174 die("missing blob object '%s'", sha1_to_hex(obj->sha1));
177 static void show_object(struct object *obj, const struct name_path *path, const char *component)
179 char *name = path_name(path, component);
180 /* An object with name "foo\n0000000..." can be used to
181 * confuse downstream "git pack-objects" very badly.
183 const char *ep = strchr(name, '\n');
185 finish_object(obj, path, name);
186 if (ep) {
187 printf("%s %.*s\n", sha1_to_hex(obj->sha1),
188 (int) (ep - name),
189 name);
191 else
192 printf("%s %s\n", sha1_to_hex(obj->sha1), name);
193 free(name);
196 static void show_edge(struct commit *commit)
198 printf("-%s\n", sha1_to_hex(commit->object.sha1));
202 * This is a truly stupid algorithm, but it's only
203 * used for bisection, and we just don't care enough.
205 * We care just barely enough to avoid recursing for
206 * non-merge entries.
208 static int count_distance(struct commit_list *entry)
210 int nr = 0;
212 while (entry) {
213 struct commit *commit = entry->item;
214 struct commit_list *p;
216 if (commit->object.flags & (UNINTERESTING | COUNTED))
217 break;
218 if (!(commit->object.flags & TREESAME))
219 nr++;
220 commit->object.flags |= COUNTED;
221 p = commit->parents;
222 entry = p;
223 if (p) {
224 p = p->next;
225 while (p) {
226 nr += count_distance(p);
227 p = p->next;
232 return nr;
235 static void clear_distance(struct commit_list *list)
237 while (list) {
238 struct commit *commit = list->item;
239 commit->object.flags &= ~COUNTED;
240 list = list->next;
244 #define DEBUG_BISECT 0
246 static inline int weight(struct commit_list *elem)
248 return *((int*)(elem->item->util));
251 static inline void weight_set(struct commit_list *elem, int weight)
253 *((int*)(elem->item->util)) = weight;
256 static int count_interesting_parents(struct commit *commit)
258 struct commit_list *p;
259 int count;
261 for (count = 0, p = commit->parents; p; p = p->next) {
262 if (p->item->object.flags & UNINTERESTING)
263 continue;
264 count++;
266 return count;
269 static inline int halfway(struct commit_list *p, int nr)
272 * Don't short-cut something we are not going to return!
274 if (p->item->object.flags & TREESAME)
275 return 0;
276 if (DEBUG_BISECT)
277 return 0;
279 * 2 and 3 are halfway of 5.
280 * 3 is halfway of 6 but 2 and 4 are not.
282 switch (2 * weight(p) - nr) {
283 case -1: case 0: case 1:
284 return 1;
285 default:
286 return 0;
290 #if !DEBUG_BISECT
291 #define show_list(a,b,c,d) do { ; } while (0)
292 #else
293 static void show_list(const char *debug, int counted, int nr,
294 struct commit_list *list)
296 struct commit_list *p;
298 fprintf(stderr, "%s (%d/%d)\n", debug, counted, nr);
300 for (p = list; p; p = p->next) {
301 struct commit_list *pp;
302 struct commit *commit = p->item;
303 unsigned flags = commit->object.flags;
304 enum object_type type;
305 unsigned long size;
306 char *buf = read_sha1_file(commit->object.sha1, &type, &size);
307 char *ep, *sp;
309 fprintf(stderr, "%c%c%c ",
310 (flags & TREESAME) ? ' ' : 'T',
311 (flags & UNINTERESTING) ? 'U' : ' ',
312 (flags & COUNTED) ? 'C' : ' ');
313 if (commit->util)
314 fprintf(stderr, "%3d", weight(p));
315 else
316 fprintf(stderr, "---");
317 fprintf(stderr, " %.*s", 8, sha1_to_hex(commit->object.sha1));
318 for (pp = commit->parents; pp; pp = pp->next)
319 fprintf(stderr, " %.*s", 8,
320 sha1_to_hex(pp->item->object.sha1));
322 sp = strstr(buf, "\n\n");
323 if (sp) {
324 sp += 2;
325 for (ep = sp; *ep && *ep != '\n'; ep++)
327 fprintf(stderr, " %.*s", (int)(ep - sp), sp);
329 fprintf(stderr, "\n");
332 #endif /* DEBUG_BISECT */
334 static struct commit_list *best_bisection(struct commit_list *list, int nr)
336 struct commit_list *p, *best;
337 int best_distance = -1;
339 best = list;
340 for (p = list; p; p = p->next) {
341 int distance;
342 unsigned flags = p->item->object.flags;
344 if (flags & TREESAME)
345 continue;
346 distance = weight(p);
347 if (nr - distance < distance)
348 distance = nr - distance;
349 if (distance > best_distance) {
350 best = p;
351 best_distance = distance;
355 return best;
358 struct commit_dist {
359 struct commit *commit;
360 int distance;
363 static int compare_commit_dist(const void *a_, const void *b_)
365 struct commit_dist *a, *b;
367 a = (struct commit_dist *)a_;
368 b = (struct commit_dist *)b_;
369 if (a->distance != b->distance)
370 return b->distance - a->distance; /* desc sort */
371 return hashcmp(a->commit->object.sha1, b->commit->object.sha1);
374 static struct commit_list *best_bisection_sorted(struct commit_list *list, int nr)
376 struct commit_list *p;
377 struct commit_dist *array = xcalloc(nr, sizeof(*array));
378 int cnt, i;
380 for (p = list, cnt = 0; p; p = p->next) {
381 int distance;
382 unsigned flags = p->item->object.flags;
384 if (flags & TREESAME)
385 continue;
386 distance = weight(p);
387 if (nr - distance < distance)
388 distance = nr - distance;
389 array[cnt].commit = p->item;
390 array[cnt].distance = distance;
391 cnt++;
393 qsort(array, cnt, sizeof(*array), compare_commit_dist);
394 for (p = list, i = 0; i < cnt; i++) {
395 struct name_decoration *r = xmalloc(sizeof(*r) + 100);
396 struct object *obj = &(array[i].commit->object);
398 sprintf(r->name, "dist=%d", array[i].distance);
399 r->next = add_decoration(&name_decoration, obj, r);
400 p->item = array[i].commit;
401 p = p->next;
403 if (p)
404 p->next = NULL;
405 free(array);
406 return list;
410 * zero or positive weight is the number of interesting commits it can
411 * reach, including itself. Especially, weight = 0 means it does not
412 * reach any tree-changing commits (e.g. just above uninteresting one
413 * but traversal is with pathspec).
415 * weight = -1 means it has one parent and its distance is yet to
416 * be computed.
418 * weight = -2 means it has more than one parent and its distance is
419 * unknown. After running count_distance() first, they will get zero
420 * or positive distance.
422 static struct commit_list *do_find_bisection(struct commit_list *list,
423 int nr, int *weights,
424 int find_all)
426 int n, counted;
427 struct commit_list *p;
429 counted = 0;
431 for (n = 0, p = list; p; p = p->next) {
432 struct commit *commit = p->item;
433 unsigned flags = commit->object.flags;
435 p->item->util = &weights[n++];
436 switch (count_interesting_parents(commit)) {
437 case 0:
438 if (!(flags & TREESAME)) {
439 weight_set(p, 1);
440 counted++;
441 show_list("bisection 2 count one",
442 counted, nr, list);
445 * otherwise, it is known not to reach any
446 * tree-changing commit and gets weight 0.
448 break;
449 case 1:
450 weight_set(p, -1);
451 break;
452 default:
453 weight_set(p, -2);
454 break;
458 show_list("bisection 2 initialize", counted, nr, list);
461 * If you have only one parent in the resulting set
462 * then you can reach one commit more than that parent
463 * can reach. So we do not have to run the expensive
464 * count_distance() for single strand of pearls.
466 * However, if you have more than one parents, you cannot
467 * just add their distance and one for yourself, since
468 * they usually reach the same ancestor and you would
469 * end up counting them twice that way.
471 * So we will first count distance of merges the usual
472 * way, and then fill the blanks using cheaper algorithm.
474 for (p = list; p; p = p->next) {
475 if (p->item->object.flags & UNINTERESTING)
476 continue;
477 if (weight(p) != -2)
478 continue;
479 weight_set(p, count_distance(p));
480 clear_distance(list);
482 /* Does it happen to be at exactly half-way? */
483 if (!find_all && halfway(p, nr))
484 return p;
485 counted++;
488 show_list("bisection 2 count_distance", counted, nr, list);
490 while (counted < nr) {
491 for (p = list; p; p = p->next) {
492 struct commit_list *q;
493 unsigned flags = p->item->object.flags;
495 if (0 <= weight(p))
496 continue;
497 for (q = p->item->parents; q; q = q->next) {
498 if (q->item->object.flags & UNINTERESTING)
499 continue;
500 if (0 <= weight(q))
501 break;
503 if (!q)
504 continue;
507 * weight for p is unknown but q is known.
508 * add one for p itself if p is to be counted,
509 * otherwise inherit it from q directly.
511 if (!(flags & TREESAME)) {
512 weight_set(p, weight(q)+1);
513 counted++;
514 show_list("bisection 2 count one",
515 counted, nr, list);
517 else
518 weight_set(p, weight(q));
520 /* Does it happen to be at exactly half-way? */
521 if (!find_all && halfway(p, nr))
522 return p;
526 show_list("bisection 2 counted all", counted, nr, list);
528 if (!find_all)
529 return best_bisection(list, nr);
530 else
531 return best_bisection_sorted(list, nr);
534 static struct commit_list *find_bisection(struct commit_list *list,
535 int *reaches, int *all,
536 int find_all)
538 int nr, on_list;
539 struct commit_list *p, *best, *next, *last;
540 int *weights;
542 show_list("bisection 2 entry", 0, 0, list);
545 * Count the number of total and tree-changing items on the
546 * list, while reversing the list.
548 for (nr = on_list = 0, last = NULL, p = list;
550 p = next) {
551 unsigned flags = p->item->object.flags;
553 next = p->next;
554 if (flags & UNINTERESTING)
555 continue;
556 p->next = last;
557 last = p;
558 if (!(flags & TREESAME))
559 nr++;
560 on_list++;
562 list = last;
563 show_list("bisection 2 sorted", 0, nr, list);
565 *all = nr;
566 weights = xcalloc(on_list, sizeof(*weights));
568 /* Do the real work of finding bisection commit. */
569 best = do_find_bisection(list, nr, weights, find_all);
570 if (best) {
571 if (!find_all)
572 best->next = NULL;
573 *reaches = weight(best);
575 free(weights);
576 return best;
579 int cmd_rev_list(int argc, const char **argv, const char *prefix)
581 struct commit_list *list;
582 int i;
583 int read_from_stdin = 0;
584 int bisect_show_vars = 0;
585 int bisect_find_all = 0;
586 int quiet = 0;
588 git_config(git_default_config, NULL);
589 init_revisions(&revs, prefix);
590 revs.abbrev = 0;
591 revs.commit_format = CMIT_FMT_UNSPECIFIED;
592 argc = setup_revisions(argc, argv, &revs, NULL);
594 quiet = DIFF_OPT_TST(&revs.diffopt, QUIET);
595 for (i = 1 ; i < argc; i++) {
596 const char *arg = argv[i];
598 if (!strcmp(arg, "--header")) {
599 revs.verbose_header = 1;
600 continue;
602 if (!strcmp(arg, "--timestamp")) {
603 show_timestamp = 1;
604 continue;
606 if (!strcmp(arg, "--bisect")) {
607 bisect_list = 1;
608 continue;
610 if (!strcmp(arg, "--bisect-all")) {
611 bisect_list = 1;
612 bisect_find_all = 1;
613 revs.show_decorations = 1;
614 continue;
616 if (!strcmp(arg, "--bisect-vars")) {
617 bisect_list = 1;
618 bisect_show_vars = 1;
619 continue;
621 if (!strcmp(arg, "--stdin")) {
622 if (read_from_stdin++)
623 die("--stdin given twice?");
624 read_revisions_from_stdin(&revs);
625 continue;
627 usage(rev_list_usage);
630 if (revs.commit_format != CMIT_FMT_UNSPECIFIED) {
631 /* The command line has a --pretty */
632 hdr_termination = '\n';
633 if (revs.commit_format == CMIT_FMT_ONELINE)
634 header_prefix = "";
635 else
636 header_prefix = "commit ";
638 else if (revs.verbose_header)
639 /* Only --header was specified */
640 revs.commit_format = CMIT_FMT_RAW;
642 list = revs.commits;
644 if ((!list &&
645 (!(revs.tag_objects||revs.tree_objects||revs.blob_objects) &&
646 !revs.pending.nr)) ||
647 revs.diff)
648 usage(rev_list_usage);
650 save_commit_buffer = revs.verbose_header ||
651 revs.grep_filter.pattern_list;
652 if (bisect_list)
653 revs.limited = 1;
655 if (prepare_revision_walk(&revs))
656 die("revision walk setup failed");
657 if (revs.tree_objects)
658 mark_edges_uninteresting(revs.commits, &revs, show_edge);
660 if (bisect_list) {
661 int reaches = reaches, all = all;
663 revs.commits = find_bisection(revs.commits, &reaches, &all,
664 bisect_find_all);
665 if (bisect_show_vars) {
666 int cnt;
667 char hex[41];
668 if (!revs.commits)
669 return 1;
671 * revs.commits can reach "reaches" commits among
672 * "all" commits. If it is good, then there are
673 * (all-reaches) commits left to be bisected.
674 * On the other hand, if it is bad, then the set
675 * to bisect is "reaches".
676 * A bisect set of size N has (N-1) commits further
677 * to test, as we already know one bad one.
679 cnt = all - reaches;
680 if (cnt < reaches)
681 cnt = reaches;
682 strcpy(hex, sha1_to_hex(revs.commits->item->object.sha1));
684 if (bisect_find_all) {
685 traverse_commit_list(&revs, show_commit, show_object);
686 printf("------\n");
689 printf("bisect_rev=%s\n"
690 "bisect_nr=%d\n"
691 "bisect_good=%d\n"
692 "bisect_bad=%d\n"
693 "bisect_all=%d\n",
694 hex,
695 cnt - 1,
696 all - reaches - 1,
697 reaches - 1,
698 all);
699 return 0;
703 traverse_commit_list(&revs,
704 quiet ? finish_commit : show_commit,
705 quiet ? finish_object : show_object);
707 return 0;