]> git.ipfire.org Git - thirdparty/git.git/blob - builtin-rev-list.c
get_pathspec(): die when an out-of-tree path is given
[thirdparty/git.git] / builtin-rev-list.c
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"
13
14 /* bits #0-15 in revision.h */
15
16 #define COUNTED (1u<<16)
17
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 " --branches\n"
29 " --tags\n"
30 " --remotes\n"
31 " --stdin\n"
32 " --quiet\n"
33 " ordering output:\n"
34 " --topo-order\n"
35 " --date-order\n"
36 " formatting output:\n"
37 " --parents\n"
38 " --objects | --objects-edge\n"
39 " --unpacked\n"
40 " --header | --pretty\n"
41 " --abbrev=nr | --no-abbrev\n"
42 " --abbrev-commit\n"
43 " --left-right\n"
44 " special purpose:\n"
45 " --bisect\n"
46 " --bisect-vars\n"
47 " --bisect-all"
48 ;
49
50 static struct rev_info revs;
51
52 static int bisect_list;
53 static int show_timestamp;
54 static int hdr_termination;
55 static const char *header_prefix;
56
57 static void finish_commit(struct commit *commit);
58 static void show_commit(struct commit *commit)
59 {
60 if (show_timestamp)
61 printf("%lu ", commit->date);
62 if (header_prefix)
63 fputs(header_prefix, stdout);
64 if (commit->object.flags & BOUNDARY)
65 putchar('-');
66 else if (commit->object.flags & UNINTERESTING)
67 putchar('^');
68 else if (revs.left_right) {
69 if (commit->object.flags & SYMMETRIC_LEFT)
70 putchar('<');
71 else
72 putchar('>');
73 }
74 if (revs.abbrev_commit && revs.abbrev)
75 fputs(find_unique_abbrev(commit->object.sha1, revs.abbrev),
76 stdout);
77 else
78 fputs(sha1_to_hex(commit->object.sha1), stdout);
79 if (revs.parents) {
80 struct commit_list *parents = commit->parents;
81 while (parents) {
82 printf(" %s", sha1_to_hex(parents->item->object.sha1));
83 parents = parents->next;
84 }
85 }
86 show_decorations(commit);
87 if (revs.commit_format == CMIT_FMT_ONELINE)
88 putchar(' ');
89 else
90 putchar('\n');
91
92 if (revs.verbose_header && commit->buffer) {
93 struct strbuf buf;
94 strbuf_init(&buf, 0);
95 pretty_print_commit(revs.commit_format, commit,
96 &buf, revs.abbrev, NULL, NULL,
97 revs.date_mode, 0);
98 if (buf.len)
99 printf("%s%c", buf.buf, hdr_termination);
100 strbuf_release(&buf);
101 }
102 maybe_flush_or_die(stdout, "stdout");
103 finish_commit(commit);
104 }
105
106 static void finish_commit(struct commit *commit)
107 {
108 if (commit->parents) {
109 free_commit_list(commit->parents);
110 commit->parents = NULL;
111 }
112 free(commit->buffer);
113 commit->buffer = NULL;
114 }
115
116 static void finish_object(struct object_array_entry *p)
117 {
118 if (p->item->type == OBJ_BLOB && !has_sha1_file(p->item->sha1))
119 die("missing blob object '%s'", sha1_to_hex(p->item->sha1));
120 }
121
122 static void show_object(struct object_array_entry *p)
123 {
124 /* An object with name "foo\n0000000..." can be used to
125 * confuse downstream git-pack-objects very badly.
126 */
127 const char *ep = strchr(p->name, '\n');
128
129 finish_object(p);
130 if (ep) {
131 printf("%s %.*s\n", sha1_to_hex(p->item->sha1),
132 (int) (ep - p->name),
133 p->name);
134 }
135 else
136 printf("%s %s\n", sha1_to_hex(p->item->sha1), p->name);
137 }
138
139 static void show_edge(struct commit *commit)
140 {
141 printf("-%s\n", sha1_to_hex(commit->object.sha1));
142 }
143
144 /*
145 * This is a truly stupid algorithm, but it's only
146 * used for bisection, and we just don't care enough.
147 *
148 * We care just barely enough to avoid recursing for
149 * non-merge entries.
150 */
151 static int count_distance(struct commit_list *entry)
152 {
153 int nr = 0;
154
155 while (entry) {
156 struct commit *commit = entry->item;
157 struct commit_list *p;
158
159 if (commit->object.flags & (UNINTERESTING | COUNTED))
160 break;
161 if (!(commit->object.flags & TREESAME))
162 nr++;
163 commit->object.flags |= COUNTED;
164 p = commit->parents;
165 entry = p;
166 if (p) {
167 p = p->next;
168 while (p) {
169 nr += count_distance(p);
170 p = p->next;
171 }
172 }
173 }
174
175 return nr;
176 }
177
178 static void clear_distance(struct commit_list *list)
179 {
180 while (list) {
181 struct commit *commit = list->item;
182 commit->object.flags &= ~COUNTED;
183 list = list->next;
184 }
185 }
186
187 #define DEBUG_BISECT 0
188
189 static inline int weight(struct commit_list *elem)
190 {
191 return *((int*)(elem->item->util));
192 }
193
194 static inline void weight_set(struct commit_list *elem, int weight)
195 {
196 *((int*)(elem->item->util)) = weight;
197 }
198
199 static int count_interesting_parents(struct commit *commit)
200 {
201 struct commit_list *p;
202 int count;
203
204 for (count = 0, p = commit->parents; p; p = p->next) {
205 if (p->item->object.flags & UNINTERESTING)
206 continue;
207 count++;
208 }
209 return count;
210 }
211
212 static inline int halfway(struct commit_list *p, int nr)
213 {
214 /*
215 * Don't short-cut something we are not going to return!
216 */
217 if (p->item->object.flags & TREESAME)
218 return 0;
219 if (DEBUG_BISECT)
220 return 0;
221 /*
222 * 2 and 3 are halfway of 5.
223 * 3 is halfway of 6 but 2 and 4 are not.
224 */
225 switch (2 * weight(p) - nr) {
226 case -1: case 0: case 1:
227 return 1;
228 default:
229 return 0;
230 }
231 }
232
233 #if !DEBUG_BISECT
234 #define show_list(a,b,c,d) do { ; } while (0)
235 #else
236 static void show_list(const char *debug, int counted, int nr,
237 struct commit_list *list)
238 {
239 struct commit_list *p;
240
241 fprintf(stderr, "%s (%d/%d)\n", debug, counted, nr);
242
243 for (p = list; p; p = p->next) {
244 struct commit_list *pp;
245 struct commit *commit = p->item;
246 unsigned flags = commit->object.flags;
247 enum object_type type;
248 unsigned long size;
249 char *buf = read_sha1_file(commit->object.sha1, &type, &size);
250 char *ep, *sp;
251
252 fprintf(stderr, "%c%c%c ",
253 (flags & TREESAME) ? ' ' : 'T',
254 (flags & UNINTERESTING) ? 'U' : ' ',
255 (flags & COUNTED) ? 'C' : ' ');
256 if (commit->util)
257 fprintf(stderr, "%3d", weight(p));
258 else
259 fprintf(stderr, "---");
260 fprintf(stderr, " %.*s", 8, sha1_to_hex(commit->object.sha1));
261 for (pp = commit->parents; pp; pp = pp->next)
262 fprintf(stderr, " %.*s", 8,
263 sha1_to_hex(pp->item->object.sha1));
264
265 sp = strstr(buf, "\n\n");
266 if (sp) {
267 sp += 2;
268 for (ep = sp; *ep && *ep != '\n'; ep++)
269 ;
270 fprintf(stderr, " %.*s", (int)(ep - sp), sp);
271 }
272 fprintf(stderr, "\n");
273 }
274 }
275 #endif /* DEBUG_BISECT */
276
277 static struct commit_list *best_bisection(struct commit_list *list, int nr)
278 {
279 struct commit_list *p, *best;
280 int best_distance = -1;
281
282 best = list;
283 for (p = list; p; p = p->next) {
284 int distance;
285 unsigned flags = p->item->object.flags;
286
287 if (flags & TREESAME)
288 continue;
289 distance = weight(p);
290 if (nr - distance < distance)
291 distance = nr - distance;
292 if (distance > best_distance) {
293 best = p;
294 best_distance = distance;
295 }
296 }
297
298 return best;
299 }
300
301 struct commit_dist {
302 struct commit *commit;
303 int distance;
304 };
305
306 static int compare_commit_dist(const void *a_, const void *b_)
307 {
308 struct commit_dist *a, *b;
309
310 a = (struct commit_dist *)a_;
311 b = (struct commit_dist *)b_;
312 if (a->distance != b->distance)
313 return b->distance - a->distance; /* desc sort */
314 return hashcmp(a->commit->object.sha1, b->commit->object.sha1);
315 }
316
317 static struct commit_list *best_bisection_sorted(struct commit_list *list, int nr)
318 {
319 struct commit_list *p;
320 struct commit_dist *array = xcalloc(nr, sizeof(*array));
321 int cnt, i;
322
323 for (p = list, cnt = 0; p; p = p->next) {
324 int distance;
325 unsigned flags = p->item->object.flags;
326
327 if (flags & TREESAME)
328 continue;
329 distance = weight(p);
330 if (nr - distance < distance)
331 distance = nr - distance;
332 array[cnt].commit = p->item;
333 array[cnt].distance = distance;
334 cnt++;
335 }
336 qsort(array, cnt, sizeof(*array), compare_commit_dist);
337 for (p = list, i = 0; i < cnt; i++) {
338 struct name_decoration *r = xmalloc(sizeof(*r) + 100);
339 struct object *obj = &(array[i].commit->object);
340
341 sprintf(r->name, "dist=%d", array[i].distance);
342 r->next = add_decoration(&name_decoration, obj, r);
343 p->item = array[i].commit;
344 p = p->next;
345 }
346 if (p)
347 p->next = NULL;
348 free(array);
349 return list;
350 }
351
352 /*
353 * zero or positive weight is the number of interesting commits it can
354 * reach, including itself. Especially, weight = 0 means it does not
355 * reach any tree-changing commits (e.g. just above uninteresting one
356 * but traversal is with pathspec).
357 *
358 * weight = -1 means it has one parent and its distance is yet to
359 * be computed.
360 *
361 * weight = -2 means it has more than one parent and its distance is
362 * unknown. After running count_distance() first, they will get zero
363 * or positive distance.
364 */
365 static struct commit_list *do_find_bisection(struct commit_list *list,
366 int nr, int *weights,
367 int find_all)
368 {
369 int n, counted;
370 struct commit_list *p;
371
372 counted = 0;
373
374 for (n = 0, p = list; p; p = p->next) {
375 struct commit *commit = p->item;
376 unsigned flags = commit->object.flags;
377
378 p->item->util = &weights[n++];
379 switch (count_interesting_parents(commit)) {
380 case 0:
381 if (!(flags & TREESAME)) {
382 weight_set(p, 1);
383 counted++;
384 show_list("bisection 2 count one",
385 counted, nr, list);
386 }
387 /*
388 * otherwise, it is known not to reach any
389 * tree-changing commit and gets weight 0.
390 */
391 break;
392 case 1:
393 weight_set(p, -1);
394 break;
395 default:
396 weight_set(p, -2);
397 break;
398 }
399 }
400
401 show_list("bisection 2 initialize", counted, nr, list);
402
403 /*
404 * If you have only one parent in the resulting set
405 * then you can reach one commit more than that parent
406 * can reach. So we do not have to run the expensive
407 * count_distance() for single strand of pearls.
408 *
409 * However, if you have more than one parents, you cannot
410 * just add their distance and one for yourself, since
411 * they usually reach the same ancestor and you would
412 * end up counting them twice that way.
413 *
414 * So we will first count distance of merges the usual
415 * way, and then fill the blanks using cheaper algorithm.
416 */
417 for (p = list; p; p = p->next) {
418 if (p->item->object.flags & UNINTERESTING)
419 continue;
420 if (weight(p) != -2)
421 continue;
422 weight_set(p, count_distance(p));
423 clear_distance(list);
424
425 /* Does it happen to be at exactly half-way? */
426 if (!find_all && halfway(p, nr))
427 return p;
428 counted++;
429 }
430
431 show_list("bisection 2 count_distance", counted, nr, list);
432
433 while (counted < nr) {
434 for (p = list; p; p = p->next) {
435 struct commit_list *q;
436 unsigned flags = p->item->object.flags;
437
438 if (0 <= weight(p))
439 continue;
440 for (q = p->item->parents; q; q = q->next) {
441 if (q->item->object.flags & UNINTERESTING)
442 continue;
443 if (0 <= weight(q))
444 break;
445 }
446 if (!q)
447 continue;
448
449 /*
450 * weight for p is unknown but q is known.
451 * add one for p itself if p is to be counted,
452 * otherwise inherit it from q directly.
453 */
454 if (!(flags & TREESAME)) {
455 weight_set(p, weight(q)+1);
456 counted++;
457 show_list("bisection 2 count one",
458 counted, nr, list);
459 }
460 else
461 weight_set(p, weight(q));
462
463 /* Does it happen to be at exactly half-way? */
464 if (!find_all && halfway(p, nr))
465 return p;
466 }
467 }
468
469 show_list("bisection 2 counted all", counted, nr, list);
470
471 if (!find_all)
472 return best_bisection(list, nr);
473 else
474 return best_bisection_sorted(list, nr);
475 }
476
477 static struct commit_list *find_bisection(struct commit_list *list,
478 int *reaches, int *all,
479 int find_all)
480 {
481 int nr, on_list;
482 struct commit_list *p, *best, *next, *last;
483 int *weights;
484
485 show_list("bisection 2 entry", 0, 0, list);
486
487 /*
488 * Count the number of total and tree-changing items on the
489 * list, while reversing the list.
490 */
491 for (nr = on_list = 0, last = NULL, p = list;
492 p;
493 p = next) {
494 unsigned flags = p->item->object.flags;
495
496 next = p->next;
497 if (flags & UNINTERESTING)
498 continue;
499 p->next = last;
500 last = p;
501 if (!(flags & TREESAME))
502 nr++;
503 on_list++;
504 }
505 list = last;
506 show_list("bisection 2 sorted", 0, nr, list);
507
508 *all = nr;
509 weights = xcalloc(on_list, sizeof(*weights));
510
511 /* Do the real work of finding bisection commit. */
512 best = do_find_bisection(list, nr, weights, find_all);
513 if (best) {
514 if (!find_all)
515 best->next = NULL;
516 *reaches = weight(best);
517 }
518 free(weights);
519 return best;
520 }
521
522 static void read_revisions_from_stdin(struct rev_info *revs)
523 {
524 char line[1000];
525
526 while (fgets(line, sizeof(line), stdin) != NULL) {
527 int len = strlen(line);
528 if (len && line[len - 1] == '\n')
529 line[--len] = 0;
530 if (!len)
531 break;
532 if (line[0] == '-')
533 die("options not supported in --stdin mode");
534 if (handle_revision_arg(line, revs, 0, 1))
535 die("bad revision '%s'", line);
536 }
537 }
538
539 int cmd_rev_list(int argc, const char **argv, const char *prefix)
540 {
541 struct commit_list *list;
542 int i;
543 int read_from_stdin = 0;
544 int bisect_show_vars = 0;
545 int bisect_find_all = 0;
546 int quiet = 0;
547
548 git_config(git_default_config);
549 init_revisions(&revs, prefix);
550 revs.abbrev = 0;
551 revs.commit_format = CMIT_FMT_UNSPECIFIED;
552 argc = setup_revisions(argc, argv, &revs, NULL);
553
554 for (i = 1 ; i < argc; i++) {
555 const char *arg = argv[i];
556
557 if (!strcmp(arg, "--header")) {
558 revs.verbose_header = 1;
559 continue;
560 }
561 if (!strcmp(arg, "--timestamp")) {
562 show_timestamp = 1;
563 continue;
564 }
565 if (!strcmp(arg, "--bisect")) {
566 bisect_list = 1;
567 continue;
568 }
569 if (!strcmp(arg, "--bisect-all")) {
570 bisect_list = 1;
571 bisect_find_all = 1;
572 continue;
573 }
574 if (!strcmp(arg, "--bisect-vars")) {
575 bisect_list = 1;
576 bisect_show_vars = 1;
577 continue;
578 }
579 if (!strcmp(arg, "--stdin")) {
580 if (read_from_stdin++)
581 die("--stdin given twice?");
582 read_revisions_from_stdin(&revs);
583 continue;
584 }
585 if (!strcmp(arg, "--quiet")) {
586 quiet = 1;
587 continue;
588 }
589 usage(rev_list_usage);
590
591 }
592 if (revs.commit_format != CMIT_FMT_UNSPECIFIED) {
593 /* The command line has a --pretty */
594 hdr_termination = '\n';
595 if (revs.commit_format == CMIT_FMT_ONELINE)
596 header_prefix = "";
597 else
598 header_prefix = "commit ";
599 }
600 else if (revs.verbose_header)
601 /* Only --header was specified */
602 revs.commit_format = CMIT_FMT_RAW;
603
604 list = revs.commits;
605
606 if ((!list &&
607 (!(revs.tag_objects||revs.tree_objects||revs.blob_objects) &&
608 !revs.pending.nr)) ||
609 revs.diff)
610 usage(rev_list_usage);
611
612 save_commit_buffer = revs.verbose_header || revs.grep_filter;
613 if (bisect_list)
614 revs.limited = 1;
615
616 if (prepare_revision_walk(&revs))
617 die("revision walk setup failed");
618 if (revs.tree_objects)
619 mark_edges_uninteresting(revs.commits, &revs, show_edge);
620
621 if (bisect_list) {
622 int reaches = reaches, all = all;
623
624 revs.commits = find_bisection(revs.commits, &reaches, &all,
625 bisect_find_all);
626 if (bisect_show_vars) {
627 int cnt;
628 char hex[41];
629 if (!revs.commits)
630 return 1;
631 /*
632 * revs.commits can reach "reaches" commits among
633 * "all" commits. If it is good, then there are
634 * (all-reaches) commits left to be bisected.
635 * On the other hand, if it is bad, then the set
636 * to bisect is "reaches".
637 * A bisect set of size N has (N-1) commits further
638 * to test, as we already know one bad one.
639 */
640 cnt = all - reaches;
641 if (cnt < reaches)
642 cnt = reaches;
643 strcpy(hex, sha1_to_hex(revs.commits->item->object.sha1));
644
645 if (bisect_find_all) {
646 traverse_commit_list(&revs, show_commit, show_object);
647 printf("------\n");
648 }
649
650 printf("bisect_rev=%s\n"
651 "bisect_nr=%d\n"
652 "bisect_good=%d\n"
653 "bisect_bad=%d\n"
654 "bisect_all=%d\n",
655 hex,
656 cnt - 1,
657 all - reaches - 1,
658 reaches - 1,
659 all);
660 return 0;
661 }
662 }
663
664 traverse_commit_list(&revs,
665 quiet ? finish_commit : show_commit,
666 quiet ? finish_object : show_object);
667
668 return 0;
669 }