Rename static variable write_index to update_index in builtin-apply.c
[git.git] / builtin-rev-list.c
blobf91685a4067630b49e6d9235f29ab8f8060faf66
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"
13 /* bits #0-15 in revision.h */
15 #define COUNTED (1u<<16)
17 static 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 " special purpose:\n"
39 " --bisect\n"
40 " --bisect-vars"
43 static struct rev_info revs;
45 static int bisect_list;
46 static int show_timestamp;
47 static int hdr_termination;
48 static const char *header_prefix;
50 static void show_commit(struct commit *commit)
52 if (show_timestamp)
53 printf("%lu ", commit->date);
54 if (header_prefix)
55 fputs(header_prefix, stdout);
56 if (commit->object.flags & BOUNDARY)
57 putchar('-');
58 else if (revs.left_right) {
59 if (commit->object.flags & SYMMETRIC_LEFT)
60 putchar('<');
61 else
62 putchar('>');
64 if (revs.abbrev_commit && revs.abbrev)
65 fputs(find_unique_abbrev(commit->object.sha1, revs.abbrev),
66 stdout);
67 else
68 fputs(sha1_to_hex(commit->object.sha1), stdout);
69 if (revs.parents) {
70 struct commit_list *parents = commit->parents;
71 while (parents) {
72 struct object *o = &(parents->item->object);
73 parents = parents->next;
74 if (o->flags & TMP_MARK)
75 continue;
76 printf(" %s", sha1_to_hex(o->sha1));
77 o->flags |= TMP_MARK;
79 /* TMP_MARK is a general purpose flag that can
80 * be used locally, but the user should clean
81 * things up after it is done with them.
83 for (parents = commit->parents;
84 parents;
85 parents = parents->next)
86 parents->item->object.flags &= ~TMP_MARK;
88 if (revs.commit_format == CMIT_FMT_ONELINE)
89 putchar(' ');
90 else
91 putchar('\n');
93 if (revs.verbose_header) {
94 static char pretty_header[16384];
95 pretty_print_commit(revs.commit_format, commit, ~0,
96 pretty_header, sizeof(pretty_header),
97 revs.abbrev, NULL, NULL, revs.relative_date);
98 printf("%s%c", pretty_header, hdr_termination);
100 fflush(stdout);
101 if (commit->parents) {
102 free_commit_list(commit->parents);
103 commit->parents = NULL;
105 free(commit->buffer);
106 commit->buffer = NULL;
109 static void show_object(struct object_array_entry *p)
111 /* An object with name "foo\n0000000..." can be used to
112 * confuse downstream git-pack-objects very badly.
114 const char *ep = strchr(p->name, '\n');
115 if (ep) {
116 printf("%s %.*s\n", sha1_to_hex(p->item->sha1),
117 (int) (ep - p->name),
118 p->name);
120 else
121 printf("%s %s\n", sha1_to_hex(p->item->sha1), p->name);
124 static void show_edge(struct commit *commit)
126 printf("-%s\n", sha1_to_hex(commit->object.sha1));
130 * This is a truly stupid algorithm, but it's only
131 * used for bisection, and we just don't care enough.
133 * We care just barely enough to avoid recursing for
134 * non-merge entries.
136 static int count_distance(struct commit_list *entry)
138 int nr = 0;
140 while (entry) {
141 struct commit *commit = entry->item;
142 struct commit_list *p;
144 if (commit->object.flags & (UNINTERESTING | COUNTED))
145 break;
146 if (!revs.prune_fn || (commit->object.flags & TREECHANGE))
147 nr++;
148 commit->object.flags |= COUNTED;
149 p = commit->parents;
150 entry = p;
151 if (p) {
152 p = p->next;
153 while (p) {
154 nr += count_distance(p);
155 p = p->next;
160 return nr;
163 static void clear_distance(struct commit_list *list)
165 while (list) {
166 struct commit *commit = list->item;
167 commit->object.flags &= ~COUNTED;
168 list = list->next;
172 #define DEBUG_BISECT 0
174 static inline int weight(struct commit_list *elem)
176 return *((int*)(elem->item->util));
179 static inline void weight_set(struct commit_list *elem, int weight)
181 *((int*)(elem->item->util)) = weight;
184 static int count_interesting_parents(struct commit *commit)
186 struct commit_list *p;
187 int count;
189 for (count = 0, p = commit->parents; p; p = p->next) {
190 if (p->item->object.flags & UNINTERESTING)
191 continue;
192 count++;
194 return count;
197 static inline int halfway(struct commit_list *p, int distance, int nr)
200 * Don't short-cut something we are not going to return!
202 if (revs.prune_fn && !(p->item->object.flags & TREECHANGE))
203 return 0;
204 if (DEBUG_BISECT)
205 return 0;
207 * 2 and 3 are halfway of 5.
208 * 3 is halfway of 6 but 2 and 4 are not.
210 distance *= 2;
211 switch (distance - nr) {
212 case -1: case 0: case 1:
213 return 1;
214 default:
215 return 0;
219 #if !DEBUG_BISECT
220 #define show_list(a,b,c,d) do { ; } while (0)
221 #else
222 static void show_list(const char *debug, int counted, int nr,
223 struct commit_list *list)
225 struct commit_list *p;
227 fprintf(stderr, "%s (%d/%d)\n", debug, counted, nr);
229 for (p = list; p; p = p->next) {
230 struct commit_list *pp;
231 struct commit *commit = p->item;
232 unsigned flags = commit->object.flags;
233 enum object_type type;
234 unsigned long size;
235 char *buf = read_sha1_file(commit->object.sha1, &type, &size);
236 char *ep, *sp;
238 fprintf(stderr, "%c%c%c ",
239 (flags & TREECHANGE) ? 'T' : ' ',
240 (flags & UNINTERESTING) ? 'U' : ' ',
241 (flags & COUNTED) ? 'C' : ' ');
242 if (commit->util)
243 fprintf(stderr, "%3d", weight(p));
244 else
245 fprintf(stderr, "---");
246 fprintf(stderr, " %.*s", 8, sha1_to_hex(commit->object.sha1));
247 for (pp = commit->parents; pp; pp = pp->next)
248 fprintf(stderr, " %.*s", 8,
249 sha1_to_hex(pp->item->object.sha1));
251 sp = strstr(buf, "\n\n");
252 if (sp) {
253 sp += 2;
254 for (ep = sp; *ep && *ep != '\n'; ep++)
256 fprintf(stderr, " %.*s", (int)(ep - sp), sp);
258 fprintf(stderr, "\n");
261 #endif /* DEBUG_BISECT */
264 * zero or positive weight is the number of interesting commits it can
265 * reach, including itself. Especially, weight = 0 means it does not
266 * reach any tree-changing commits (e.g. just above uninteresting one
267 * but traversal is with pathspec).
269 * weight = -1 means it has one parent and its distance is yet to
270 * be computed.
272 * weight = -2 means it has more than one parent and its distance is
273 * unknown. After running count_distance() first, they will get zero
274 * or positive distance.
277 static struct commit_list *find_bisection(struct commit_list *list,
278 int *reaches, int *all)
280 int n, nr, on_list, counted, distance;
281 struct commit_list *p, *best, *next, *last;
282 int *weights;
284 show_list("bisection 2 entry", 0, 0, list);
287 * Count the number of total and tree-changing items on the
288 * list, while reversing the list.
290 for (nr = on_list = 0, last = NULL, p = list;
292 p = next) {
293 unsigned flags = p->item->object.flags;
295 next = p->next;
296 if (flags & UNINTERESTING)
297 continue;
298 p->next = last;
299 last = p;
300 if (!revs.prune_fn || (flags & TREECHANGE))
301 nr++;
302 on_list++;
304 list = last;
305 show_list("bisection 2 sorted", 0, nr, list);
307 *all = nr;
308 weights = xcalloc(on_list, sizeof(int*));
309 counted = 0;
311 for (n = 0, p = list; p; p = p->next) {
312 struct commit *commit = p->item;
313 unsigned flags = commit->object.flags;
315 p->item->util = &weights[n++];
316 switch (count_interesting_parents(commit)) {
317 case 0:
318 if (!revs.prune_fn || (flags & TREECHANGE)) {
319 weight_set(p, 1);
320 counted++;
321 show_list("bisection 2 count one",
322 counted, nr, list);
325 * otherwise, it is known not to reach any
326 * tree-changing commit and gets weight 0.
328 break;
329 case 1:
330 weight_set(p, -1);
331 break;
332 default:
333 weight_set(p, -2);
334 break;
338 show_list("bisection 2 initialize", counted, nr, list);
341 * If you have only one parent in the resulting set
342 * then you can reach one commit more than that parent
343 * can reach. So we do not have to run the expensive
344 * count_distance() for single strand of pearls.
346 * However, if you have more than one parents, you cannot
347 * just add their distance and one for yourself, since
348 * they usually reach the same ancestor and you would
349 * end up counting them twice that way.
351 * So we will first count distance of merges the usual
352 * way, and then fill the blanks using cheaper algorithm.
354 for (p = list; p; p = p->next) {
355 if (p->item->object.flags & UNINTERESTING)
356 continue;
357 n = weight(p);
358 if (n != -2)
359 continue;
360 distance = count_distance(p);
361 clear_distance(list);
362 weight_set(p, distance);
364 /* Does it happen to be at exactly half-way? */
365 if (halfway(p, distance, nr)) {
366 p->next = NULL;
367 *reaches = distance;
368 free(weights);
369 return p;
371 counted++;
374 show_list("bisection 2 count_distance", counted, nr, list);
376 while (counted < nr) {
377 for (p = list; p; p = p->next) {
378 struct commit_list *q;
379 unsigned flags = p->item->object.flags;
381 if (0 <= weight(p))
382 continue;
383 for (q = p->item->parents; q; q = q->next) {
384 if (q->item->object.flags & UNINTERESTING)
385 continue;
386 if (0 <= weight(q))
387 break;
389 if (!q)
390 continue;
393 * weight for p is unknown but q is known.
394 * add one for p itself if p is to be counted,
395 * otherwise inherit it from q directly.
397 if (!revs.prune_fn || (flags & TREECHANGE)) {
398 weight_set(p, weight(q)+1);
399 counted++;
400 show_list("bisection 2 count one",
401 counted, nr, list);
403 else
404 weight_set(p, weight(q));
406 /* Does it happen to be at exactly half-way? */
407 distance = weight(p);
408 if (halfway(p, distance, nr)) {
409 p->next = NULL;
410 *reaches = distance;
411 free(weights);
412 return p;
417 show_list("bisection 2 counted all", counted, nr, list);
419 /* Then find the best one */
420 counted = -1;
421 best = list;
422 for (p = list; p; p = p->next) {
423 unsigned flags = p->item->object.flags;
425 if (revs.prune_fn && !(flags & TREECHANGE))
426 continue;
427 distance = weight(p);
428 if (nr - distance < distance)
429 distance = nr - distance;
430 if (distance > counted) {
431 best = p;
432 counted = distance;
433 *reaches = weight(p);
436 if (best)
437 best->next = NULL;
438 free(weights);
439 return best;
442 static void read_revisions_from_stdin(struct rev_info *revs)
444 char line[1000];
446 while (fgets(line, sizeof(line), stdin) != NULL) {
447 int len = strlen(line);
448 if (line[len - 1] == '\n')
449 line[--len] = 0;
450 if (!len)
451 break;
452 if (line[0] == '-')
453 die("options not supported in --stdin mode");
454 if (handle_revision_arg(line, revs, 0, 1))
455 die("bad revision '%s'", line);
459 int cmd_rev_list(int argc, const char **argv, const char *prefix)
461 struct commit_list *list;
462 int i;
463 int read_from_stdin = 0;
464 int bisect_show_vars = 0;
466 git_config(git_default_config);
467 init_revisions(&revs, prefix);
468 revs.abbrev = 0;
469 revs.commit_format = CMIT_FMT_UNSPECIFIED;
470 argc = setup_revisions(argc, argv, &revs, NULL);
472 for (i = 1 ; i < argc; i++) {
473 const char *arg = argv[i];
475 if (!strcmp(arg, "--header")) {
476 revs.verbose_header = 1;
477 continue;
479 if (!strcmp(arg, "--timestamp")) {
480 show_timestamp = 1;
481 continue;
483 if (!strcmp(arg, "--bisect")) {
484 bisect_list = 1;
485 continue;
487 if (!strcmp(arg, "--bisect-vars")) {
488 bisect_list = 1;
489 bisect_show_vars = 1;
490 continue;
492 if (!strcmp(arg, "--stdin")) {
493 if (read_from_stdin++)
494 die("--stdin given twice?");
495 read_revisions_from_stdin(&revs);
496 continue;
498 usage(rev_list_usage);
501 if (revs.commit_format != CMIT_FMT_UNSPECIFIED) {
502 /* The command line has a --pretty */
503 hdr_termination = '\n';
504 if (revs.commit_format == CMIT_FMT_ONELINE)
505 header_prefix = "";
506 else
507 header_prefix = "commit ";
509 else if (revs.verbose_header)
510 /* Only --header was specified */
511 revs.commit_format = CMIT_FMT_RAW;
513 list = revs.commits;
515 if ((!list &&
516 (!(revs.tag_objects||revs.tree_objects||revs.blob_objects) &&
517 !revs.pending.nr)) ||
518 revs.diff)
519 usage(rev_list_usage);
521 save_commit_buffer = revs.verbose_header || revs.grep_filter;
522 track_object_refs = 0;
523 if (bisect_list)
524 revs.limited = 1;
526 prepare_revision_walk(&revs);
527 if (revs.tree_objects)
528 mark_edges_uninteresting(revs.commits, &revs, show_edge);
530 if (bisect_list) {
531 int reaches = reaches, all = all;
533 revs.commits = find_bisection(revs.commits, &reaches, &all);
534 if (bisect_show_vars) {
535 int cnt;
536 if (!revs.commits)
537 return 1;
539 * revs.commits can reach "reaches" commits among
540 * "all" commits. If it is good, then there are
541 * (all-reaches) commits left to be bisected.
542 * On the other hand, if it is bad, then the set
543 * to bisect is "reaches".
544 * A bisect set of size N has (N-1) commits further
545 * to test, as we already know one bad one.
547 cnt = all-reaches;
548 if (cnt < reaches)
549 cnt = reaches;
550 printf("bisect_rev=%s\n"
551 "bisect_nr=%d\n"
552 "bisect_good=%d\n"
553 "bisect_bad=%d\n"
554 "bisect_all=%d\n",
555 sha1_to_hex(revs.commits->item->object.sha1),
556 cnt - 1,
557 all - reaches - 1,
558 reaches - 1,
559 all);
560 return 0;
564 traverse_commit_list(&revs, show_commit, show_object);
566 return 0;