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 struct object *o = &(parents->item->object);
74 parents = parents->next;
75 if (o->flags & TMP_MARK)
77 printf(" %s", sha1_to_hex(o->sha1));
80 /* TMP_MARK is a general purpose flag that can
81 * be used locally, but the user should clean
82 * things up after it is done with them.
84 for (parents = commit->parents;
86 parents = parents->next)
87 parents->item->object.flags &= ~TMP_MARK;
89 if (revs.commit_format == CMIT_FMT_ONELINE)
94 if (revs.verbose_header) {
95 static char pretty_header[16384];
96 pretty_print_commit(revs.commit_format, commit, ~0,
97 pretty_header, sizeof(pretty_header),
98 revs.abbrev, NULL, NULL, revs.relative_date);
99 printf("%s%c", pretty_header, hdr_termination);
102 if (commit->parents) {
103 free_commit_list(commit->parents);
104 commit->parents = NULL;
106 free(commit->buffer);
107 commit->buffer = NULL;
110 static void show_object(struct object_array_entry *p)
112 /* An object with name "foo\n0000000..." can be used to
113 * confuse downstream git-pack-objects very badly.
115 const char *ep = strchr(p->name, '\n');
117 printf("%s %.*s\n", sha1_to_hex(p->item->sha1),
118 (int) (ep - p->name),
122 printf("%s %s\n", sha1_to_hex(p->item->sha1), p->name);
125 static void show_edge(struct commit *commit)
127 printf("-%s\n", sha1_to_hex(commit->object.sha1));
131 * This is a truly stupid algorithm, but it's only
132 * used for bisection, and we just don't care enough.
134 * We care just barely enough to avoid recursing for
137 static int count_distance(struct commit_list *entry)
142 struct commit *commit = entry->item;
143 struct commit_list *p;
145 if (commit->object.flags & (UNINTERESTING | COUNTED))
147 if (!revs.prune_fn || (commit->object.flags & TREECHANGE))
149 commit->object.flags |= COUNTED;
155 nr += count_distance(p);
164 static void clear_distance(struct commit_list *list)
167 struct commit *commit = list->item;
168 commit->object.flags &= ~COUNTED;
173 #define DEBUG_BISECT 0
175 static inline int weight(struct commit_list *elem)
177 return *((int*)(elem->item->util));
180 static inline void weight_set(struct commit_list *elem, int weight)
182 *((int*)(elem->item->util)) = weight;
185 static int count_interesting_parents(struct commit *commit)
187 struct commit_list *p;
190 for (count = 0, p = commit->parents; p; p = p->next) {
191 if (p->item->object.flags & UNINTERESTING)
198 static inline int halfway(struct commit_list *p, int distance, int nr)
201 * Don't short-cut something we are not going to return!
203 if (revs.prune_fn && !(p->item->object.flags & TREECHANGE))
208 * 2 and 3 are halfway of 5.
209 * 3 is halfway of 6 but 2 and 4 are not.
212 switch (distance - nr) {
213 case -1: case 0: case 1:
221 #define show_list(a,b,c,d) do { ; } while (0)
223 static void show_list(const char *debug, int counted, int nr,
224 struct commit_list *list)
226 struct commit_list *p;
228 fprintf(stderr, "%s (%d/%d)\n", debug, counted, nr);
230 for (p = list; p; p = p->next) {
231 struct commit_list *pp;
232 struct commit *commit = p->item;
233 unsigned flags = commit->object.flags;
234 enum object_type type;
236 char *buf = read_sha1_file(commit->object.sha1, &type, &size);
239 fprintf(stderr, "%c%c%c ",
240 (flags & TREECHANGE) ? 'T' : ' ',
241 (flags & UNINTERESTING) ? 'U' : ' ',
242 (flags & COUNTED) ? 'C' : ' ');
244 fprintf(stderr, "%3d", weight(p));
246 fprintf(stderr, "---");
247 fprintf(stderr, " %.*s", 8, sha1_to_hex(commit->object.sha1));
248 for (pp = commit->parents; pp; pp = pp->next)
249 fprintf(stderr, " %.*s", 8,
250 sha1_to_hex(pp->item->object.sha1));
252 sp = strstr(buf, "\n\n");
255 for (ep = sp; *ep && *ep != '\n'; ep++)
257 fprintf(stderr, " %.*s", (int)(ep - sp), sp);
259 fprintf(stderr, "\n");
262 #endif /* DEBUG_BISECT */
265 * zero or positive weight is the number of interesting commits it can
266 * reach, including itself. Especially, weight = 0 means it does not
267 * reach any tree-changing commits (e.g. just above uninteresting one
268 * but traversal is with pathspec).
270 * weight = -1 means it has one parent and its distance is yet to
273 * weight = -2 means it has more than one parent and its distance is
274 * unknown. After running count_distance() first, they will get zero
275 * or positive distance.
278 static struct commit_list *find_bisection(struct commit_list *list,
279 int *reaches, int *all)
281 int n, nr, on_list, counted, distance;
282 struct commit_list *p, *best, *next, *last;
285 show_list("bisection 2 entry", 0, 0, list);
288 * Count the number of total and tree-changing items on the
289 * list, while reversing the list.
291 for (nr = on_list = 0, last = NULL, p = list;
294 unsigned flags = p->item->object.flags;
297 if (flags & UNINTERESTING)
301 if (!revs.prune_fn || (flags & TREECHANGE))
306 show_list("bisection 2 sorted", 0, nr, list);
309 weights = xcalloc(on_list, sizeof(int*));
312 for (n = 0, p = list; p; p = p->next) {
313 struct commit *commit = p->item;
314 unsigned flags = commit->object.flags;
316 p->item->util = &weights[n++];
317 switch (count_interesting_parents(commit)) {
319 if (!revs.prune_fn || (flags & TREECHANGE)) {
322 show_list("bisection 2 count one",
326 * otherwise, it is known not to reach any
327 * tree-changing commit and gets weight 0.
339 show_list("bisection 2 initialize", counted, nr, list);
342 * If you have only one parent in the resulting set
343 * then you can reach one commit more than that parent
344 * can reach. So we do not have to run the expensive
345 * count_distance() for single strand of pearls.
347 * However, if you have more than one parents, you cannot
348 * just add their distance and one for yourself, since
349 * they usually reach the same ancestor and you would
350 * end up counting them twice that way.
352 * So we will first count distance of merges the usual
353 * way, and then fill the blanks using cheaper algorithm.
355 for (p = list; p; p = p->next) {
356 if (p->item->object.flags & UNINTERESTING)
361 distance = count_distance(p);
362 clear_distance(list);
363 weight_set(p, distance);
365 /* Does it happen to be at exactly half-way? */
366 if (halfway(p, distance, nr)) {
375 show_list("bisection 2 count_distance", counted, nr, list);
377 while (counted < nr) {
378 for (p = list; p; p = p->next) {
379 struct commit_list *q;
380 unsigned flags = p->item->object.flags;
384 for (q = p->item->parents; q; q = q->next) {
385 if (q->item->object.flags & UNINTERESTING)
394 * weight for p is unknown but q is known.
395 * add one for p itself if p is to be counted,
396 * otherwise inherit it from q directly.
398 if (!revs.prune_fn || (flags & TREECHANGE)) {
399 weight_set(p, weight(q)+1);
401 show_list("bisection 2 count one",
405 weight_set(p, weight(q));
407 /* Does it happen to be at exactly half-way? */
408 distance = weight(p);
409 if (halfway(p, distance, nr)) {
418 show_list("bisection 2 counted all", counted, nr, list);
420 /* Then find the best one */
423 for (p = list; p; p = p->next) {
424 unsigned flags = p->item->object.flags;
426 if (revs.prune_fn && !(flags & TREECHANGE))
428 distance = weight(p);
429 if (nr - distance < distance)
430 distance = nr - distance;
431 if (distance > counted) {
434 *reaches = weight(p);
443 static void read_revisions_from_stdin(struct rev_info *revs)
447 while (fgets(line, sizeof(line), stdin) != NULL) {
448 int len = strlen(line);
449 if (line[len - 1] == '\n')
454 die("options not supported in --stdin mode");
455 if (handle_revision_arg(line, revs, 0, 1))
456 die("bad revision '%s'", line);
460 int cmd_rev_list(int argc, const char **argv, const char *prefix)
462 struct commit_list *list;
464 int read_from_stdin = 0;
465 int bisect_show_vars = 0;
467 git_config(git_default_config);
468 init_revisions(&revs, prefix);
470 revs.commit_format = CMIT_FMT_UNSPECIFIED;
471 argc = setup_revisions(argc, argv, &revs, NULL);
473 for (i = 1 ; i < argc; i++) {
474 const char *arg = argv[i];
476 if (!strcmp(arg, "--header")) {
477 revs.verbose_header = 1;
480 if (!strcmp(arg, "--timestamp")) {
484 if (!strcmp(arg, "--bisect")) {
488 if (!strcmp(arg, "--bisect-vars")) {
490 bisect_show_vars = 1;
493 if (!strcmp(arg, "--stdin")) {
494 if (read_from_stdin++)
495 die("--stdin given twice?");
496 read_revisions_from_stdin(&revs);
499 usage(rev_list_usage);
502 if (revs.commit_format != CMIT_FMT_UNSPECIFIED) {
503 /* The command line has a --pretty */
504 hdr_termination = '\n';
505 if (revs.commit_format == CMIT_FMT_ONELINE)
508 header_prefix = "commit ";
510 else if (revs.verbose_header)
511 /* Only --header was specified */
512 revs.commit_format = CMIT_FMT_RAW;
517 (!(revs.tag_objects||revs.tree_objects||revs.blob_objects) &&
518 !revs.pending.nr)) ||
520 usage(rev_list_usage);
522 save_commit_buffer = revs.verbose_header || revs.grep_filter;
523 track_object_refs = 0;
527 prepare_revision_walk(&revs);
528 if (revs.tree_objects)
529 mark_edges_uninteresting(revs.commits, &revs, show_edge);
532 int reaches = reaches, all = all;
534 revs.commits = find_bisection(revs.commits, &reaches, &all);
535 if (bisect_show_vars) {
540 * revs.commits can reach "reaches" commits among
541 * "all" commits. If it is good, then there are
542 * (all-reaches) commits left to be bisected.
543 * On the other hand, if it is bad, then the set
544 * to bisect is "reaches".
545 * A bisect set of size N has (N-1) commits further
546 * to test, as we already know one bad one.
551 printf("bisect_rev=%s\n"
556 sha1_to_hex(revs.commits->item->object.sha1),
565 traverse_commit_list(&revs, show_commit, show_object);