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