show_object(): push path_name() call further down
[git/dscho.git] / builtin-rev-list.c
blobaa3c962e56bd08e8e2a632b7170283d05efb69a2
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(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;
111 strbuf_init(&buf, 0);
112 pretty_print_commit(revs.commit_format, commit,
113 &buf, revs.abbrev, NULL, NULL,
114 revs.date_mode, 0);
115 if (revs.graph) {
116 if (buf.len) {
117 if (revs.commit_format != CMIT_FMT_ONELINE)
118 graph_show_oneline(revs.graph);
120 graph_show_commit_msg(revs.graph, &buf);
123 * Add a newline after the commit message.
125 * Usually, this newline produces a blank
126 * padding line between entries, in which case
127 * we need to add graph padding on this line.
129 * However, the commit message may not end in a
130 * newline. In this case the newline simply
131 * ends the last line of the commit message,
132 * and we don't need any graph output. (This
133 * always happens with CMIT_FMT_ONELINE, and it
134 * happens with CMIT_FMT_USERFORMAT when the
135 * format doesn't explicitly end in a newline.)
137 if (buf.len && buf.buf[buf.len - 1] == '\n')
138 graph_show_padding(revs.graph);
139 putchar('\n');
140 } else {
142 * If the message buffer is empty, just show
143 * the rest of the graph output for this
144 * commit.
146 if (graph_show_remainder(revs.graph))
147 putchar('\n');
149 } else {
150 if (buf.len)
151 printf("%s%c", buf.buf, hdr_termination);
153 strbuf_release(&buf);
154 } else {
155 if (graph_show_remainder(revs.graph))
156 putchar('\n');
158 maybe_flush_or_die(stdout, "stdout");
159 finish_commit(commit);
162 static void finish_commit(struct commit *commit)
164 if (commit->parents) {
165 free_commit_list(commit->parents);
166 commit->parents = NULL;
168 free(commit->buffer);
169 commit->buffer = NULL;
172 static void finish_object(struct object *obj, const struct name_path *path, const char *name)
174 if (obj->type == OBJ_BLOB && !has_sha1_file(obj->sha1))
175 die("missing blob object '%s'", sha1_to_hex(obj->sha1));
178 static void show_object(struct object *obj, const struct name_path *path, const char *component)
180 char *name = path_name(path, component);
181 /* An object with name "foo\n0000000..." can be used to
182 * confuse downstream "git pack-objects" very badly.
184 const char *ep = strchr(name, '\n');
186 finish_object(obj, path, name);
187 if (ep) {
188 printf("%s %.*s\n", sha1_to_hex(obj->sha1),
189 (int) (ep - name),
190 name);
192 else
193 printf("%s %s\n", sha1_to_hex(obj->sha1), name);
194 free(name);
197 static void show_edge(struct commit *commit)
199 printf("-%s\n", sha1_to_hex(commit->object.sha1));
203 * This is a truly stupid algorithm, but it's only
204 * used for bisection, and we just don't care enough.
206 * We care just barely enough to avoid recursing for
207 * non-merge entries.
209 static int count_distance(struct commit_list *entry)
211 int nr = 0;
213 while (entry) {
214 struct commit *commit = entry->item;
215 struct commit_list *p;
217 if (commit->object.flags & (UNINTERESTING | COUNTED))
218 break;
219 if (!(commit->object.flags & TREESAME))
220 nr++;
221 commit->object.flags |= COUNTED;
222 p = commit->parents;
223 entry = p;
224 if (p) {
225 p = p->next;
226 while (p) {
227 nr += count_distance(p);
228 p = p->next;
233 return nr;
236 static void clear_distance(struct commit_list *list)
238 while (list) {
239 struct commit *commit = list->item;
240 commit->object.flags &= ~COUNTED;
241 list = list->next;
245 #define DEBUG_BISECT 0
247 static inline int weight(struct commit_list *elem)
249 return *((int*)(elem->item->util));
252 static inline void weight_set(struct commit_list *elem, int weight)
254 *((int*)(elem->item->util)) = weight;
257 static int count_interesting_parents(struct commit *commit)
259 struct commit_list *p;
260 int count;
262 for (count = 0, p = commit->parents; p; p = p->next) {
263 if (p->item->object.flags & UNINTERESTING)
264 continue;
265 count++;
267 return count;
270 static inline int halfway(struct commit_list *p, int nr)
273 * Don't short-cut something we are not going to return!
275 if (p->item->object.flags & TREESAME)
276 return 0;
277 if (DEBUG_BISECT)
278 return 0;
280 * 2 and 3 are halfway of 5.
281 * 3 is halfway of 6 but 2 and 4 are not.
283 switch (2 * weight(p) - nr) {
284 case -1: case 0: case 1:
285 return 1;
286 default:
287 return 0;
291 #if !DEBUG_BISECT
292 #define show_list(a,b,c,d) do { ; } while (0)
293 #else
294 static void show_list(const char *debug, int counted, int nr,
295 struct commit_list *list)
297 struct commit_list *p;
299 fprintf(stderr, "%s (%d/%d)\n", debug, counted, nr);
301 for (p = list; p; p = p->next) {
302 struct commit_list *pp;
303 struct commit *commit = p->item;
304 unsigned flags = commit->object.flags;
305 enum object_type type;
306 unsigned long size;
307 char *buf = read_sha1_file(commit->object.sha1, &type, &size);
308 char *ep, *sp;
310 fprintf(stderr, "%c%c%c ",
311 (flags & TREESAME) ? ' ' : 'T',
312 (flags & UNINTERESTING) ? 'U' : ' ',
313 (flags & COUNTED) ? 'C' : ' ');
314 if (commit->util)
315 fprintf(stderr, "%3d", weight(p));
316 else
317 fprintf(stderr, "---");
318 fprintf(stderr, " %.*s", 8, sha1_to_hex(commit->object.sha1));
319 for (pp = commit->parents; pp; pp = pp->next)
320 fprintf(stderr, " %.*s", 8,
321 sha1_to_hex(pp->item->object.sha1));
323 sp = strstr(buf, "\n\n");
324 if (sp) {
325 sp += 2;
326 for (ep = sp; *ep && *ep != '\n'; ep++)
328 fprintf(stderr, " %.*s", (int)(ep - sp), sp);
330 fprintf(stderr, "\n");
333 #endif /* DEBUG_BISECT */
335 static struct commit_list *best_bisection(struct commit_list *list, int nr)
337 struct commit_list *p, *best;
338 int best_distance = -1;
340 best = list;
341 for (p = list; p; p = p->next) {
342 int distance;
343 unsigned flags = p->item->object.flags;
345 if (flags & TREESAME)
346 continue;
347 distance = weight(p);
348 if (nr - distance < distance)
349 distance = nr - distance;
350 if (distance > best_distance) {
351 best = p;
352 best_distance = distance;
356 return best;
359 struct commit_dist {
360 struct commit *commit;
361 int distance;
364 static int compare_commit_dist(const void *a_, const void *b_)
366 struct commit_dist *a, *b;
368 a = (struct commit_dist *)a_;
369 b = (struct commit_dist *)b_;
370 if (a->distance != b->distance)
371 return b->distance - a->distance; /* desc sort */
372 return hashcmp(a->commit->object.sha1, b->commit->object.sha1);
375 static struct commit_list *best_bisection_sorted(struct commit_list *list, int nr)
377 struct commit_list *p;
378 struct commit_dist *array = xcalloc(nr, sizeof(*array));
379 int cnt, i;
381 for (p = list, cnt = 0; p; p = p->next) {
382 int distance;
383 unsigned flags = p->item->object.flags;
385 if (flags & TREESAME)
386 continue;
387 distance = weight(p);
388 if (nr - distance < distance)
389 distance = nr - distance;
390 array[cnt].commit = p->item;
391 array[cnt].distance = distance;
392 cnt++;
394 qsort(array, cnt, sizeof(*array), compare_commit_dist);
395 for (p = list, i = 0; i < cnt; i++) {
396 struct name_decoration *r = xmalloc(sizeof(*r) + 100);
397 struct object *obj = &(array[i].commit->object);
399 sprintf(r->name, "dist=%d", array[i].distance);
400 r->next = add_decoration(&name_decoration, obj, r);
401 p->item = array[i].commit;
402 p = p->next;
404 if (p)
405 p->next = NULL;
406 free(array);
407 return list;
411 * zero or positive weight is the number of interesting commits it can
412 * reach, including itself. Especially, weight = 0 means it does not
413 * reach any tree-changing commits (e.g. just above uninteresting one
414 * but traversal is with pathspec).
416 * weight = -1 means it has one parent and its distance is yet to
417 * be computed.
419 * weight = -2 means it has more than one parent and its distance is
420 * unknown. After running count_distance() first, they will get zero
421 * or positive distance.
423 static struct commit_list *do_find_bisection(struct commit_list *list,
424 int nr, int *weights,
425 int find_all)
427 int n, counted;
428 struct commit_list *p;
430 counted = 0;
432 for (n = 0, p = list; p; p = p->next) {
433 struct commit *commit = p->item;
434 unsigned flags = commit->object.flags;
436 p->item->util = &weights[n++];
437 switch (count_interesting_parents(commit)) {
438 case 0:
439 if (!(flags & TREESAME)) {
440 weight_set(p, 1);
441 counted++;
442 show_list("bisection 2 count one",
443 counted, nr, list);
446 * otherwise, it is known not to reach any
447 * tree-changing commit and gets weight 0.
449 break;
450 case 1:
451 weight_set(p, -1);
452 break;
453 default:
454 weight_set(p, -2);
455 break;
459 show_list("bisection 2 initialize", counted, nr, list);
462 * If you have only one parent in the resulting set
463 * then you can reach one commit more than that parent
464 * can reach. So we do not have to run the expensive
465 * count_distance() for single strand of pearls.
467 * However, if you have more than one parents, you cannot
468 * just add their distance and one for yourself, since
469 * they usually reach the same ancestor and you would
470 * end up counting them twice that way.
472 * So we will first count distance of merges the usual
473 * way, and then fill the blanks using cheaper algorithm.
475 for (p = list; p; p = p->next) {
476 if (p->item->object.flags & UNINTERESTING)
477 continue;
478 if (weight(p) != -2)
479 continue;
480 weight_set(p, count_distance(p));
481 clear_distance(list);
483 /* Does it happen to be at exactly half-way? */
484 if (!find_all && halfway(p, nr))
485 return p;
486 counted++;
489 show_list("bisection 2 count_distance", counted, nr, list);
491 while (counted < nr) {
492 for (p = list; p; p = p->next) {
493 struct commit_list *q;
494 unsigned flags = p->item->object.flags;
496 if (0 <= weight(p))
497 continue;
498 for (q = p->item->parents; q; q = q->next) {
499 if (q->item->object.flags & UNINTERESTING)
500 continue;
501 if (0 <= weight(q))
502 break;
504 if (!q)
505 continue;
508 * weight for p is unknown but q is known.
509 * add one for p itself if p is to be counted,
510 * otherwise inherit it from q directly.
512 if (!(flags & TREESAME)) {
513 weight_set(p, weight(q)+1);
514 counted++;
515 show_list("bisection 2 count one",
516 counted, nr, list);
518 else
519 weight_set(p, weight(q));
521 /* Does it happen to be at exactly half-way? */
522 if (!find_all && halfway(p, nr))
523 return p;
527 show_list("bisection 2 counted all", counted, nr, list);
529 if (!find_all)
530 return best_bisection(list, nr);
531 else
532 return best_bisection_sorted(list, nr);
535 static struct commit_list *find_bisection(struct commit_list *list,
536 int *reaches, int *all,
537 int find_all)
539 int nr, on_list;
540 struct commit_list *p, *best, *next, *last;
541 int *weights;
543 show_list("bisection 2 entry", 0, 0, list);
546 * Count the number of total and tree-changing items on the
547 * list, while reversing the list.
549 for (nr = on_list = 0, last = NULL, p = list;
551 p = next) {
552 unsigned flags = p->item->object.flags;
554 next = p->next;
555 if (flags & UNINTERESTING)
556 continue;
557 p->next = last;
558 last = p;
559 if (!(flags & TREESAME))
560 nr++;
561 on_list++;
563 list = last;
564 show_list("bisection 2 sorted", 0, nr, list);
566 *all = nr;
567 weights = xcalloc(on_list, sizeof(*weights));
569 /* Do the real work of finding bisection commit. */
570 best = do_find_bisection(list, nr, weights, find_all);
571 if (best) {
572 if (!find_all)
573 best->next = NULL;
574 *reaches = weight(best);
576 free(weights);
577 return best;
580 int cmd_rev_list(int argc, const char **argv, const char *prefix)
582 struct commit_list *list;
583 int i;
584 int read_from_stdin = 0;
585 int bisect_show_vars = 0;
586 int bisect_find_all = 0;
587 int quiet = 0;
589 git_config(git_default_config, NULL);
590 init_revisions(&revs, prefix);
591 revs.abbrev = 0;
592 revs.commit_format = CMIT_FMT_UNSPECIFIED;
593 argc = setup_revisions(argc, argv, &revs, NULL);
595 quiet = DIFF_OPT_TST(&revs.diffopt, QUIET);
596 for (i = 1 ; i < argc; i++) {
597 const char *arg = argv[i];
599 if (!strcmp(arg, "--header")) {
600 revs.verbose_header = 1;
601 continue;
603 if (!strcmp(arg, "--timestamp")) {
604 show_timestamp = 1;
605 continue;
607 if (!strcmp(arg, "--bisect")) {
608 bisect_list = 1;
609 continue;
611 if (!strcmp(arg, "--bisect-all")) {
612 bisect_list = 1;
613 bisect_find_all = 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;