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"
33 " formatting output:\n"
35 " --objects | --objects-edge\n"
37 " --header | --pretty\n"
38 " --abbrev=nr | --no-abbrev\n"
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)
58 printf("%lu ", commit->date);
60 fputs(header_prefix, stdout);
61 if (commit->object.flags & BOUNDARY)
63 else if (commit->object.flags & UNINTERESTING)
65 else if (revs.left_right) {
66 if (commit->object.flags & SYMMETRIC_LEFT)
71 if (revs.abbrev_commit && revs.abbrev)
72 fputs(find_unique_abbrev(commit->object.sha1, revs.abbrev),
75 fputs(sha1_to_hex(commit->object.sha1), stdout);
77 struct commit_list *parents = commit->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)
89 if (revs.verbose_header && commit->buffer) {
92 pretty_print_commit(revs.commit_format, commit,
93 &buf, revs.abbrev, NULL, NULL,
96 printf("%s%c", buf.buf, hdr_termination);
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');
128 printf("%s %.*s\n", sha1_to_hex(p->item->sha1),
129 (int) (ep - p->name),
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
148 static int count_distance(struct commit_list *entry)
153 struct commit *commit = entry->item;
154 struct commit_list *p;
156 if (commit->object.flags & (UNINTERESTING | COUNTED))
158 if (!(commit->object.flags & TREESAME))
160 commit->object.flags |= COUNTED;
166 nr += count_distance(p);
175 static void clear_distance(struct commit_list *list)
178 struct commit *commit = list->item;
179 commit->object.flags &= ~COUNTED;
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;
201 for (count = 0, p = commit->parents; p; p = p->next) {
202 if (p->item->object.flags & UNINTERESTING)
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)
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:
231 #define show_list(a,b,c,d) do { ; } while (0)
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;
246 char *buf = read_sha1_file(commit->object.sha1, &type, &size);
249 fprintf(stderr, "%c%c%c ",
250 (flags & TREESAME) ? ' ' : 'T',
251 (flags & UNINTERESTING) ? 'U' : ' ',
252 (flags & COUNTED) ? 'C' : ' ');
254 fprintf(stderr, "%3d", weight(p));
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");
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;
280 for (p = list; p; p = p->next) {
282 unsigned flags = p->item->object.flags;
284 if (flags & TREESAME)
286 distance = weight(p);
287 if (nr - distance < distance)
288 distance = nr - distance;
289 if (distance > best_distance) {
291 best_distance = distance;
299 struct commit *commit;
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));
320 for (p = list, cnt = 0; p; p = p->next) {
322 unsigned flags = p->item->object.flags;
324 if (flags & TREESAME)
326 distance = weight(p);
327 if (nr - distance < distance)
328 distance = nr - distance;
329 array[cnt].commit = p->item;
330 array[cnt].distance = distance;
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;
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
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,
367 struct commit_list *p;
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)) {
378 if (!(flags & TREESAME)) {
381 show_list("bisection 2 count one",
385 * otherwise, it is known not to reach any
386 * tree-changing commit and gets weight 0.
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)
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))
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;
437 for (q = p->item->parents; q; q = q->next) {
438 if (q->item->object.flags & UNINTERESTING)
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);
454 show_list("bisection 2 count one",
458 weight_set(p, weight(q));
460 /* Does it happen to be at exactly half-way? */
461 if (!find_all && halfway(p, nr))
466 show_list("bisection 2 counted all", counted, nr, list);
469 return best_bisection(list, nr);
471 return best_bisection_sorted(list, nr);
474 static struct commit_list *find_bisection(struct commit_list *list,
475 int *reaches, int *all,
479 struct commit_list *p, *best, *next, *last;
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;
491 unsigned flags = p->item->object.flags;
494 if (flags & UNINTERESTING)
498 if (!(flags & TREESAME))
503 show_list("bisection 2 sorted", 0, nr, list);
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);
513 *reaches = weight(best);
519 static void read_revisions_from_stdin(struct rev_info *revs)
523 while (fgets(line, sizeof(line), stdin) != NULL) {
524 int len = strlen(line);
525 if (len && line[len - 1] == '\n')
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;
540 int read_from_stdin = 0;
541 int bisect_show_vars = 0;
542 int bisect_find_all = 0;
545 git_config(git_default_config);
546 init_revisions(&revs, prefix);
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;
558 if (!strcmp(arg, "--timestamp")) {
562 if (!strcmp(arg, "--bisect")) {
566 if (!strcmp(arg, "--bisect-all")) {
571 if (!strcmp(arg, "--bisect-vars")) {
573 bisect_show_vars = 1;
576 if (!strcmp(arg, "--stdin")) {
577 if (read_from_stdin++)
578 die("--stdin given twice?");
579 read_revisions_from_stdin(&revs);
582 if (!strcmp(arg, "--quiet")) {
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)
595 header_prefix = "commit ";
597 else if (revs.verbose_header)
598 /* Only --header was specified */
599 revs.commit_format = CMIT_FMT_RAW;
604 (!(revs.tag_objects||revs.tree_objects||revs.blob_objects) &&
605 !revs.pending.nr)) ||
607 usage(rev_list_usage);
609 save_commit_buffer = revs.verbose_header || revs.grep_filter;
610 track_object_refs = 0;
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);
620 int reaches = reaches, all = all;
622 revs.commits = find_bisection(revs.commits, &reaches, &all,
624 if (bisect_show_vars) {
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.
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);
648 printf("bisect_rev=%s\n"
662 traverse_commit_list(&revs,
663 quiet ? finish_commit : show_commit,
664 quiet ? finish_object : show_object);