gitweb: Read repo config using 'git config -z -l'
[git/git-p4.git] / builtin-rev-list.c
blob44393320e8ec27a35ccc1da550852ce3333a25a8
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 #include "log-tree.h"
14 /* bits #0-15 in revision.h */
16 #define COUNTED (1u<<16)
18 static const char rev_list_usage[] =
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"
26 " --remove-empty\n"
27 " --all\n"
28 " --stdin\n"
29 " ordering output:\n"
30 " --topo-order\n"
31 " --date-order\n"
32 " formatting output:\n"
33 " --parents\n"
34 " --objects | --objects-edge\n"
35 " --unpacked\n"
36 " --header | --pretty\n"
37 " --abbrev=nr | --no-abbrev\n"
38 " --abbrev-commit\n"
39 " --left-right\n"
40 " special purpose:\n"
41 " --bisect\n"
42 " --bisect-vars\n"
43 " --bisect-all"
46 static struct rev_info revs;
48 static int bisect_list;
49 static int show_timestamp;
50 static int hdr_termination;
51 static const char *header_prefix;
53 static void show_commit(struct commit *commit)
55 if (show_timestamp)
56 printf("%lu ", commit->date);
57 if (header_prefix)
58 fputs(header_prefix, stdout);
59 if (commit->object.flags & BOUNDARY)
60 putchar('-');
61 else if (revs.left_right) {
62 if (commit->object.flags & SYMMETRIC_LEFT)
63 putchar('<');
64 else
65 putchar('>');
67 if (revs.abbrev_commit && revs.abbrev)
68 fputs(find_unique_abbrev(commit->object.sha1, revs.abbrev),
69 stdout);
70 else
71 fputs(sha1_to_hex(commit->object.sha1), stdout);
72 if (revs.parents) {
73 struct commit_list *parents = commit->parents;
74 while (parents) {
75 printf(" %s", sha1_to_hex(parents->item->object.sha1));
76 parents = parents->next;
79 show_decorations(commit);
80 if (revs.commit_format == CMIT_FMT_ONELINE)
81 putchar(' ');
82 else
83 putchar('\n');
85 if (revs.verbose_header) {
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);
90 if (buf.len)
91 printf("%s%c", buf.buf, hdr_termination);
92 strbuf_release(&buf);
94 maybe_flush_or_die(stdout, "stdout");
95 if (commit->parents) {
96 free_commit_list(commit->parents);
97 commit->parents = NULL;
99 free(commit->buffer);
100 commit->buffer = NULL;
103 static void show_object(struct object_array_entry *p)
105 /* An object with name "foo\n0000000..." can be used to
106 * confuse downstream git-pack-objects very badly.
108 const char *ep = strchr(p->name, '\n');
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));
113 if (ep) {
114 printf("%s %.*s\n", sha1_to_hex(p->item->sha1),
115 (int) (ep - p->name),
116 p->name);
118 else
119 printf("%s %s\n", sha1_to_hex(p->item->sha1), p->name);
122 static void show_edge(struct commit *commit)
124 printf("-%s\n", sha1_to_hex(commit->object.sha1));
128 * This is a truly stupid algorithm, but it's only
129 * used for bisection, and we just don't care enough.
131 * We care just barely enough to avoid recursing for
132 * non-merge entries.
134 static int count_distance(struct commit_list *entry)
136 int nr = 0;
138 while (entry) {
139 struct commit *commit = entry->item;
140 struct commit_list *p;
142 if (commit->object.flags & (UNINTERESTING | COUNTED))
143 break;
144 if (!revs.prune_fn || (commit->object.flags & TREECHANGE))
145 nr++;
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;
158 return nr;
161 static void clear_distance(struct commit_list *list)
163 while (list) {
164 struct commit *commit = list->item;
165 commit->object.flags &= ~COUNTED;
166 list = list->next;
170 #define DEBUG_BISECT 0
172 static inline int weight(struct commit_list *elem)
174 return *((int*)(elem->item->util));
177 static inline void weight_set(struct commit_list *elem, int weight)
179 *((int*)(elem->item->util)) = weight;
182 static int count_interesting_parents(struct commit *commit)
184 struct commit_list *p;
185 int count;
187 for (count = 0, p = commit->parents; p; p = p->next) {
188 if (p->item->object.flags & UNINTERESTING)
189 continue;
190 count++;
192 return count;
195 static inline int halfway(struct commit_list *p, int nr)
198 * Don't short-cut something we are not going to return!
200 if (revs.prune_fn && !(p->item->object.flags & TREECHANGE))
201 return 0;
202 if (DEBUG_BISECT)
203 return 0;
205 * 2 and 3 are halfway of 5.
206 * 3 is halfway of 6 but 2 and 4 are not.
208 switch (2 * weight(p) - nr) {
209 case -1: case 0: case 1:
210 return 1;
211 default:
212 return 0;
216 #if !DEBUG_BISECT
217 #define show_list(a,b,c,d) do { ; } while (0)
218 #else
219 static void show_list(const char *debug, int counted, int nr,
220 struct commit_list *list)
222 struct commit_list *p;
224 fprintf(stderr, "%s (%d/%d)\n", debug, counted, nr);
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;
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));
248 sp = strstr(buf, "\n\n");
249 if (sp) {
250 sp += 2;
251 for (ep = sp; *ep && *ep != '\n'; ep++)
253 fprintf(stderr, " %.*s", (int)(ep - sp), sp);
255 fprintf(stderr, "\n");
258 #endif /* DEBUG_BISECT */
260 static struct commit_list *best_bisection(struct commit_list *list, int nr)
262 struct commit_list *p, *best;
263 int best_distance = -1;
265 best = list;
266 for (p = list; p; p = p->next) {
267 int distance;
268 unsigned flags = p->item->object.flags;
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;
281 return best;
284 struct commit_dist {
285 struct commit *commit;
286 int distance;
289 static int compare_commit_dist(const void *a_, const void *b_)
291 struct commit_dist *a, *b;
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);
300 static struct commit_list *best_bisection_sorted(struct commit_list *list, int nr)
302 struct commit_list *p;
303 struct commit_dist *array = xcalloc(nr, sizeof(*array));
304 int cnt, i;
306 for (p = list, cnt = 0; p; p = p->next) {
307 int distance;
308 unsigned flags = p->item->object.flags;
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++;
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);
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;
329 if (p)
330 p->next = NULL;
331 free(array);
332 return list;
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).
341 * weight = -1 means it has one parent and its distance is yet to
342 * be computed.
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.
348 static struct commit_list *do_find_bisection(struct commit_list *list,
349 int nr, int *weights,
350 int find_all)
352 int n, counted;
353 struct commit_list *p;
355 counted = 0;
357 for (n = 0, p = list; p; p = p->next) {
358 struct commit *commit = p->item;
359 unsigned flags = commit->object.flags;
361 p->item->util = &weights[n++];
362 switch (count_interesting_parents(commit)) {
363 case 0:
364 if (!revs.prune_fn || (flags & TREECHANGE)) {
365 weight_set(p, 1);
366 counted++;
367 show_list("bisection 2 count one",
368 counted, nr, list);
371 * otherwise, it is known not to reach any
372 * tree-changing commit and gets weight 0.
374 break;
375 case 1:
376 weight_set(p, -1);
377 break;
378 default:
379 weight_set(p, -2);
380 break;
384 show_list("bisection 2 initialize", counted, nr, list);
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.
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.
397 * So we will first count distance of merges the usual
398 * way, and then fill the blanks using cheaper algorithm.
400 for (p = list; p; p = p->next) {
401 if (p->item->object.flags & UNINTERESTING)
402 continue;
403 if (weight(p) != -2)
404 continue;
405 weight_set(p, count_distance(p));
406 clear_distance(list);
408 /* Does it happen to be at exactly half-way? */
409 if (!find_all && halfway(p, nr))
410 return p;
411 counted++;
414 show_list("bisection 2 count_distance", counted, nr, list);
416 while (counted < nr) {
417 for (p = list; p; p = p->next) {
418 struct commit_list *q;
419 unsigned flags = p->item->object.flags;
421 if (0 <= weight(p))
422 continue;
423 for (q = p->item->parents; q; q = q->next) {
424 if (q->item->object.flags & UNINTERESTING)
425 continue;
426 if (0 <= weight(q))
427 break;
429 if (!q)
430 continue;
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.
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);
443 else
444 weight_set(p, weight(q));
446 /* Does it happen to be at exactly half-way? */
447 if (!find_all && halfway(p, nr))
448 return p;
452 show_list("bisection 2 counted all", counted, nr, list);
454 if (!find_all)
455 return best_bisection(list, nr);
456 else
457 return best_bisection_sorted(list, nr);
460 static struct commit_list *find_bisection(struct commit_list *list,
461 int *reaches, int *all,
462 int find_all)
464 int nr, on_list;
465 struct commit_list *p, *best, *next, *last;
466 int *weights;
468 show_list("bisection 2 entry", 0, 0, list);
471 * Count the number of total and tree-changing items on the
472 * list, while reversing the list.
474 for (nr = on_list = 0, last = NULL, p = list;
476 p = next) {
477 unsigned flags = p->item->object.flags;
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++;
488 list = last;
489 show_list("bisection 2 sorted", 0, nr, list);
491 *all = nr;
492 weights = xcalloc(on_list, sizeof(*weights));
494 /* Do the real work of finding bisection commit. */
495 best = do_find_bisection(list, nr, weights, find_all);
496 if (best) {
497 if (!find_all)
498 best->next = NULL;
499 *reaches = weight(best);
501 free(weights);
502 return best;
505 static void read_revisions_from_stdin(struct rev_info *revs)
507 char line[1000];
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);
522 int cmd_rev_list(int argc, const char **argv, const char *prefix)
524 struct commit_list *list;
525 int i;
526 int read_from_stdin = 0;
527 int bisect_show_vars = 0;
528 int bisect_find_all = 0;
530 git_config(git_default_config);
531 init_revisions(&revs, prefix);
532 revs.abbrev = 0;
533 revs.commit_format = CMIT_FMT_UNSPECIFIED;
534 argc = setup_revisions(argc, argv, &revs, NULL);
536 for (i = 1 ; i < argc; i++) {
537 const char *arg = argv[i];
539 if (!strcmp(arg, "--header")) {
540 revs.verbose_header = 1;
541 continue;
543 if (!strcmp(arg, "--timestamp")) {
544 show_timestamp = 1;
545 continue;
547 if (!strcmp(arg, "--bisect")) {
548 bisect_list = 1;
549 continue;
551 if (!strcmp(arg, "--bisect-all")) {
552 bisect_list = 1;
553 bisect_find_all = 1;
554 continue;
556 if (!strcmp(arg, "--bisect-vars")) {
557 bisect_list = 1;
558 bisect_show_vars = 1;
559 continue;
561 if (!strcmp(arg, "--stdin")) {
562 if (read_from_stdin++)
563 die("--stdin given twice?");
564 read_revisions_from_stdin(&revs);
565 continue;
567 usage(rev_list_usage);
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)
574 header_prefix = "";
575 else
576 header_prefix = "commit ";
578 else if (revs.verbose_header)
579 /* Only --header was specified */
580 revs.commit_format = CMIT_FMT_RAW;
582 list = revs.commits;
584 if ((!list &&
585 (!(revs.tag_objects||revs.tree_objects||revs.blob_objects) &&
586 !revs.pending.nr)) ||
587 revs.diff)
588 usage(rev_list_usage);
590 save_commit_buffer = revs.verbose_header || revs.grep_filter;
591 track_object_refs = 0;
592 if (bisect_list)
593 revs.limited = 1;
595 prepare_revision_walk(&revs);
596 if (revs.tree_objects)
597 mark_edges_uninteresting(revs.commits, &revs, show_edge);
599 if (bisect_list) {
600 int reaches = reaches, all = all;
602 revs.commits = find_bisection(revs.commits, &reaches, &all,
603 bisect_find_all);
604 if (bisect_show_vars) {
605 int cnt;
606 char hex[41];
607 if (!revs.commits)
608 return 1;
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.
618 cnt = all - reaches;
619 if (cnt < reaches)
620 cnt = reaches;
621 strcpy(hex, sha1_to_hex(revs.commits->item->object.sha1));
623 if (bisect_find_all) {
624 traverse_commit_list(&revs, show_commit, show_object);
625 printf("------\n");
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",
633 hex,
634 cnt - 1,
635 all - reaches - 1,
636 reaches - 1,
637 all);
638 return 0;
642 traverse_commit_list(&revs, show_commit, show_object);
644 return 0;