Prepare git-rev-list for tracking tag objects too
[git/git.git] / rev-list.c
CommitLineData
64745109
LT
1#include "cache.h"
2#include "commit.h"
9de48752
LT
3#include "tree.h"
4#include "blob.h"
a3437b8c 5#include "epoch.h"
64745109 6
8906300f
LT
7#define SEEN (1u << 0)
8#define INTERESTING (1u << 1)
8b3a1e05 9#define COUNTED (1u << 2)
51b1e171 10#define SHOWN (LAST_EPOCH_FLAG << 2)
8906300f 11
a6f68d47
LT
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"
9d97aa64 17 " --header\n"
a3437b8c
JS
18 " --pretty\n"
19 " --merge-order [ --show-breaks ]";
a6f68d47 20
8b3a1e05 21static int bisect_list = 0;
3c90f03d 22static int tag_objects = 0;
9de48752
LT
23static int tree_objects = 0;
24static int blob_objects = 0;
81f2bb1f
LT
25static int verbose_header = 0;
26static int show_parents = 0;
81f2bb1f
LT
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;
000182ea 32static enum cmit_fmt commit_format = CMIT_FMT_RAW;
a3437b8c
JS
33static int merge_order = 0;
34static int show_breaks = 0;
5e749e25 35static int stop_traversal = 0;
81f2bb1f
LT
36
37static void show_commit(struct commit *commit)
38{
51b1e171 39 commit->object.flags |= SHOWN;
a3437b8c
JS
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 }
81f2bb1f
LT
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) {
000182ea
LT
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);
a3437b8c
JS
61 }
62}
63
64static int filter_commit(struct commit * commit)
65{
5e749e25
JS
66 if (merge_order && stop_traversal && commit->object.flags & BOUNDARY)
67 return STOP;
51b1e171 68 if (commit->object.flags & (UNINTERESTING|SHOWN))
a3437b8c
JS
69 return CONTINUE;
70 if (min_age != -1 && (commit->date > min_age))
71 return CONTINUE;
5e749e25
JS
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 }
a3437b8c
JS
80 if (max_count != -1 && !max_count--)
81 return STOP;
a3437b8c
JS
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;
81f2bb1f 95 }
a3437b8c
JS
96
97 show_commit(commit);
98
99 return CONTINUE;
81f2bb1f
LT
100}
101
9ce43d1c 102static struct object_list **add_object(struct object *obj, struct object_list **p, const char *name)
9de48752
LT
103{
104 struct object_list *entry = xmalloc(sizeof(*entry));
105 entry->item = obj;
106 entry->next = NULL;
9ce43d1c 107 entry->name = name;
9de48752
LT
108 *p = entry;
109 return &entry->next;
110}
111
9ce43d1c 112static struct object_list **process_blob(struct blob *blob, struct object_list **p, const char *name)
9de48752
LT
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;
9ce43d1c 121 return add_object(obj, p, name);
9de48752
LT
122}
123
9ce43d1c 124static struct object_list **process_tree(struct tree *tree, struct object_list **p, const char *name)
9de48752
LT
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;
9ce43d1c 136 p = add_object(obj, p, name);
9de48752
LT
137 for (entry = tree->entries ; entry ; entry = entry->next) {
138 if (entry->directory)
9ce43d1c 139 p = process_tree(entry->item.tree, p, entry->name);
9de48752 140 else
9ce43d1c 141 p = process_blob(entry->item.blob, p, entry->name);
9de48752
LT
142 }
143 return p;
144}
145
81f2bb1f
LT
146static void show_commit_list(struct commit_list *list)
147{
9de48752 148 struct object_list *objects = NULL, **p = &objects;
81f2bb1f
LT
149 while (list) {
150 struct commit *commit = pop_most_recent_commit(&list, SEEN);
151
9ce43d1c 152 p = process_tree(commit->tree, p, "");
a3437b8c 153 if (process_commit(commit) == STOP)
81f2bb1f 154 break;
81f2bb1f 155 }
9de48752 156 while (objects) {
9ce43d1c 157 printf("%s %s\n", sha1_to_hex(objects->item->sha1), objects->name);
9de48752
LT
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 }
81f2bb1f
LT
191}
192
8906300f
LT
193static void mark_parents_uninteresting(struct commit *commit)
194{
195 struct commit_list *parents = commit->parents;
196
9de48752
LT
197 if (tree_objects)
198 mark_tree_uninteresting(commit->tree);
8906300f
LT
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
8b3a1e05
LT
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
3d958064 250static void clear_distance(struct commit_list *list)
8b3a1e05
LT
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
337cb3fb 290struct commit_list *limit_list(struct commit_list *list)
3b42a63c
LT
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
337cb3fb 298 if (obj->flags & UNINTERESTING) {
3b42a63c
LT
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);
8b3a1e05
LT
306 if (bisect_list)
307 newlist = find_bisection(newlist);
3b42a63c
LT
308 return newlist;
309}
310
3c90f03d
LT
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
64745109
LT
325int main(int argc, char **argv)
326{
64745109 327 struct commit_list *list = NULL;
337cb3fb 328 int i, limited = 0;
64745109 329
fcfda02b 330 for (i = 1 ; i < argc; i++) {
337cb3fb 331 int flags;
fcfda02b 332 char *arg = argv[i];
337cb3fb 333 struct commit *commit;
fcfda02b
KS
334
335 if (!strncmp(arg, "--max-count=", 12)) {
336 max_count = atoi(arg + 12);
a6f68d47
LT
337 continue;
338 }
339 if (!strncmp(arg, "--max-age=", 10)) {
fcfda02b 340 max_age = atoi(arg + 10);
a6f68d47
LT
341 continue;
342 }
343 if (!strncmp(arg, "--min-age=", 10)) {
fcfda02b 344 min_age = atoi(arg + 10);
a6f68d47 345 continue;
fcfda02b 346 }
a6f68d47
LT
347 if (!strcmp(arg, "--header")) {
348 verbose_header = 1;
349 continue;
350 }
000182ea
LT
351 if (!strncmp(arg, "--pretty", 8)) {
352 commit_format = get_commit_format(arg+8);
9d97aa64 353 verbose_header = 1;
9d97aa64
LT
354 hdr_termination = '\n';
355 prefix = "commit ";
356 continue;
357 }
97658004
LT
358 if (!strcmp(arg, "--parents")) {
359 show_parents = 1;
360 continue;
361 }
8b3a1e05
LT
362 if (!strcmp(arg, "--bisect")) {
363 bisect_list = 1;
364 continue;
365 }
9de48752 366 if (!strcmp(arg, "--objects")) {
3c90f03d 367 tag_objects = 1;
9de48752
LT
368 tree_objects = 1;
369 blob_objects = 1;
370 continue;
371 }
a3437b8c
JS
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 }
a6f68d47 380
337cb3fb
LT
381 flags = 0;
382 if (*arg == '^') {
383 flags = UNINTERESTING;
384 arg++;
385 limited = 1;
386 }
3c90f03d 387 if (show_breaks && !merge_order)
a6f68d47 388 usage(rev_list_usage);
3c90f03d
LT
389 commit = get_commit_reference(arg, flags);
390 if (!commit)
391 continue;
337cb3fb 392 commit_list_insert(commit, &list);
fcfda02b
KS
393 }
394
337cb3fb 395 if (!list)
a6f68d47 396 usage(rev_list_usage);
64745109 397
a3437b8c 398 if (!merge_order) {
17ebe977 399 if (limited)
a3437b8c
JS
400 list = limit_list(list);
401 show_commit_list(list);
a3437b8c 402 } else {
a3437b8c
JS
403 if (sort_list_in_merge_order(list, &process_commit)) {
404 die("merge order sort failed\n");
405 }
a3437b8c 406 }
8906300f 407
64745109
LT
408 return 0;
409}