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 "epoch.h"
8
9#define SEEN (1u << 0)
10#define INTERESTING (1u << 1)
11#define COUNTED (1u << 2)
12#define SHOWN (1u << 3)
13
14static const char rev_list_usage[] =
15 "git-rev-list [OPTION] commit-id <commit-id>\n"
16 " --max-count=nr\n"
17 " --max-age=epoch\n"
18 " --min-age=epoch\n"
19 " --parents\n"
20 " --bisect\n"
21 " --objects\n"
22 " --unpacked\n"
23 " --header\n"
24 " --pretty\n"
25 " --no-merges\n"
26 " --merge-order [ --show-breaks ]\n"
27 " --topo-order";
28
29static int unpacked = 0;
30static int bisect_list = 0;
31static int tag_objects = 0;
32static int tree_objects = 0;
33static int blob_objects = 0;
34static int verbose_header = 0;
35static int show_parents = 0;
36static int hdr_termination = 0;
37static const char *commit_prefix = "";
38static unsigned long max_age = -1;
39static unsigned long min_age = -1;
40static int max_count = -1;
41static enum cmit_fmt commit_format = CMIT_FMT_RAW;
42static int merge_order = 0;
43static int show_breaks = 0;
44static int stop_traversal = 0;
45static int topo_order = 0;
46static int no_merges = 0;
47
48static void show_commit(struct commit *commit)
49{
50 commit->object.flags |= SHOWN;
51 if (show_breaks) {
52 commit_prefix = "| ";
53 if (commit->object.flags & DISCONTINUITY) {
54 commit_prefix = "^ ";
55 } else if (commit->object.flags & BOUNDARY) {
56 commit_prefix = "= ";
57 }
58 }
59 printf("%s%s", commit_prefix, sha1_to_hex(commit->object.sha1));
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 }
67 if (commit_format == CMIT_FMT_ONELINE)
68 putchar(' ');
69 else
70 putchar('\n');
71
72 if (verbose_header) {
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);
76 }
77 fflush(stdout);
78}
79
80static int filter_commit(struct commit * commit)
81{
82 if (stop_traversal && (commit->object.flags & BOUNDARY))
83 return STOP;
84 if (commit->object.flags & (UNINTERESTING|SHOWN))
85 return CONTINUE;
86 if (min_age != -1 && (commit->date > min_age))
87 return CONTINUE;
88 if (max_age != -1 && (commit->date < max_age)) {
89 stop_traversal=1;
90 return CONTINUE;
91 }
92 if (max_count != -1 && !max_count--)
93 return STOP;
94 if (no_merges && (commit->parents && commit->parents->next))
95 return CONTINUE;
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;
109 }
110
111 show_commit(commit);
112
113 return CONTINUE;
114}
115
116static struct object_list **add_object(struct object *obj, struct object_list **p, const char *name)
117{
118 struct object_list *entry = xmalloc(sizeof(*entry));
119 entry->item = obj;
120 entry->next = *p;
121 entry->name = name;
122 *p = entry;
123 return &entry->next;
124}
125
126static struct object_list **process_blob(struct blob *blob, struct object_list **p, const char *name)
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;
135 return add_object(obj, p, name);
136}
137
138static struct object_list **process_tree(struct tree *tree, struct object_list **p, const char *name)
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;
150 p = add_object(obj, p, name);
151 entry = tree->entries;
152 tree->entries = NULL;
153 while (entry) {
154 struct tree_entry_list *next = entry->next;
155 if (entry->directory)
156 p = process_tree(entry->item.tree, p, entry->name);
157 else
158 p = process_blob(entry->item.blob, p, entry->name);
159 free(entry);
160 entry = next;
161 }
162 return p;
163}
164
165static struct object_list *pending_objects = NULL;
166
167static void show_commit_list(struct commit_list *list)
168{
169 struct object_list *objects = NULL, **p = &objects, *pending;
170 while (list) {
171 struct commit *commit = pop_most_recent_commit(&list, SEEN);
172
173 p = process_tree(commit->tree, p, "");
174 if (process_commit(commit) == STOP)
175 break;
176 }
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 }
197 while (objects) {
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);
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;
232 if (!has_sha1_file(obj->sha1))
233 return;
234 if (parse_tree(tree) < 0)
235 die("bad tree %s", sha1_to_hex(obj->sha1));
236 entry = tree->entries;
237 tree->entries = NULL;
238 while (entry) {
239 struct tree_entry_list *next = entry->next;
240 if (entry->directory)
241 mark_tree_uninteresting(entry->item.tree);
242 else
243 mark_blob_uninteresting(entry->item.blob);
244 free(entry);
245 entry = next;
246 }
247}
248
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;
256
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
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;
278 parents = parents->next;
279 }
280}
281
282static int everybody_uninteresting(struct commit_list *orig)
283{
284 struct commit_list *list = orig;
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
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
327static void clear_distance(struct commit_list *list)
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
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
380static struct commit_list *limit_list(struct commit_list *list)
381{
382 struct commit_list *newlist = NULL;
383 struct commit_list **p = &newlist;
384 while (list) {
385 struct commit *commit = pop_most_recent_commit(&list, SEEN);
386 struct object *obj = &commit->object;
387
388 if (max_age != -1 && (commit->date < max_age))
389 obj->flags |= UNINTERESTING;
390 if (unpacked && has_sha1_pack(obj->sha1))
391 obj->flags |= UNINTERESTING;
392 if (obj->flags & UNINTERESTING) {
393 mark_parents_uninteresting(commit);
394 if (everybody_uninteresting(list))
395 break;
396 continue;
397 }
398 if (min_age != -1 && (commit->date > min_age))
399 continue;
400 p = &commit_list_insert(commit, p)->next;
401 }
402 if (tree_objects)
403 mark_edges_uninteresting(newlist);
404 if (bisect_list)
405 newlist = find_bisection(newlist);
406 return newlist;
407}
408
409static void add_pending_object(struct object *obj, const char *name)
410{
411 add_object(obj, &pending_objects, name);
412}
413
414static struct commit *get_commit_reference(const char *name, unsigned int flags)
415{
416 unsigned char sha1[20];
417 struct object *object;
418
419 if (get_sha1(name, sha1))
420 usage(rev_list_usage);
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 */
428 while (object->type == tag_type) {
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);
433 object = parse_object(tag->tagged->sha1);
434 if (!object)
435 die("bad object %s", sha1_to_hex(tag->tagged->sha1));
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);
447 if (flags & UNINTERESTING)
448 mark_parents_uninteresting(commit);
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)
459 return NULL;
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)
474 return NULL;
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);
483}
484
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
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}
509
510int main(int argc, char **argv)
511{
512 struct commit_list *list = NULL;
513 int i, limited = 0;
514
515 setup_git_directory();
516 for (i = 1 ; i < argc; i++) {
517 int flags;
518 char *arg = argv[i];
519 char *dotdot;
520 struct commit *commit;
521
522 if (!strncmp(arg, "--max-count=", 12)) {
523 max_count = atoi(arg + 12);
524 continue;
525 }
526 if (!strncmp(arg, "--max-age=", 10)) {
527 max_age = atoi(arg + 10);
528 limited = 1;
529 continue;
530 }
531 if (!strncmp(arg, "--min-age=", 10)) {
532 min_age = atoi(arg + 10);
533 limited = 1;
534 continue;
535 }
536 if (!strcmp(arg, "--header")) {
537 verbose_header = 1;
538 continue;
539 }
540 if (!strncmp(arg, "--pretty", 8)) {
541 commit_format = get_commit_format(arg+8);
542 verbose_header = 1;
543 hdr_termination = '\n';
544 if (commit_format == CMIT_FMT_ONELINE)
545 commit_prefix = "";
546 else
547 commit_prefix = "commit ";
548 continue;
549 }
550 if (!strncmp(arg, "--no-merges", 11)) {
551 no_merges = 1;
552 continue;
553 }
554 if (!strcmp(arg, "--parents")) {
555 show_parents = 1;
556 continue;
557 }
558 if (!strcmp(arg, "--bisect")) {
559 bisect_list = 1;
560 continue;
561 }
562 if (!strcmp(arg, "--all")) {
563 handle_all(&list);
564 continue;
565 }
566 if (!strcmp(arg, "--objects")) {
567 tag_objects = 1;
568 tree_objects = 1;
569 blob_objects = 1;
570 continue;
571 }
572 if (!strcmp(arg, "--unpacked")) {
573 unpacked = 1;
574 limited = 1;
575 continue;
576 }
577 if (!strcmp(arg, "--merge-order")) {
578 merge_order = 1;
579 continue;
580 }
581 if (!strcmp(arg, "--show-breaks")) {
582 show_breaks = 1;
583 continue;
584 }
585 if (!strcmp(arg, "--topo-order")) {
586 topo_order = 1;
587 limited = 1;
588 continue;
589 }
590
591 if (show_breaks && !merge_order)
592 usage(rev_list_usage);
593
594 flags = 0;
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;
601 if (!*next)
602 next = "HEAD";
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 }
611 *dotdot = '.';
612 }
613 if (*arg == '^') {
614 flags = UNINTERESTING;
615 arg++;
616 limited = 1;
617 }
618 commit = get_commit_reference(arg, flags);
619 handle_one_commit(commit, &list);
620 }
621
622 save_commit_buffer = verbose_header;
623 track_object_refs = 0;
624
625 if (!merge_order) {
626 sort_by_date(&list);
627 if (limited)
628 list = limit_list(list);
629 if (topo_order)
630 sort_in_topological_order(&list);
631 show_commit_list(list);
632 } else {
633#ifndef NO_OPENSSL
634 if (sort_list_in_merge_order(list, &process_commit)) {
635 die("merge order sort failed\n");
636 }
637#else
638 die("merge order sort unsupported, OpenSSL not linked");
639#endif
640 }
641
642 return 0;
643}