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