remove dead code from the csum-file interface
[git/repo.git] / builtin-rev-list.c
blob697046723fc64f38ab4fb589c84f1c5d5934894a
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 " ordering output:\n"
30 " --topo-order\n"
31 " --date-order\n"
32 " formatting output:\n"
33 " --parents\n"
34 " --objects | --objects-edge\n"
35 " --unpacked\n"
36 " --header | --pretty\n"
37 " --abbrev=nr | --no-abbrev\n"
38 " --abbrev-commit\n"
39 " --left-right\n"
40 " special purpose:\n"
41 " --bisect\n"
42 " --bisect-vars\n"
43 " --bisect-all"
46 static struct rev_info revs;
48 static int bisect_list;
49 static int show_timestamp;
50 static int hdr_termination;
51 static const char *header_prefix;
53 static void show_commit(struct commit *commit)
55 if (show_timestamp)
56 printf("%lu ", commit->date);
57 if (header_prefix)
58 fputs(header_prefix, stdout);
59 if (commit->object.flags & BOUNDARY)
60 putchar('-');
61 else if (revs.left_right) {
62 if (commit->object.flags & SYMMETRIC_LEFT)
63 putchar('<');
64 else
65 putchar('>');
67 if (revs.abbrev_commit && revs.abbrev)
68 fputs(find_unique_abbrev(commit->object.sha1, revs.abbrev),
69 stdout);
70 else
71 fputs(sha1_to_hex(commit->object.sha1), stdout);
72 if (revs.parents) {
73 struct commit_list *parents = commit->parents;
74 while (parents) {
75 printf(" %s", sha1_to_hex(parents->item->object.sha1));
76 parents = parents->next;
79 show_decorations(commit);
80 if (revs.commit_format == CMIT_FMT_ONELINE)
81 putchar(' ');
82 else
83 putchar('\n');
85 if (revs.verbose_header) {
86 struct strbuf buf;
87 strbuf_init(&buf, 0);
88 pretty_print_commit(revs.commit_format, commit,
89 &buf, revs.abbrev, NULL, NULL,
90 revs.date_mode, 0);
91 if (buf.len)
92 printf("%s%c", buf.buf, hdr_termination);
93 strbuf_release(&buf);
95 maybe_flush_or_die(stdout, "stdout");
96 if (commit->parents) {
97 free_commit_list(commit->parents);
98 commit->parents = NULL;
100 free(commit->buffer);
101 commit->buffer = NULL;
104 static void show_object(struct object_array_entry *p)
106 /* An object with name "foo\n0000000..." can be used to
107 * confuse downstream git-pack-objects very badly.
109 const char *ep = strchr(p->name, '\n');
111 if (p->item->type == OBJ_BLOB && !has_sha1_file(p->item->sha1))
112 die("missing blob object '%s'", sha1_to_hex(p->item->sha1));
114 if (ep) {
115 printf("%s %.*s\n", sha1_to_hex(p->item->sha1),
116 (int) (ep - p->name),
117 p->name);
119 else
120 printf("%s %s\n", sha1_to_hex(p->item->sha1), p->name);
123 static void show_edge(struct commit *commit)
125 printf("-%s\n", sha1_to_hex(commit->object.sha1));
129 * This is a truly stupid algorithm, but it's only
130 * used for bisection, and we just don't care enough.
132 * We care just barely enough to avoid recursing for
133 * non-merge entries.
135 static int count_distance(struct commit_list *entry)
137 int nr = 0;
139 while (entry) {
140 struct commit *commit = entry->item;
141 struct commit_list *p;
143 if (commit->object.flags & (UNINTERESTING | COUNTED))
144 break;
145 if (!revs.prune_fn || (commit->object.flags & TREECHANGE))
146 nr++;
147 commit->object.flags |= COUNTED;
148 p = commit->parents;
149 entry = p;
150 if (p) {
151 p = p->next;
152 while (p) {
153 nr += count_distance(p);
154 p = p->next;
159 return nr;
162 static void clear_distance(struct commit_list *list)
164 while (list) {
165 struct commit *commit = list->item;
166 commit->object.flags &= ~COUNTED;
167 list = list->next;
171 #define DEBUG_BISECT 0
173 static inline int weight(struct commit_list *elem)
175 return *((int*)(elem->item->util));
178 static inline void weight_set(struct commit_list *elem, int weight)
180 *((int*)(elem->item->util)) = weight;
183 static int count_interesting_parents(struct commit *commit)
185 struct commit_list *p;
186 int count;
188 for (count = 0, p = commit->parents; p; p = p->next) {
189 if (p->item->object.flags & UNINTERESTING)
190 continue;
191 count++;
193 return count;
196 static inline int halfway(struct commit_list *p, int nr)
199 * Don't short-cut something we are not going to return!
201 if (revs.prune_fn && !(p->item->object.flags & TREECHANGE))
202 return 0;
203 if (DEBUG_BISECT)
204 return 0;
206 * 2 and 3 are halfway of 5.
207 * 3 is halfway of 6 but 2 and 4 are not.
209 switch (2 * weight(p) - nr) {
210 case -1: case 0: case 1:
211 return 1;
212 default:
213 return 0;
217 #if !DEBUG_BISECT
218 #define show_list(a,b,c,d) do { ; } while (0)
219 #else
220 static void show_list(const char *debug, int counted, int nr,
221 struct commit_list *list)
223 struct commit_list *p;
225 fprintf(stderr, "%s (%d/%d)\n", debug, counted, nr);
227 for (p = list; p; p = p->next) {
228 struct commit_list *pp;
229 struct commit *commit = p->item;
230 unsigned flags = commit->object.flags;
231 enum object_type type;
232 unsigned long size;
233 char *buf = read_sha1_file(commit->object.sha1, &type, &size);
234 char *ep, *sp;
236 fprintf(stderr, "%c%c%c ",
237 (flags & TREECHANGE) ? 'T' : ' ',
238 (flags & UNINTERESTING) ? 'U' : ' ',
239 (flags & COUNTED) ? 'C' : ' ');
240 if (commit->util)
241 fprintf(stderr, "%3d", weight(p));
242 else
243 fprintf(stderr, "---");
244 fprintf(stderr, " %.*s", 8, sha1_to_hex(commit->object.sha1));
245 for (pp = commit->parents; pp; pp = pp->next)
246 fprintf(stderr, " %.*s", 8,
247 sha1_to_hex(pp->item->object.sha1));
249 sp = strstr(buf, "\n\n");
250 if (sp) {
251 sp += 2;
252 for (ep = sp; *ep && *ep != '\n'; ep++)
254 fprintf(stderr, " %.*s", (int)(ep - sp), sp);
256 fprintf(stderr, "\n");
259 #endif /* DEBUG_BISECT */
261 static struct commit_list *best_bisection(struct commit_list *list, int nr)
263 struct commit_list *p, *best;
264 int best_distance = -1;
266 best = list;
267 for (p = list; p; p = p->next) {
268 int distance;
269 unsigned flags = p->item->object.flags;
271 if (revs.prune_fn && !(flags & TREECHANGE))
272 continue;
273 distance = weight(p);
274 if (nr - distance < distance)
275 distance = nr - distance;
276 if (distance > best_distance) {
277 best = p;
278 best_distance = distance;
282 return best;
285 struct commit_dist {
286 struct commit *commit;
287 int distance;
290 static int compare_commit_dist(const void *a_, const void *b_)
292 struct commit_dist *a, *b;
294 a = (struct commit_dist *)a_;
295 b = (struct commit_dist *)b_;
296 if (a->distance != b->distance)
297 return b->distance - a->distance; /* desc sort */
298 return hashcmp(a->commit->object.sha1, b->commit->object.sha1);
301 static struct commit_list *best_bisection_sorted(struct commit_list *list, int nr)
303 struct commit_list *p;
304 struct commit_dist *array = xcalloc(nr, sizeof(*array));
305 int cnt, i;
307 for (p = list, cnt = 0; p; p = p->next) {
308 int distance;
309 unsigned flags = p->item->object.flags;
311 if (revs.prune_fn && !(flags & TREECHANGE))
312 continue;
313 distance = weight(p);
314 if (nr - distance < distance)
315 distance = nr - distance;
316 array[cnt].commit = p->item;
317 array[cnt].distance = distance;
318 cnt++;
320 qsort(array, cnt, sizeof(*array), compare_commit_dist);
321 for (p = list, i = 0; i < cnt; i++) {
322 struct name_decoration *r = xmalloc(sizeof(*r) + 100);
323 struct object *obj = &(array[i].commit->object);
325 sprintf(r->name, "dist=%d", array[i].distance);
326 r->next = add_decoration(&name_decoration, obj, r);
327 p->item = array[i].commit;
328 p = p->next;
330 if (p)
331 p->next = NULL;
332 free(array);
333 return list;
337 * zero or positive weight is the number of interesting commits it can
338 * reach, including itself. Especially, weight = 0 means it does not
339 * reach any tree-changing commits (e.g. just above uninteresting one
340 * but traversal is with pathspec).
342 * weight = -1 means it has one parent and its distance is yet to
343 * be computed.
345 * weight = -2 means it has more than one parent and its distance is
346 * unknown. After running count_distance() first, they will get zero
347 * or positive distance.
349 static struct commit_list *do_find_bisection(struct commit_list *list,
350 int nr, int *weights,
351 int find_all)
353 int n, counted;
354 struct commit_list *p;
356 counted = 0;
358 for (n = 0, p = list; p; p = p->next) {
359 struct commit *commit = p->item;
360 unsigned flags = commit->object.flags;
362 p->item->util = &weights[n++];
363 switch (count_interesting_parents(commit)) {
364 case 0:
365 if (!revs.prune_fn || (flags & TREECHANGE)) {
366 weight_set(p, 1);
367 counted++;
368 show_list("bisection 2 count one",
369 counted, nr, list);
372 * otherwise, it is known not to reach any
373 * tree-changing commit and gets weight 0.
375 break;
376 case 1:
377 weight_set(p, -1);
378 break;
379 default:
380 weight_set(p, -2);
381 break;
385 show_list("bisection 2 initialize", counted, nr, list);
388 * If you have only one parent in the resulting set
389 * then you can reach one commit more than that parent
390 * can reach. So we do not have to run the expensive
391 * count_distance() for single strand of pearls.
393 * However, if you have more than one parents, you cannot
394 * just add their distance and one for yourself, since
395 * they usually reach the same ancestor and you would
396 * end up counting them twice that way.
398 * So we will first count distance of merges the usual
399 * way, and then fill the blanks using cheaper algorithm.
401 for (p = list; p; p = p->next) {
402 if (p->item->object.flags & UNINTERESTING)
403 continue;
404 if (weight(p) != -2)
405 continue;
406 weight_set(p, count_distance(p));
407 clear_distance(list);
409 /* Does it happen to be at exactly half-way? */
410 if (!find_all && halfway(p, nr))
411 return p;
412 counted++;
415 show_list("bisection 2 count_distance", counted, nr, list);
417 while (counted < nr) {
418 for (p = list; p; p = p->next) {
419 struct commit_list *q;
420 unsigned flags = p->item->object.flags;
422 if (0 <= weight(p))
423 continue;
424 for (q = p->item->parents; q; q = q->next) {
425 if (q->item->object.flags & UNINTERESTING)
426 continue;
427 if (0 <= weight(q))
428 break;
430 if (!q)
431 continue;
434 * weight for p is unknown but q is known.
435 * add one for p itself if p is to be counted,
436 * otherwise inherit it from q directly.
438 if (!revs.prune_fn || (flags & TREECHANGE)) {
439 weight_set(p, weight(q)+1);
440 counted++;
441 show_list("bisection 2 count one",
442 counted, nr, list);
444 else
445 weight_set(p, weight(q));
447 /* Does it happen to be at exactly half-way? */
448 if (!find_all && halfway(p, nr))
449 return p;
453 show_list("bisection 2 counted all", counted, nr, list);
455 if (!find_all)
456 return best_bisection(list, nr);
457 else
458 return best_bisection_sorted(list, nr);
461 static struct commit_list *find_bisection(struct commit_list *list,
462 int *reaches, int *all,
463 int find_all)
465 int nr, on_list;
466 struct commit_list *p, *best, *next, *last;
467 int *weights;
469 show_list("bisection 2 entry", 0, 0, list);
472 * Count the number of total and tree-changing items on the
473 * list, while reversing the list.
475 for (nr = on_list = 0, last = NULL, p = list;
477 p = next) {
478 unsigned flags = p->item->object.flags;
480 next = p->next;
481 if (flags & UNINTERESTING)
482 continue;
483 p->next = last;
484 last = p;
485 if (!revs.prune_fn || (flags & TREECHANGE))
486 nr++;
487 on_list++;
489 list = last;
490 show_list("bisection 2 sorted", 0, nr, list);
492 *all = nr;
493 weights = xcalloc(on_list, sizeof(*weights));
495 /* Do the real work of finding bisection commit. */
496 best = do_find_bisection(list, nr, weights, find_all);
497 if (best) {
498 if (!find_all)
499 best->next = NULL;
500 *reaches = weight(best);
502 free(weights);
503 return best;
506 static void read_revisions_from_stdin(struct rev_info *revs)
508 char line[1000];
510 while (fgets(line, sizeof(line), stdin) != NULL) {
511 int len = strlen(line);
512 if (line[len - 1] == '\n')
513 line[--len] = 0;
514 if (!len)
515 break;
516 if (line[0] == '-')
517 die("options not supported in --stdin mode");
518 if (handle_revision_arg(line, revs, 0, 1))
519 die("bad revision '%s'", line);
523 int cmd_rev_list(int argc, const char **argv, const char *prefix)
525 struct commit_list *list;
526 int i;
527 int read_from_stdin = 0;
528 int bisect_show_vars = 0;
529 int bisect_find_all = 0;
531 git_config(git_default_config);
532 init_revisions(&revs, prefix);
533 revs.abbrev = 0;
534 revs.commit_format = CMIT_FMT_UNSPECIFIED;
535 argc = setup_revisions(argc, argv, &revs, NULL);
537 for (i = 1 ; i < argc; i++) {
538 const char *arg = argv[i];
540 if (!strcmp(arg, "--header")) {
541 revs.verbose_header = 1;
542 continue;
544 if (!strcmp(arg, "--timestamp")) {
545 show_timestamp = 1;
546 continue;
548 if (!strcmp(arg, "--bisect")) {
549 bisect_list = 1;
550 continue;
552 if (!strcmp(arg, "--bisect-all")) {
553 bisect_list = 1;
554 bisect_find_all = 1;
555 continue;
557 if (!strcmp(arg, "--bisect-vars")) {
558 bisect_list = 1;
559 bisect_show_vars = 1;
560 continue;
562 if (!strcmp(arg, "--stdin")) {
563 if (read_from_stdin++)
564 die("--stdin given twice?");
565 read_revisions_from_stdin(&revs);
566 continue;
568 usage(rev_list_usage);
571 if (revs.commit_format != CMIT_FMT_UNSPECIFIED) {
572 /* The command line has a --pretty */
573 hdr_termination = '\n';
574 if (revs.commit_format == CMIT_FMT_ONELINE)
575 header_prefix = "";
576 else
577 header_prefix = "commit ";
579 else if (revs.verbose_header)
580 /* Only --header was specified */
581 revs.commit_format = CMIT_FMT_RAW;
583 list = revs.commits;
585 if ((!list &&
586 (!(revs.tag_objects||revs.tree_objects||revs.blob_objects) &&
587 !revs.pending.nr)) ||
588 revs.diff)
589 usage(rev_list_usage);
591 save_commit_buffer = revs.verbose_header || revs.grep_filter;
592 track_object_refs = 0;
593 if (bisect_list)
594 revs.limited = 1;
596 prepare_revision_walk(&revs);
597 if (revs.tree_objects)
598 mark_edges_uninteresting(revs.commits, &revs, show_edge);
600 if (bisect_list) {
601 int reaches = reaches, all = all;
603 revs.commits = find_bisection(revs.commits, &reaches, &all,
604 bisect_find_all);
605 if (bisect_show_vars) {
606 int cnt;
607 char hex[41];
608 if (!revs.commits)
609 return 1;
611 * revs.commits can reach "reaches" commits among
612 * "all" commits. If it is good, then there are
613 * (all-reaches) commits left to be bisected.
614 * On the other hand, if it is bad, then the set
615 * to bisect is "reaches".
616 * A bisect set of size N has (N-1) commits further
617 * to test, as we already know one bad one.
619 cnt = all - reaches;
620 if (cnt < reaches)
621 cnt = reaches;
622 strcpy(hex, sha1_to_hex(revs.commits->item->object.sha1));
624 if (bisect_find_all) {
625 traverse_commit_list(&revs, show_commit, show_object);
626 printf("------\n");
629 printf("bisect_rev=%s\n"
630 "bisect_nr=%d\n"
631 "bisect_good=%d\n"
632 "bisect_bad=%d\n"
633 "bisect_all=%d\n",
634 hex,
635 cnt - 1,
636 all - reaches - 1,
637 reaches - 1,
638 all);
639 return 0;
643 traverse_commit_list(&revs, show_commit, show_object);
645 return 0;