1 #include "cache.h"
2 #include "commit.h"
4 #define SEEN (1u << 0)
5 #define INTERESTING (1u << 1)
6 #define UNINTERESTING (1u << 2)
8 static 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\n"
14 " --pretty";
16 static int verbose_header = 0;
17 static int show_parents = 0;
18 static int pretty_print = 0;
19 static int hdr_termination = 0;
20 static const char *prefix = "";
21 static unsigned long max_age = -1;
22 static unsigned long min_age = -1;
23 static int max_count = -1;
25 static void show_commit(struct commit *commit)
26 {
27 printf("%s%s", prefix, sha1_to_hex(commit->object.sha1));
28 if (show_parents) {
29 struct commit_list *parents = commit->parents;
30 while (parents) {
31 printf(" %s", sha1_to_hex(parents->item->object.sha1));
32 parents = parents->next;
33 }
34 }
35 putchar('\n');
36 if (verbose_header) {
37 const char *buf = commit->buffer;
38 if (pretty_print) {
39 static char pretty_header[16384];
40 pretty_print_commit(commit->buffer, ~0, pretty_header, sizeof(pretty_header));
41 buf = pretty_header;
42 }
43 printf("%s%c", buf, hdr_termination);
44 }
45 }
47 static void show_commit_list(struct commit_list *list)
48 {
49 while (list) {
50 struct commit *commit = pop_most_recent_commit(&list, SEEN);
52 if (commit->object.flags & UNINTERESTING)
53 continue;
54 if (min_age != -1 && (commit->date > min_age))
55 continue;
56 if (max_age != -1 && (commit->date < max_age))
57 break;
58 if (max_count != -1 && !max_count--)
59 break;
60 show_commit(commit);
61 }
62 }
64 static void mark_parents_uninteresting(struct commit *commit)
65 {
66 struct commit_list *parents = commit->parents;
68 while (parents) {
69 struct commit *commit = parents->item;
70 commit->object.flags |= UNINTERESTING;
71 parents = parents->next;
72 }
73 }
75 static int everybody_uninteresting(struct commit_list *list)
76 {
77 while (list) {
78 struct commit *commit = list->item;
79 list = list->next;
80 if (commit->object.flags & UNINTERESTING)
81 continue;
82 return 0;
83 }
84 return 1;
85 }
87 struct commit_list *limit_list(struct commit_list *list)
88 {
89 struct commit_list *newlist = NULL;
90 struct commit_list **p = &newlist;
91 do {
92 struct commit *commit = pop_most_recent_commit(&list, SEEN);
93 struct object *obj = &commit->object;
95 if (obj->flags & UNINTERESTING) {
96 mark_parents_uninteresting(commit);
97 if (everybody_uninteresting(list))
98 break;
99 continue;
100 }
101 p = &commit_list_insert(commit, p)->next;
102 } while (list);
103 return newlist;
104 }
106 int main(int argc, char **argv)
107 {
108 struct commit_list *list = NULL;
109 int i, limited = 0;
111 for (i = 1 ; i < argc; i++) {
112 int flags;
113 char *arg = argv[i];
114 unsigned char sha1[20];
115 struct commit *commit;
117 if (!strncmp(arg, "--max-count=", 12)) {
118 max_count = atoi(arg + 12);
119 continue;
120 }
121 if (!strncmp(arg, "--max-age=", 10)) {
122 max_age = atoi(arg + 10);
123 continue;
124 }
125 if (!strncmp(arg, "--min-age=", 10)) {
126 min_age = atoi(arg + 10);
127 continue;
128 }
129 if (!strcmp(arg, "--header")) {
130 verbose_header = 1;
131 continue;
132 }
133 if (!strcmp(arg, "--pretty")) {
134 verbose_header = 1;
135 pretty_print = 1;
136 hdr_termination = '\n';
137 prefix = "commit ";
138 continue;
139 }
140 if (!strcmp(arg, "--parents")) {
141 show_parents = 1;
142 continue;
143 }
145 flags = 0;
146 if (*arg == '^') {
147 flags = UNINTERESTING;
148 arg++;
149 limited = 1;
150 }
151 if (get_sha1(arg, sha1))
152 usage(rev_list_usage);
153 commit = lookup_commit_reference(sha1);
154 if (!commit || parse_commit(commit) < 0)
155 die("bad commit object %s", arg);
156 commit->object.flags |= flags;
157 commit_list_insert(commit, &list);
158 }
160 if (!list)
161 usage(rev_list_usage);
163 if (limited)
164 list = limit_list(list);
166 show_commit_list(list);
167 return 0;
168 }