t/: Use "test_must_fail git" instead of "! git"
[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"
50e62a8e 12#include "log-tree.h"
7fefda5c 13#include "graph.h"
ae563542 14
1b65a5aa 15/* bits #0-15 in revision.h */
64745109 16
1b65a5aa 17#define COUNTED (1u<<16)
8906300f 18
a6f68d47 19static const char rev_list_usage[] =
69e0c256
JH
20"git-rev-list [OPTION] <commit-id>... [ -- paths... ]\n"
21" limiting output:\n"
22" --max-count=nr\n"
23" --max-age=epoch\n"
24" --min-age=epoch\n"
25" --sparse\n"
26" --no-merges\n"
93b74bca 27" --remove-empty\n"
69e0c256 28" --all\n"
a5aa930d
UKK
29" --branches\n"
30" --tags\n"
31" --remotes\n"
42cabc34 32" --stdin\n"
27350891 33" --quiet\n"
69e0c256 34" ordering output:\n"
69e0c256 35" --topo-order\n"
4c8725f1 36" --date-order\n"
7ccd3667 37" --reverse\n"
69e0c256
JH
38" formatting output:\n"
39" --parents\n"
c6496575 40" --objects | --objects-edge\n"
69e0c256
JH
41" --unpacked\n"
42" --header | --pretty\n"
9da5c2f0 43" --abbrev=nr | --no-abbrev\n"
5c51c985 44" --abbrev-commit\n"
b24bace5 45" --left-right\n"
69e0c256 46" special purpose:\n"
457f08a0 47" --bisect\n"
50e62a8e
CC
48" --bisect-vars\n"
49" --bisect-all"
69e0c256 50;
a6f68d47 51
5fb61b8d 52static struct rev_info revs;
ae563542 53
96f1e58f
DR
54static int bisect_list;
55static int show_timestamp;
56static int hdr_termination;
91539833 57static const char *header_prefix;
e646de0d 58
27350891 59static void finish_commit(struct commit *commit);
81f2bb1f
LT
60static void show_commit(struct commit *commit)
61{
7fefda5c
AS
62 graph_show_commit(revs.graph);
63
dc68c4ff
JH
64 if (show_timestamp)
65 printf("%lu ", commit->date);
91539833
LT
66 if (header_prefix)
67 fputs(header_prefix, stdout);
7528f27d
AS
68
69 if (!revs.graph) {
70 if (commit->object.flags & BOUNDARY)
71 putchar('-');
72 else if (commit->object.flags & UNINTERESTING)
73 putchar('^');
74 else if (revs.left_right) {
75 if (commit->object.flags & SYMMETRIC_LEFT)
76 putchar('<');
77 else
78 putchar('>');
79 }
577ed5c2 80 }
7594c4b2
JH
81 if (revs.abbrev_commit && revs.abbrev)
82 fputs(find_unique_abbrev(commit->object.sha1, revs.abbrev),
83 stdout);
5c51c985
JH
84 else
85 fputs(sha1_to_hex(commit->object.sha1), stdout);
885cf808 86 if (revs.print_parents) {
81f2bb1f
LT
87 struct commit_list *parents = commit->parents;
88 while (parents) {
1ed84157 89 printf(" %s", sha1_to_hex(parents->item->object.sha1));
81f2bb1f
LT
90 parents = parents->next;
91 }
92 }
50e62a8e 93 show_decorations(commit);
7594c4b2 94 if (revs.commit_format == CMIT_FMT_ONELINE)
d87449c5
JH
95 putchar(' ');
96 else
97 putchar('\n');
98
3131b713 99 if (revs.verbose_header && commit->buffer) {
674d1727
PH
100 struct strbuf buf;
101 strbuf_init(&buf, 0);
102 pretty_print_commit(revs.commit_format, commit,
4593fb84
JH
103 &buf, revs.abbrev, NULL, NULL,
104 revs.date_mode, 0);
7fefda5c
AS
105 if (revs.graph) {
106 if (buf.len) {
107 if (revs.commit_format != CMIT_FMT_ONELINE)
108 graph_show_oneline(revs.graph);
109
110 graph_show_commit_msg(revs.graph, &buf);
111
112 /*
113 * Add a newline after the commit message.
114 *
115 * Usually, this newline produces a blank
116 * padding line between entries, in which case
117 * we need to add graph padding on this line.
118 *
119 * However, the commit message may not end in a
120 * newline. In this case the newline simply
121 * ends the last line of the commit message,
122 * and we don't need any graph output. (This
123 * always happens with CMIT_FMT_ONELINE, and it
124 * happens with CMIT_FMT_USERFORMAT when the
125 * format doesn't explicitly end in a newline.)
126 */
127 if (buf.len && buf.buf[buf.len - 1] == '\n')
128 graph_show_padding(revs.graph);
129 putchar('\n');
130 } else {
131 /*
132 * If the message buffer is empty, just show
133 * the rest of the graph output for this
134 * commit.
135 */
136 if (graph_show_remainder(revs.graph))
137 putchar('\n');
138 }
139 } else {
140 if (buf.len)
141 printf("%s%c", buf.buf, hdr_termination);
142 }
674d1727 143 strbuf_release(&buf);
7fefda5c
AS
144 } else {
145 if (graph_show_remainder(revs.graph))
146 putchar('\n');
7620d39f 147 }
06f59e9f 148 maybe_flush_or_die(stdout, "stdout");
27350891
SP
149 finish_commit(commit);
150}
151
152static void finish_commit(struct commit *commit)
153{
cb115748
LT
154 if (commit->parents) {
155 free_commit_list(commit->parents);
156 commit->parents = NULL;
157 }
4cac42b1
JH
158 free(commit->buffer);
159 commit->buffer = NULL;
a3437b8c
JS
160}
161
27350891
SP
162static void finish_object(struct object_array_entry *p)
163{
164 if (p->item->type == OBJ_BLOB && !has_sha1_file(p->item->sha1))
165 die("missing blob object '%s'", sha1_to_hex(p->item->sha1));
166}
167
c64ed70d 168static void show_object(struct object_array_entry *p)
9de48752 169{
c64ed70d
JH
170 /* An object with name "foo\n0000000..." can be used to
171 * confuse downstream git-pack-objects very badly.
172 */
173 const char *ep = strchr(p->name, '\n');
b9849a1a 174
27350891 175 finish_object(p);
c64ed70d
JH
176 if (ep) {
177 printf("%s %.*s\n", sha1_to_hex(p->item->sha1),
178 (int) (ep - p->name),
179 p->name);
9de48752 180 }
c64ed70d
JH
181 else
182 printf("%s %s\n", sha1_to_hex(p->item->sha1), p->name);
9de48752
LT
183}
184
8d1d8f83
JH
185static void show_edge(struct commit *commit)
186{
187 printf("-%s\n", sha1_to_hex(commit->object.sha1));
188}
189
8b3a1e05
LT
190/*
191 * This is a truly stupid algorithm, but it's only
192 * used for bisection, and we just don't care enough.
193 *
194 * We care just barely enough to avoid recursing for
195 * non-merge entries.
196 */
197static int count_distance(struct commit_list *entry)
198{
199 int nr = 0;
200
201 while (entry) {
202 struct commit *commit = entry->item;
203 struct commit_list *p;
204
205 if (commit->object.flags & (UNINTERESTING | COUNTED))
206 break;
7dc0fe3b 207 if (!(commit->object.flags & TREESAME))
b3cfd939 208 nr++;
8b3a1e05
LT
209 commit->object.flags |= COUNTED;
210 p = commit->parents;
211 entry = p;
212 if (p) {
213 p = p->next;
214 while (p) {
215 nr += count_distance(p);
216 p = p->next;
217 }
218 }
219 }
b3cfd939 220
8b3a1e05
LT
221 return nr;
222}
223
3d958064 224static void clear_distance(struct commit_list *list)
8b3a1e05
LT
225{
226 while (list) {
227 struct commit *commit = list->item;
228 commit->object.flags &= ~COUNTED;
229 list = list->next;
230 }
231}
232
1daa09d9 233#define DEBUG_BISECT 0
1c4fea3a
JH
234
235static inline int weight(struct commit_list *elem)
236{
237 return *((int*)(elem->item->util));
238}
239
240static inline void weight_set(struct commit_list *elem, int weight)
241{
242 *((int*)(elem->item->util)) = weight;
243}
244
1daa09d9 245static int count_interesting_parents(struct commit *commit)
1c4fea3a 246{
1daa09d9
JH
247 struct commit_list *p;
248 int count;
249
250 for (count = 0, p = commit->parents; p; p = p->next) {
251 if (p->item->object.flags & UNINTERESTING)
252 continue;
253 count++;
1c4fea3a 254 }
1daa09d9 255 return count;
1c4fea3a
JH
256}
257
53271411 258static inline int halfway(struct commit_list *p, int nr)
2a464690
JH
259{
260 /*
261 * Don't short-cut something we are not going to return!
262 */
7dc0fe3b 263 if (p->item->object.flags & TREESAME)
2a464690 264 return 0;
1daa09d9
JH
265 if (DEBUG_BISECT)
266 return 0;
2a464690
JH
267 /*
268 * 2 and 3 are halfway of 5.
269 * 3 is halfway of 6 but 2 and 4 are not.
270 */
53271411 271 switch (2 * weight(p) - nr) {
2a464690
JH
272 case -1: case 0: case 1:
273 return 1;
274 default:
275 return 0;
276 }
277}
278
1daa09d9
JH
279#if !DEBUG_BISECT
280#define show_list(a,b,c,d) do { ; } while (0)
281#else
282static void show_list(const char *debug, int counted, int nr,
283 struct commit_list *list)
1c4fea3a 284{
1daa09d9
JH
285 struct commit_list *p;
286
287 fprintf(stderr, "%s (%d/%d)\n", debug, counted, nr);
288
289 for (p = list; p; p = p->next) {
290 struct commit_list *pp;
291 struct commit *commit = p->item;
292 unsigned flags = commit->object.flags;
293 enum object_type type;
294 unsigned long size;
295 char *buf = read_sha1_file(commit->object.sha1, &type, &size);
296 char *ep, *sp;
297
298 fprintf(stderr, "%c%c%c ",
7dc0fe3b 299 (flags & TREESAME) ? ' ' : 'T',
1daa09d9
JH
300 (flags & UNINTERESTING) ? 'U' : ' ',
301 (flags & COUNTED) ? 'C' : ' ');
302 if (commit->util)
303 fprintf(stderr, "%3d", weight(p));
304 else
305 fprintf(stderr, "---");
306 fprintf(stderr, " %.*s", 8, sha1_to_hex(commit->object.sha1));
307 for (pp = commit->parents; pp; pp = pp->next)
308 fprintf(stderr, " %.*s", 8,
309 sha1_to_hex(pp->item->object.sha1));
310
311 sp = strstr(buf, "\n\n");
312 if (sp) {
313 sp += 2;
314 for (ep = sp; *ep && *ep != '\n'; ep++)
315 ;
316 fprintf(stderr, " %.*s", (int)(ep - sp), sp);
317 }
318 fprintf(stderr, "\n");
319 }
320}
321#endif /* DEBUG_BISECT */
322
77c11e06
CC
323static struct commit_list *best_bisection(struct commit_list *list, int nr)
324{
325 struct commit_list *p, *best;
326 int best_distance = -1;
327
328 best = list;
329 for (p = list; p; p = p->next) {
330 int distance;
331 unsigned flags = p->item->object.flags;
332
7dc0fe3b 333 if (flags & TREESAME)
77c11e06
CC
334 continue;
335 distance = weight(p);
336 if (nr - distance < distance)
337 distance = nr - distance;
338 if (distance > best_distance) {
339 best = p;
340 best_distance = distance;
341 }
342 }
343
344 return best;
345}
346
50e62a8e
CC
347struct commit_dist {
348 struct commit *commit;
349 int distance;
350};
351
352static int compare_commit_dist(const void *a_, const void *b_)
353{
354 struct commit_dist *a, *b;
355
356 a = (struct commit_dist *)a_;
357 b = (struct commit_dist *)b_;
358 if (a->distance != b->distance)
359 return b->distance - a->distance; /* desc sort */
360 return hashcmp(a->commit->object.sha1, b->commit->object.sha1);
361}
362
363static struct commit_list *best_bisection_sorted(struct commit_list *list, int nr)
364{
365 struct commit_list *p;
366 struct commit_dist *array = xcalloc(nr, sizeof(*array));
367 int cnt, i;
368
369 for (p = list, cnt = 0; p; p = p->next) {
370 int distance;
371 unsigned flags = p->item->object.flags;
372
7dc0fe3b 373 if (flags & TREESAME)
50e62a8e
CC
374 continue;
375 distance = weight(p);
376 if (nr - distance < distance)
377 distance = nr - distance;
378 array[cnt].commit = p->item;
379 array[cnt].distance = distance;
380 cnt++;
381 }
382 qsort(array, cnt, sizeof(*array), compare_commit_dist);
383 for (p = list, i = 0; i < cnt; i++) {
384 struct name_decoration *r = xmalloc(sizeof(*r) + 100);
385 struct object *obj = &(array[i].commit->object);
386
387 sprintf(r->name, "dist=%d", array[i].distance);
388 r->next = add_decoration(&name_decoration, obj, r);
389 p->item = array[i].commit;
390 p = p->next;
391 }
392 if (p)
393 p->next = NULL;
394 free(array);
395 return list;
396}
397
1daa09d9
JH
398/*
399 * zero or positive weight is the number of interesting commits it can
400 * reach, including itself. Especially, weight = 0 means it does not
401 * reach any tree-changing commits (e.g. just above uninteresting one
402 * but traversal is with pathspec).
403 *
404 * weight = -1 means it has one parent and its distance is yet to
405 * be computed.
406 *
407 * weight = -2 means it has more than one parent and its distance is
408 * unknown. After running count_distance() first, they will get zero
409 * or positive distance.
410 */
ce0cbad7 411static struct commit_list *do_find_bisection(struct commit_list *list,
50e62a8e
CC
412 int nr, int *weights,
413 int find_all)
1daa09d9 414{
53271411 415 int n, counted;
77c11e06 416 struct commit_list *p;
1daa09d9 417
1c4fea3a
JH
418 counted = 0;
419
420 for (n = 0, p = list; p; p = p->next) {
1daa09d9
JH
421 struct commit *commit = p->item;
422 unsigned flags = commit->object.flags;
423
424 p->item->util = &weights[n++];
425 switch (count_interesting_parents(commit)) {
426 case 0:
7dc0fe3b 427 if (!(flags & TREESAME)) {
1c4fea3a
JH
428 weight_set(p, 1);
429 counted++;
1daa09d9
JH
430 show_list("bisection 2 count one",
431 counted, nr, list);
1c4fea3a 432 }
1daa09d9
JH
433 /*
434 * otherwise, it is known not to reach any
435 * tree-changing commit and gets weight 0.
436 */
437 break;
438 case 1:
439 weight_set(p, -1);
440 break;
441 default:
442 weight_set(p, -2);
443 break;
1c4fea3a
JH
444 }
445 }
446
1daa09d9
JH
447 show_list("bisection 2 initialize", counted, nr, list);
448
1c4fea3a
JH
449 /*
450 * If you have only one parent in the resulting set
451 * then you can reach one commit more than that parent
452 * can reach. So we do not have to run the expensive
453 * count_distance() for single strand of pearls.
454 *
455 * However, if you have more than one parents, you cannot
456 * just add their distance and one for yourself, since
457 * they usually reach the same ancestor and you would
458 * end up counting them twice that way.
459 *
460 * So we will first count distance of merges the usual
461 * way, and then fill the blanks using cheaper algorithm.
462 */
463 for (p = list; p; p = p->next) {
1daa09d9 464 if (p->item->object.flags & UNINTERESTING)
1c4fea3a 465 continue;
53271411 466 if (weight(p) != -2)
1c4fea3a 467 continue;
53271411 468 weight_set(p, count_distance(p));
1daa09d9 469 clear_distance(list);
1c4fea3a
JH
470
471 /* Does it happen to be at exactly half-way? */
50e62a8e 472 if (!find_all && halfway(p, nr))
1c4fea3a 473 return p;
1c4fea3a
JH
474 counted++;
475 }
476
1daa09d9
JH
477 show_list("bisection 2 count_distance", counted, nr, list);
478
1c4fea3a
JH
479 while (counted < nr) {
480 for (p = list; p; p = p->next) {
481 struct commit_list *q;
1daa09d9 482 unsigned flags = p->item->object.flags;
1c4fea3a 483
1daa09d9 484 if (0 <= weight(p))
1c4fea3a 485 continue;
1daa09d9
JH
486 for (q = p->item->parents; q; q = q->next) {
487 if (q->item->object.flags & UNINTERESTING)
488 continue;
489 if (0 <= weight(q))
1c4fea3a 490 break;
1daa09d9 491 }
1c4fea3a
JH
492 if (!q)
493 continue;
1daa09d9
JH
494
495 /*
496 * weight for p is unknown but q is known.
497 * add one for p itself if p is to be counted,
498 * otherwise inherit it from q directly.
499 */
7dc0fe3b 500 if (!(flags & TREESAME)) {
1daa09d9
JH
501 weight_set(p, weight(q)+1);
502 counted++;
503 show_list("bisection 2 count one",
504 counted, nr, list);
505 }
506 else
507 weight_set(p, weight(q));
1c4fea3a
JH
508
509 /* Does it happen to be at exactly half-way? */
50e62a8e 510 if (!find_all && halfway(p, nr))
1c4fea3a 511 return p;
1c4fea3a
JH
512 }
513 }
514
1daa09d9
JH
515 show_list("bisection 2 counted all", counted, nr, list);
516
50e62a8e
CC
517 if (!find_all)
518 return best_bisection(list, nr);
519 else
520 return best_bisection_sorted(list, nr);
ce0cbad7
CC
521}
522
523static struct commit_list *find_bisection(struct commit_list *list,
50e62a8e
CC
524 int *reaches, int *all,
525 int find_all)
ce0cbad7
CC
526{
527 int nr, on_list;
528 struct commit_list *p, *best, *next, *last;
529 int *weights;
530
531 show_list("bisection 2 entry", 0, 0, list);
532
533 /*
534 * Count the number of total and tree-changing items on the
535 * list, while reversing the list.
536 */
537 for (nr = on_list = 0, last = NULL, p = list;
538 p;
539 p = next) {
540 unsigned flags = p->item->object.flags;
541
542 next = p->next;
543 if (flags & UNINTERESTING)
544 continue;
545 p->next = last;
546 last = p;
7dc0fe3b 547 if (!(flags & TREESAME))
ce0cbad7
CC
548 nr++;
549 on_list++;
550 }
551 list = last;
552 show_list("bisection 2 sorted", 0, nr, list);
553
554 *all = nr;
555 weights = xcalloc(on_list, sizeof(*weights));
556
557 /* Do the real work of finding bisection commit. */
50e62a8e 558 best = do_find_bisection(list, nr, weights, find_all);
17ed1580 559 if (best) {
50e62a8e
CC
560 if (!find_all)
561 best->next = NULL;
17ed1580
CC
562 *reaches = weight(best);
563 }
1c4fea3a
JH
564 free(weights);
565 return best;
566}
567
a633fca0 568int cmd_rev_list(int argc, const char **argv, const char *prefix)
64745109 569{
ae563542 570 struct commit_list *list;
d9a83684 571 int i;
42cabc34 572 int read_from_stdin = 0;
457f08a0 573 int bisect_show_vars = 0;
50e62a8e 574 int bisect_find_all = 0;
27350891 575 int quiet = 0;
64745109 576
ef90d6d4 577 git_config(git_default_config, NULL);
a633fca0 578 init_revisions(&revs, prefix);
7594c4b2
JH
579 revs.abbrev = 0;
580 revs.commit_format = CMIT_FMT_UNSPECIFIED;
a4a88b2b 581 argc = setup_revisions(argc, argv, &revs, NULL);
ae563542 582
fcfda02b 583 for (i = 1 ; i < argc; i++) {
cf484544 584 const char *arg = argv[i];
fcfda02b 585
a6f68d47 586 if (!strcmp(arg, "--header")) {
7594c4b2 587 revs.verbose_header = 1;
9d97aa64
LT
588 continue;
589 }
dc68c4ff
JH
590 if (!strcmp(arg, "--timestamp")) {
591 show_timestamp = 1;
592 continue;
593 }
8b3a1e05
LT
594 if (!strcmp(arg, "--bisect")) {
595 bisect_list = 1;
596 continue;
597 }
50e62a8e
CC
598 if (!strcmp(arg, "--bisect-all")) {
599 bisect_list = 1;
600 bisect_find_all = 1;
601 continue;
602 }
457f08a0
JH
603 if (!strcmp(arg, "--bisect-vars")) {
604 bisect_list = 1;
605 bisect_show_vars = 1;
606 continue;
607 }
42cabc34
JH
608 if (!strcmp(arg, "--stdin")) {
609 if (read_from_stdin++)
610 die("--stdin given twice?");
611 read_revisions_from_stdin(&revs);
612 continue;
613 }
27350891
SP
614 if (!strcmp(arg, "--quiet")) {
615 quiet = 1;
616 continue;
617 }
ae563542 618 usage(rev_list_usage);
a6f68d47 619
fcfda02b 620 }
7594c4b2
JH
621 if (revs.commit_format != CMIT_FMT_UNSPECIFIED) {
622 /* The command line has a --pretty */
623 hdr_termination = '\n';
624 if (revs.commit_format == CMIT_FMT_ONELINE)
91539833 625 header_prefix = "";
7594c4b2 626 else
91539833 627 header_prefix = "commit ";
7594c4b2 628 }
db89665f
JH
629 else if (revs.verbose_header)
630 /* Only --header was specified */
631 revs.commit_format = CMIT_FMT_RAW;
fcfda02b 632
ae563542 633 list = revs.commits;
ae563542 634
8c1f0b44
JH
635 if ((!list &&
636 (!(revs.tag_objects||revs.tree_objects||revs.blob_objects) &&
1f1e895f 637 !revs.pending.nr)) ||
8c1f0b44 638 revs.diff)
7b34c2fa
LT
639 usage(rev_list_usage);
640
2d10c555 641 save_commit_buffer = revs.verbose_header || revs.grep_filter;
4e1dc640
JH
642 if (bisect_list)
643 revs.limited = 1;
9181ca2c 644
3d51e1b5
MK
645 if (prepare_revision_walk(&revs))
646 die("revision walk setup failed");
a4a88b2b 647 if (revs.tree_objects)
8d1d8f83 648 mark_edges_uninteresting(revs.commits, &revs, show_edge);
a4a88b2b 649
457f08a0
JH
650 if (bisect_list) {
651 int reaches = reaches, all = all;
652
50e62a8e
CC
653 revs.commits = find_bisection(revs.commits, &reaches, &all,
654 bisect_find_all);
457f08a0
JH
655 if (bisect_show_vars) {
656 int cnt;
50e62a8e 657 char hex[41];
457f08a0
JH
658 if (!revs.commits)
659 return 1;
660 /*
661 * revs.commits can reach "reaches" commits among
662 * "all" commits. If it is good, then there are
663 * (all-reaches) commits left to be bisected.
664 * On the other hand, if it is bad, then the set
665 * to bisect is "reaches".
666 * A bisect set of size N has (N-1) commits further
667 * to test, as we already know one bad one.
668 */
50e62a8e 669 cnt = all - reaches;
457f08a0
JH
670 if (cnt < reaches)
671 cnt = reaches;
50e62a8e
CC
672 strcpy(hex, sha1_to_hex(revs.commits->item->object.sha1));
673
674 if (bisect_find_all) {
675 traverse_commit_list(&revs, show_commit, show_object);
676 printf("------\n");
677 }
678
457f08a0
JH
679 printf("bisect_rev=%s\n"
680 "bisect_nr=%d\n"
681 "bisect_good=%d\n"
682 "bisect_bad=%d\n"
683 "bisect_all=%d\n",
50e62a8e 684 hex,
457f08a0
JH
685 cnt - 1,
686 all - reaches - 1,
687 reaches - 1,
688 all);
689 return 0;
690 }
691 }
7b34c2fa 692
27350891
SP
693 traverse_commit_list(&revs,
694 quiet ? finish_commit : show_commit,
695 quiet ? finish_object : show_object);
8906300f 696
64745109
LT
697 return 0;
698}