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