Ooh. Make git-rev-list --object associate a name with objects.
[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;
9de48752
LT
22static int tree_objects = 0;
23static int blob_objects = 0;
81f2bb1f
LT
24static int verbose_header = 0;
25static int show_parents = 0;
81f2bb1f
LT
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;
000182ea 31static enum cmit_fmt commit_format = CMIT_FMT_RAW;
a3437b8c
JS
32static int merge_order = 0;
33static int show_breaks = 0;
5e749e25 34static int stop_traversal = 0;
81f2bb1f
LT
35
36static void show_commit(struct commit *commit)
37{
51b1e171 38 commit->object.flags |= SHOWN;
a3437b8c
JS
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 }
81f2bb1f
LT
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) {
000182ea
LT
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);
a3437b8c
JS
60 }
61}
62
63static int filter_commit(struct commit * commit)
64{
5e749e25
JS
65 if (merge_order && stop_traversal && commit->object.flags & BOUNDARY)
66 return STOP;
51b1e171 67 if (commit->object.flags & (UNINTERESTING|SHOWN))
a3437b8c
JS
68 return CONTINUE;
69 if (min_age != -1 && (commit->date > min_age))
70 return CONTINUE;
5e749e25
JS
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 }
a3437b8c
JS
79 if (max_count != -1 && !max_count--)
80 return STOP;
a3437b8c
JS
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;
81f2bb1f 94 }
a3437b8c
JS
95
96 show_commit(commit);
97
98 return CONTINUE;
81f2bb1f
LT
99}
100
9ce43d1c 101static struct object_list **add_object(struct object *obj, struct object_list **p, const char *name)
9de48752
LT
102{
103 struct object_list *entry = xmalloc(sizeof(*entry));
104 entry->item = obj;
105 entry->next = NULL;
9ce43d1c 106 entry->name = name;
9de48752
LT
107 *p = entry;
108 return &entry->next;
109}
110
9ce43d1c 111static struct object_list **process_blob(struct blob *blob, struct object_list **p, const char *name)
9de48752
LT
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;
9ce43d1c 120 return add_object(obj, p, name);
9de48752
LT
121}
122
9ce43d1c 123static struct object_list **process_tree(struct tree *tree, struct object_list **p, const char *name)
9de48752
LT
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;
9ce43d1c 135 p = add_object(obj, p, name);
9de48752
LT
136 for (entry = tree->entries ; entry ; entry = entry->next) {
137 if (entry->directory)
9ce43d1c 138 p = process_tree(entry->item.tree, p, entry->name);
9de48752 139 else
9ce43d1c 140 p = process_blob(entry->item.blob, p, entry->name);
9de48752
LT
141 }
142 return p;
143}
144
81f2bb1f
LT
145static void show_commit_list(struct commit_list *list)
146{
9de48752 147 struct object_list *objects = NULL, **p = &objects;
81f2bb1f
LT
148 while (list) {
149 struct commit *commit = pop_most_recent_commit(&list, SEEN);
150
9ce43d1c 151 p = process_tree(commit->tree, p, "");
a3437b8c 152 if (process_commit(commit) == STOP)
81f2bb1f 153 break;
81f2bb1f 154 }
9de48752 155 while (objects) {
9ce43d1c 156 printf("%s %s\n", sha1_to_hex(objects->item->sha1), objects->name);
9de48752
LT
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 }
81f2bb1f
LT
190}
191
8906300f
LT
192static void mark_parents_uninteresting(struct commit *commit)
193{
194 struct commit_list *parents = commit->parents;
195
9de48752
LT
196 if (tree_objects)
197 mark_tree_uninteresting(commit->tree);
8906300f
LT
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
8b3a1e05
LT
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
3d958064 249static void clear_distance(struct commit_list *list)
8b3a1e05
LT
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
337cb3fb 289struct commit_list *limit_list(struct commit_list *list)
3b42a63c
LT
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
337cb3fb 297 if (obj->flags & UNINTERESTING) {
3b42a63c
LT
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);
8b3a1e05
LT
305 if (bisect_list)
306 newlist = find_bisection(newlist);
3b42a63c
LT
307 return newlist;
308}
309
000182ea
LT
310static enum cmit_fmt get_commit_format(const char *arg)
311{
312 if (!*arg)
313 return CMIT_FMT_DEFAULT;
314 if (!strcmp(arg, "=raw"))
315 return CMIT_FMT_RAW;
316 if (!strcmp(arg, "=medium"))
317 return CMIT_FMT_MEDIUM;
318 if (!strcmp(arg, "=short"))
319 return CMIT_FMT_SHORT;
320 usage(rev_list_usage);
321}
322
323
64745109
LT
324int main(int argc, char **argv)
325{
64745109 326 struct commit_list *list = NULL;
337cb3fb 327 int i, limited = 0;
64745109 328
fcfda02b 329 for (i = 1 ; i < argc; i++) {
337cb3fb 330 int flags;
fcfda02b 331 char *arg = argv[i];
337cb3fb
LT
332 unsigned char sha1[20];
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
LT
366 if (!strcmp(arg, "--objects")) {
367 tree_objects = 1;
368 blob_objects = 1;
369 continue;
370 }
a3437b8c
JS
371 if (!strncmp(arg, "--merge-order", 13)) {
372 merge_order = 1;
373 continue;
374 }
375 if (!strncmp(arg, "--show-breaks", 13)) {
376 show_breaks = 1;
377 continue;
378 }
a6f68d47 379
337cb3fb
LT
380 flags = 0;
381 if (*arg == '^') {
382 flags = UNINTERESTING;
383 arg++;
384 limited = 1;
385 }
a3437b8c 386 if (get_sha1(arg, sha1) || (show_breaks && !merge_order))
a6f68d47 387 usage(rev_list_usage);
337cb3fb
LT
388 commit = lookup_commit_reference(sha1);
389 if (!commit || parse_commit(commit) < 0)
390 die("bad commit object %s", arg);
391 commit->object.flags |= flags;
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}