diff.c: make built-in hunk header pattern a separate table
[alt-git.git] / builtin-rev-list.c
blob86db8b03fe4295497cf011a75617d6dc8f172c64
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 " --left-right\n"
39 " special purpose:\n"
40 " --bisect\n"
41 " --bisect-vars"
44 static struct rev_info revs;
46 static int bisect_list;
47 static int show_timestamp;
48 static int hdr_termination;
49 static const char *header_prefix;
51 static void show_commit(struct commit *commit)
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('>');
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 struct object *o = &(parents->item->object);
74 parents = parents->next;
75 if (o->flags & TMP_MARK)
76 continue;
77 printf(" %s", sha1_to_hex(o->sha1));
78 o->flags |= TMP_MARK;
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.
84 for (parents = commit->parents;
85 parents;
86 parents = parents->next)
87 parents->item->object.flags &= ~TMP_MARK;
89 if (revs.commit_format == CMIT_FMT_ONELINE)
90 putchar(' ');
91 else
92 putchar('\n');
94 if (revs.verbose_header) {
95 char *buf = NULL;
96 unsigned long buflen = 0;
97 pretty_print_commit(revs.commit_format, commit, ~0,
98 &buf, &buflen,
99 revs.abbrev, NULL, NULL, revs.date_mode);
100 printf("%s%c", buf, hdr_termination);
101 free(buf);
103 maybe_flush_or_die(stdout, "stdout");
104 if (commit->parents) {
105 free_commit_list(commit->parents);
106 commit->parents = NULL;
108 free(commit->buffer);
109 commit->buffer = NULL;
112 static void show_object(struct object_array_entry *p)
114 /* An object with name "foo\n0000000..." can be used to
115 * confuse downstream git-pack-objects very badly.
117 const char *ep = strchr(p->name, '\n');
119 if (p->item->type == OBJ_BLOB && !has_sha1_file(p->item->sha1))
120 die("missing blob object '%s'", sha1_to_hex(p->item->sha1));
122 if (ep) {
123 printf("%s %.*s\n", sha1_to_hex(p->item->sha1),
124 (int) (ep - p->name),
125 p->name);
127 else
128 printf("%s %s\n", sha1_to_hex(p->item->sha1), p->name);
131 static void show_edge(struct commit *commit)
133 printf("-%s\n", sha1_to_hex(commit->object.sha1));
137 * This is a truly stupid algorithm, but it's only
138 * used for bisection, and we just don't care enough.
140 * We care just barely enough to avoid recursing for
141 * non-merge entries.
143 static int count_distance(struct commit_list *entry)
145 int nr = 0;
147 while (entry) {
148 struct commit *commit = entry->item;
149 struct commit_list *p;
151 if (commit->object.flags & (UNINTERESTING | COUNTED))
152 break;
153 if (!revs.prune_fn || (commit->object.flags & TREECHANGE))
154 nr++;
155 commit->object.flags |= COUNTED;
156 p = commit->parents;
157 entry = p;
158 if (p) {
159 p = p->next;
160 while (p) {
161 nr += count_distance(p);
162 p = p->next;
167 return nr;
170 static void clear_distance(struct commit_list *list)
172 while (list) {
173 struct commit *commit = list->item;
174 commit->object.flags &= ~COUNTED;
175 list = list->next;
179 #define DEBUG_BISECT 0
181 static inline int weight(struct commit_list *elem)
183 return *((int*)(elem->item->util));
186 static inline void weight_set(struct commit_list *elem, int weight)
188 *((int*)(elem->item->util)) = weight;
191 static int count_interesting_parents(struct commit *commit)
193 struct commit_list *p;
194 int count;
196 for (count = 0, p = commit->parents; p; p = p->next) {
197 if (p->item->object.flags & UNINTERESTING)
198 continue;
199 count++;
201 return count;
204 static inline int halfway(struct commit_list *p, int distance, int nr)
207 * Don't short-cut something we are not going to return!
209 if (revs.prune_fn && !(p->item->object.flags & TREECHANGE))
210 return 0;
211 if (DEBUG_BISECT)
212 return 0;
214 * 2 and 3 are halfway of 5.
215 * 3 is halfway of 6 but 2 and 4 are not.
217 distance *= 2;
218 switch (distance - nr) {
219 case -1: case 0: case 1:
220 return 1;
221 default:
222 return 0;
226 #if !DEBUG_BISECT
227 #define show_list(a,b,c,d) do { ; } while (0)
228 #else
229 static void show_list(const char *debug, int counted, int nr,
230 struct commit_list *list)
232 struct commit_list *p;
234 fprintf(stderr, "%s (%d/%d)\n", debug, counted, nr);
236 for (p = list; p; p = p->next) {
237 struct commit_list *pp;
238 struct commit *commit = p->item;
239 unsigned flags = commit->object.flags;
240 enum object_type type;
241 unsigned long size;
242 char *buf = read_sha1_file(commit->object.sha1, &type, &size);
243 char *ep, *sp;
245 fprintf(stderr, "%c%c%c ",
246 (flags & TREECHANGE) ? 'T' : ' ',
247 (flags & UNINTERESTING) ? 'U' : ' ',
248 (flags & COUNTED) ? 'C' : ' ');
249 if (commit->util)
250 fprintf(stderr, "%3d", weight(p));
251 else
252 fprintf(stderr, "---");
253 fprintf(stderr, " %.*s", 8, sha1_to_hex(commit->object.sha1));
254 for (pp = commit->parents; pp; pp = pp->next)
255 fprintf(stderr, " %.*s", 8,
256 sha1_to_hex(pp->item->object.sha1));
258 sp = strstr(buf, "\n\n");
259 if (sp) {
260 sp += 2;
261 for (ep = sp; *ep && *ep != '\n'; ep++)
263 fprintf(stderr, " %.*s", (int)(ep - sp), sp);
265 fprintf(stderr, "\n");
268 #endif /* DEBUG_BISECT */
271 * zero or positive weight is the number of interesting commits it can
272 * reach, including itself. Especially, weight = 0 means it does not
273 * reach any tree-changing commits (e.g. just above uninteresting one
274 * but traversal is with pathspec).
276 * weight = -1 means it has one parent and its distance is yet to
277 * be computed.
279 * weight = -2 means it has more than one parent and its distance is
280 * unknown. After running count_distance() first, they will get zero
281 * or positive distance.
284 static struct commit_list *find_bisection(struct commit_list *list,
285 int *reaches, int *all)
287 int n, nr, on_list, counted, distance;
288 struct commit_list *p, *best, *next, *last;
289 int *weights;
291 show_list("bisection 2 entry", 0, 0, list);
294 * Count the number of total and tree-changing items on the
295 * list, while reversing the list.
297 for (nr = on_list = 0, last = NULL, p = list;
299 p = next) {
300 unsigned flags = p->item->object.flags;
302 next = p->next;
303 if (flags & UNINTERESTING)
304 continue;
305 p->next = last;
306 last = p;
307 if (!revs.prune_fn || (flags & TREECHANGE))
308 nr++;
309 on_list++;
311 list = last;
312 show_list("bisection 2 sorted", 0, nr, list);
314 *all = nr;
315 weights = xcalloc(on_list, sizeof(int*));
316 counted = 0;
318 for (n = 0, p = list; p; p = p->next) {
319 struct commit *commit = p->item;
320 unsigned flags = commit->object.flags;
322 p->item->util = &weights[n++];
323 switch (count_interesting_parents(commit)) {
324 case 0:
325 if (!revs.prune_fn || (flags & TREECHANGE)) {
326 weight_set(p, 1);
327 counted++;
328 show_list("bisection 2 count one",
329 counted, nr, list);
332 * otherwise, it is known not to reach any
333 * tree-changing commit and gets weight 0.
335 break;
336 case 1:
337 weight_set(p, -1);
338 break;
339 default:
340 weight_set(p, -2);
341 break;
345 show_list("bisection 2 initialize", counted, nr, list);
348 * If you have only one parent in the resulting set
349 * then you can reach one commit more than that parent
350 * can reach. So we do not have to run the expensive
351 * count_distance() for single strand of pearls.
353 * However, if you have more than one parents, you cannot
354 * just add their distance and one for yourself, since
355 * they usually reach the same ancestor and you would
356 * end up counting them twice that way.
358 * So we will first count distance of merges the usual
359 * way, and then fill the blanks using cheaper algorithm.
361 for (p = list; p; p = p->next) {
362 if (p->item->object.flags & UNINTERESTING)
363 continue;
364 n = weight(p);
365 if (n != -2)
366 continue;
367 distance = count_distance(p);
368 clear_distance(list);
369 weight_set(p, distance);
371 /* Does it happen to be at exactly half-way? */
372 if (halfway(p, distance, nr)) {
373 p->next = NULL;
374 *reaches = distance;
375 free(weights);
376 return p;
378 counted++;
381 show_list("bisection 2 count_distance", counted, nr, list);
383 while (counted < nr) {
384 for (p = list; p; p = p->next) {
385 struct commit_list *q;
386 unsigned flags = p->item->object.flags;
388 if (0 <= weight(p))
389 continue;
390 for (q = p->item->parents; q; q = q->next) {
391 if (q->item->object.flags & UNINTERESTING)
392 continue;
393 if (0 <= weight(q))
394 break;
396 if (!q)
397 continue;
400 * weight for p is unknown but q is known.
401 * add one for p itself if p is to be counted,
402 * otherwise inherit it from q directly.
404 if (!revs.prune_fn || (flags & TREECHANGE)) {
405 weight_set(p, weight(q)+1);
406 counted++;
407 show_list("bisection 2 count one",
408 counted, nr, list);
410 else
411 weight_set(p, weight(q));
413 /* Does it happen to be at exactly half-way? */
414 distance = weight(p);
415 if (halfway(p, distance, nr)) {
416 p->next = NULL;
417 *reaches = distance;
418 free(weights);
419 return p;
424 show_list("bisection 2 counted all", counted, nr, list);
426 /* Then find the best one */
427 counted = -1;
428 best = list;
429 for (p = list; p; p = p->next) {
430 unsigned flags = p->item->object.flags;
432 if (revs.prune_fn && !(flags & TREECHANGE))
433 continue;
434 distance = weight(p);
435 if (nr - distance < distance)
436 distance = nr - distance;
437 if (distance > counted) {
438 best = p;
439 counted = distance;
440 *reaches = weight(p);
443 if (best)
444 best->next = NULL;
445 free(weights);
446 return best;
449 static void read_revisions_from_stdin(struct rev_info *revs)
451 char line[1000];
453 while (fgets(line, sizeof(line), stdin) != NULL) {
454 int len = strlen(line);
455 if (line[len - 1] == '\n')
456 line[--len] = 0;
457 if (!len)
458 break;
459 if (line[0] == '-')
460 die("options not supported in --stdin mode");
461 if (handle_revision_arg(line, revs, 0, 1))
462 die("bad revision '%s'", line);
466 int cmd_rev_list(int argc, const char **argv, const char *prefix)
468 struct commit_list *list;
469 int i;
470 int read_from_stdin = 0;
471 int bisect_show_vars = 0;
473 git_config(git_default_config);
474 init_revisions(&revs, prefix);
475 revs.abbrev = 0;
476 revs.commit_format = CMIT_FMT_UNSPECIFIED;
477 argc = setup_revisions(argc, argv, &revs, NULL);
479 for (i = 1 ; i < argc; i++) {
480 const char *arg = argv[i];
482 if (!strcmp(arg, "--header")) {
483 revs.verbose_header = 1;
484 continue;
486 if (!strcmp(arg, "--timestamp")) {
487 show_timestamp = 1;
488 continue;
490 if (!strcmp(arg, "--bisect")) {
491 bisect_list = 1;
492 continue;
494 if (!strcmp(arg, "--bisect-vars")) {
495 bisect_list = 1;
496 bisect_show_vars = 1;
497 continue;
499 if (!strcmp(arg, "--stdin")) {
500 if (read_from_stdin++)
501 die("--stdin given twice?");
502 read_revisions_from_stdin(&revs);
503 continue;
505 usage(rev_list_usage);
508 if (revs.commit_format != CMIT_FMT_UNSPECIFIED) {
509 /* The command line has a --pretty */
510 hdr_termination = '\n';
511 if (revs.commit_format == CMIT_FMT_ONELINE)
512 header_prefix = "";
513 else
514 header_prefix = "commit ";
516 else if (revs.verbose_header)
517 /* Only --header was specified */
518 revs.commit_format = CMIT_FMT_RAW;
520 list = revs.commits;
522 if ((!list &&
523 (!(revs.tag_objects||revs.tree_objects||revs.blob_objects) &&
524 !revs.pending.nr)) ||
525 revs.diff)
526 usage(rev_list_usage);
528 save_commit_buffer = revs.verbose_header || revs.grep_filter;
529 track_object_refs = 0;
530 if (bisect_list)
531 revs.limited = 1;
533 prepare_revision_walk(&revs);
534 if (revs.tree_objects)
535 mark_edges_uninteresting(revs.commits, &revs, show_edge);
537 if (bisect_list) {
538 int reaches = reaches, all = all;
540 revs.commits = find_bisection(revs.commits, &reaches, &all);
541 if (bisect_show_vars) {
542 int cnt;
543 if (!revs.commits)
544 return 1;
546 * revs.commits can reach "reaches" commits among
547 * "all" commits. If it is good, then there are
548 * (all-reaches) commits left to be bisected.
549 * On the other hand, if it is bad, then the set
550 * to bisect is "reaches".
551 * A bisect set of size N has (N-1) commits further
552 * to test, as we already know one bad one.
554 cnt = all-reaches;
555 if (cnt < reaches)
556 cnt = reaches;
557 printf("bisect_rev=%s\n"
558 "bisect_nr=%d\n"
559 "bisect_good=%d\n"
560 "bisect_bad=%d\n"
561 "bisect_all=%d\n",
562 sha1_to_hex(revs.commits->item->object.sha1),
563 cnt - 1,
564 all - reaches - 1,
565 reaches - 1,
566 all);
567 return 0;
571 traverse_commit_list(&revs, show_commit, show_object);
573 return 0;