git-rev-list: use proper lazy reachability analysis
[git/git.git] / rev-list.c
CommitLineData
64745109
LT
1#include "cache.h"
2#include "commit.h"
3
8906300f
LT
4#define SEEN (1u << 0)
5#define INTERESTING (1u << 1)
6#define UNINTERESTING (1u << 2)
7
a6f68d47
LT
8static const char rev_list_usage[] =
9 "usage: git-rev-list [OPTION] commit-id <commit-id>\n"
10 " --max-count=nr\n"
11 " --max-age=epoch\n"
12 " --min-age=epoch\n"
13 " --header";
14
8906300f
LT
15static void mark_parents_uninteresting(struct commit *commit)
16{
17 struct commit_list *parents = commit->parents;
18
19 while (parents) {
20 struct commit *commit = parents->item;
21 commit->object.flags |= UNINTERESTING;
22 parents = parents->next;
23 }
24}
25
26static int everybody_uninteresting(struct commit_list *list)
27{
28 while (list) {
29 struct commit *commit = list->item;
30 list = list->next;
31 if (commit->object.flags & UNINTERESTING)
32 continue;
33 return 0;
34 }
35 return 1;
36}
37
64745109
LT
38int main(int argc, char **argv)
39{
a6f68d47
LT
40 int nr_sha;
41 unsigned char sha1[2][20];
64745109 42 struct commit_list *list = NULL;
a6f68d47
LT
43 struct commit *commit, *end;
44 int i, verbose_header = 0;
fcfda02b
KS
45 unsigned long max_age = -1;
46 unsigned long min_age = -1;
47 int max_count = -1;
64745109 48
a6f68d47 49 nr_sha = 0;
fcfda02b
KS
50 for (i = 1 ; i < argc; i++) {
51 char *arg = argv[i];
52
53 if (!strncmp(arg, "--max-count=", 12)) {
54 max_count = atoi(arg + 12);
a6f68d47
LT
55 continue;
56 }
57 if (!strncmp(arg, "--max-age=", 10)) {
fcfda02b 58 max_age = atoi(arg + 10);
a6f68d47
LT
59 continue;
60 }
61 if (!strncmp(arg, "--min-age=", 10)) {
fcfda02b 62 min_age = atoi(arg + 10);
a6f68d47 63 continue;
fcfda02b 64 }
a6f68d47
LT
65 if (!strcmp(arg, "--header")) {
66 verbose_header = 1;
67 continue;
68 }
69
70 if (nr_sha > 2 || get_sha1(arg, sha1[nr_sha]))
71 usage(rev_list_usage);
72 nr_sha++;
fcfda02b
KS
73 }
74
a6f68d47
LT
75 if (!nr_sha)
76 usage(rev_list_usage);
64745109 77
a6f68d47 78 commit = lookup_commit_reference(sha1[0]);
64745109 79 if (!commit || parse_commit(commit) < 0)
a6f68d47
LT
80 die("bad starting commit object");
81
82 end = NULL;
83 if (nr_sha > 1) {
84 end = lookup_commit_reference(sha1[1]);
85 if (!end || parse_commit(end) < 0)
86 die("bad ending commit object");
87 }
64745109
LT
88
89 commit_list_insert(commit, &list);
8906300f
LT
90 if (end) {
91 struct commit_list *newlist = NULL;
92 struct commit_list **p = &newlist;
93 do {
94 struct commit *commit = pop_most_recent_commit(&list, SEEN);
95 struct object *obj = &commit->object;
fcfda02b 96
8906300f
LT
97 if (commit == end || (obj->flags & UNINTERESTING)) {
98 mark_parents_uninteresting(commit);
99 if (everybody_uninteresting(list))
100 break;
101 continue;
102 }
103 p = &commit_list_insert(commit, p)->next;
104 } while (list);
105 list = newlist;
106 }
107
108 while (list) {
109 struct commit *commit = pop_most_recent_commit(&list, SEEN);
110
111 if (commit->object.flags & UNINTERESTING)
112 continue;
fcfda02b
KS
113 if (min_age != -1 && (commit->date > min_age))
114 continue;
115 if (max_age != -1 && (commit->date < max_age))
116 break;
117 if (max_count != -1 && !max_count--)
118 break;
64745109 119 printf("%s\n", sha1_to_hex(commit->object.sha1));
a6f68d47
LT
120 if (verbose_header)
121 printf("%s%c", commit->buffer, 0);
8906300f 122 }
64745109
LT
123 return 0;
124}