start_command(), if .in/.out > 0, closes file descriptors, not the callers
[git/raj.git] / builtin-rev-list.c
blob6f7d5f8214e54c24983e9c8c295edfefba54fb2b
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"
14 /* bits #0-15 in revision.h */
16 #define COUNTED (1u<<16)
18 static const char rev_list_usage[] =
19 "git-rev-list [OPTION] <commit-id>... [ -- paths... ]\n"
20 " limiting output:\n"
21 " --max-count=nr\n"
22 " --max-age=epoch\n"
23 " --min-age=epoch\n"
24 " --sparse\n"
25 " --no-merges\n"
26 " --remove-empty\n"
27 " --all\n"
28 " --stdin\n"
29 " --quiet\n"
30 " ordering output:\n"
31 " --topo-order\n"
32 " --date-order\n"
33 " formatting output:\n"
34 " --parents\n"
35 " --objects | --objects-edge\n"
36 " --unpacked\n"
37 " --header | --pretty\n"
38 " --abbrev=nr | --no-abbrev\n"
39 " --abbrev-commit\n"
40 " --left-right\n"
41 " special purpose:\n"
42 " --bisect\n"
43 " --bisect-vars\n"
44 " --bisect-all"
47 static struct rev_info revs;
49 static int bisect_list;
50 static int show_timestamp;
51 static int hdr_termination;
52 static const char *header_prefix;
54 static void finish_commit(struct commit *commit);
55 static void show_commit(struct commit *commit)
57 if (show_timestamp)
58 printf("%lu ", commit->date);
59 if (header_prefix)
60 fputs(header_prefix, stdout);
61 if (commit->object.flags & BOUNDARY)
62 putchar('-');
63 else if (commit->object.flags & UNINTERESTING)
64 putchar('^');
65 else if (revs.left_right) {
66 if (commit->object.flags & SYMMETRIC_LEFT)
67 putchar('<');
68 else
69 putchar('>');
71 if (revs.abbrev_commit && revs.abbrev)
72 fputs(find_unique_abbrev(commit->object.sha1, revs.abbrev),
73 stdout);
74 else
75 fputs(sha1_to_hex(commit->object.sha1), stdout);
76 if (revs.parents) {
77 struct commit_list *parents = commit->parents;
78 while (parents) {
79 printf(" %s", sha1_to_hex(parents->item->object.sha1));
80 parents = parents->next;
83 show_decorations(commit);
84 if (revs.commit_format == CMIT_FMT_ONELINE)
85 putchar(' ');
86 else
87 putchar('\n');
89 if (revs.verbose_header && commit->buffer) {
90 struct strbuf buf;
91 strbuf_init(&buf, 0);
92 pretty_print_commit(revs.commit_format, commit,
93 &buf, revs.abbrev, NULL, NULL,
94 revs.date_mode, 0);
95 if (buf.len)
96 printf("%s%c", buf.buf, hdr_termination);
97 strbuf_release(&buf);
99 maybe_flush_or_die(stdout, "stdout");
100 finish_commit(commit);
103 static void finish_commit(struct commit *commit)
105 if (commit->parents) {
106 free_commit_list(commit->parents);
107 commit->parents = NULL;
109 free(commit->buffer);
110 commit->buffer = NULL;
113 static void finish_object(struct object_array_entry *p)
115 if (p->item->type == OBJ_BLOB && !has_sha1_file(p->item->sha1))
116 die("missing blob object '%s'", sha1_to_hex(p->item->sha1));
119 static void show_object(struct object_array_entry *p)
121 /* An object with name "foo\n0000000..." can be used to
122 * confuse downstream git-pack-objects very badly.
124 const char *ep = strchr(p->name, '\n');
126 finish_object(p);
127 if (ep) {
128 printf("%s %.*s\n", sha1_to_hex(p->item->sha1),
129 (int) (ep - p->name),
130 p->name);
132 else
133 printf("%s %s\n", sha1_to_hex(p->item->sha1), p->name);
136 static void show_edge(struct commit *commit)
138 printf("-%s\n", sha1_to_hex(commit->object.sha1));
142 * This is a truly stupid algorithm, but it's only
143 * used for bisection, and we just don't care enough.
145 * We care just barely enough to avoid recursing for
146 * non-merge entries.
148 static int count_distance(struct commit_list *entry)
150 int nr = 0;
152 while (entry) {
153 struct commit *commit = entry->item;
154 struct commit_list *p;
156 if (commit->object.flags & (UNINTERESTING | COUNTED))
157 break;
158 if (!(commit->object.flags & TREESAME))
159 nr++;
160 commit->object.flags |= COUNTED;
161 p = commit->parents;
162 entry = p;
163 if (p) {
164 p = p->next;
165 while (p) {
166 nr += count_distance(p);
167 p = p->next;
172 return nr;
175 static void clear_distance(struct commit_list *list)
177 while (list) {
178 struct commit *commit = list->item;
179 commit->object.flags &= ~COUNTED;
180 list = list->next;
184 #define DEBUG_BISECT 0
186 static inline int weight(struct commit_list *elem)
188 return *((int*)(elem->item->util));
191 static inline void weight_set(struct commit_list *elem, int weight)
193 *((int*)(elem->item->util)) = weight;
196 static int count_interesting_parents(struct commit *commit)
198 struct commit_list *p;
199 int count;
201 for (count = 0, p = commit->parents; p; p = p->next) {
202 if (p->item->object.flags & UNINTERESTING)
203 continue;
204 count++;
206 return count;
209 static inline int halfway(struct commit_list *p, int nr)
212 * Don't short-cut something we are not going to return!
214 if (p->item->object.flags & TREESAME)
215 return 0;
216 if (DEBUG_BISECT)
217 return 0;
219 * 2 and 3 are halfway of 5.
220 * 3 is halfway of 6 but 2 and 4 are not.
222 switch (2 * weight(p) - nr) {
223 case -1: case 0: case 1:
224 return 1;
225 default:
226 return 0;
230 #if !DEBUG_BISECT
231 #define show_list(a,b,c,d) do { ; } while (0)
232 #else
233 static void show_list(const char *debug, int counted, int nr,
234 struct commit_list *list)
236 struct commit_list *p;
238 fprintf(stderr, "%s (%d/%d)\n", debug, counted, nr);
240 for (p = list; p; p = p->next) {
241 struct commit_list *pp;
242 struct commit *commit = p->item;
243 unsigned flags = commit->object.flags;
244 enum object_type type;
245 unsigned long size;
246 char *buf = read_sha1_file(commit->object.sha1, &type, &size);
247 char *ep, *sp;
249 fprintf(stderr, "%c%c%c ",
250 (flags & TREESAME) ? ' ' : 'T',
251 (flags & UNINTERESTING) ? 'U' : ' ',
252 (flags & COUNTED) ? 'C' : ' ');
253 if (commit->util)
254 fprintf(stderr, "%3d", weight(p));
255 else
256 fprintf(stderr, "---");
257 fprintf(stderr, " %.*s", 8, sha1_to_hex(commit->object.sha1));
258 for (pp = commit->parents; pp; pp = pp->next)
259 fprintf(stderr, " %.*s", 8,
260 sha1_to_hex(pp->item->object.sha1));
262 sp = strstr(buf, "\n\n");
263 if (sp) {
264 sp += 2;
265 for (ep = sp; *ep && *ep != '\n'; ep++)
267 fprintf(stderr, " %.*s", (int)(ep - sp), sp);
269 fprintf(stderr, "\n");
272 #endif /* DEBUG_BISECT */
274 static struct commit_list *best_bisection(struct commit_list *list, int nr)
276 struct commit_list *p, *best;
277 int best_distance = -1;
279 best = list;
280 for (p = list; p; p = p->next) {
281 int distance;
282 unsigned flags = p->item->object.flags;
284 if (flags & TREESAME)
285 continue;
286 distance = weight(p);
287 if (nr - distance < distance)
288 distance = nr - distance;
289 if (distance > best_distance) {
290 best = p;
291 best_distance = distance;
295 return best;
298 struct commit_dist {
299 struct commit *commit;
300 int distance;
303 static int compare_commit_dist(const void *a_, const void *b_)
305 struct commit_dist *a, *b;
307 a = (struct commit_dist *)a_;
308 b = (struct commit_dist *)b_;
309 if (a->distance != b->distance)
310 return b->distance - a->distance; /* desc sort */
311 return hashcmp(a->commit->object.sha1, b->commit->object.sha1);
314 static struct commit_list *best_bisection_sorted(struct commit_list *list, int nr)
316 struct commit_list *p;
317 struct commit_dist *array = xcalloc(nr, sizeof(*array));
318 int cnt, i;
320 for (p = list, cnt = 0; p; p = p->next) {
321 int distance;
322 unsigned flags = p->item->object.flags;
324 if (flags & TREESAME)
325 continue;
326 distance = weight(p);
327 if (nr - distance < distance)
328 distance = nr - distance;
329 array[cnt].commit = p->item;
330 array[cnt].distance = distance;
331 cnt++;
333 qsort(array, cnt, sizeof(*array), compare_commit_dist);
334 for (p = list, i = 0; i < cnt; i++) {
335 struct name_decoration *r = xmalloc(sizeof(*r) + 100);
336 struct object *obj = &(array[i].commit->object);
338 sprintf(r->name, "dist=%d", array[i].distance);
339 r->next = add_decoration(&name_decoration, obj, r);
340 p->item = array[i].commit;
341 p = p->next;
343 if (p)
344 p->next = NULL;
345 free(array);
346 return list;
350 * zero or positive weight is the number of interesting commits it can
351 * reach, including itself. Especially, weight = 0 means it does not
352 * reach any tree-changing commits (e.g. just above uninteresting one
353 * but traversal is with pathspec).
355 * weight = -1 means it has one parent and its distance is yet to
356 * be computed.
358 * weight = -2 means it has more than one parent and its distance is
359 * unknown. After running count_distance() first, they will get zero
360 * or positive distance.
362 static struct commit_list *do_find_bisection(struct commit_list *list,
363 int nr, int *weights,
364 int find_all)
366 int n, counted;
367 struct commit_list *p;
369 counted = 0;
371 for (n = 0, p = list; p; p = p->next) {
372 struct commit *commit = p->item;
373 unsigned flags = commit->object.flags;
375 p->item->util = &weights[n++];
376 switch (count_interesting_parents(commit)) {
377 case 0:
378 if (!(flags & TREESAME)) {
379 weight_set(p, 1);
380 counted++;
381 show_list("bisection 2 count one",
382 counted, nr, list);
385 * otherwise, it is known not to reach any
386 * tree-changing commit and gets weight 0.
388 break;
389 case 1:
390 weight_set(p, -1);
391 break;
392 default:
393 weight_set(p, -2);
394 break;
398 show_list("bisection 2 initialize", counted, nr, list);
401 * If you have only one parent in the resulting set
402 * then you can reach one commit more than that parent
403 * can reach. So we do not have to run the expensive
404 * count_distance() for single strand of pearls.
406 * However, if you have more than one parents, you cannot
407 * just add their distance and one for yourself, since
408 * they usually reach the same ancestor and you would
409 * end up counting them twice that way.
411 * So we will first count distance of merges the usual
412 * way, and then fill the blanks using cheaper algorithm.
414 for (p = list; p; p = p->next) {
415 if (p->item->object.flags & UNINTERESTING)
416 continue;
417 if (weight(p) != -2)
418 continue;
419 weight_set(p, count_distance(p));
420 clear_distance(list);
422 /* Does it happen to be at exactly half-way? */
423 if (!find_all && halfway(p, nr))
424 return p;
425 counted++;
428 show_list("bisection 2 count_distance", counted, nr, list);
430 while (counted < nr) {
431 for (p = list; p; p = p->next) {
432 struct commit_list *q;
433 unsigned flags = p->item->object.flags;
435 if (0 <= weight(p))
436 continue;
437 for (q = p->item->parents; q; q = q->next) {
438 if (q->item->object.flags & UNINTERESTING)
439 continue;
440 if (0 <= weight(q))
441 break;
443 if (!q)
444 continue;
447 * weight for p is unknown but q is known.
448 * add one for p itself if p is to be counted,
449 * otherwise inherit it from q directly.
451 if (!(flags & TREESAME)) {
452 weight_set(p, weight(q)+1);
453 counted++;
454 show_list("bisection 2 count one",
455 counted, nr, list);
457 else
458 weight_set(p, weight(q));
460 /* Does it happen to be at exactly half-way? */
461 if (!find_all && halfway(p, nr))
462 return p;
466 show_list("bisection 2 counted all", counted, nr, list);
468 if (!find_all)
469 return best_bisection(list, nr);
470 else
471 return best_bisection_sorted(list, nr);
474 static struct commit_list *find_bisection(struct commit_list *list,
475 int *reaches, int *all,
476 int find_all)
478 int nr, on_list;
479 struct commit_list *p, *best, *next, *last;
480 int *weights;
482 show_list("bisection 2 entry", 0, 0, list);
485 * Count the number of total and tree-changing items on the
486 * list, while reversing the list.
488 for (nr = on_list = 0, last = NULL, p = list;
490 p = next) {
491 unsigned flags = p->item->object.flags;
493 next = p->next;
494 if (flags & UNINTERESTING)
495 continue;
496 p->next = last;
497 last = p;
498 if (!(flags & TREESAME))
499 nr++;
500 on_list++;
502 list = last;
503 show_list("bisection 2 sorted", 0, nr, list);
505 *all = nr;
506 weights = xcalloc(on_list, sizeof(*weights));
508 /* Do the real work of finding bisection commit. */
509 best = do_find_bisection(list, nr, weights, find_all);
510 if (best) {
511 if (!find_all)
512 best->next = NULL;
513 *reaches = weight(best);
515 free(weights);
516 return best;
519 static void read_revisions_from_stdin(struct rev_info *revs)
521 char line[1000];
523 while (fgets(line, sizeof(line), stdin) != NULL) {
524 int len = strlen(line);
525 if (len && line[len - 1] == '\n')
526 line[--len] = 0;
527 if (!len)
528 break;
529 if (line[0] == '-')
530 die("options not supported in --stdin mode");
531 if (handle_revision_arg(line, revs, 0, 1))
532 die("bad revision '%s'", line);
536 int cmd_rev_list(int argc, const char **argv, const char *prefix)
538 struct commit_list *list;
539 int i;
540 int read_from_stdin = 0;
541 int bisect_show_vars = 0;
542 int bisect_find_all = 0;
543 int quiet = 0;
545 git_config(git_default_config);
546 init_revisions(&revs, prefix);
547 revs.abbrev = 0;
548 revs.commit_format = CMIT_FMT_UNSPECIFIED;
549 argc = setup_revisions(argc, argv, &revs, NULL);
551 for (i = 1 ; i < argc; i++) {
552 const char *arg = argv[i];
554 if (!strcmp(arg, "--header")) {
555 revs.verbose_header = 1;
556 continue;
558 if (!strcmp(arg, "--timestamp")) {
559 show_timestamp = 1;
560 continue;
562 if (!strcmp(arg, "--bisect")) {
563 bisect_list = 1;
564 continue;
566 if (!strcmp(arg, "--bisect-all")) {
567 bisect_list = 1;
568 bisect_find_all = 1;
569 continue;
571 if (!strcmp(arg, "--bisect-vars")) {
572 bisect_list = 1;
573 bisect_show_vars = 1;
574 continue;
576 if (!strcmp(arg, "--stdin")) {
577 if (read_from_stdin++)
578 die("--stdin given twice?");
579 read_revisions_from_stdin(&revs);
580 continue;
582 if (!strcmp(arg, "--quiet")) {
583 quiet = 1;
584 continue;
586 usage(rev_list_usage);
589 if (revs.commit_format != CMIT_FMT_UNSPECIFIED) {
590 /* The command line has a --pretty */
591 hdr_termination = '\n';
592 if (revs.commit_format == CMIT_FMT_ONELINE)
593 header_prefix = "";
594 else
595 header_prefix = "commit ";
597 else if (revs.verbose_header)
598 /* Only --header was specified */
599 revs.commit_format = CMIT_FMT_RAW;
601 list = revs.commits;
603 if ((!list &&
604 (!(revs.tag_objects||revs.tree_objects||revs.blob_objects) &&
605 !revs.pending.nr)) ||
606 revs.diff)
607 usage(rev_list_usage);
609 save_commit_buffer = revs.verbose_header || revs.grep_filter;
610 track_object_refs = 0;
611 if (bisect_list)
612 revs.limited = 1;
614 if (prepare_revision_walk(&revs))
615 die("revision walk setup failed");
616 if (revs.tree_objects)
617 mark_edges_uninteresting(revs.commits, &revs, show_edge);
619 if (bisect_list) {
620 int reaches = reaches, all = all;
622 revs.commits = find_bisection(revs.commits, &reaches, &all,
623 bisect_find_all);
624 if (bisect_show_vars) {
625 int cnt;
626 char hex[41];
627 if (!revs.commits)
628 return 1;
630 * revs.commits can reach "reaches" commits among
631 * "all" commits. If it is good, then there are
632 * (all-reaches) commits left to be bisected.
633 * On the other hand, if it is bad, then the set
634 * to bisect is "reaches".
635 * A bisect set of size N has (N-1) commits further
636 * to test, as we already know one bad one.
638 cnt = all - reaches;
639 if (cnt < reaches)
640 cnt = reaches;
641 strcpy(hex, sha1_to_hex(revs.commits->item->object.sha1));
643 if (bisect_find_all) {
644 traverse_commit_list(&revs, show_commit, show_object);
645 printf("------\n");
648 printf("bisect_rev=%s\n"
649 "bisect_nr=%d\n"
650 "bisect_good=%d\n"
651 "bisect_bad=%d\n"
652 "bisect_all=%d\n",
653 hex,
654 cnt - 1,
655 all - reaches - 1,
656 reaches - 1,
657 all);
658 return 0;
662 traverse_commit_list(&revs,
663 quiet ? finish_commit : show_commit,
664 quiet ? finish_object : show_object);
666 return 0;