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