Add test for git rebase --abort
[git/debian.git] / builtin-rev-list.c
blob6754e7f124cd3baa263d4b4f2c7e7be453260112
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 " --branches\n"
29 " --tags\n"
30 " --remotes\n"
31 " --stdin\n"
32 " --quiet\n"
33 " ordering output:\n"
34 " --topo-order\n"
35 " --date-order\n"
36 " formatting output:\n"
37 " --parents\n"
38 " --objects | --objects-edge\n"
39 " --unpacked\n"
40 " --header | --pretty\n"
41 " --abbrev=nr | --no-abbrev\n"
42 " --abbrev-commit\n"
43 " --left-right\n"
44 " special purpose:\n"
45 " --bisect\n"
46 " --bisect-vars\n"
47 " --bisect-all"
50 static struct rev_info revs;
52 static int bisect_list;
53 static int show_timestamp;
54 static int hdr_termination;
55 static const char *header_prefix;
57 static void finish_commit(struct commit *commit);
58 static void show_commit(struct commit *commit)
60 if (show_timestamp)
61 printf("%lu ", commit->date);
62 if (header_prefix)
63 fputs(header_prefix, stdout);
64 if (commit->object.flags & BOUNDARY)
65 putchar('-');
66 else if (commit->object.flags & UNINTERESTING)
67 putchar('^');
68 else if (revs.left_right) {
69 if (commit->object.flags & SYMMETRIC_LEFT)
70 putchar('<');
71 else
72 putchar('>');
74 if (revs.abbrev_commit && revs.abbrev)
75 fputs(find_unique_abbrev(commit->object.sha1, revs.abbrev),
76 stdout);
77 else
78 fputs(sha1_to_hex(commit->object.sha1), stdout);
79 if (revs.parents) {
80 struct commit_list *parents = commit->parents;
81 while (parents) {
82 printf(" %s", sha1_to_hex(parents->item->object.sha1));
83 parents = parents->next;
86 show_decorations(commit);
87 if (revs.commit_format == CMIT_FMT_ONELINE)
88 putchar(' ');
89 else
90 putchar('\n');
92 if (revs.verbose_header && commit->buffer) {
93 struct strbuf buf;
94 strbuf_init(&buf, 0);
95 pretty_print_commit(revs.commit_format, commit,
96 &buf, revs.abbrev, NULL, NULL,
97 revs.date_mode, 0);
98 if (buf.len)
99 printf("%s%c", buf.buf, hdr_termination);
100 strbuf_release(&buf);
102 maybe_flush_or_die(stdout, "stdout");
103 finish_commit(commit);
106 static void finish_commit(struct commit *commit)
108 if (commit->parents) {
109 free_commit_list(commit->parents);
110 commit->parents = NULL;
112 free(commit->buffer);
113 commit->buffer = NULL;
116 static void finish_object(struct object_array_entry *p)
118 if (p->item->type == OBJ_BLOB && !has_sha1_file(p->item->sha1))
119 die("missing blob object '%s'", sha1_to_hex(p->item->sha1));
122 static void show_object(struct object_array_entry *p)
124 /* An object with name "foo\n0000000..." can be used to
125 * confuse downstream git-pack-objects very badly.
127 const char *ep = strchr(p->name, '\n');
129 finish_object(p);
130 if (ep) {
131 printf("%s %.*s\n", sha1_to_hex(p->item->sha1),
132 (int) (ep - p->name),
133 p->name);
135 else
136 printf("%s %s\n", sha1_to_hex(p->item->sha1), p->name);
139 static void show_edge(struct commit *commit)
141 printf("-%s\n", sha1_to_hex(commit->object.sha1));
145 * This is a truly stupid algorithm, but it's only
146 * used for bisection, and we just don't care enough.
148 * We care just barely enough to avoid recursing for
149 * non-merge entries.
151 static int count_distance(struct commit_list *entry)
153 int nr = 0;
155 while (entry) {
156 struct commit *commit = entry->item;
157 struct commit_list *p;
159 if (commit->object.flags & (UNINTERESTING | COUNTED))
160 break;
161 if (!(commit->object.flags & TREESAME))
162 nr++;
163 commit->object.flags |= COUNTED;
164 p = commit->parents;
165 entry = p;
166 if (p) {
167 p = p->next;
168 while (p) {
169 nr += count_distance(p);
170 p = p->next;
175 return nr;
178 static void clear_distance(struct commit_list *list)
180 while (list) {
181 struct commit *commit = list->item;
182 commit->object.flags &= ~COUNTED;
183 list = list->next;
187 #define DEBUG_BISECT 0
189 static inline int weight(struct commit_list *elem)
191 return *((int*)(elem->item->util));
194 static inline void weight_set(struct commit_list *elem, int weight)
196 *((int*)(elem->item->util)) = weight;
199 static int count_interesting_parents(struct commit *commit)
201 struct commit_list *p;
202 int count;
204 for (count = 0, p = commit->parents; p; p = p->next) {
205 if (p->item->object.flags & UNINTERESTING)
206 continue;
207 count++;
209 return count;
212 static inline int halfway(struct commit_list *p, int nr)
215 * Don't short-cut something we are not going to return!
217 if (p->item->object.flags & TREESAME)
218 return 0;
219 if (DEBUG_BISECT)
220 return 0;
222 * 2 and 3 are halfway of 5.
223 * 3 is halfway of 6 but 2 and 4 are not.
225 switch (2 * weight(p) - nr) {
226 case -1: case 0: case 1:
227 return 1;
228 default:
229 return 0;
233 #if !DEBUG_BISECT
234 #define show_list(a,b,c,d) do { ; } while (0)
235 #else
236 static void show_list(const char *debug, int counted, int nr,
237 struct commit_list *list)
239 struct commit_list *p;
241 fprintf(stderr, "%s (%d/%d)\n", debug, counted, nr);
243 for (p = list; p; p = p->next) {
244 struct commit_list *pp;
245 struct commit *commit = p->item;
246 unsigned flags = commit->object.flags;
247 enum object_type type;
248 unsigned long size;
249 char *buf = read_sha1_file(commit->object.sha1, &type, &size);
250 char *ep, *sp;
252 fprintf(stderr, "%c%c%c ",
253 (flags & TREESAME) ? ' ' : 'T',
254 (flags & UNINTERESTING) ? 'U' : ' ',
255 (flags & COUNTED) ? 'C' : ' ');
256 if (commit->util)
257 fprintf(stderr, "%3d", weight(p));
258 else
259 fprintf(stderr, "---");
260 fprintf(stderr, " %.*s", 8, sha1_to_hex(commit->object.sha1));
261 for (pp = commit->parents; pp; pp = pp->next)
262 fprintf(stderr, " %.*s", 8,
263 sha1_to_hex(pp->item->object.sha1));
265 sp = strstr(buf, "\n\n");
266 if (sp) {
267 sp += 2;
268 for (ep = sp; *ep && *ep != '\n'; ep++)
270 fprintf(stderr, " %.*s", (int)(ep - sp), sp);
272 fprintf(stderr, "\n");
275 #endif /* DEBUG_BISECT */
277 static struct commit_list *best_bisection(struct commit_list *list, int nr)
279 struct commit_list *p, *best;
280 int best_distance = -1;
282 best = list;
283 for (p = list; p; p = p->next) {
284 int distance;
285 unsigned flags = p->item->object.flags;
287 if (flags & TREESAME)
288 continue;
289 distance = weight(p);
290 if (nr - distance < distance)
291 distance = nr - distance;
292 if (distance > best_distance) {
293 best = p;
294 best_distance = distance;
298 return best;
301 struct commit_dist {
302 struct commit *commit;
303 int distance;
306 static int compare_commit_dist(const void *a_, const void *b_)
308 struct commit_dist *a, *b;
310 a = (struct commit_dist *)a_;
311 b = (struct commit_dist *)b_;
312 if (a->distance != b->distance)
313 return b->distance - a->distance; /* desc sort */
314 return hashcmp(a->commit->object.sha1, b->commit->object.sha1);
317 static struct commit_list *best_bisection_sorted(struct commit_list *list, int nr)
319 struct commit_list *p;
320 struct commit_dist *array = xcalloc(nr, sizeof(*array));
321 int cnt, i;
323 for (p = list, cnt = 0; p; p = p->next) {
324 int distance;
325 unsigned flags = p->item->object.flags;
327 if (flags & TREESAME)
328 continue;
329 distance = weight(p);
330 if (nr - distance < distance)
331 distance = nr - distance;
332 array[cnt].commit = p->item;
333 array[cnt].distance = distance;
334 cnt++;
336 qsort(array, cnt, sizeof(*array), compare_commit_dist);
337 for (p = list, i = 0; i < cnt; i++) {
338 struct name_decoration *r = xmalloc(sizeof(*r) + 100);
339 struct object *obj = &(array[i].commit->object);
341 sprintf(r->name, "dist=%d", array[i].distance);
342 r->next = add_decoration(&name_decoration, obj, r);
343 p->item = array[i].commit;
344 p = p->next;
346 if (p)
347 p->next = NULL;
348 free(array);
349 return list;
353 * zero or positive weight is the number of interesting commits it can
354 * reach, including itself. Especially, weight = 0 means it does not
355 * reach any tree-changing commits (e.g. just above uninteresting one
356 * but traversal is with pathspec).
358 * weight = -1 means it has one parent and its distance is yet to
359 * be computed.
361 * weight = -2 means it has more than one parent and its distance is
362 * unknown. After running count_distance() first, they will get zero
363 * or positive distance.
365 static struct commit_list *do_find_bisection(struct commit_list *list,
366 int nr, int *weights,
367 int find_all)
369 int n, counted;
370 struct commit_list *p;
372 counted = 0;
374 for (n = 0, p = list; p; p = p->next) {
375 struct commit *commit = p->item;
376 unsigned flags = commit->object.flags;
378 p->item->util = &weights[n++];
379 switch (count_interesting_parents(commit)) {
380 case 0:
381 if (!(flags & TREESAME)) {
382 weight_set(p, 1);
383 counted++;
384 show_list("bisection 2 count one",
385 counted, nr, list);
388 * otherwise, it is known not to reach any
389 * tree-changing commit and gets weight 0.
391 break;
392 case 1:
393 weight_set(p, -1);
394 break;
395 default:
396 weight_set(p, -2);
397 break;
401 show_list("bisection 2 initialize", counted, nr, list);
404 * If you have only one parent in the resulting set
405 * then you can reach one commit more than that parent
406 * can reach. So we do not have to run the expensive
407 * count_distance() for single strand of pearls.
409 * However, if you have more than one parents, you cannot
410 * just add their distance and one for yourself, since
411 * they usually reach the same ancestor and you would
412 * end up counting them twice that way.
414 * So we will first count distance of merges the usual
415 * way, and then fill the blanks using cheaper algorithm.
417 for (p = list; p; p = p->next) {
418 if (p->item->object.flags & UNINTERESTING)
419 continue;
420 if (weight(p) != -2)
421 continue;
422 weight_set(p, count_distance(p));
423 clear_distance(list);
425 /* Does it happen to be at exactly half-way? */
426 if (!find_all && halfway(p, nr))
427 return p;
428 counted++;
431 show_list("bisection 2 count_distance", counted, nr, list);
433 while (counted < nr) {
434 for (p = list; p; p = p->next) {
435 struct commit_list *q;
436 unsigned flags = p->item->object.flags;
438 if (0 <= weight(p))
439 continue;
440 for (q = p->item->parents; q; q = q->next) {
441 if (q->item->object.flags & UNINTERESTING)
442 continue;
443 if (0 <= weight(q))
444 break;
446 if (!q)
447 continue;
450 * weight for p is unknown but q is known.
451 * add one for p itself if p is to be counted,
452 * otherwise inherit it from q directly.
454 if (!(flags & TREESAME)) {
455 weight_set(p, weight(q)+1);
456 counted++;
457 show_list("bisection 2 count one",
458 counted, nr, list);
460 else
461 weight_set(p, weight(q));
463 /* Does it happen to be at exactly half-way? */
464 if (!find_all && halfway(p, nr))
465 return p;
469 show_list("bisection 2 counted all", counted, nr, list);
471 if (!find_all)
472 return best_bisection(list, nr);
473 else
474 return best_bisection_sorted(list, nr);
477 static struct commit_list *find_bisection(struct commit_list *list,
478 int *reaches, int *all,
479 int find_all)
481 int nr, on_list;
482 struct commit_list *p, *best, *next, *last;
483 int *weights;
485 show_list("bisection 2 entry", 0, 0, list);
488 * Count the number of total and tree-changing items on the
489 * list, while reversing the list.
491 for (nr = on_list = 0, last = NULL, p = list;
493 p = next) {
494 unsigned flags = p->item->object.flags;
496 next = p->next;
497 if (flags & UNINTERESTING)
498 continue;
499 p->next = last;
500 last = p;
501 if (!(flags & TREESAME))
502 nr++;
503 on_list++;
505 list = last;
506 show_list("bisection 2 sorted", 0, nr, list);
508 *all = nr;
509 weights = xcalloc(on_list, sizeof(*weights));
511 /* Do the real work of finding bisection commit. */
512 best = do_find_bisection(list, nr, weights, find_all);
513 if (best) {
514 if (!find_all)
515 best->next = NULL;
516 *reaches = weight(best);
518 free(weights);
519 return best;
522 static void read_revisions_from_stdin(struct rev_info *revs)
524 char line[1000];
526 while (fgets(line, sizeof(line), stdin) != NULL) {
527 int len = strlen(line);
528 if (len && line[len - 1] == '\n')
529 line[--len] = 0;
530 if (!len)
531 break;
532 if (line[0] == '-')
533 die("options not supported in --stdin mode");
534 if (handle_revision_arg(line, revs, 0, 1))
535 die("bad revision '%s'", line);
539 int cmd_rev_list(int argc, const char **argv, const char *prefix)
541 struct commit_list *list;
542 int i;
543 int read_from_stdin = 0;
544 int bisect_show_vars = 0;
545 int bisect_find_all = 0;
546 int quiet = 0;
548 git_config(git_default_config);
549 init_revisions(&revs, prefix);
550 revs.abbrev = 0;
551 revs.commit_format = CMIT_FMT_UNSPECIFIED;
552 argc = setup_revisions(argc, argv, &revs, NULL);
554 for (i = 1 ; i < argc; i++) {
555 const char *arg = argv[i];
557 if (!strcmp(arg, "--header")) {
558 revs.verbose_header = 1;
559 continue;
561 if (!strcmp(arg, "--timestamp")) {
562 show_timestamp = 1;
563 continue;
565 if (!strcmp(arg, "--bisect")) {
566 bisect_list = 1;
567 continue;
569 if (!strcmp(arg, "--bisect-all")) {
570 bisect_list = 1;
571 bisect_find_all = 1;
572 continue;
574 if (!strcmp(arg, "--bisect-vars")) {
575 bisect_list = 1;
576 bisect_show_vars = 1;
577 continue;
579 if (!strcmp(arg, "--stdin")) {
580 if (read_from_stdin++)
581 die("--stdin given twice?");
582 read_revisions_from_stdin(&revs);
583 continue;
585 if (!strcmp(arg, "--quiet")) {
586 quiet = 1;
587 continue;
589 usage(rev_list_usage);
592 if (revs.commit_format != CMIT_FMT_UNSPECIFIED) {
593 /* The command line has a --pretty */
594 hdr_termination = '\n';
595 if (revs.commit_format == CMIT_FMT_ONELINE)
596 header_prefix = "";
597 else
598 header_prefix = "commit ";
600 else if (revs.verbose_header)
601 /* Only --header was specified */
602 revs.commit_format = CMIT_FMT_RAW;
604 list = revs.commits;
606 if ((!list &&
607 (!(revs.tag_objects||revs.tree_objects||revs.blob_objects) &&
608 !revs.pending.nr)) ||
609 revs.diff)
610 usage(rev_list_usage);
612 save_commit_buffer = revs.verbose_header || revs.grep_filter;
613 track_object_refs = 0;
614 if (bisect_list)
615 revs.limited = 1;
617 if (prepare_revision_walk(&revs))
618 die("revision walk setup failed");
619 if (revs.tree_objects)
620 mark_edges_uninteresting(revs.commits, &revs, show_edge);
622 if (bisect_list) {
623 int reaches = reaches, all = all;
625 revs.commits = find_bisection(revs.commits, &reaches, &all,
626 bisect_find_all);
627 if (bisect_show_vars) {
628 int cnt;
629 char hex[41];
630 if (!revs.commits)
631 return 1;
633 * revs.commits can reach "reaches" commits among
634 * "all" commits. If it is good, then there are
635 * (all-reaches) commits left to be bisected.
636 * On the other hand, if it is bad, then the set
637 * to bisect is "reaches".
638 * A bisect set of size N has (N-1) commits further
639 * to test, as we already know one bad one.
641 cnt = all - reaches;
642 if (cnt < reaches)
643 cnt = reaches;
644 strcpy(hex, sha1_to_hex(revs.commits->item->object.sha1));
646 if (bisect_find_all) {
647 traverse_commit_list(&revs, show_commit, show_object);
648 printf("------\n");
651 printf("bisect_rev=%s\n"
652 "bisect_nr=%d\n"
653 "bisect_good=%d\n"
654 "bisect_bad=%d\n"
655 "bisect_all=%d\n",
656 hex,
657 cnt - 1,
658 all - reaches - 1,
659 reaches - 1,
660 all);
661 return 0;
665 traverse_commit_list(&revs,
666 quiet ? finish_commit : show_commit,
667 quiet ? finish_object : show_object);
669 return 0;