10 #include "list-objects.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"
36 " formatting output:\n"
38 " --objects | --objects-edge\n"
40 " --header | --pretty\n"
41 " --abbrev=nr | --no-abbrev\n"
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)
61 printf("%lu ", commit->date);
63 fputs(header_prefix, stdout);
64 if (commit->object.flags & BOUNDARY)
66 else if (commit->object.flags & UNINTERESTING)
68 else if (revs.left_right) {
69 if (commit->object.flags & SYMMETRIC_LEFT)
74 if (revs.abbrev_commit && revs.abbrev)
75 fputs(find_unique_abbrev(commit->object.sha1, revs.abbrev),
78 fputs(sha1_to_hex(commit->object.sha1), stdout);
80 struct commit_list *parents = commit->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)
92 if (revs.verbose_header && commit->buffer) {
95 pretty_print_commit(revs.commit_format, commit,
96 &buf, revs.abbrev, NULL, NULL,
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');
131 printf("%s %.*s\n", sha1_to_hex(p->item->sha1),
132 (int) (ep - p->name),
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
151 static int count_distance(struct commit_list *entry)
156 struct commit *commit = entry->item;
157 struct commit_list *p;
159 if (commit->object.flags & (UNINTERESTING | COUNTED))
161 if (!(commit->object.flags & TREESAME))
163 commit->object.flags |= COUNTED;
169 nr += count_distance(p);
178 static void clear_distance(struct commit_list *list)
181 struct commit *commit = list->item;
182 commit->object.flags &= ~COUNTED;
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;
204 for (count = 0, p = commit->parents; p; p = p->next) {
205 if (p->item->object.flags & UNINTERESTING)
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)
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:
234 #define show_list(a,b,c,d) do { ; } while (0)
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;
249 char *buf = read_sha1_file(commit->object.sha1, &type, &size);
252 fprintf(stderr, "%c%c%c ",
253 (flags & TREESAME) ? ' ' : 'T',
254 (flags & UNINTERESTING) ? 'U' : ' ',
255 (flags & COUNTED) ? 'C' : ' ');
257 fprintf(stderr, "%3d", weight(p));
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");
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;
283 for (p = list; p; p = p->next) {
285 unsigned flags = p->item->object.flags;
287 if (flags & TREESAME)
289 distance = weight(p);
290 if (nr - distance < distance)
291 distance = nr - distance;
292 if (distance > best_distance) {
294 best_distance = distance;
302 struct commit *commit;
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));
323 for (p = list, cnt = 0; p; p = p->next) {
325 unsigned flags = p->item->object.flags;
327 if (flags & TREESAME)
329 distance = weight(p);
330 if (nr - distance < distance)
331 distance = nr - distance;
332 array[cnt].commit = p->item;
333 array[cnt].distance = distance;
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;
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
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,
370 struct commit_list *p;
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)) {
381 if (!(flags & TREESAME)) {
384 show_list("bisection 2 count one",
388 * otherwise, it is known not to reach any
389 * tree-changing commit and gets weight 0.
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)
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))
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;
440 for (q = p->item->parents; q; q = q->next) {
441 if (q->item->object.flags & UNINTERESTING)
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);
457 show_list("bisection 2 count one",
461 weight_set(p, weight(q));
463 /* Does it happen to be at exactly half-way? */
464 if (!find_all && halfway(p, nr))
469 show_list("bisection 2 counted all", counted, nr, list);
472 return best_bisection(list, nr);
474 return best_bisection_sorted(list, nr);
477 static struct commit_list *find_bisection(struct commit_list *list,
478 int *reaches, int *all,
482 struct commit_list *p, *best, *next, *last;
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;
494 unsigned flags = p->item->object.flags;
497 if (flags & UNINTERESTING)
501 if (!(flags & TREESAME))
506 show_list("bisection 2 sorted", 0, nr, list);
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);
516 *reaches = weight(best);
522 static void read_revisions_from_stdin(struct rev_info *revs)
526 while (fgets(line, sizeof(line), stdin) != NULL) {
527 int len = strlen(line);
528 if (len && line[len - 1] == '\n')
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;
543 int read_from_stdin = 0;
544 int bisect_show_vars = 0;
545 int bisect_find_all = 0;
548 git_config(git_default_config);
549 init_revisions(&revs, prefix);
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;
561 if (!strcmp(arg, "--timestamp")) {
565 if (!strcmp(arg, "--bisect")) {
569 if (!strcmp(arg, "--bisect-all")) {
574 if (!strcmp(arg, "--bisect-vars")) {
576 bisect_show_vars = 1;
579 if (!strcmp(arg, "--stdin")) {
580 if (read_from_stdin++)
581 die("--stdin given twice?");
582 read_revisions_from_stdin(&revs);
585 if (!strcmp(arg, "--quiet")) {
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)
598 header_prefix = "commit ";
600 else if (revs.verbose_header)
601 /* Only --header was specified */
602 revs.commit_format = CMIT_FMT_RAW;
607 (!(revs.tag_objects||revs.tree_objects||revs.blob_objects) &&
608 !revs.pending.nr)) ||
610 usage(rev_list_usage);
612 save_commit_buffer = revs.verbose_header || revs.grep_filter;
616 if (prepare_revision_walk(&revs))
617 die("revision walk setup failed");
618 if (revs.tree_objects)
619 mark_edges_uninteresting(revs.commits, &revs, show_edge);
622 int reaches = reaches, all = all;
624 revs.commits = find_bisection(revs.commits, &reaches, &all,
626 if (bisect_show_vars) {
632 * revs.commits can reach "reaches" commits among
633 * "all" commits. If it is good, then there are
634 * (all-reaches) commits left to be bisected.
635 * On the other hand, if it is bad, then the set
636 * to bisect is "reaches".
637 * A bisect set of size N has (N-1) commits further
638 * to test, as we already know one bad one.
643 strcpy(hex, sha1_to_hex(revs.commits->item->object.sha1));
645 if (bisect_find_all) {
646 traverse_commit_list(&revs, show_commit, show_object);
650 printf("bisect_rev=%s\n"
664 traverse_commit_list(&revs,
665 quiet ? finish_commit : show_commit,
666 quiet ? finish_object : show_object);