log_ref_write() -- do not chomp reflog message at the first LF
[git/haiku.git] / builtin-rev-list.c
blob8efd609b12e3c8cf20fbe1bf5c0d55643d34ea3c
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 printf(" %s", sha1_to_hex(parents->item->object.sha1));
74 parents = parents->next;
77 if (revs.commit_format == CMIT_FMT_ONELINE)
78 putchar(' ');
79 else
80 putchar('\n');
82 if (revs.verbose_header) {
83 char *buf = NULL;
84 unsigned long buflen = 0;
85 pretty_print_commit(revs.commit_format, commit, ~0,
86 &buf, &buflen,
87 revs.abbrev, NULL, NULL, revs.date_mode);
88 printf("%s%c", buf, hdr_termination);
89 free(buf);
91 maybe_flush_or_die(stdout, "stdout");
92 if (commit->parents) {
93 free_commit_list(commit->parents);
94 commit->parents = NULL;
96 free(commit->buffer);
97 commit->buffer = NULL;
100 static void show_object(struct object_array_entry *p)
102 /* An object with name "foo\n0000000..." can be used to
103 * confuse downstream git-pack-objects very badly.
105 const char *ep = strchr(p->name, '\n');
107 if (p->item->type == OBJ_BLOB && !has_sha1_file(p->item->sha1))
108 die("missing blob object '%s'", sha1_to_hex(p->item->sha1));
110 if (ep) {
111 printf("%s %.*s\n", sha1_to_hex(p->item->sha1),
112 (int) (ep - p->name),
113 p->name);
115 else
116 printf("%s %s\n", sha1_to_hex(p->item->sha1), p->name);
119 static void show_edge(struct commit *commit)
121 printf("-%s\n", sha1_to_hex(commit->object.sha1));
125 * This is a truly stupid algorithm, but it's only
126 * used for bisection, and we just don't care enough.
128 * We care just barely enough to avoid recursing for
129 * non-merge entries.
131 static int count_distance(struct commit_list *entry)
133 int nr = 0;
135 while (entry) {
136 struct commit *commit = entry->item;
137 struct commit_list *p;
139 if (commit->object.flags & (UNINTERESTING | COUNTED))
140 break;
141 if (!revs.prune_fn || (commit->object.flags & TREECHANGE))
142 nr++;
143 commit->object.flags |= COUNTED;
144 p = commit->parents;
145 entry = p;
146 if (p) {
147 p = p->next;
148 while (p) {
149 nr += count_distance(p);
150 p = p->next;
155 return nr;
158 static void clear_distance(struct commit_list *list)
160 while (list) {
161 struct commit *commit = list->item;
162 commit->object.flags &= ~COUNTED;
163 list = list->next;
167 #define DEBUG_BISECT 0
169 static inline int weight(struct commit_list *elem)
171 return *((int*)(elem->item->util));
174 static inline void weight_set(struct commit_list *elem, int weight)
176 *((int*)(elem->item->util)) = weight;
179 static int count_interesting_parents(struct commit *commit)
181 struct commit_list *p;
182 int count;
184 for (count = 0, p = commit->parents; p; p = p->next) {
185 if (p->item->object.flags & UNINTERESTING)
186 continue;
187 count++;
189 return count;
192 static inline int halfway(struct commit_list *p, int distance, int nr)
195 * Don't short-cut something we are not going to return!
197 if (revs.prune_fn && !(p->item->object.flags & TREECHANGE))
198 return 0;
199 if (DEBUG_BISECT)
200 return 0;
202 * 2 and 3 are halfway of 5.
203 * 3 is halfway of 6 but 2 and 4 are not.
205 distance *= 2;
206 switch (distance - nr) {
207 case -1: case 0: case 1:
208 return 1;
209 default:
210 return 0;
214 #if !DEBUG_BISECT
215 #define show_list(a,b,c,d) do { ; } while (0)
216 #else
217 static void show_list(const char *debug, int counted, int nr,
218 struct commit_list *list)
220 struct commit_list *p;
222 fprintf(stderr, "%s (%d/%d)\n", debug, counted, nr);
224 for (p = list; p; p = p->next) {
225 struct commit_list *pp;
226 struct commit *commit = p->item;
227 unsigned flags = commit->object.flags;
228 enum object_type type;
229 unsigned long size;
230 char *buf = read_sha1_file(commit->object.sha1, &type, &size);
231 char *ep, *sp;
233 fprintf(stderr, "%c%c%c ",
234 (flags & TREECHANGE) ? 'T' : ' ',
235 (flags & UNINTERESTING) ? 'U' : ' ',
236 (flags & COUNTED) ? 'C' : ' ');
237 if (commit->util)
238 fprintf(stderr, "%3d", weight(p));
239 else
240 fprintf(stderr, "---");
241 fprintf(stderr, " %.*s", 8, sha1_to_hex(commit->object.sha1));
242 for (pp = commit->parents; pp; pp = pp->next)
243 fprintf(stderr, " %.*s", 8,
244 sha1_to_hex(pp->item->object.sha1));
246 sp = strstr(buf, "\n\n");
247 if (sp) {
248 sp += 2;
249 for (ep = sp; *ep && *ep != '\n'; ep++)
251 fprintf(stderr, " %.*s", (int)(ep - sp), sp);
253 fprintf(stderr, "\n");
256 #endif /* DEBUG_BISECT */
259 * zero or positive weight is the number of interesting commits it can
260 * reach, including itself. Especially, weight = 0 means it does not
261 * reach any tree-changing commits (e.g. just above uninteresting one
262 * but traversal is with pathspec).
264 * weight = -1 means it has one parent and its distance is yet to
265 * be computed.
267 * weight = -2 means it has more than one parent and its distance is
268 * unknown. After running count_distance() first, they will get zero
269 * or positive distance.
272 static struct commit_list *find_bisection(struct commit_list *list,
273 int *reaches, int *all)
275 int n, nr, on_list, counted, distance;
276 struct commit_list *p, *best, *next, *last;
277 int *weights;
279 show_list("bisection 2 entry", 0, 0, list);
282 * Count the number of total and tree-changing items on the
283 * list, while reversing the list.
285 for (nr = on_list = 0, last = NULL, p = list;
287 p = next) {
288 unsigned flags = p->item->object.flags;
290 next = p->next;
291 if (flags & UNINTERESTING)
292 continue;
293 p->next = last;
294 last = p;
295 if (!revs.prune_fn || (flags & TREECHANGE))
296 nr++;
297 on_list++;
299 list = last;
300 show_list("bisection 2 sorted", 0, nr, list);
302 *all = nr;
303 weights = xcalloc(on_list, sizeof(int*));
304 counted = 0;
306 for (n = 0, p = list; p; p = p->next) {
307 struct commit *commit = p->item;
308 unsigned flags = commit->object.flags;
310 p->item->util = &weights[n++];
311 switch (count_interesting_parents(commit)) {
312 case 0:
313 if (!revs.prune_fn || (flags & TREECHANGE)) {
314 weight_set(p, 1);
315 counted++;
316 show_list("bisection 2 count one",
317 counted, nr, list);
320 * otherwise, it is known not to reach any
321 * tree-changing commit and gets weight 0.
323 break;
324 case 1:
325 weight_set(p, -1);
326 break;
327 default:
328 weight_set(p, -2);
329 break;
333 show_list("bisection 2 initialize", counted, nr, list);
336 * If you have only one parent in the resulting set
337 * then you can reach one commit more than that parent
338 * can reach. So we do not have to run the expensive
339 * count_distance() for single strand of pearls.
341 * However, if you have more than one parents, you cannot
342 * just add their distance and one for yourself, since
343 * they usually reach the same ancestor and you would
344 * end up counting them twice that way.
346 * So we will first count distance of merges the usual
347 * way, and then fill the blanks using cheaper algorithm.
349 for (p = list; p; p = p->next) {
350 if (p->item->object.flags & UNINTERESTING)
351 continue;
352 n = weight(p);
353 if (n != -2)
354 continue;
355 distance = count_distance(p);
356 clear_distance(list);
357 weight_set(p, distance);
359 /* Does it happen to be at exactly half-way? */
360 if (halfway(p, distance, nr)) {
361 p->next = NULL;
362 *reaches = distance;
363 free(weights);
364 return p;
366 counted++;
369 show_list("bisection 2 count_distance", counted, nr, list);
371 while (counted < nr) {
372 for (p = list; p; p = p->next) {
373 struct commit_list *q;
374 unsigned flags = p->item->object.flags;
376 if (0 <= weight(p))
377 continue;
378 for (q = p->item->parents; q; q = q->next) {
379 if (q->item->object.flags & UNINTERESTING)
380 continue;
381 if (0 <= weight(q))
382 break;
384 if (!q)
385 continue;
388 * weight for p is unknown but q is known.
389 * add one for p itself if p is to be counted,
390 * otherwise inherit it from q directly.
392 if (!revs.prune_fn || (flags & TREECHANGE)) {
393 weight_set(p, weight(q)+1);
394 counted++;
395 show_list("bisection 2 count one",
396 counted, nr, list);
398 else
399 weight_set(p, weight(q));
401 /* Does it happen to be at exactly half-way? */
402 distance = weight(p);
403 if (halfway(p, distance, nr)) {
404 p->next = NULL;
405 *reaches = distance;
406 free(weights);
407 return p;
412 show_list("bisection 2 counted all", counted, nr, list);
414 /* Then find the best one */
415 counted = -1;
416 best = list;
417 for (p = list; p; p = p->next) {
418 unsigned flags = p->item->object.flags;
420 if (revs.prune_fn && !(flags & TREECHANGE))
421 continue;
422 distance = weight(p);
423 if (nr - distance < distance)
424 distance = nr - distance;
425 if (distance > counted) {
426 best = p;
427 counted = distance;
428 *reaches = weight(p);
431 if (best)
432 best->next = NULL;
433 free(weights);
434 return best;
437 static void read_revisions_from_stdin(struct rev_info *revs)
439 char line[1000];
441 while (fgets(line, sizeof(line), stdin) != NULL) {
442 int len = strlen(line);
443 if (line[len - 1] == '\n')
444 line[--len] = 0;
445 if (!len)
446 break;
447 if (line[0] == '-')
448 die("options not supported in --stdin mode");
449 if (handle_revision_arg(line, revs, 0, 1))
450 die("bad revision '%s'", line);
454 int cmd_rev_list(int argc, const char **argv, const char *prefix)
456 struct commit_list *list;
457 int i;
458 int read_from_stdin = 0;
459 int bisect_show_vars = 0;
461 git_config(git_default_config);
462 init_revisions(&revs, prefix);
463 revs.abbrev = 0;
464 revs.commit_format = CMIT_FMT_UNSPECIFIED;
465 argc = setup_revisions(argc, argv, &revs, NULL);
467 for (i = 1 ; i < argc; i++) {
468 const char *arg = argv[i];
470 if (!strcmp(arg, "--header")) {
471 revs.verbose_header = 1;
472 continue;
474 if (!strcmp(arg, "--timestamp")) {
475 show_timestamp = 1;
476 continue;
478 if (!strcmp(arg, "--bisect")) {
479 bisect_list = 1;
480 continue;
482 if (!strcmp(arg, "--bisect-vars")) {
483 bisect_list = 1;
484 bisect_show_vars = 1;
485 continue;
487 if (!strcmp(arg, "--stdin")) {
488 if (read_from_stdin++)
489 die("--stdin given twice?");
490 read_revisions_from_stdin(&revs);
491 continue;
493 usage(rev_list_usage);
496 if (revs.commit_format != CMIT_FMT_UNSPECIFIED) {
497 /* The command line has a --pretty */
498 hdr_termination = '\n';
499 if (revs.commit_format == CMIT_FMT_ONELINE)
500 header_prefix = "";
501 else
502 header_prefix = "commit ";
504 else if (revs.verbose_header)
505 /* Only --header was specified */
506 revs.commit_format = CMIT_FMT_RAW;
508 list = revs.commits;
510 if ((!list &&
511 (!(revs.tag_objects||revs.tree_objects||revs.blob_objects) &&
512 !revs.pending.nr)) ||
513 revs.diff)
514 usage(rev_list_usage);
516 save_commit_buffer = revs.verbose_header || revs.grep_filter;
517 track_object_refs = 0;
518 if (bisect_list)
519 revs.limited = 1;
521 prepare_revision_walk(&revs);
522 if (revs.tree_objects)
523 mark_edges_uninteresting(revs.commits, &revs, show_edge);
525 if (bisect_list) {
526 int reaches = reaches, all = all;
528 revs.commits = find_bisection(revs.commits, &reaches, &all);
529 if (bisect_show_vars) {
530 int cnt;
531 if (!revs.commits)
532 return 1;
534 * revs.commits can reach "reaches" commits among
535 * "all" commits. If it is good, then there are
536 * (all-reaches) commits left to be bisected.
537 * On the other hand, if it is bad, then the set
538 * to bisect is "reaches".
539 * A bisect set of size N has (N-1) commits further
540 * to test, as we already know one bad one.
542 cnt = all-reaches;
543 if (cnt < reaches)
544 cnt = reaches;
545 printf("bisect_rev=%s\n"
546 "bisect_nr=%d\n"
547 "bisect_good=%d\n"
548 "bisect_bad=%d\n"
549 "bisect_all=%d\n",
550 sha1_to_hex(revs.commits->item->object.sha1),
551 cnt - 1,
552 all - reaches - 1,
553 reaches - 1,
554 all);
555 return 0;
559 traverse_commit_list(&revs, show_commit, show_object);
561 return 0;