Add log.date config variable
[git/dscho.git] / builtin-rev-list.c
blob54d55cc3a33e55d5c3021eae14b7a771b4e1c23f
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);
68 if (commit->object.flags & BOUNDARY)
69 putchar('-');
70 else if (commit->object.flags & UNINTERESTING)
71 putchar('^');
72 else if (revs.left_right) {
73 if (commit->object.flags & SYMMETRIC_LEFT)
74 putchar('<');
75 else
76 putchar('>');
78 if (revs.abbrev_commit && revs.abbrev)
79 fputs(find_unique_abbrev(commit->object.sha1, revs.abbrev),
80 stdout);
81 else
82 fputs(sha1_to_hex(commit->object.sha1), stdout);
83 if (revs.print_parents) {
84 struct commit_list *parents = commit->parents;
85 while (parents) {
86 printf(" %s", sha1_to_hex(parents->item->object.sha1));
87 parents = parents->next;
90 show_decorations(commit);
91 if (revs.commit_format == CMIT_FMT_ONELINE)
92 putchar(' ');
93 else
94 putchar('\n');
96 if (revs.verbose_header && commit->buffer) {
97 struct strbuf buf;
98 strbuf_init(&buf, 0);
99 pretty_print_commit(revs.commit_format, commit,
100 &buf, revs.abbrev, NULL, NULL,
101 revs.date_mode, 0);
102 if (revs.graph) {
103 if (buf.len) {
104 if (revs.commit_format != CMIT_FMT_ONELINE)
105 graph_show_oneline(revs.graph);
107 graph_show_commit_msg(revs.graph, &buf);
110 * Add a newline after the commit message.
112 * Usually, this newline produces a blank
113 * padding line between entries, in which case
114 * we need to add graph padding on this line.
116 * However, the commit message may not end in a
117 * newline. In this case the newline simply
118 * ends the last line of the commit message,
119 * and we don't need any graph output. (This
120 * always happens with CMIT_FMT_ONELINE, and it
121 * happens with CMIT_FMT_USERFORMAT when the
122 * format doesn't explicitly end in a newline.)
124 if (buf.len && buf.buf[buf.len - 1] == '\n')
125 graph_show_padding(revs.graph);
126 putchar('\n');
127 } else {
129 * If the message buffer is empty, just show
130 * the rest of the graph output for this
131 * commit.
133 if (graph_show_remainder(revs.graph))
134 putchar('\n');
136 } else {
137 if (buf.len)
138 printf("%s%c", buf.buf, hdr_termination);
140 strbuf_release(&buf);
141 } else {
142 if (graph_show_remainder(revs.graph))
143 putchar('\n');
145 maybe_flush_or_die(stdout, "stdout");
146 finish_commit(commit);
149 static void finish_commit(struct commit *commit)
151 if (commit->parents) {
152 free_commit_list(commit->parents);
153 commit->parents = NULL;
155 free(commit->buffer);
156 commit->buffer = NULL;
159 static void finish_object(struct object_array_entry *p)
161 if (p->item->type == OBJ_BLOB && !has_sha1_file(p->item->sha1))
162 die("missing blob object '%s'", sha1_to_hex(p->item->sha1));
165 static void show_object(struct object_array_entry *p)
167 /* An object with name "foo\n0000000..." can be used to
168 * confuse downstream git-pack-objects very badly.
170 const char *ep = strchr(p->name, '\n');
172 finish_object(p);
173 if (ep) {
174 printf("%s %.*s\n", sha1_to_hex(p->item->sha1),
175 (int) (ep - p->name),
176 p->name);
178 else
179 printf("%s %s\n", sha1_to_hex(p->item->sha1), p->name);
182 static void show_edge(struct commit *commit)
184 printf("-%s\n", sha1_to_hex(commit->object.sha1));
188 * This is a truly stupid algorithm, but it's only
189 * used for bisection, and we just don't care enough.
191 * We care just barely enough to avoid recursing for
192 * non-merge entries.
194 static int count_distance(struct commit_list *entry)
196 int nr = 0;
198 while (entry) {
199 struct commit *commit = entry->item;
200 struct commit_list *p;
202 if (commit->object.flags & (UNINTERESTING | COUNTED))
203 break;
204 if (!(commit->object.flags & TREESAME))
205 nr++;
206 commit->object.flags |= COUNTED;
207 p = commit->parents;
208 entry = p;
209 if (p) {
210 p = p->next;
211 while (p) {
212 nr += count_distance(p);
213 p = p->next;
218 return nr;
221 static void clear_distance(struct commit_list *list)
223 while (list) {
224 struct commit *commit = list->item;
225 commit->object.flags &= ~COUNTED;
226 list = list->next;
230 #define DEBUG_BISECT 0
232 static inline int weight(struct commit_list *elem)
234 return *((int*)(elem->item->util));
237 static inline void weight_set(struct commit_list *elem, int weight)
239 *((int*)(elem->item->util)) = weight;
242 static int count_interesting_parents(struct commit *commit)
244 struct commit_list *p;
245 int count;
247 for (count = 0, p = commit->parents; p; p = p->next) {
248 if (p->item->object.flags & UNINTERESTING)
249 continue;
250 count++;
252 return count;
255 static inline int halfway(struct commit_list *p, int nr)
258 * Don't short-cut something we are not going to return!
260 if (p->item->object.flags & TREESAME)
261 return 0;
262 if (DEBUG_BISECT)
263 return 0;
265 * 2 and 3 are halfway of 5.
266 * 3 is halfway of 6 but 2 and 4 are not.
268 switch (2 * weight(p) - nr) {
269 case -1: case 0: case 1:
270 return 1;
271 default:
272 return 0;
276 #if !DEBUG_BISECT
277 #define show_list(a,b,c,d) do { ; } while (0)
278 #else
279 static void show_list(const char *debug, int counted, int nr,
280 struct commit_list *list)
282 struct commit_list *p;
284 fprintf(stderr, "%s (%d/%d)\n", debug, counted, nr);
286 for (p = list; p; p = p->next) {
287 struct commit_list *pp;
288 struct commit *commit = p->item;
289 unsigned flags = commit->object.flags;
290 enum object_type type;
291 unsigned long size;
292 char *buf = read_sha1_file(commit->object.sha1, &type, &size);
293 char *ep, *sp;
295 fprintf(stderr, "%c%c%c ",
296 (flags & TREESAME) ? ' ' : 'T',
297 (flags & UNINTERESTING) ? 'U' : ' ',
298 (flags & COUNTED) ? 'C' : ' ');
299 if (commit->util)
300 fprintf(stderr, "%3d", weight(p));
301 else
302 fprintf(stderr, "---");
303 fprintf(stderr, " %.*s", 8, sha1_to_hex(commit->object.sha1));
304 for (pp = commit->parents; pp; pp = pp->next)
305 fprintf(stderr, " %.*s", 8,
306 sha1_to_hex(pp->item->object.sha1));
308 sp = strstr(buf, "\n\n");
309 if (sp) {
310 sp += 2;
311 for (ep = sp; *ep && *ep != '\n'; ep++)
313 fprintf(stderr, " %.*s", (int)(ep - sp), sp);
315 fprintf(stderr, "\n");
318 #endif /* DEBUG_BISECT */
320 static struct commit_list *best_bisection(struct commit_list *list, int nr)
322 struct commit_list *p, *best;
323 int best_distance = -1;
325 best = list;
326 for (p = list; p; p = p->next) {
327 int distance;
328 unsigned flags = p->item->object.flags;
330 if (flags & TREESAME)
331 continue;
332 distance = weight(p);
333 if (nr - distance < distance)
334 distance = nr - distance;
335 if (distance > best_distance) {
336 best = p;
337 best_distance = distance;
341 return best;
344 struct commit_dist {
345 struct commit *commit;
346 int distance;
349 static int compare_commit_dist(const void *a_, const void *b_)
351 struct commit_dist *a, *b;
353 a = (struct commit_dist *)a_;
354 b = (struct commit_dist *)b_;
355 if (a->distance != b->distance)
356 return b->distance - a->distance; /* desc sort */
357 return hashcmp(a->commit->object.sha1, b->commit->object.sha1);
360 static struct commit_list *best_bisection_sorted(struct commit_list *list, int nr)
362 struct commit_list *p;
363 struct commit_dist *array = xcalloc(nr, sizeof(*array));
364 int cnt, i;
366 for (p = list, cnt = 0; p; p = p->next) {
367 int distance;
368 unsigned flags = p->item->object.flags;
370 if (flags & TREESAME)
371 continue;
372 distance = weight(p);
373 if (nr - distance < distance)
374 distance = nr - distance;
375 array[cnt].commit = p->item;
376 array[cnt].distance = distance;
377 cnt++;
379 qsort(array, cnt, sizeof(*array), compare_commit_dist);
380 for (p = list, i = 0; i < cnt; i++) {
381 struct name_decoration *r = xmalloc(sizeof(*r) + 100);
382 struct object *obj = &(array[i].commit->object);
384 sprintf(r->name, "dist=%d", array[i].distance);
385 r->next = add_decoration(&name_decoration, obj, r);
386 p->item = array[i].commit;
387 p = p->next;
389 if (p)
390 p->next = NULL;
391 free(array);
392 return list;
396 * zero or positive weight is the number of interesting commits it can
397 * reach, including itself. Especially, weight = 0 means it does not
398 * reach any tree-changing commits (e.g. just above uninteresting one
399 * but traversal is with pathspec).
401 * weight = -1 means it has one parent and its distance is yet to
402 * be computed.
404 * weight = -2 means it has more than one parent and its distance is
405 * unknown. After running count_distance() first, they will get zero
406 * or positive distance.
408 static struct commit_list *do_find_bisection(struct commit_list *list,
409 int nr, int *weights,
410 int find_all)
412 int n, counted;
413 struct commit_list *p;
415 counted = 0;
417 for (n = 0, p = list; p; p = p->next) {
418 struct commit *commit = p->item;
419 unsigned flags = commit->object.flags;
421 p->item->util = &weights[n++];
422 switch (count_interesting_parents(commit)) {
423 case 0:
424 if (!(flags & TREESAME)) {
425 weight_set(p, 1);
426 counted++;
427 show_list("bisection 2 count one",
428 counted, nr, list);
431 * otherwise, it is known not to reach any
432 * tree-changing commit and gets weight 0.
434 break;
435 case 1:
436 weight_set(p, -1);
437 break;
438 default:
439 weight_set(p, -2);
440 break;
444 show_list("bisection 2 initialize", counted, nr, list);
447 * If you have only one parent in the resulting set
448 * then you can reach one commit more than that parent
449 * can reach. So we do not have to run the expensive
450 * count_distance() for single strand of pearls.
452 * However, if you have more than one parents, you cannot
453 * just add their distance and one for yourself, since
454 * they usually reach the same ancestor and you would
455 * end up counting them twice that way.
457 * So we will first count distance of merges the usual
458 * way, and then fill the blanks using cheaper algorithm.
460 for (p = list; p; p = p->next) {
461 if (p->item->object.flags & UNINTERESTING)
462 continue;
463 if (weight(p) != -2)
464 continue;
465 weight_set(p, count_distance(p));
466 clear_distance(list);
468 /* Does it happen to be at exactly half-way? */
469 if (!find_all && halfway(p, nr))
470 return p;
471 counted++;
474 show_list("bisection 2 count_distance", counted, nr, list);
476 while (counted < nr) {
477 for (p = list; p; p = p->next) {
478 struct commit_list *q;
479 unsigned flags = p->item->object.flags;
481 if (0 <= weight(p))
482 continue;
483 for (q = p->item->parents; q; q = q->next) {
484 if (q->item->object.flags & UNINTERESTING)
485 continue;
486 if (0 <= weight(q))
487 break;
489 if (!q)
490 continue;
493 * weight for p is unknown but q is known.
494 * add one for p itself if p is to be counted,
495 * otherwise inherit it from q directly.
497 if (!(flags & TREESAME)) {
498 weight_set(p, weight(q)+1);
499 counted++;
500 show_list("bisection 2 count one",
501 counted, nr, list);
503 else
504 weight_set(p, weight(q));
506 /* Does it happen to be at exactly half-way? */
507 if (!find_all && halfway(p, nr))
508 return p;
512 show_list("bisection 2 counted all", counted, nr, list);
514 if (!find_all)
515 return best_bisection(list, nr);
516 else
517 return best_bisection_sorted(list, nr);
520 static struct commit_list *find_bisection(struct commit_list *list,
521 int *reaches, int *all,
522 int find_all)
524 int nr, on_list;
525 struct commit_list *p, *best, *next, *last;
526 int *weights;
528 show_list("bisection 2 entry", 0, 0, list);
531 * Count the number of total and tree-changing items on the
532 * list, while reversing the list.
534 for (nr = on_list = 0, last = NULL, p = list;
536 p = next) {
537 unsigned flags = p->item->object.flags;
539 next = p->next;
540 if (flags & UNINTERESTING)
541 continue;
542 p->next = last;
543 last = p;
544 if (!(flags & TREESAME))
545 nr++;
546 on_list++;
548 list = last;
549 show_list("bisection 2 sorted", 0, nr, list);
551 *all = nr;
552 weights = xcalloc(on_list, sizeof(*weights));
554 /* Do the real work of finding bisection commit. */
555 best = do_find_bisection(list, nr, weights, find_all);
556 if (best) {
557 if (!find_all)
558 best->next = NULL;
559 *reaches = weight(best);
561 free(weights);
562 return best;
565 static void read_revisions_from_stdin(struct rev_info *revs)
567 char line[1000];
569 while (fgets(line, sizeof(line), stdin) != NULL) {
570 int len = strlen(line);
571 if (len && line[len - 1] == '\n')
572 line[--len] = 0;
573 if (!len)
574 break;
575 if (line[0] == '-')
576 die("options not supported in --stdin mode");
577 if (handle_revision_arg(line, revs, 0, 1))
578 die("bad revision '%s'", line);
582 int cmd_rev_list(int argc, const char **argv, const char *prefix)
584 struct commit_list *list;
585 int i;
586 int read_from_stdin = 0;
587 int bisect_show_vars = 0;
588 int bisect_find_all = 0;
589 int quiet = 0;
591 git_config(git_default_config);
592 init_revisions(&revs, prefix);
593 revs.abbrev = 0;
594 revs.commit_format = CMIT_FMT_UNSPECIFIED;
595 argc = setup_revisions(argc, argv, &revs, NULL);
597 for (i = 1 ; i < argc; i++) {
598 const char *arg = argv[i];
600 if (!strcmp(arg, "--header")) {
601 revs.verbose_header = 1;
602 continue;
604 if (!strcmp(arg, "--timestamp")) {
605 show_timestamp = 1;
606 continue;
608 if (!strcmp(arg, "--bisect")) {
609 bisect_list = 1;
610 continue;
612 if (!strcmp(arg, "--bisect-all")) {
613 bisect_list = 1;
614 bisect_find_all = 1;
615 continue;
617 if (!strcmp(arg, "--bisect-vars")) {
618 bisect_list = 1;
619 bisect_show_vars = 1;
620 continue;
622 if (!strcmp(arg, "--stdin")) {
623 if (read_from_stdin++)
624 die("--stdin given twice?");
625 read_revisions_from_stdin(&revs);
626 continue;
628 if (!strcmp(arg, "--quiet")) {
629 quiet = 1;
630 continue;
632 usage(rev_list_usage);
635 if (revs.commit_format != CMIT_FMT_UNSPECIFIED) {
636 /* The command line has a --pretty */
637 hdr_termination = '\n';
638 if (revs.commit_format == CMIT_FMT_ONELINE)
639 header_prefix = "";
640 else
641 header_prefix = "commit ";
643 else if (revs.verbose_header)
644 /* Only --header was specified */
645 revs.commit_format = CMIT_FMT_RAW;
647 list = revs.commits;
649 if ((!list &&
650 (!(revs.tag_objects||revs.tree_objects||revs.blob_objects) &&
651 !revs.pending.nr)) ||
652 revs.diff)
653 usage(rev_list_usage);
655 save_commit_buffer = revs.verbose_header || revs.grep_filter;
656 if (bisect_list)
657 revs.limited = 1;
659 if (prepare_revision_walk(&revs))
660 die("revision walk setup failed");
661 if (revs.tree_objects)
662 mark_edges_uninteresting(revs.commits, &revs, show_edge);
664 if (bisect_list) {
665 int reaches = reaches, all = all;
667 revs.commits = find_bisection(revs.commits, &reaches, &all,
668 bisect_find_all);
669 if (bisect_show_vars) {
670 int cnt;
671 char hex[41];
672 if (!revs.commits)
673 return 1;
675 * revs.commits can reach "reaches" commits among
676 * "all" commits. If it is good, then there are
677 * (all-reaches) commits left to be bisected.
678 * On the other hand, if it is bad, then the set
679 * to bisect is "reaches".
680 * A bisect set of size N has (N-1) commits further
681 * to test, as we already know one bad one.
683 cnt = all - reaches;
684 if (cnt < reaches)
685 cnt = reaches;
686 strcpy(hex, sha1_to_hex(revs.commits->item->object.sha1));
688 if (bisect_find_all) {
689 traverse_commit_list(&revs, show_commit, show_object);
690 printf("------\n");
693 printf("bisect_rev=%s\n"
694 "bisect_nr=%d\n"
695 "bisect_good=%d\n"
696 "bisect_bad=%d\n"
697 "bisect_all=%d\n",
698 hex,
699 cnt - 1,
700 all - reaches - 1,
701 reaches - 1,
702 all);
703 return 0;
707 traverse_commit_list(&revs,
708 quiet ? finish_commit : show_commit,
709 quiet ? finish_object : show_object);
711 return 0;