rev-list --bisect: Fix "halfway" optimization.
[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"
69e0c256 38" special purpose:\n"
457f08a0
JH
39" --bisect\n"
40" --bisect-vars"
69e0c256 41;
a6f68d47 42
5fb61b8d 43static struct rev_info revs;
ae563542 44
96f1e58f
DR
45static int bisect_list;
46static int show_timestamp;
47static int hdr_termination;
91539833 48static const char *header_prefix;
e646de0d 49
81f2bb1f
LT
50static void show_commit(struct commit *commit)
51{
dc68c4ff
JH
52 if (show_timestamp)
53 printf("%lu ", commit->date);
91539833
LT
54 if (header_prefix)
55 fputs(header_prefix, stdout);
384e99a4
JH
56 if (commit->object.flags & BOUNDARY)
57 putchar('-');
74bd9029 58 else if (revs.left_right) {
577ed5c2
JH
59 if (commit->object.flags & SYMMETRIC_LEFT)
60 putchar('<');
61 else
62 putchar('>');
63 }
7594c4b2
JH
64 if (revs.abbrev_commit && revs.abbrev)
65 fputs(find_unique_abbrev(commit->object.sha1, revs.abbrev),
66 stdout);
5c51c985
JH
67 else
68 fputs(sha1_to_hex(commit->object.sha1), stdout);
7b0c9966 69 if (revs.parents) {
81f2bb1f
LT
70 struct commit_list *parents = commit->parents;
71 while (parents) {
88494423 72 struct object *o = &(parents->item->object);
81f2bb1f 73 parents = parents->next;
88494423
JH
74 if (o->flags & TMP_MARK)
75 continue;
76 printf(" %s", sha1_to_hex(o->sha1));
77 o->flags |= TMP_MARK;
81f2bb1f 78 }
88494423
JH
79 /* TMP_MARK is a general purpose flag that can
80 * be used locally, but the user should clean
81 * things up after it is done with them.
82 */
83 for (parents = commit->parents;
84 parents;
85 parents = parents->next)
86 parents->item->object.flags &= ~TMP_MARK;
81f2bb1f 87 }
7594c4b2 88 if (revs.commit_format == CMIT_FMT_ONELINE)
d87449c5
JH
89 putchar(' ');
90 else
91 putchar('\n');
92
7594c4b2 93 if (revs.verbose_header) {
000182ea 94 static char pretty_header[16384];
7594c4b2
JH
95 pretty_print_commit(revs.commit_format, commit, ~0,
96 pretty_header, sizeof(pretty_header),
3dfb9278 97 revs.abbrev, NULL, NULL, revs.relative_date);
000182ea 98 printf("%s%c", pretty_header, hdr_termination);
7620d39f
LT
99 }
100 fflush(stdout);
cb115748
LT
101 if (commit->parents) {
102 free_commit_list(commit->parents);
103 commit->parents = NULL;
104 }
4cac42b1
JH
105 free(commit->buffer);
106 commit->buffer = NULL;
a3437b8c
JS
107}
108
c64ed70d 109static void show_object(struct object_array_entry *p)
9de48752 110{
c64ed70d
JH
111 /* An object with name "foo\n0000000..." can be used to
112 * confuse downstream git-pack-objects very badly.
113 */
114 const char *ep = strchr(p->name, '\n');
115 if (ep) {
116 printf("%s %.*s\n", sha1_to_hex(p->item->sha1),
117 (int) (ep - p->name),
118 p->name);
9de48752 119 }
c64ed70d
JH
120 else
121 printf("%s %s\n", sha1_to_hex(p->item->sha1), p->name);
9de48752
LT
122}
123
8d1d8f83
JH
124static void show_edge(struct commit *commit)
125{
126 printf("-%s\n", sha1_to_hex(commit->object.sha1));
127}
128
8b3a1e05
LT
129/*
130 * This is a truly stupid algorithm, but it's only
131 * used for bisection, and we just don't care enough.
132 *
133 * We care just barely enough to avoid recursing for
134 * non-merge entries.
135 */
136static int count_distance(struct commit_list *entry)
137{
138 int nr = 0;
139
140 while (entry) {
141 struct commit *commit = entry->item;
142 struct commit_list *p;
143
144 if (commit->object.flags & (UNINTERESTING | COUNTED))
145 break;
8efdc326 146 if (!revs.prune_fn || (commit->object.flags & TREECHANGE))
b3cfd939 147 nr++;
8b3a1e05
LT
148 commit->object.flags |= COUNTED;
149 p = commit->parents;
150 entry = p;
151 if (p) {
152 p = p->next;
153 while (p) {
154 nr += count_distance(p);
155 p = p->next;
156 }
157 }
158 }
b3cfd939 159
8b3a1e05
LT
160 return nr;
161}
162
3d958064 163static void clear_distance(struct commit_list *list)
8b3a1e05
LT
164{
165 while (list) {
166 struct commit *commit = list->item;
167 commit->object.flags &= ~COUNTED;
168 list = list->next;
169 }
170}
171
457f08a0
JH
172static struct commit_list *find_bisection(struct commit_list *list,
173 int *reaches, int *all)
8b3a1e05
LT
174{
175 int nr, closest;
176 struct commit_list *p, *best;
177
178 nr = 0;
179 p = list;
180 while (p) {
8efdc326 181 if (!revs.prune_fn || (p->item->object.flags & TREECHANGE))
b3cfd939 182 nr++;
8b3a1e05
LT
183 p = p->next;
184 }
a4e9d71e 185 closest = -1;
8b3a1e05 186 best = list;
1c2c6112 187 *all = nr;
8b3a1e05 188
b3cfd939 189 for (p = list; p; p = p->next) {
457f08a0 190 int distance, reach;
b3cfd939 191
8efdc326 192 if (revs.prune_fn && !(p->item->object.flags & TREECHANGE))
b3cfd939
LT
193 continue;
194
457f08a0 195 distance = reach = count_distance(p);
8b3a1e05
LT
196 clear_distance(list);
197 if (nr - distance < distance)
198 distance = nr - distance;
199 if (distance > closest) {
200 best = p;
457f08a0 201 *reaches = reach;
8b3a1e05
LT
202 closest = distance;
203 }
8b3a1e05
LT
204 }
205 if (best)
206 best->next = NULL;
207 return best;
208}
209
1c4fea3a
JH
210static inline int commit_interesting(struct commit_list *elem)
211{
212 unsigned flags = elem->item->object.flags;
213 if (flags & UNINTERESTING)
214 return 0;
215 return (!revs.prune_fn || (flags & TREECHANGE));
216}
217
218static inline int weight(struct commit_list *elem)
219{
220 return *((int*)(elem->item->util));
221}
222
223static inline void weight_set(struct commit_list *elem, int weight)
224{
225 *((int*)(elem->item->util)) = weight;
226}
227
228static int count_interesting_parents(struct commit_list *elem)
229{
230 int cnt = 0;
231 if (!elem->item->parents)
232 return cnt;
233 for (elem = elem->item->parents; elem; elem = elem->next) {
234 if (commit_interesting(elem))
235 cnt++;
236 }
237 return cnt;
238}
239
2a464690
JH
240static inline int halfway(struct commit_list *p, int distance, int nr)
241{
242 /*
243 * Don't short-cut something we are not going to return!
244 */
245 if (revs.prune_fn && !(p->item->object.flags & TREECHANGE))
246 return 0;
247
248 /*
249 * 2 and 3 are halfway of 5.
250 * 3 is halfway of 6 but 2 and 4 are not.
251 */
252 distance *= 2;
253 switch (distance - nr) {
254 case -1: case 0: case 1:
255 return 1;
256 default:
257 return 0;
258 }
259}
260
1c4fea3a
JH
261static struct commit_list *find_bisection_2(struct commit_list *list,
262 int *reaches, int *all)
263{
264 int n, nr, counted, distance;
265 struct commit_list *p, *best;
266 int *weights;
267
268 for (nr = 0, p = list; p; p = p->next) {
269 if (commit_interesting(p))
270 nr++;
271 }
272 *all = nr;
273 weights = xcalloc(nr, sizeof(int*));
274 counted = 0;
275
276 for (n = 0, p = list; p; p = p->next) {
277 if (!commit_interesting(p))
278 continue;
279 if (commit_interesting(p)) {
280 /*
281 * positive weight is the number of interesting
282 * commits it can reach, including itself.
283 * weight = 0 means it has one parent and
284 * its distance is unknown.
285 * weight < 0 means it has more than one
286 * parent and its distance is unknown.
287 */
288 p->item->util = &weights[n++];
289 switch (count_interesting_parents(p)) {
290 case 0:
291 weight_set(p, 1);
292 counted++;
293 break;
294 case 1:
295 weight_set(p, 0);
296 break;
297 default:
298 weight_set(p, -1);
299 break;
300 }
301 }
302 }
303
304 /*
305 * If you have only one parent in the resulting set
306 * then you can reach one commit more than that parent
307 * can reach. So we do not have to run the expensive
308 * count_distance() for single strand of pearls.
309 *
310 * However, if you have more than one parents, you cannot
311 * just add their distance and one for yourself, since
312 * they usually reach the same ancestor and you would
313 * end up counting them twice that way.
314 *
315 * So we will first count distance of merges the usual
316 * way, and then fill the blanks using cheaper algorithm.
317 */
318 for (p = list; p; p = p->next) {
319 if (!commit_interesting(p))
320 continue;
321 n = weight(p);
322 if (0 <= n)
323 continue;
324 distance = count_distance(p);
325 clear_distance(p);
326 weight_set(p, distance);
327
328 /* Does it happen to be at exactly half-way? */
2a464690 329 if (halfway(p, distance, nr)) {
1c4fea3a 330 p->next = NULL;
2a464690 331 *reaches = distance;
1c4fea3a
JH
332 free(weights);
333 return p;
334 }
335 counted++;
336 }
337
338 while (counted < nr) {
339 for (p = list; p; p = p->next) {
340 struct commit_list *q;
341
342 if (!commit_interesting(p) || 0 < weight(p))
343 continue;
344 for (q = p->item->parents; q; q = q->next)
345 if (commit_interesting(q) && 0 < weight(q))
346 break;
347 if (!q)
348 continue;
349 weight_set(p, weight(q)+1);
350 counted++;
351
352 /* Does it happen to be at exactly half-way? */
2a464690
JH
353 distance = weight(p);
354 if (halfway(p, distance, nr)) {
1c4fea3a 355 p->next = NULL;
2a464690 356 *reaches = distance;
1c4fea3a
JH
357 free(weights);
358 return p;
359 }
360 }
361 }
362
363 /* Then find the best one */
364 counted = 0;
365 best = list;
366 for (p = list; p; p = p->next) {
367 if (!commit_interesting(p))
368 continue;
369 distance = weight(p);
370 if (nr - distance < distance)
371 distance = nr - distance;
372 if (distance > counted) {
373 best = p;
374 counted = distance;
375 *reaches = weight(p);
376 }
377 }
378 if (best)
379 best->next = NULL;
380 free(weights);
381 return best;
382}
383
42cabc34
JH
384static void read_revisions_from_stdin(struct rev_info *revs)
385{
386 char line[1000];
387
388 while (fgets(line, sizeof(line), stdin) != NULL) {
389 int len = strlen(line);
390 if (line[len - 1] == '\n')
391 line[--len] = 0;
392 if (!len)
393 break;
394 if (line[0] == '-')
395 die("options not supported in --stdin mode");
396 if (handle_revision_arg(line, revs, 0, 1))
397 die("bad revision '%s'", line);
398 }
399}
400
a633fca0 401int cmd_rev_list(int argc, const char **argv, const char *prefix)
64745109 402{
ae563542 403 struct commit_list *list;
d9a83684 404 int i;
42cabc34 405 int read_from_stdin = 0;
457f08a0 406 int bisect_show_vars = 0;
64745109 407
256c3fe6 408 git_config(git_default_config);
a633fca0 409 init_revisions(&revs, prefix);
7594c4b2
JH
410 revs.abbrev = 0;
411 revs.commit_format = CMIT_FMT_UNSPECIFIED;
a4a88b2b 412 argc = setup_revisions(argc, argv, &revs, NULL);
ae563542 413
fcfda02b 414 for (i = 1 ; i < argc; i++) {
cf484544 415 const char *arg = argv[i];
fcfda02b 416
a6f68d47 417 if (!strcmp(arg, "--header")) {
7594c4b2 418 revs.verbose_header = 1;
9d97aa64
LT
419 continue;
420 }
dc68c4ff
JH
421 if (!strcmp(arg, "--timestamp")) {
422 show_timestamp = 1;
423 continue;
424 }
8b3a1e05
LT
425 if (!strcmp(arg, "--bisect")) {
426 bisect_list = 1;
427 continue;
428 }
457f08a0
JH
429 if (!strcmp(arg, "--bisect-vars")) {
430 bisect_list = 1;
431 bisect_show_vars = 1;
432 continue;
433 }
42cabc34
JH
434 if (!strcmp(arg, "--stdin")) {
435 if (read_from_stdin++)
436 die("--stdin given twice?");
437 read_revisions_from_stdin(&revs);
438 continue;
439 }
ae563542 440 usage(rev_list_usage);
a6f68d47 441
fcfda02b 442 }
7594c4b2
JH
443 if (revs.commit_format != CMIT_FMT_UNSPECIFIED) {
444 /* The command line has a --pretty */
445 hdr_termination = '\n';
446 if (revs.commit_format == CMIT_FMT_ONELINE)
91539833 447 header_prefix = "";
7594c4b2 448 else
91539833 449 header_prefix = "commit ";
7594c4b2 450 }
db89665f
JH
451 else if (revs.verbose_header)
452 /* Only --header was specified */
453 revs.commit_format = CMIT_FMT_RAW;
fcfda02b 454
ae563542 455 list = revs.commits;
ae563542 456
8c1f0b44
JH
457 if ((!list &&
458 (!(revs.tag_objects||revs.tree_objects||revs.blob_objects) &&
1f1e895f 459 !revs.pending.nr)) ||
8c1f0b44 460 revs.diff)
7b34c2fa
LT
461 usage(rev_list_usage);
462
2d10c555 463 save_commit_buffer = revs.verbose_header || revs.grep_filter;
9181ca2c 464 track_object_refs = 0;
4e1dc640
JH
465 if (bisect_list)
466 revs.limited = 1;
9181ca2c 467
a4a88b2b
LT
468 prepare_revision_walk(&revs);
469 if (revs.tree_objects)
8d1d8f83 470 mark_edges_uninteresting(revs.commits, &revs, show_edge);
a4a88b2b 471
457f08a0
JH
472 if (bisect_list) {
473 int reaches = reaches, all = all;
474
1c4fea3a
JH
475 if (!revs.prune_fn)
476 revs.commits = find_bisection_2(revs.commits,
477 &reaches, &all);
478 else
479 revs.commits = find_bisection(revs.commits,
480 &reaches, &all);
457f08a0
JH
481 if (bisect_show_vars) {
482 int cnt;
483 if (!revs.commits)
484 return 1;
485 /*
486 * revs.commits can reach "reaches" commits among
487 * "all" commits. If it is good, then there are
488 * (all-reaches) commits left to be bisected.
489 * On the other hand, if it is bad, then the set
490 * to bisect is "reaches".
491 * A bisect set of size N has (N-1) commits further
492 * to test, as we already know one bad one.
493 */
494 cnt = all-reaches;
495 if (cnt < reaches)
496 cnt = reaches;
497 printf("bisect_rev=%s\n"
498 "bisect_nr=%d\n"
499 "bisect_good=%d\n"
500 "bisect_bad=%d\n"
501 "bisect_all=%d\n",
502 sha1_to_hex(revs.commits->item->object.sha1),
503 cnt - 1,
504 all - reaches - 1,
505 reaches - 1,
506 all);
507 return 0;
508 }
509 }
7b34c2fa 510
c64ed70d 511 traverse_commit_list(&revs, show_commit, show_object);
8906300f 512
64745109
LT
513 return 0;
514}