10 #include "list-objects.h"
13 /* bits #0-15 in revision.h */
15 #define COUNTED (1u<<16)
17 static const char rev_list_usage[] =
18 "git-rev-list [OPTION] <commit-id>... [ -- paths... ]\n"
31 " formatting output:\n"
33 " --objects | --objects-edge\n"
35 " --header | --pretty\n"
36 " --abbrev=nr | --no-abbrev\n"
44 static struct rev_info revs;
46 static int bisect_list;
47 static int show_timestamp;
48 static int hdr_termination;
49 static const char *header_prefix;
51 static void show_commit(struct commit *commit)
54 printf("%lu ", commit->date);
56 fputs(header_prefix, stdout);
57 if (commit->object.flags & BOUNDARY)
59 else if (revs.left_right) {
60 if (commit->object.flags & SYMMETRIC_LEFT)
65 if (revs.abbrev_commit && revs.abbrev)
66 fputs(find_unique_abbrev(commit->object.sha1, revs.abbrev),
69 fputs(sha1_to_hex(commit->object.sha1), stdout);
71 struct commit_list *parents = commit->parents;
73 printf(" %s", sha1_to_hex(parents->item->object.sha1));
74 parents = parents->next;
77 if (revs.commit_format == CMIT_FMT_ONELINE)
82 if (revs.verbose_header) {
84 unsigned long buflen = 0;
85 pretty_print_commit(revs.commit_format, commit, ~0,
87 revs.abbrev, NULL, NULL, revs.date_mode);
89 printf("%s%c", buf, hdr_termination);
92 maybe_flush_or_die(stdout, "stdout");
93 if (commit->parents) {
94 free_commit_list(commit->parents);
95 commit->parents = NULL;
98 commit->buffer = NULL;
101 static void show_object(struct object_array_entry *p)
103 /* An object with name "foo\n0000000..." can be used to
104 * confuse downstream git-pack-objects very badly.
106 const char *ep = strchr(p->name, '\n');
108 if (p->item->type == OBJ_BLOB && !has_sha1_file(p->item->sha1))
109 die("missing blob object '%s'", sha1_to_hex(p->item->sha1));
112 printf("%s %.*s\n", sha1_to_hex(p->item->sha1),
113 (int) (ep - p->name),
117 printf("%s %s\n", sha1_to_hex(p->item->sha1), p->name);
120 static void show_edge(struct commit *commit)
122 printf("-%s\n", sha1_to_hex(commit->object.sha1));
126 * This is a truly stupid algorithm, but it's only
127 * used for bisection, and we just don't care enough.
129 * We care just barely enough to avoid recursing for
132 static int count_distance(struct commit_list *entry)
137 struct commit *commit = entry->item;
138 struct commit_list *p;
140 if (commit->object.flags & (UNINTERESTING | COUNTED))
142 if (!revs.prune_fn || (commit->object.flags & TREECHANGE))
144 commit->object.flags |= COUNTED;
150 nr += count_distance(p);
159 static void clear_distance(struct commit_list *list)
162 struct commit *commit = list->item;
163 commit->object.flags &= ~COUNTED;
168 #define DEBUG_BISECT 0
170 static inline int weight(struct commit_list *elem)
172 return *((int*)(elem->item->util));
175 static inline void weight_set(struct commit_list *elem, int weight)
177 *((int*)(elem->item->util)) = weight;
180 static int count_interesting_parents(struct commit *commit)
182 struct commit_list *p;
185 for (count = 0, p = commit->parents; p; p = p->next) {
186 if (p->item->object.flags & UNINTERESTING)
193 static inline int halfway(struct commit_list *p, int nr)
196 * Don't short-cut something we are not going to return!
198 if (revs.prune_fn && !(p->item->object.flags & TREECHANGE))
203 * 2 and 3 are halfway of 5.
204 * 3 is halfway of 6 but 2 and 4 are not.
206 switch (2 * weight(p) - nr) {
207 case -1: case 0: case 1:
215 #define show_list(a,b,c,d) do { ; } while (0)
217 static void show_list(const char *debug, int counted, int nr,
218 struct commit_list *list)
220 struct commit_list *p;
222 fprintf(stderr, "%s (%d/%d)\n", debug, counted, nr);
224 for (p = list; p; p = p->next) {
225 struct commit_list *pp;
226 struct commit *commit = p->item;
227 unsigned flags = commit->object.flags;
228 enum object_type type;
230 char *buf = read_sha1_file(commit->object.sha1, &type, &size);
233 fprintf(stderr, "%c%c%c ",
234 (flags & TREECHANGE) ? 'T' : ' ',
235 (flags & UNINTERESTING) ? 'U' : ' ',
236 (flags & COUNTED) ? 'C' : ' ');
238 fprintf(stderr, "%3d", weight(p));
240 fprintf(stderr, "---");
241 fprintf(stderr, " %.*s", 8, sha1_to_hex(commit->object.sha1));
242 for (pp = commit->parents; pp; pp = pp->next)
243 fprintf(stderr, " %.*s", 8,
244 sha1_to_hex(pp->item->object.sha1));
246 sp = strstr(buf, "\n\n");
249 for (ep = sp; *ep && *ep != '\n'; ep++)
251 fprintf(stderr, " %.*s", (int)(ep - sp), sp);
253 fprintf(stderr, "\n");
256 #endif /* DEBUG_BISECT */
258 static struct commit_list *best_bisection(struct commit_list *list, int nr)
260 struct commit_list *p, *best;
261 int best_distance = -1;
264 for (p = list; p; p = p->next) {
266 unsigned flags = p->item->object.flags;
268 if (revs.prune_fn && !(flags & TREECHANGE))
270 distance = weight(p);
271 if (nr - distance < distance)
272 distance = nr - distance;
273 if (distance > best_distance) {
275 best_distance = distance;
283 * zero or positive weight is the number of interesting commits it can
284 * reach, including itself. Especially, weight = 0 means it does not
285 * reach any tree-changing commits (e.g. just above uninteresting one
286 * but traversal is with pathspec).
288 * weight = -1 means it has one parent and its distance is yet to
291 * weight = -2 means it has more than one parent and its distance is
292 * unknown. After running count_distance() first, they will get zero
293 * or positive distance.
295 static struct commit_list *do_find_bisection(struct commit_list *list,
296 int nr, int *weights)
299 struct commit_list *p;
303 for (n = 0, p = list; p; p = p->next) {
304 struct commit *commit = p->item;
305 unsigned flags = commit->object.flags;
307 p->item->util = &weights[n++];
308 switch (count_interesting_parents(commit)) {
310 if (!revs.prune_fn || (flags & TREECHANGE)) {
313 show_list("bisection 2 count one",
317 * otherwise, it is known not to reach any
318 * tree-changing commit and gets weight 0.
330 show_list("bisection 2 initialize", counted, nr, list);
333 * If you have only one parent in the resulting set
334 * then you can reach one commit more than that parent
335 * can reach. So we do not have to run the expensive
336 * count_distance() for single strand of pearls.
338 * However, if you have more than one parents, you cannot
339 * just add their distance and one for yourself, since
340 * they usually reach the same ancestor and you would
341 * end up counting them twice that way.
343 * So we will first count distance of merges the usual
344 * way, and then fill the blanks using cheaper algorithm.
346 for (p = list; p; p = p->next) {
347 if (p->item->object.flags & UNINTERESTING)
351 weight_set(p, count_distance(p));
352 clear_distance(list);
354 /* Does it happen to be at exactly half-way? */
360 show_list("bisection 2 count_distance", counted, nr, list);
362 while (counted < nr) {
363 for (p = list; p; p = p->next) {
364 struct commit_list *q;
365 unsigned flags = p->item->object.flags;
369 for (q = p->item->parents; q; q = q->next) {
370 if (q->item->object.flags & UNINTERESTING)
379 * weight for p is unknown but q is known.
380 * add one for p itself if p is to be counted,
381 * otherwise inherit it from q directly.
383 if (!revs.prune_fn || (flags & TREECHANGE)) {
384 weight_set(p, weight(q)+1);
386 show_list("bisection 2 count one",
390 weight_set(p, weight(q));
392 /* Does it happen to be at exactly half-way? */
398 show_list("bisection 2 counted all", counted, nr, list);
400 /* Then find the best one */
401 return best_bisection(list, nr);
404 static struct commit_list *find_bisection(struct commit_list *list,
405 int *reaches, int *all)
408 struct commit_list *p, *best, *next, *last;
411 show_list("bisection 2 entry", 0, 0, list);
414 * Count the number of total and tree-changing items on the
415 * list, while reversing the list.
417 for (nr = on_list = 0, last = NULL, p = list;
420 unsigned flags = p->item->object.flags;
423 if (flags & UNINTERESTING)
427 if (!revs.prune_fn || (flags & TREECHANGE))
432 show_list("bisection 2 sorted", 0, nr, list);
435 weights = xcalloc(on_list, sizeof(*weights));
437 /* Do the real work of finding bisection commit. */
438 best = do_find_bisection(list, nr, weights);
442 *reaches = weight(best);
449 static void read_revisions_from_stdin(struct rev_info *revs)
453 while (fgets(line, sizeof(line), stdin) != NULL) {
454 int len = strlen(line);
455 if (line[len - 1] == '\n')
460 die("options not supported in --stdin mode");
461 if (handle_revision_arg(line, revs, 0, 1))
462 die("bad revision '%s'", line);
466 int cmd_rev_list(int argc, const char **argv, const char *prefix)
468 struct commit_list *list;
470 int read_from_stdin = 0;
471 int bisect_show_vars = 0;
473 git_config(git_default_config);
474 init_revisions(&revs, prefix);
476 revs.commit_format = CMIT_FMT_UNSPECIFIED;
477 argc = setup_revisions(argc, argv, &revs, NULL);
479 for (i = 1 ; i < argc; i++) {
480 const char *arg = argv[i];
482 if (!strcmp(arg, "--header")) {
483 revs.verbose_header = 1;
486 if (!strcmp(arg, "--timestamp")) {
490 if (!strcmp(arg, "--bisect")) {
494 if (!strcmp(arg, "--bisect-vars")) {
496 bisect_show_vars = 1;
499 if (!strcmp(arg, "--stdin")) {
500 if (read_from_stdin++)
501 die("--stdin given twice?");
502 read_revisions_from_stdin(&revs);
505 usage(rev_list_usage);
508 if (revs.commit_format != CMIT_FMT_UNSPECIFIED) {
509 /* The command line has a --pretty */
510 hdr_termination = '\n';
511 if (revs.commit_format == CMIT_FMT_ONELINE)
514 header_prefix = "commit ";
516 else if (revs.verbose_header)
517 /* Only --header was specified */
518 revs.commit_format = CMIT_FMT_RAW;
523 (!(revs.tag_objects||revs.tree_objects||revs.blob_objects) &&
524 !revs.pending.nr)) ||
526 usage(rev_list_usage);
528 save_commit_buffer = revs.verbose_header || revs.grep_filter;
529 track_object_refs = 0;
533 prepare_revision_walk(&revs);
534 if (revs.tree_objects)
535 mark_edges_uninteresting(revs.commits, &revs, show_edge);
538 int reaches = reaches, all = all;
540 revs.commits = find_bisection(revs.commits, &reaches, &all);
541 if (bisect_show_vars) {
546 * revs.commits can reach "reaches" commits among
547 * "all" commits. If it is good, then there are
548 * (all-reaches) commits left to be bisected.
549 * On the other hand, if it is bad, then the set
550 * to bisect is "reaches".
551 * A bisect set of size N has (N-1) commits further
552 * to test, as we already know one bad one.
557 printf("bisect_rev=%s\n"
562 sha1_to_hex(revs.commits->item->object.sha1),
571 traverse_commit_list(&revs, show_commit, show_object);