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