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