Lift 16kB limit of log message output
[git/git.git] / builtin-rev-list.c
CommitLineData
64745109 1#include "cache.h"
e091eb93 2#include "refs.h"
36f8d174 3#include "tag.h"
64745109 4#include "commit.h"
9de48752
LT
5#include "tree.h"
6#include "blob.h"
1b0c7174 7#include "tree-walk.h"
c4e05b1a 8#include "diff.h"
ae563542 9#include "revision.h"
c64ed70d 10#include "list-objects.h"
5fb61b8d 11#include "builtin.h"
ae563542 12
1b65a5aa 13/* bits #0-15 in revision.h */
64745109 14
1b65a5aa 15#define COUNTED (1u<<16)
8906300f 16
a6f68d47 17static const char rev_list_usage[] =
69e0c256
JH
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"
93b74bca 25" --remove-empty\n"
69e0c256 26" --all\n"
42cabc34 27" --stdin\n"
69e0c256 28" ordering output:\n"
69e0c256 29" --topo-order\n"
4c8725f1 30" --date-order\n"
69e0c256
JH
31" formatting output:\n"
32" --parents\n"
c6496575 33" --objects | --objects-edge\n"
69e0c256
JH
34" --unpacked\n"
35" --header | --pretty\n"
9da5c2f0 36" --abbrev=nr | --no-abbrev\n"
5c51c985 37" --abbrev-commit\n"
b24bace5 38" --left-right\n"
69e0c256 39" special purpose:\n"
457f08a0
JH
40" --bisect\n"
41" --bisect-vars"
69e0c256 42;
a6f68d47 43
5fb61b8d 44static struct rev_info revs;
ae563542 45
96f1e58f
DR
46static int bisect_list;
47static int show_timestamp;
48static int hdr_termination;
91539833 49static const char *header_prefix;
e646de0d 50
81f2bb1f
LT
51static void show_commit(struct commit *commit)
52{
dc68c4ff
JH
53 if (show_timestamp)
54 printf("%lu ", commit->date);
91539833
LT
55 if (header_prefix)
56 fputs(header_prefix, stdout);
384e99a4
JH
57 if (commit->object.flags & BOUNDARY)
58 putchar('-');
74bd9029 59 else if (revs.left_right) {
577ed5c2
JH
60 if (commit->object.flags & SYMMETRIC_LEFT)
61 putchar('<');
62 else
63 putchar('>');
64 }
7594c4b2
JH
65 if (revs.abbrev_commit && revs.abbrev)
66 fputs(find_unique_abbrev(commit->object.sha1, revs.abbrev),
67 stdout);
5c51c985
JH
68 else
69 fputs(sha1_to_hex(commit->object.sha1), stdout);
7b0c9966 70 if (revs.parents) {
81f2bb1f
LT
71 struct commit_list *parents = commit->parents;
72 while (parents) {
88494423 73 struct object *o = &(parents->item->object);
81f2bb1f 74 parents = parents->next;
88494423
JH
75 if (o->flags & TMP_MARK)
76 continue;
77 printf(" %s", sha1_to_hex(o->sha1));
78 o->flags |= TMP_MARK;
81f2bb1f 79 }
88494423
JH
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.
83 */
84 for (parents = commit->parents;
85 parents;
86 parents = parents->next)
87 parents->item->object.flags &= ~TMP_MARK;
81f2bb1f 88 }
7594c4b2 89 if (revs.commit_format == CMIT_FMT_ONELINE)
d87449c5
JH
90 putchar(' ');
91 else
92 putchar('\n');
93
7594c4b2 94 if (revs.verbose_header) {
80583c0e
JH
95 char *buf = NULL;
96 unsigned long buflen = 0;
7594c4b2 97 pretty_print_commit(revs.commit_format, commit, ~0,
80583c0e 98 &buf, &buflen,
a7b02ccf 99 revs.abbrev, NULL, NULL, revs.date_mode);
80583c0e
JH
100 printf("%s%c", buf, hdr_termination);
101 free(buf);
7620d39f
LT
102 }
103 fflush(stdout);
cb115748
LT
104 if (commit->parents) {
105 free_commit_list(commit->parents);
106 commit->parents = NULL;
107 }
4cac42b1
JH
108 free(commit->buffer);
109 commit->buffer = NULL;
a3437b8c
JS
110}
111
c64ed70d 112static void show_object(struct object_array_entry *p)
9de48752 113{
c64ed70d
JH
114 /* An object with name "foo\n0000000..." can be used to
115 * confuse downstream git-pack-objects very badly.
116 */
117 const char *ep = strchr(p->name, '\n');
b9849a1a
JH
118
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));
121
c64ed70d
JH
122 if (ep) {
123 printf("%s %.*s\n", sha1_to_hex(p->item->sha1),
124 (int) (ep - p->name),
125 p->name);
9de48752 126 }
c64ed70d
JH
127 else
128 printf("%s %s\n", sha1_to_hex(p->item->sha1), p->name);
9de48752
LT
129}
130
8d1d8f83
JH
131static void show_edge(struct commit *commit)
132{
133 printf("-%s\n", sha1_to_hex(commit->object.sha1));
134}
135
8b3a1e05
LT
136/*
137 * This is a truly stupid algorithm, but it's only
138 * used for bisection, and we just don't care enough.
139 *
140 * We care just barely enough to avoid recursing for
141 * non-merge entries.
142 */
143static int count_distance(struct commit_list *entry)
144{
145 int nr = 0;
146
147 while (entry) {
148 struct commit *commit = entry->item;
149 struct commit_list *p;
150
151 if (commit->object.flags & (UNINTERESTING | COUNTED))
152 break;
8efdc326 153 if (!revs.prune_fn || (commit->object.flags & TREECHANGE))
b3cfd939 154 nr++;
8b3a1e05
LT
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;
163 }
164 }
165 }
b3cfd939 166
8b3a1e05
LT
167 return nr;
168}
169
3d958064 170static void clear_distance(struct commit_list *list)
8b3a1e05
LT
171{
172 while (list) {
173 struct commit *commit = list->item;
174 commit->object.flags &= ~COUNTED;
175 list = list->next;
176 }
177}
178
1daa09d9 179#define DEBUG_BISECT 0
1c4fea3a
JH
180
181static inline int weight(struct commit_list *elem)
182{
183 return *((int*)(elem->item->util));
184}
185
186static inline void weight_set(struct commit_list *elem, int weight)
187{
188 *((int*)(elem->item->util)) = weight;
189}
190
1daa09d9 191static int count_interesting_parents(struct commit *commit)
1c4fea3a 192{
1daa09d9
JH
193 struct commit_list *p;
194 int count;
195
196 for (count = 0, p = commit->parents; p; p = p->next) {
197 if (p->item->object.flags & UNINTERESTING)
198 continue;
199 count++;
1c4fea3a 200 }
1daa09d9 201 return count;
1c4fea3a
JH
202}
203
2a464690
JH
204static inline int halfway(struct commit_list *p, int distance, int nr)
205{
206 /*
207 * Don't short-cut something we are not going to return!
208 */
209 if (revs.prune_fn && !(p->item->object.flags & TREECHANGE))
210 return 0;
1daa09d9
JH
211 if (DEBUG_BISECT)
212 return 0;
2a464690
JH
213 /*
214 * 2 and 3 are halfway of 5.
215 * 3 is halfway of 6 but 2 and 4 are not.
216 */
217 distance *= 2;
218 switch (distance - nr) {
219 case -1: case 0: case 1:
220 return 1;
221 default:
222 return 0;
223 }
224}
225
1daa09d9
JH
226#if !DEBUG_BISECT
227#define show_list(a,b,c,d) do { ; } while (0)
228#else
229static void show_list(const char *debug, int counted, int nr,
230 struct commit_list *list)
1c4fea3a 231{
1daa09d9
JH
232 struct commit_list *p;
233
234 fprintf(stderr, "%s (%d/%d)\n", debug, counted, nr);
235
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;
244
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));
257
258 sp = strstr(buf, "\n\n");
259 if (sp) {
260 sp += 2;
261 for (ep = sp; *ep && *ep != '\n'; ep++)
262 ;
263 fprintf(stderr, " %.*s", (int)(ep - sp), sp);
264 }
265 fprintf(stderr, "\n");
266 }
267}
268#endif /* DEBUG_BISECT */
269
270/*
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).
275 *
276 * weight = -1 means it has one parent and its distance is yet to
277 * be computed.
278 *
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.
282 */
283
284static struct commit_list *find_bisection(struct commit_list *list,
285 int *reaches, int *all)
286{
287 int n, nr, on_list, counted, distance;
288 struct commit_list *p, *best, *next, *last;
1c4fea3a
JH
289 int *weights;
290
1daa09d9
JH
291 show_list("bisection 2 entry", 0, 0, list);
292
293 /*
294 * Count the number of total and tree-changing items on the
295 * list, while reversing the list.
296 */
297 for (nr = on_list = 0, last = NULL, p = list;
298 p;
299 p = next) {
300 unsigned flags = p->item->object.flags;
301
302 next = p->next;
303 if (flags & UNINTERESTING)
304 continue;
305 p->next = last;
306 last = p;
307 if (!revs.prune_fn || (flags & TREECHANGE))
1c4fea3a 308 nr++;
1daa09d9 309 on_list++;
1c4fea3a 310 }
1daa09d9
JH
311 list = last;
312 show_list("bisection 2 sorted", 0, nr, list);
313
1c4fea3a 314 *all = nr;
1daa09d9 315 weights = xcalloc(on_list, sizeof(int*));
1c4fea3a
JH
316 counted = 0;
317
318 for (n = 0, p = list; p; p = p->next) {
1daa09d9
JH
319 struct commit *commit = p->item;
320 unsigned flags = commit->object.flags;
321
322 p->item->util = &weights[n++];
323 switch (count_interesting_parents(commit)) {
324 case 0:
325 if (!revs.prune_fn || (flags & TREECHANGE)) {
1c4fea3a
JH
326 weight_set(p, 1);
327 counted++;
1daa09d9
JH
328 show_list("bisection 2 count one",
329 counted, nr, list);
1c4fea3a 330 }
1daa09d9
JH
331 /*
332 * otherwise, it is known not to reach any
333 * tree-changing commit and gets weight 0.
334 */
335 break;
336 case 1:
337 weight_set(p, -1);
338 break;
339 default:
340 weight_set(p, -2);
341 break;
1c4fea3a
JH
342 }
343 }
344
1daa09d9
JH
345 show_list("bisection 2 initialize", counted, nr, list);
346
1c4fea3a
JH
347 /*
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.
352 *
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.
357 *
358 * So we will first count distance of merges the usual
359 * way, and then fill the blanks using cheaper algorithm.
360 */
361 for (p = list; p; p = p->next) {
1daa09d9 362 if (p->item->object.flags & UNINTERESTING)
1c4fea3a
JH
363 continue;
364 n = weight(p);
1daa09d9 365 if (n != -2)
1c4fea3a
JH
366 continue;
367 distance = count_distance(p);
1daa09d9 368 clear_distance(list);
1c4fea3a
JH
369 weight_set(p, distance);
370
371 /* Does it happen to be at exactly half-way? */
2a464690 372 if (halfway(p, distance, nr)) {
1c4fea3a 373 p->next = NULL;
2a464690 374 *reaches = distance;
1c4fea3a
JH
375 free(weights);
376 return p;
377 }
378 counted++;
379 }
380
1daa09d9
JH
381 show_list("bisection 2 count_distance", counted, nr, list);
382
1c4fea3a
JH
383 while (counted < nr) {
384 for (p = list; p; p = p->next) {
385 struct commit_list *q;
1daa09d9 386 unsigned flags = p->item->object.flags;
1c4fea3a 387
1daa09d9 388 if (0 <= weight(p))
1c4fea3a 389 continue;
1daa09d9
JH
390 for (q = p->item->parents; q; q = q->next) {
391 if (q->item->object.flags & UNINTERESTING)
392 continue;
393 if (0 <= weight(q))
1c4fea3a 394 break;
1daa09d9 395 }
1c4fea3a
JH
396 if (!q)
397 continue;
1daa09d9
JH
398
399 /*
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.
403 */
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);
409 }
410 else
411 weight_set(p, weight(q));
1c4fea3a
JH
412
413 /* Does it happen to be at exactly half-way? */
2a464690
JH
414 distance = weight(p);
415 if (halfway(p, distance, nr)) {
1c4fea3a 416 p->next = NULL;
2a464690 417 *reaches = distance;
1c4fea3a
JH
418 free(weights);
419 return p;
420 }
421 }
422 }
423
1daa09d9
JH
424 show_list("bisection 2 counted all", counted, nr, list);
425
1c4fea3a 426 /* Then find the best one */
1daa09d9 427 counted = -1;
1c4fea3a
JH
428 best = list;
429 for (p = list; p; p = p->next) {
1daa09d9
JH
430 unsigned flags = p->item->object.flags;
431
432 if (revs.prune_fn && !(flags & TREECHANGE))
1c4fea3a
JH
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);
441 }
442 }
443 if (best)
444 best->next = NULL;
445 free(weights);
446 return best;
447}
448
42cabc34
JH
449static void read_revisions_from_stdin(struct rev_info *revs)
450{
451 char line[1000];
452
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);
463 }
464}
465
a633fca0 466int cmd_rev_list(int argc, const char **argv, const char *prefix)
64745109 467{
ae563542 468 struct commit_list *list;
d9a83684 469 int i;
42cabc34 470 int read_from_stdin = 0;
457f08a0 471 int bisect_show_vars = 0;
64745109 472
256c3fe6 473 git_config(git_default_config);
a633fca0 474 init_revisions(&revs, prefix);
7594c4b2
JH
475 revs.abbrev = 0;
476 revs.commit_format = CMIT_FMT_UNSPECIFIED;
a4a88b2b 477 argc = setup_revisions(argc, argv, &revs, NULL);
ae563542 478
fcfda02b 479 for (i = 1 ; i < argc; i++) {
cf484544 480 const char *arg = argv[i];
fcfda02b 481
a6f68d47 482 if (!strcmp(arg, "--header")) {
7594c4b2 483 revs.verbose_header = 1;
9d97aa64
LT
484 continue;
485 }
dc68c4ff
JH
486 if (!strcmp(arg, "--timestamp")) {
487 show_timestamp = 1;
488 continue;
489 }
8b3a1e05
LT
490 if (!strcmp(arg, "--bisect")) {
491 bisect_list = 1;
492 continue;
493 }
457f08a0
JH
494 if (!strcmp(arg, "--bisect-vars")) {
495 bisect_list = 1;
496 bisect_show_vars = 1;
497 continue;
498 }
42cabc34
JH
499 if (!strcmp(arg, "--stdin")) {
500 if (read_from_stdin++)
501 die("--stdin given twice?");
502 read_revisions_from_stdin(&revs);
503 continue;
504 }
ae563542 505 usage(rev_list_usage);
a6f68d47 506
fcfda02b 507 }
7594c4b2
JH
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)
91539833 512 header_prefix = "";
7594c4b2 513 else
91539833 514 header_prefix = "commit ";
7594c4b2 515 }
db89665f
JH
516 else if (revs.verbose_header)
517 /* Only --header was specified */
518 revs.commit_format = CMIT_FMT_RAW;
fcfda02b 519
ae563542 520 list = revs.commits;
ae563542 521
8c1f0b44
JH
522 if ((!list &&
523 (!(revs.tag_objects||revs.tree_objects||revs.blob_objects) &&
1f1e895f 524 !revs.pending.nr)) ||
8c1f0b44 525 revs.diff)
7b34c2fa
LT
526 usage(rev_list_usage);
527
2d10c555 528 save_commit_buffer = revs.verbose_header || revs.grep_filter;
9181ca2c 529 track_object_refs = 0;
4e1dc640
JH
530 if (bisect_list)
531 revs.limited = 1;
9181ca2c 532
a4a88b2b
LT
533 prepare_revision_walk(&revs);
534 if (revs.tree_objects)
8d1d8f83 535 mark_edges_uninteresting(revs.commits, &revs, show_edge);
a4a88b2b 536
457f08a0
JH
537 if (bisect_list) {
538 int reaches = reaches, all = all;
539
1daa09d9 540 revs.commits = find_bisection(revs.commits, &reaches, &all);
457f08a0
JH
541 if (bisect_show_vars) {
542 int cnt;
543 if (!revs.commits)
544 return 1;
545 /*
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.
553 */
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;
568 }
569 }
7b34c2fa 570
c64ed70d 571 traverse_commit_list(&revs, show_commit, show_object);
8906300f 572
64745109
LT
573 return 0;
574}