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