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