1#include "cache.h"
2#include "commit.h"
3#include "tree.h"
4#include "blob.h"
5#include "epoch.h"
6
7#define SEEN (1u << 0)
8#define INTERESTING (1u << 1)
9#define COUNTED (1u << 2)
10#define SHOWN (LAST_EPOCH_FLAG << 2)
11
12static const char rev_list_usage[] =
13 "usage: git-rev-list [OPTION] commit-id <commit-id>\n"
14 " --max-count=nr\n"
15 " --max-age=epoch\n"
16 " --min-age=epoch\n"
17 " --header\n"
18 " --pretty\n"
19 " --merge-order [ --show-breaks ]";
20
21static int bisect_list = 0;
22static int tree_objects = 0;
23static int blob_objects = 0;
24static int verbose_header = 0;
25static int show_parents = 0;
26static int hdr_termination = 0;
27static const char *prefix = "";
28static unsigned long max_age = -1;
29static unsigned long min_age = -1;
30static int max_count = -1;
31static enum cmit_fmt commit_format = CMIT_FMT_RAW;
32static int merge_order = 0;
33static int show_breaks = 0;
34static int stop_traversal = 0;
35
36static void show_commit(struct commit *commit)
37{
38 commit->object.flags |= SHOWN;
39 if (show_breaks) {
40 prefix = "| ";
41 if (commit->object.flags & DISCONTINUITY) {
42 prefix = "^ ";
43 } else if (commit->object.flags & BOUNDARY) {
44 prefix = "= ";
45 }
46 }
47 printf("%s%s", prefix, sha1_to_hex(commit->object.sha1));
48 if (show_parents) {
49 struct commit_list *parents = commit->parents;
50 while (parents) {
51 printf(" %s", sha1_to_hex(parents->item->object.sha1));
52 parents = parents->next;
53 }
54 }
55 putchar('\n');
56 if (verbose_header) {
57 static char pretty_header[16384];
58 pretty_print_commit(commit_format, commit->buffer, ~0, pretty_header, sizeof(pretty_header));
59 printf("%s%c", pretty_header, hdr_termination);
60 }
61}
62
63static int filter_commit(struct commit * commit)
64{
65 if (merge_order && stop_traversal && commit->object.flags & BOUNDARY)
66 return STOP;
67 if (commit->object.flags & (UNINTERESTING|SHOWN))
68 return CONTINUE;
69 if (min_age != -1 && (commit->date > min_age))
70 return CONTINUE;
71 if (max_age != -1 && (commit->date < max_age)) {
72 if (!merge_order)
73 return STOP;
74 else {
75 stop_traversal = 1;
76 return CONTINUE;
77 }
78 }
79 if (max_count != -1 && !max_count--)
80 return STOP;
81 return DO;
82}
83
84static int process_commit(struct commit * commit)
85{
86 int action=filter_commit(commit);
87
88 if (action == STOP) {
89 return STOP;
90 }
91
92 if (action == CONTINUE) {
93 return CONTINUE;
94 }
95
96 show_commit(commit);
97
98 return CONTINUE;
99}
100
101static struct object_list **add_object(struct object *obj, struct object_list **p, const char *name)
102{
103 struct object_list *entry = xmalloc(sizeof(*entry));
104 entry->item = obj;
105 entry->next = NULL;
106 entry->name = name;
107 *p = entry;
108 return &entry->next;
109}
110
111static struct object_list **process_blob(struct blob *blob, struct object_list **p, const char *name)
112{
113 struct object *obj = &blob->object;
114
115 if (!blob_objects)
116 return p;
117 if (obj->flags & (UNINTERESTING | SEEN))
118 return p;
119 obj->flags |= SEEN;
120 return add_object(obj, p, name);
121}
122
123static struct object_list **process_tree(struct tree *tree, struct object_list **p, const char *name)
124{
125 struct object *obj = &tree->object;
126 struct tree_entry_list *entry;
127
128 if (!tree_objects)
129 return p;
130 if (obj->flags & (UNINTERESTING | SEEN))
131 return p;
132 if (parse_tree(tree) < 0)
133 die("bad tree object %s", sha1_to_hex(obj->sha1));
134 obj->flags |= SEEN;
135 p = add_object(obj, p, name);
136 for (entry = tree->entries ; entry ; entry = entry->next) {
137 if (entry->directory)
138 p = process_tree(entry->item.tree, p, entry->name);
139 else
140 p = process_blob(entry->item.blob, p, entry->name);
141 }
142 return p;
143}
144
145static void show_commit_list(struct commit_list *list)
146{
147 struct object_list *objects = NULL, **p = &objects;
148 while (list) {
149 struct commit *commit = pop_most_recent_commit(&list, SEEN);
150
151 p = process_tree(commit->tree, p, "");
152 if (process_commit(commit) == STOP)
153 break;
154 }
155 while (objects) {
156 printf("%s %s\n", sha1_to_hex(objects->item->sha1), objects->name);
157 objects = objects->next;
158 }
159}
160
161static void mark_blob_uninteresting(struct blob *blob)
162{
163 if (!blob_objects)
164 return;
165 if (blob->object.flags & UNINTERESTING)
166 return;
167 blob->object.flags |= UNINTERESTING;
168}
169
170static void mark_tree_uninteresting(struct tree *tree)
171{
172 struct object *obj = &tree->object;
173 struct tree_entry_list *entry;
174
175 if (!tree_objects)
176 return;
177 if (obj->flags & UNINTERESTING)
178 return;
179 obj->flags |= UNINTERESTING;
180 if (parse_tree(tree) < 0)
181 die("bad tree %s", sha1_to_hex(obj->sha1));
182 entry = tree->entries;
183 while (entry) {
184 if (entry->directory)
185 mark_tree_uninteresting(entry->item.tree);
186 else
187 mark_blob_uninteresting(entry->item.blob);
188 entry = entry->next;
189 }
190}
191
192static void mark_parents_uninteresting(struct commit *commit)
193{
194 struct commit_list *parents = commit->parents;
195
196 if (tree_objects)
197 mark_tree_uninteresting(commit->tree);
198 while (parents) {
199 struct commit *commit = parents->item;
200 commit->object.flags |= UNINTERESTING;
201 parents = parents->next;
202 }
203}
204
205static int everybody_uninteresting(struct commit_list *list)
206{
207 while (list) {
208 struct commit *commit = list->item;
209 list = list->next;
210 if (commit->object.flags & UNINTERESTING)
211 continue;
212 return 0;
213 }
214 return 1;
215}
216
217/*
218 * This is a truly stupid algorithm, but it's only
219 * used for bisection, and we just don't care enough.
220 *
221 * We care just barely enough to avoid recursing for
222 * non-merge entries.
223 */
224static int count_distance(struct commit_list *entry)
225{
226 int nr = 0;
227
228 while (entry) {
229 struct commit *commit = entry->item;
230 struct commit_list *p;
231
232 if (commit->object.flags & (UNINTERESTING | COUNTED))
233 break;
234 nr++;
235 commit->object.flags |= COUNTED;
236 p = commit->parents;
237 entry = p;
238 if (p) {
239 p = p->next;
240 while (p) {
241 nr += count_distance(p);
242 p = p->next;
243 }
244 }
245 }
246 return nr;
247}
248
249static void clear_distance(struct commit_list *list)
250{
251 while (list) {
252 struct commit *commit = list->item;
253 commit->object.flags &= ~COUNTED;
254 list = list->next;
255 }
256}
257
258static struct commit_list *find_bisection(struct commit_list *list)
259{
260 int nr, closest;
261 struct commit_list *p, *best;
262
263 nr = 0;
264 p = list;
265 while (p) {
266 nr++;
267 p = p->next;
268 }
269 closest = 0;
270 best = list;
271
272 p = list;
273 while (p) {
274 int distance = count_distance(p);
275 clear_distance(list);
276 if (nr - distance < distance)
277 distance = nr - distance;
278 if (distance > closest) {
279 best = p;
280 closest = distance;
281 }
282 p = p->next;
283 }
284 if (best)
285 best->next = NULL;
286 return best;
287}
288
289struct commit_list *limit_list(struct commit_list *list)
290{
291 struct commit_list *newlist = NULL;
292 struct commit_list **p = &newlist;
293 do {
294 struct commit *commit = pop_most_recent_commit(&list, SEEN);
295 struct object *obj = &commit->object;
296
297 if (obj->flags & UNINTERESTING) {
298 mark_parents_uninteresting(commit);
299 if (everybody_uninteresting(list))
300 break;
301 continue;
302 }
303 p = &commit_list_insert(commit, p)->next;
304 } while (list);
305 if (bisect_list)
306 newlist = find_bisection(newlist);
307 return newlist;
308}
309
310int main(int argc, char **argv)
311{
312 struct commit_list *list = NULL;
313 int i, limited = 0;
314
315 for (i = 1 ; i < argc; i++) {
316 int flags;
317 char *arg = argv[i];
318 unsigned char sha1[20];
319 struct commit *commit;
320
321 if (!strncmp(arg, "--max-count=", 12)) {
322 max_count = atoi(arg + 12);
323 continue;
324 }
325 if (!strncmp(arg, "--max-age=", 10)) {
326 max_age = atoi(arg + 10);
327 continue;
328 }
329 if (!strncmp(arg, "--min-age=", 10)) {
330 min_age = atoi(arg + 10);
331 continue;
332 }
333 if (!strcmp(arg, "--header")) {
334 verbose_header = 1;
335 continue;
336 }
337 if (!strncmp(arg, "--pretty", 8)) {
338 commit_format = get_commit_format(arg+8);
339 verbose_header = 1;
340 hdr_termination = '\n';
341 prefix = "commit ";
342 continue;
343 }
344 if (!strcmp(arg, "--parents")) {
345 show_parents = 1;
346 continue;
347 }
348 if (!strcmp(arg, "--bisect")) {
349 bisect_list = 1;
350 continue;
351 }
352 if (!strcmp(arg, "--objects")) {
353 tree_objects = 1;
354 blob_objects = 1;
355 continue;
356 }
357 if (!strncmp(arg, "--merge-order", 13)) {
358 merge_order = 1;
359 continue;
360 }
361 if (!strncmp(arg, "--show-breaks", 13)) {
362 show_breaks = 1;
363 continue;
364 }
365
366 flags = 0;
367 if (*arg == '^') {
368 flags = UNINTERESTING;
369 arg++;
370 limited = 1;
371 }
372 if (get_sha1(arg, sha1) || (show_breaks && !merge_order))
373 usage(rev_list_usage);
374 commit = lookup_commit_reference(sha1);
375 if (!commit || parse_commit(commit) < 0)
376 die("bad commit object %s", arg);
377 commit->object.flags |= flags;
378 commit_list_insert(commit, &list);
379 }
380
381 if (!list)
382 usage(rev_list_usage);
383
384 if (!merge_order) {
385 if (limited)
386 list = limit_list(list);
387 show_commit_list(list);
388 } else {
389 if (sort_list_in_merge_order(list, &process_commit)) {
390 die("merge order sort failed\n");
391 }
392 }
393
394 return 0;
395}