rev-list --bisect: Move finding bisection into do_find_bisection.
[git/git.git] / builtin-rev-list.c
CommitLineData
64745109 1#include "cache.h"
e091eb93 2#include "refs.h"
36f8d174 3#include "tag.h"
64745109 4#include "commit.h"
9de48752
LT
5#include "tree.h"
6#include "blob.h"
1b0c7174 7#include "tree-walk.h"
c4e05b1a 8#include "diff.h"
ae563542 9#include "revision.h"
c64ed70d 10#include "list-objects.h"
5fb61b8d 11#include "builtin.h"
ae563542 12
1b65a5aa 13/* bits #0-15 in revision.h */
64745109 14
1b65a5aa 15#define COUNTED (1u<<16)
8906300f 16
a6f68d47 17static const char rev_list_usage[] =
69e0c256
JH
18"git-rev-list [OPTION] <commit-id>... [ -- paths... ]\n"
19" limiting output:\n"
20" --max-count=nr\n"
21" --max-age=epoch\n"
22" --min-age=epoch\n"
23" --sparse\n"
24" --no-merges\n"
93b74bca 25" --remove-empty\n"
69e0c256 26" --all\n"
42cabc34 27" --stdin\n"
69e0c256 28" ordering output:\n"
69e0c256 29" --topo-order\n"
4c8725f1 30" --date-order\n"
69e0c256
JH
31" formatting output:\n"
32" --parents\n"
c6496575 33" --objects | --objects-edge\n"
69e0c256
JH
34" --unpacked\n"
35" --header | --pretty\n"
9da5c2f0 36" --abbrev=nr | --no-abbrev\n"
5c51c985 37" --abbrev-commit\n"
b24bace5 38" --left-right\n"
69e0c256 39" special purpose:\n"
457f08a0
JH
40" --bisect\n"
41" --bisect-vars"
69e0c256 42;
a6f68d47 43
5fb61b8d 44static struct rev_info revs;
ae563542 45
96f1e58f
DR
46static int bisect_list;
47static int show_timestamp;
48static int hdr_termination;
91539833 49static const char *header_prefix;
e646de0d 50
81f2bb1f
LT
51static void show_commit(struct commit *commit)
52{
dc68c4ff
JH
53 if (show_timestamp)
54 printf("%lu ", commit->date);
91539833
LT
55 if (header_prefix)
56 fputs(header_prefix, stdout);
384e99a4
JH
57 if (commit->object.flags & BOUNDARY)
58 putchar('-');
74bd9029 59 else if (revs.left_right) {
577ed5c2
JH
60 if (commit->object.flags & SYMMETRIC_LEFT)
61 putchar('<');
62 else
63 putchar('>');
64 }
7594c4b2
JH
65 if (revs.abbrev_commit && revs.abbrev)
66 fputs(find_unique_abbrev(commit->object.sha1, revs.abbrev),
67 stdout);
5c51c985
JH
68 else
69 fputs(sha1_to_hex(commit->object.sha1), stdout);
7b0c9966 70 if (revs.parents) {
81f2bb1f
LT
71 struct commit_list *parents = commit->parents;
72 while (parents) {
1ed84157 73 printf(" %s", sha1_to_hex(parents->item->object.sha1));
81f2bb1f
LT
74 parents = parents->next;
75 }
76 }
7594c4b2 77 if (revs.commit_format == CMIT_FMT_ONELINE)
d87449c5
JH
78 putchar(' ');
79 else
80 putchar('\n');
81
7594c4b2 82 if (revs.verbose_header) {
80583c0e
JH
83 char *buf = NULL;
84 unsigned long buflen = 0;
7594c4b2 85 pretty_print_commit(revs.commit_format, commit, ~0,
80583c0e 86 &buf, &buflen,
a7b02ccf 87 revs.abbrev, NULL, NULL, revs.date_mode);
80583c0e
JH
88 printf("%s%c", buf, hdr_termination);
89 free(buf);
7620d39f 90 }
06f59e9f 91 maybe_flush_or_die(stdout, "stdout");
cb115748
LT
92 if (commit->parents) {
93 free_commit_list(commit->parents);
94 commit->parents = NULL;
95 }
4cac42b1
JH
96 free(commit->buffer);
97 commit->buffer = NULL;
a3437b8c
JS
98}
99
c64ed70d 100static void show_object(struct object_array_entry *p)
9de48752 101{
c64ed70d
JH
102 /* An object with name "foo\n0000000..." can be used to
103 * confuse downstream git-pack-objects very badly.
104 */
105 const char *ep = strchr(p->name, '\n');
b9849a1a
JH
106
107 if (p->item->type == OBJ_BLOB && !has_sha1_file(p->item->sha1))
108 die("missing blob object '%s'", sha1_to_hex(p->item->sha1));
109
c64ed70d
JH
110 if (ep) {
111 printf("%s %.*s\n", sha1_to_hex(p->item->sha1),
112 (int) (ep - p->name),
113 p->name);
9de48752 114 }
c64ed70d
JH
115 else
116 printf("%s %s\n", sha1_to_hex(p->item->sha1), p->name);
9de48752
LT
117}
118
8d1d8f83
JH
119static void show_edge(struct commit *commit)
120{
121 printf("-%s\n", sha1_to_hex(commit->object.sha1));
122}
123
8b3a1e05
LT
124/*
125 * This is a truly stupid algorithm, but it's only
126 * used for bisection, and we just don't care enough.
127 *
128 * We care just barely enough to avoid recursing for
129 * non-merge entries.
130 */
131static int count_distance(struct commit_list *entry)
132{
133 int nr = 0;
134
135 while (entry) {
136 struct commit *commit = entry->item;
137 struct commit_list *p;
138
139 if (commit->object.flags & (UNINTERESTING | COUNTED))
140 break;
8efdc326 141 if (!revs.prune_fn || (commit->object.flags & TREECHANGE))
b3cfd939 142 nr++;
8b3a1e05
LT
143 commit->object.flags |= COUNTED;
144 p = commit->parents;
145 entry = p;
146 if (p) {
147 p = p->next;
148 while (p) {
149 nr += count_distance(p);
150 p = p->next;
151 }
152 }
153 }
b3cfd939 154
8b3a1e05
LT
155 return nr;
156}
157
3d958064 158static void clear_distance(struct commit_list *list)
8b3a1e05
LT
159{
160 while (list) {
161 struct commit *commit = list->item;
162 commit->object.flags &= ~COUNTED;
163 list = list->next;
164 }
165}
166
1daa09d9 167#define DEBUG_BISECT 0
1c4fea3a
JH
168
169static inline int weight(struct commit_list *elem)
170{
171 return *((int*)(elem->item->util));
172}
173
174static inline void weight_set(struct commit_list *elem, int weight)
175{
176 *((int*)(elem->item->util)) = weight;
177}
178
1daa09d9 179static int count_interesting_parents(struct commit *commit)
1c4fea3a 180{
1daa09d9
JH
181 struct commit_list *p;
182 int count;
183
184 for (count = 0, p = commit->parents; p; p = p->next) {
185 if (p->item->object.flags & UNINTERESTING)
186 continue;
187 count++;
1c4fea3a 188 }
1daa09d9 189 return count;
1c4fea3a
JH
190}
191
2a464690
JH
192static inline int halfway(struct commit_list *p, int distance, int nr)
193{
194 /*
195 * Don't short-cut something we are not going to return!
196 */
197 if (revs.prune_fn && !(p->item->object.flags & TREECHANGE))
198 return 0;
1daa09d9
JH
199 if (DEBUG_BISECT)
200 return 0;
2a464690
JH
201 /*
202 * 2 and 3 are halfway of 5.
203 * 3 is halfway of 6 but 2 and 4 are not.
204 */
205 distance *= 2;
206 switch (distance - nr) {
207 case -1: case 0: case 1:
208 return 1;
209 default:
210 return 0;
211 }
212}
213
1daa09d9
JH
214#if !DEBUG_BISECT
215#define show_list(a,b,c,d) do { ; } while (0)
216#else
217static void show_list(const char *debug, int counted, int nr,
218 struct commit_list *list)
1c4fea3a 219{
1daa09d9
JH
220 struct commit_list *p;
221
222 fprintf(stderr, "%s (%d/%d)\n", debug, counted, nr);
223
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;
229 unsigned long size;
230 char *buf = read_sha1_file(commit->object.sha1, &type, &size);
231 char *ep, *sp;
232
233 fprintf(stderr, "%c%c%c ",
234 (flags & TREECHANGE) ? 'T' : ' ',
235 (flags & UNINTERESTING) ? 'U' : ' ',
236 (flags & COUNTED) ? 'C' : ' ');
237 if (commit->util)
238 fprintf(stderr, "%3d", weight(p));
239 else
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));
245
246 sp = strstr(buf, "\n\n");
247 if (sp) {
248 sp += 2;
249 for (ep = sp; *ep && *ep != '\n'; ep++)
250 ;
251 fprintf(stderr, " %.*s", (int)(ep - sp), sp);
252 }
253 fprintf(stderr, "\n");
254 }
255}
256#endif /* DEBUG_BISECT */
257
258/*
259 * zero or positive weight is the number of interesting commits it can
260 * reach, including itself. Especially, weight = 0 means it does not
261 * reach any tree-changing commits (e.g. just above uninteresting one
262 * but traversal is with pathspec).
263 *
264 * weight = -1 means it has one parent and its distance is yet to
265 * be computed.
266 *
267 * weight = -2 means it has more than one parent and its distance is
268 * unknown. After running count_distance() first, they will get zero
269 * or positive distance.
270 */
ce0cbad7
CC
271static struct commit_list *do_find_bisection(struct commit_list *list,
272 int nr, int *weights)
1daa09d9 273{
ce0cbad7
CC
274 int n, counted, distance;
275 struct commit_list *p, *best;
1daa09d9 276
1c4fea3a
JH
277 counted = 0;
278
279 for (n = 0, p = list; p; p = p->next) {
1daa09d9
JH
280 struct commit *commit = p->item;
281 unsigned flags = commit->object.flags;
282
283 p->item->util = &weights[n++];
284 switch (count_interesting_parents(commit)) {
285 case 0:
286 if (!revs.prune_fn || (flags & TREECHANGE)) {
1c4fea3a
JH
287 weight_set(p, 1);
288 counted++;
1daa09d9
JH
289 show_list("bisection 2 count one",
290 counted, nr, list);
1c4fea3a 291 }
1daa09d9
JH
292 /*
293 * otherwise, it is known not to reach any
294 * tree-changing commit and gets weight 0.
295 */
296 break;
297 case 1:
298 weight_set(p, -1);
299 break;
300 default:
301 weight_set(p, -2);
302 break;
1c4fea3a
JH
303 }
304 }
305
1daa09d9
JH
306 show_list("bisection 2 initialize", counted, nr, list);
307
1c4fea3a
JH
308 /*
309 * If you have only one parent in the resulting set
310 * then you can reach one commit more than that parent
311 * can reach. So we do not have to run the expensive
312 * count_distance() for single strand of pearls.
313 *
314 * However, if you have more than one parents, you cannot
315 * just add their distance and one for yourself, since
316 * they usually reach the same ancestor and you would
317 * end up counting them twice that way.
318 *
319 * So we will first count distance of merges the usual
320 * way, and then fill the blanks using cheaper algorithm.
321 */
322 for (p = list; p; p = p->next) {
1daa09d9 323 if (p->item->object.flags & UNINTERESTING)
1c4fea3a
JH
324 continue;
325 n = weight(p);
1daa09d9 326 if (n != -2)
1c4fea3a
JH
327 continue;
328 distance = count_distance(p);
1daa09d9 329 clear_distance(list);
1c4fea3a
JH
330 weight_set(p, distance);
331
332 /* Does it happen to be at exactly half-way? */
ce0cbad7 333 if (halfway(p, distance, nr))
1c4fea3a 334 return p;
1c4fea3a
JH
335 counted++;
336 }
337
1daa09d9
JH
338 show_list("bisection 2 count_distance", counted, nr, list);
339
1c4fea3a
JH
340 while (counted < nr) {
341 for (p = list; p; p = p->next) {
342 struct commit_list *q;
1daa09d9 343 unsigned flags = p->item->object.flags;
1c4fea3a 344
1daa09d9 345 if (0 <= weight(p))
1c4fea3a 346 continue;
1daa09d9
JH
347 for (q = p->item->parents; q; q = q->next) {
348 if (q->item->object.flags & UNINTERESTING)
349 continue;
350 if (0 <= weight(q))
1c4fea3a 351 break;
1daa09d9 352 }
1c4fea3a
JH
353 if (!q)
354 continue;
1daa09d9
JH
355
356 /*
357 * weight for p is unknown but q is known.
358 * add one for p itself if p is to be counted,
359 * otherwise inherit it from q directly.
360 */
361 if (!revs.prune_fn || (flags & TREECHANGE)) {
362 weight_set(p, weight(q)+1);
363 counted++;
364 show_list("bisection 2 count one",
365 counted, nr, list);
366 }
367 else
368 weight_set(p, weight(q));
1c4fea3a
JH
369
370 /* Does it happen to be at exactly half-way? */
2a464690 371 distance = weight(p);
ce0cbad7 372 if (halfway(p, distance, nr))
1c4fea3a 373 return p;
1c4fea3a
JH
374 }
375 }
376
1daa09d9
JH
377 show_list("bisection 2 counted all", counted, nr, list);
378
1c4fea3a 379 /* Then find the best one */
1daa09d9 380 counted = -1;
1c4fea3a
JH
381 best = list;
382 for (p = list; p; p = p->next) {
1daa09d9
JH
383 unsigned flags = p->item->object.flags;
384
385 if (revs.prune_fn && !(flags & TREECHANGE))
1c4fea3a
JH
386 continue;
387 distance = weight(p);
388 if (nr - distance < distance)
389 distance = nr - distance;
390 if (distance > counted) {
391 best = p;
392 counted = distance;
1c4fea3a
JH
393 }
394 }
ce0cbad7
CC
395 return best;
396}
397
398static struct commit_list *find_bisection(struct commit_list *list,
399 int *reaches, int *all)
400{
401 int nr, on_list;
402 struct commit_list *p, *best, *next, *last;
403 int *weights;
404
405 show_list("bisection 2 entry", 0, 0, list);
406
407 /*
408 * Count the number of total and tree-changing items on the
409 * list, while reversing the list.
410 */
411 for (nr = on_list = 0, last = NULL, p = list;
412 p;
413 p = next) {
414 unsigned flags = p->item->object.flags;
415
416 next = p->next;
417 if (flags & UNINTERESTING)
418 continue;
419 p->next = last;
420 last = p;
421 if (!revs.prune_fn || (flags & TREECHANGE))
422 nr++;
423 on_list++;
424 }
425 list = last;
426 show_list("bisection 2 sorted", 0, nr, list);
427
428 *all = nr;
429 weights = xcalloc(on_list, sizeof(*weights));
430
431 /* Do the real work of finding bisection commit. */
432 best = do_find_bisection(list, nr, weights);
433
1c4fea3a
JH
434 if (best)
435 best->next = NULL;
ce0cbad7
CC
436
437 *reaches = weight(best);
1c4fea3a 438 free(weights);
ce0cbad7 439
1c4fea3a
JH
440 return best;
441}
442
42cabc34
JH
443static void read_revisions_from_stdin(struct rev_info *revs)
444{
445 char line[1000];
446
447 while (fgets(line, sizeof(line), stdin) != NULL) {
448 int len = strlen(line);
449 if (line[len - 1] == '\n')
450 line[--len] = 0;
451 if (!len)
452 break;
453 if (line[0] == '-')
454 die("options not supported in --stdin mode");
455 if (handle_revision_arg(line, revs, 0, 1))
456 die("bad revision '%s'", line);
457 }
458}
459
a633fca0 460int cmd_rev_list(int argc, const char **argv, const char *prefix)
64745109 461{
ae563542 462 struct commit_list *list;
d9a83684 463 int i;
42cabc34 464 int read_from_stdin = 0;
457f08a0 465 int bisect_show_vars = 0;
64745109 466
256c3fe6 467 git_config(git_default_config);
a633fca0 468 init_revisions(&revs, prefix);
7594c4b2
JH
469 revs.abbrev = 0;
470 revs.commit_format = CMIT_FMT_UNSPECIFIED;
a4a88b2b 471 argc = setup_revisions(argc, argv, &revs, NULL);
ae563542 472
fcfda02b 473 for (i = 1 ; i < argc; i++) {
cf484544 474 const char *arg = argv[i];
fcfda02b 475
a6f68d47 476 if (!strcmp(arg, "--header")) {
7594c4b2 477 revs.verbose_header = 1;
9d97aa64
LT
478 continue;
479 }
dc68c4ff
JH
480 if (!strcmp(arg, "--timestamp")) {
481 show_timestamp = 1;
482 continue;
483 }
8b3a1e05
LT
484 if (!strcmp(arg, "--bisect")) {
485 bisect_list = 1;
486 continue;
487 }
457f08a0
JH
488 if (!strcmp(arg, "--bisect-vars")) {
489 bisect_list = 1;
490 bisect_show_vars = 1;
491 continue;
492 }
42cabc34
JH
493 if (!strcmp(arg, "--stdin")) {
494 if (read_from_stdin++)
495 die("--stdin given twice?");
496 read_revisions_from_stdin(&revs);
497 continue;
498 }
ae563542 499 usage(rev_list_usage);
a6f68d47 500
fcfda02b 501 }
7594c4b2
JH
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)
91539833 506 header_prefix = "";
7594c4b2 507 else
91539833 508 header_prefix = "commit ";
7594c4b2 509 }
db89665f
JH
510 else if (revs.verbose_header)
511 /* Only --header was specified */
512 revs.commit_format = CMIT_FMT_RAW;
fcfda02b 513
ae563542 514 list = revs.commits;
ae563542 515
8c1f0b44
JH
516 if ((!list &&
517 (!(revs.tag_objects||revs.tree_objects||revs.blob_objects) &&
1f1e895f 518 !revs.pending.nr)) ||
8c1f0b44 519 revs.diff)
7b34c2fa
LT
520 usage(rev_list_usage);
521
2d10c555 522 save_commit_buffer = revs.verbose_header || revs.grep_filter;
9181ca2c 523 track_object_refs = 0;
4e1dc640
JH
524 if (bisect_list)
525 revs.limited = 1;
9181ca2c 526
a4a88b2b
LT
527 prepare_revision_walk(&revs);
528 if (revs.tree_objects)
8d1d8f83 529 mark_edges_uninteresting(revs.commits, &revs, show_edge);
a4a88b2b 530
457f08a0
JH
531 if (bisect_list) {
532 int reaches = reaches, all = all;
533
1daa09d9 534 revs.commits = find_bisection(revs.commits, &reaches, &all);
457f08a0
JH
535 if (bisect_show_vars) {
536 int cnt;
537 if (!revs.commits)
538 return 1;
539 /*
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.
547 */
548 cnt = all-reaches;
549 if (cnt < reaches)
550 cnt = reaches;
551 printf("bisect_rev=%s\n"
552 "bisect_nr=%d\n"
553 "bisect_good=%d\n"
554 "bisect_bad=%d\n"
555 "bisect_all=%d\n",
556 sha1_to_hex(revs.commits->item->object.sha1),
557 cnt - 1,
558 all - reaches - 1,
559 reaches - 1,
560 all);
561 return 0;
562 }
563 }
7b34c2fa 564
c64ed70d 565 traverse_commit_list(&revs, show_commit, show_object);
8906300f 566
64745109
LT
567 return 0;
568}