]> git.ipfire.org Git - thirdparty/git.git/blame - rev-list.c
Split up tree diff functions into tree-diff.c library
[thirdparty/git.git] / 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"
a3437b8c 7#include "epoch.h"
64745109 8
8906300f
LT
9#define SEEN (1u << 0)
10#define INTERESTING (1u << 1)
8b3a1e05 11#define COUNTED (1u << 2)
bce62866 12#define SHOWN (1u << 3)
8906300f 13
a6f68d47 14static const char rev_list_usage[] =
54c6870e 15 "git-rev-list [OPTION] commit-id <commit-id>\n"
a6f68d47
LT
16 " --max-count=nr\n"
17 " --max-age=epoch\n"
18 " --min-age=epoch\n"
76cd8eb6 19 " --parents\n"
12d2a187
LT
20 " --bisect\n"
21 " --objects\n"
22 " --unpacked\n"
9d97aa64 23 " --header\n"
a3437b8c 24 " --pretty\n"
76cd8eb6
JS
25 " --no-merges\n"
26 " --merge-order [ --show-breaks ]\n"
27 " --topo-order";
a6f68d47 28
12d2a187 29static int unpacked = 0;
8b3a1e05 30static int bisect_list = 0;
3c90f03d 31static int tag_objects = 0;
9de48752
LT
32static int tree_objects = 0;
33static int blob_objects = 0;
81f2bb1f
LT
34static int verbose_header = 0;
35static int show_parents = 0;
81f2bb1f 36static int hdr_termination = 0;
d998a089 37static const char *commit_prefix = "";
81f2bb1f
LT
38static unsigned long max_age = -1;
39static unsigned long min_age = -1;
40static int max_count = -1;
000182ea 41static enum cmit_fmt commit_format = CMIT_FMT_RAW;
a3437b8c
JS
42static int merge_order = 0;
43static int show_breaks = 0;
5e749e25 44static int stop_traversal = 0;
d2d02a49 45static int topo_order = 0;
76cd8eb6 46static int no_merges = 0;
81f2bb1f
LT
47
48static void show_commit(struct commit *commit)
49{
51b1e171 50 commit->object.flags |= SHOWN;
a3437b8c 51 if (show_breaks) {
d998a089 52 commit_prefix = "| ";
a3437b8c 53 if (commit->object.flags & DISCONTINUITY) {
d998a089 54 commit_prefix = "^ ";
a3437b8c 55 } else if (commit->object.flags & BOUNDARY) {
d998a089 56 commit_prefix = "= ";
a3437b8c
JS
57 }
58 }
d998a089 59 printf("%s%s", commit_prefix, sha1_to_hex(commit->object.sha1));
81f2bb1f
LT
60 if (show_parents) {
61 struct commit_list *parents = commit->parents;
62 while (parents) {
63 printf(" %s", sha1_to_hex(parents->item->object.sha1));
64 parents = parents->next;
65 }
66 }
d87449c5
JH
67 if (commit_format == CMIT_FMT_ONELINE)
68 putchar(' ');
69 else
70 putchar('\n');
71
81f2bb1f 72 if (verbose_header) {
000182ea
LT
73 static char pretty_header[16384];
74 pretty_print_commit(commit_format, commit->buffer, ~0, pretty_header, sizeof(pretty_header));
75 printf("%s%c", pretty_header, hdr_termination);
7620d39f
LT
76 }
77 fflush(stdout);
a3437b8c
JS
78}
79
80static int filter_commit(struct commit * commit)
81{
d2775a81 82 if (stop_traversal && (commit->object.flags & BOUNDARY))
5e749e25 83 return STOP;
51b1e171 84 if (commit->object.flags & (UNINTERESTING|SHOWN))
a3437b8c
JS
85 return CONTINUE;
86 if (min_age != -1 && (commit->date > min_age))
87 return CONTINUE;
5e749e25 88 if (max_age != -1 && (commit->date < max_age)) {
d2775a81 89 stop_traversal=1;
27cfe2e2 90 return CONTINUE;
5e749e25 91 }
a3437b8c
JS
92 if (max_count != -1 && !max_count--)
93 return STOP;
76cd8eb6
JS
94 if (no_merges && (commit->parents && commit->parents->next))
95 return CONTINUE;
a3437b8c
JS
96 return DO;
97}
98
99static int process_commit(struct commit * commit)
100{
101 int action=filter_commit(commit);
102
103 if (action == STOP) {
104 return STOP;
105 }
106
107 if (action == CONTINUE) {
108 return CONTINUE;
81f2bb1f 109 }
a3437b8c
JS
110
111 show_commit(commit);
112
113 return CONTINUE;
81f2bb1f
LT
114}
115
9ce43d1c 116static struct object_list **add_object(struct object *obj, struct object_list **p, const char *name)
9de48752
LT
117{
118 struct object_list *entry = xmalloc(sizeof(*entry));
119 entry->item = obj;
36f8d174 120 entry->next = *p;
9ce43d1c 121 entry->name = name;
9de48752
LT
122 *p = entry;
123 return &entry->next;
124}
125
9ce43d1c 126static struct object_list **process_blob(struct blob *blob, struct object_list **p, const char *name)
9de48752
LT
127{
128 struct object *obj = &blob->object;
129
130 if (!blob_objects)
131 return p;
132 if (obj->flags & (UNINTERESTING | SEEN))
133 return p;
134 obj->flags |= SEEN;
9ce43d1c 135 return add_object(obj, p, name);
9de48752
LT
136}
137
9ce43d1c 138static struct object_list **process_tree(struct tree *tree, struct object_list **p, const char *name)
9de48752
LT
139{
140 struct object *obj = &tree->object;
141 struct tree_entry_list *entry;
142
143 if (!tree_objects)
144 return p;
145 if (obj->flags & (UNINTERESTING | SEEN))
146 return p;
147 if (parse_tree(tree) < 0)
148 die("bad tree object %s", sha1_to_hex(obj->sha1));
149 obj->flags |= SEEN;
9ce43d1c 150 p = add_object(obj, p, name);
b0d8923e
LT
151 entry = tree->entries;
152 tree->entries = NULL;
153 while (entry) {
154 struct tree_entry_list *next = entry->next;
9de48752 155 if (entry->directory)
9ce43d1c 156 p = process_tree(entry->item.tree, p, entry->name);
9de48752 157 else
9ce43d1c 158 p = process_blob(entry->item.blob, p, entry->name);
b0d8923e
LT
159 free(entry);
160 entry = next;
9de48752
LT
161 }
162 return p;
163}
164
36f8d174
LT
165static struct object_list *pending_objects = NULL;
166
81f2bb1f
LT
167static void show_commit_list(struct commit_list *list)
168{
36f8d174 169 struct object_list *objects = NULL, **p = &objects, *pending;
81f2bb1f
LT
170 while (list) {
171 struct commit *commit = pop_most_recent_commit(&list, SEEN);
172
9ce43d1c 173 p = process_tree(commit->tree, p, "");
a3437b8c 174 if (process_commit(commit) == STOP)
81f2bb1f 175 break;
81f2bb1f 176 }
36f8d174
LT
177 for (pending = pending_objects; pending; pending = pending->next) {
178 struct object *obj = pending->item;
179 const char *name = pending->name;
180 if (obj->flags & (UNINTERESTING | SEEN))
181 continue;
182 if (obj->type == tag_type) {
183 obj->flags |= SEEN;
184 p = add_object(obj, p, name);
185 continue;
186 }
187 if (obj->type == tree_type) {
188 p = process_tree((struct tree *)obj, p, name);
189 continue;
190 }
191 if (obj->type == blob_type) {
192 p = process_blob((struct blob *)obj, p, name);
193 continue;
194 }
195 die("unknown pending object %s (%s)", sha1_to_hex(obj->sha1), name);
196 }
9de48752 197 while (objects) {
c807f771
JH
198 /* An object with name "foo\n0000000000000000000000000000000000000000"
199 * can be used confuse downstream git-pack-objects very badly.
200 */
201 const char *ep = strchr(objects->name, '\n');
202 if (ep) {
203 printf("%s %.*s\n", sha1_to_hex(objects->item->sha1),
204 (int) (ep - objects->name),
205 objects->name);
206 }
207 else
208 printf("%s %s\n", sha1_to_hex(objects->item->sha1), objects->name);
9de48752
LT
209 objects = objects->next;
210 }
211}
212
213static void mark_blob_uninteresting(struct blob *blob)
214{
215 if (!blob_objects)
216 return;
217 if (blob->object.flags & UNINTERESTING)
218 return;
219 blob->object.flags |= UNINTERESTING;
220}
221
222static void mark_tree_uninteresting(struct tree *tree)
223{
224 struct object *obj = &tree->object;
225 struct tree_entry_list *entry;
226
227 if (!tree_objects)
228 return;
229 if (obj->flags & UNINTERESTING)
230 return;
231 obj->flags |= UNINTERESTING;
454fbbcd
LT
232 if (!has_sha1_file(obj->sha1))
233 return;
9de48752
LT
234 if (parse_tree(tree) < 0)
235 die("bad tree %s", sha1_to_hex(obj->sha1));
236 entry = tree->entries;
b0d8923e 237 tree->entries = NULL;
9de48752 238 while (entry) {
b0d8923e 239 struct tree_entry_list *next = entry->next;
9de48752
LT
240 if (entry->directory)
241 mark_tree_uninteresting(entry->item.tree);
242 else
243 mark_blob_uninteresting(entry->item.blob);
b0d8923e
LT
244 free(entry);
245 entry = next;
9de48752 246 }
81f2bb1f
LT
247}
248
8906300f
LT
249static void mark_parents_uninteresting(struct commit *commit)
250{
251 struct commit_list *parents = commit->parents;
252
253 while (parents) {
254 struct commit *commit = parents->item;
255 commit->object.flags |= UNINTERESTING;
454fbbcd 256
6c3b84c8
LT
257 /*
258 * Normally we haven't parsed the parent
259 * yet, so we won't have a parent of a parent
260 * here. However, it may turn out that we've
261 * reached this commit some other way (where it
262 * wasn't uninteresting), in which case we need
263 * to mark its parents recursively too..
264 */
265 if (commit->parents)
266 mark_parents_uninteresting(commit);
267
454fbbcd
LT
268 /*
269 * A missing commit is ok iff its parent is marked
270 * uninteresting.
271 *
272 * We just mark such a thing parsed, so that when
273 * it is popped next time around, we won't be trying
274 * to parse it and get an error.
275 */
276 if (!has_sha1_file(commit->object.sha1))
277 commit->object.parsed = 1;
8906300f
LT
278 parents = parents->next;
279 }
280}
281
4311d328 282static int everybody_uninteresting(struct commit_list *orig)
8906300f 283{
4311d328 284 struct commit_list *list = orig;
8906300f
LT
285 while (list) {
286 struct commit *commit = list->item;
287 list = list->next;
288 if (commit->object.flags & UNINTERESTING)
289 continue;
290 return 0;
291 }
292 return 1;
293}
294
8b3a1e05
LT
295/*
296 * This is a truly stupid algorithm, but it's only
297 * used for bisection, and we just don't care enough.
298 *
299 * We care just barely enough to avoid recursing for
300 * non-merge entries.
301 */
302static int count_distance(struct commit_list *entry)
303{
304 int nr = 0;
305
306 while (entry) {
307 struct commit *commit = entry->item;
308 struct commit_list *p;
309
310 if (commit->object.flags & (UNINTERESTING | COUNTED))
311 break;
312 nr++;
313 commit->object.flags |= COUNTED;
314 p = commit->parents;
315 entry = p;
316 if (p) {
317 p = p->next;
318 while (p) {
319 nr += count_distance(p);
320 p = p->next;
321 }
322 }
323 }
324 return nr;
325}
326
3d958064 327static void clear_distance(struct commit_list *list)
8b3a1e05
LT
328{
329 while (list) {
330 struct commit *commit = list->item;
331 commit->object.flags &= ~COUNTED;
332 list = list->next;
333 }
334}
335
336static struct commit_list *find_bisection(struct commit_list *list)
337{
338 int nr, closest;
339 struct commit_list *p, *best;
340
341 nr = 0;
342 p = list;
343 while (p) {
344 nr++;
345 p = p->next;
346 }
347 closest = 0;
348 best = list;
349
350 p = list;
351 while (p) {
352 int distance = count_distance(p);
353 clear_distance(list);
354 if (nr - distance < distance)
355 distance = nr - distance;
356 if (distance > closest) {
357 best = p;
358 closest = distance;
359 }
360 p = p->next;
361 }
362 if (best)
363 best->next = NULL;
364 return best;
365}
366
5bdbaaa4
LT
367static void mark_edges_uninteresting(struct commit_list *list)
368{
369 for ( ; list; list = list->next) {
370 struct commit_list *parents = list->item->parents;
371
372 for ( ; parents; parents = parents->next) {
373 struct commit *commit = parents->item;
374 if (commit->object.flags & UNINTERESTING)
375 mark_tree_uninteresting(commit->tree);
376 }
377 }
378}
379
6da4016a 380static struct commit_list *limit_list(struct commit_list *list)
3b42a63c
LT
381{
382 struct commit_list *newlist = NULL;
383 struct commit_list **p = &newlist;
36f8d174 384 while (list) {
3b42a63c
LT
385 struct commit *commit = pop_most_recent_commit(&list, SEEN);
386 struct object *obj = &commit->object;
387
27cfe2e2
LT
388 if (max_age != -1 && (commit->date < max_age))
389 obj->flags |= UNINTERESTING;
12d2a187
LT
390 if (unpacked && has_sha1_pack(obj->sha1))
391 obj->flags |= UNINTERESTING;
337cb3fb 392 if (obj->flags & UNINTERESTING) {
3b42a63c
LT
393 mark_parents_uninteresting(commit);
394 if (everybody_uninteresting(list))
395 break;
396 continue;
397 }
27cfe2e2
LT
398 if (min_age != -1 && (commit->date > min_age))
399 continue;
3b42a63c 400 p = &commit_list_insert(commit, p)->next;
36f8d174 401 }
5bdbaaa4
LT
402 if (tree_objects)
403 mark_edges_uninteresting(newlist);
8b3a1e05
LT
404 if (bisect_list)
405 newlist = find_bisection(newlist);
3b42a63c
LT
406 return newlist;
407}
408
36f8d174
LT
409static void add_pending_object(struct object *obj, const char *name)
410{
411 add_object(obj, &pending_objects, name);
412}
413
3c90f03d
LT
414static struct commit *get_commit_reference(const char *name, unsigned int flags)
415{
416 unsigned char sha1[20];
36f8d174 417 struct object *object;
3c90f03d
LT
418
419 if (get_sha1(name, sha1))
420 usage(rev_list_usage);
36f8d174
LT
421 object = parse_object(sha1);
422 if (!object)
423 die("bad object %s", name);
424
425 /*
426 * Tag object? Look what it points to..
427 */
013aab82 428 while (object->type == tag_type) {
36f8d174
LT
429 struct tag *tag = (struct tag *) object;
430 object->flags |= flags;
431 if (tag_objects && !(object->flags & UNINTERESTING))
432 add_pending_object(object, tag->tag);
013aab82 433 object = parse_object(tag->tagged->sha1);
7f1335c7
SV
434 if (!object)
435 die("bad object %s", sha1_to_hex(tag->tagged->sha1));
36f8d174
LT
436 }
437
438 /*
439 * Commit object? Just return it, we'll do all the complex
440 * reachability crud.
441 */
442 if (object->type == commit_type) {
443 struct commit *commit = (struct commit *)object;
444 object->flags |= flags;
445 if (parse_commit(commit) < 0)
446 die("unable to parse commit %s", name);
454fbbcd
LT
447 if (flags & UNINTERESTING)
448 mark_parents_uninteresting(commit);
36f8d174
LT
449 return commit;
450 }
451
452 /*
453 * Tree object? Either mark it uniniteresting, or add it
454 * to the list of objects to look at later..
455 */
456 if (object->type == tree_type) {
457 struct tree *tree = (struct tree *)object;
458 if (!tree_objects)
960bba0d 459 return NULL;
36f8d174
LT
460 if (flags & UNINTERESTING) {
461 mark_tree_uninteresting(tree);
462 return NULL;
463 }
464 add_pending_object(object, "");
465 return NULL;
466 }
467
468 /*
469 * Blob object? You know the drill by now..
470 */
471 if (object->type == blob_type) {
472 struct blob *blob = (struct blob *)object;
473 if (!blob_objects)
960bba0d 474 return NULL;
36f8d174
LT
475 if (flags & UNINTERESTING) {
476 mark_blob_uninteresting(blob);
477 return NULL;
478 }
479 add_pending_object(object, "");
480 return NULL;
481 }
482 die("%s is unknown object", name);
3c90f03d
LT
483}
484
1215879c
JH
485static void handle_one_commit(struct commit *com, struct commit_list **lst)
486{
487 if (!com || com->object.flags & SEEN)
488 return;
489 com->object.flags |= SEEN;
490 commit_list_insert(com, lst);
491}
492
e091eb93
JH
493/* for_each_ref() callback does not allow user data -- Yuck. */
494static struct commit_list **global_lst;
495
496static int include_one_commit(const char *path, const unsigned char *sha1)
497{
498 struct commit *com = get_commit_reference(path, 0);
499 handle_one_commit(com, global_lst);
500 return 0;
501}
502
503static void handle_all(struct commit_list **lst)
504{
505 global_lst = lst;
506 for_each_ref(include_one_commit);
507 global_lst = NULL;
508}
1215879c 509
64745109
LT
510int main(int argc, char **argv)
511{
64745109 512 struct commit_list *list = NULL;
337cb3fb 513 int i, limited = 0;
64745109 514
d998a089 515 setup_git_directory();
fcfda02b 516 for (i = 1 ; i < argc; i++) {
337cb3fb 517 int flags;
fcfda02b 518 char *arg = argv[i];
1215879c 519 char *dotdot;
337cb3fb 520 struct commit *commit;
fcfda02b
KS
521
522 if (!strncmp(arg, "--max-count=", 12)) {
523 max_count = atoi(arg + 12);
a6f68d47
LT
524 continue;
525 }
526 if (!strncmp(arg, "--max-age=", 10)) {
fcfda02b 527 max_age = atoi(arg + 10);
27cfe2e2 528 limited = 1;
a6f68d47
LT
529 continue;
530 }
531 if (!strncmp(arg, "--min-age=", 10)) {
fcfda02b 532 min_age = atoi(arg + 10);
27cfe2e2 533 limited = 1;
a6f68d47 534 continue;
fcfda02b 535 }
a6f68d47
LT
536 if (!strcmp(arg, "--header")) {
537 verbose_header = 1;
538 continue;
539 }
000182ea
LT
540 if (!strncmp(arg, "--pretty", 8)) {
541 commit_format = get_commit_format(arg+8);
9d97aa64 542 verbose_header = 1;
9d97aa64 543 hdr_termination = '\n';
d87449c5 544 if (commit_format == CMIT_FMT_ONELINE)
d998a089 545 commit_prefix = "";
d87449c5 546 else
d998a089 547 commit_prefix = "commit ";
9d97aa64
LT
548 continue;
549 }
76cd8eb6
JS
550 if (!strncmp(arg, "--no-merges", 11)) {
551 no_merges = 1;
552 continue;
553 }
97658004
LT
554 if (!strcmp(arg, "--parents")) {
555 show_parents = 1;
556 continue;
557 }
8b3a1e05
LT
558 if (!strcmp(arg, "--bisect")) {
559 bisect_list = 1;
560 continue;
561 }
e091eb93
JH
562 if (!strcmp(arg, "--all")) {
563 handle_all(&list);
564 continue;
565 }
9de48752 566 if (!strcmp(arg, "--objects")) {
3c90f03d 567 tag_objects = 1;
9de48752
LT
568 tree_objects = 1;
569 blob_objects = 1;
570 continue;
571 }
12d2a187
LT
572 if (!strcmp(arg, "--unpacked")) {
573 unpacked = 1;
574 limited = 1;
575 continue;
576 }
12ba7eaf 577 if (!strcmp(arg, "--merge-order")) {
a3437b8c
JS
578 merge_order = 1;
579 continue;
580 }
12ba7eaf 581 if (!strcmp(arg, "--show-breaks")) {
a3437b8c
JS
582 show_breaks = 1;
583 continue;
584 }
d2d02a49
LT
585 if (!strcmp(arg, "--topo-order")) {
586 topo_order = 1;
e6c3505b 587 limited = 1;
d2d02a49
LT
588 continue;
589 }
a6f68d47 590
1215879c
JH
591 if (show_breaks && !merge_order)
592 usage(rev_list_usage);
593
337cb3fb 594 flags = 0;
1215879c
JH
595 dotdot = strstr(arg, "..");
596 if (dotdot) {
597 char *next = dotdot + 2;
598 struct commit *exclude = NULL;
599 struct commit *include = NULL;
600 *dotdot = 0;
2a7055ae
LT
601 if (!*next)
602 next = "HEAD";
1215879c
JH
603 exclude = get_commit_reference(arg, UNINTERESTING);
604 include = get_commit_reference(next, 0);
605 if (exclude && include) {
606 limited = 1;
607 handle_one_commit(exclude, &list);
608 handle_one_commit(include, &list);
609 continue;
610 }
2a7055ae 611 *dotdot = '.';
1215879c 612 }
337cb3fb
LT
613 if (*arg == '^') {
614 flags = UNINTERESTING;
615 arg++;
616 limited = 1;
617 }
3c90f03d 618 commit = get_commit_reference(arg, flags);
1215879c 619 handle_one_commit(commit, &list);
fcfda02b
KS
620 }
621
60ab26de 622 save_commit_buffer = verbose_header;
8805ccac 623 track_object_refs = 0;
60ab26de 624
a3437b8c 625 if (!merge_order) {
a7336ae5 626 sort_by_date(&list);
fe5f51ce
LT
627 if (list && !limited && max_count == 1 &&
628 !tag_objects && !tree_objects && !blob_objects) {
629 show_commit(list->item);
630 return 0;
631 }
17ebe977 632 if (limited)
a3437b8c 633 list = limit_list(list);
d2d02a49
LT
634 if (topo_order)
635 sort_in_topological_order(&list);
a3437b8c 636 show_commit_list(list);
a3437b8c 637 } else {
dd53c7ab 638#ifndef NO_OPENSSL
a3437b8c 639 if (sort_list_in_merge_order(list, &process_commit)) {
dd53c7ab 640 die("merge order sort failed\n");
a3437b8c 641 }
dd53c7ab
PB
642#else
643 die("merge order sort unsupported, OpenSSL not linked");
644#endif
a3437b8c 645 }
8906300f 646
64745109
LT
647 return 0;
648}