Clean up commit insertion in git-rev-list
[git/git.git] / rev-list.c
CommitLineData
64745109 1#include "cache.h"
36f8d174 2#include "tag.h"
64745109 3#include "commit.h"
9de48752
LT
4#include "tree.h"
5#include "blob.h"
a3437b8c 6#include "epoch.h"
64745109 7
8906300f
LT
8#define SEEN (1u << 0)
9#define INTERESTING (1u << 1)
8b3a1e05 10#define COUNTED (1u << 2)
51b1e171 11#define SHOWN (LAST_EPOCH_FLAG << 2)
8906300f 12
a6f68d47
LT
13static const char rev_list_usage[] =
14 "usage: git-rev-list [OPTION] commit-id <commit-id>\n"
15 " --max-count=nr\n"
16 " --max-age=epoch\n"
17 " --min-age=epoch\n"
12d2a187
LT
18 " --bisect\n"
19 " --objects\n"
20 " --unpacked\n"
9d97aa64 21 " --header\n"
a3437b8c
JS
22 " --pretty\n"
23 " --merge-order [ --show-breaks ]";
a6f68d47 24
12d2a187 25static int unpacked = 0;
8b3a1e05 26static int bisect_list = 0;
3c90f03d 27static int tag_objects = 0;
9de48752
LT
28static int tree_objects = 0;
29static int blob_objects = 0;
81f2bb1f
LT
30static int verbose_header = 0;
31static int show_parents = 0;
81f2bb1f
LT
32static int hdr_termination = 0;
33static const char *prefix = "";
34static unsigned long max_age = -1;
35static unsigned long min_age = -1;
36static int max_count = -1;
000182ea 37static enum cmit_fmt commit_format = CMIT_FMT_RAW;
a3437b8c
JS
38static int merge_order = 0;
39static int show_breaks = 0;
5e749e25 40static int stop_traversal = 0;
81f2bb1f
LT
41
42static void show_commit(struct commit *commit)
43{
51b1e171 44 commit->object.flags |= SHOWN;
a3437b8c
JS
45 if (show_breaks) {
46 prefix = "| ";
47 if (commit->object.flags & DISCONTINUITY) {
48 prefix = "^ ";
49 } else if (commit->object.flags & BOUNDARY) {
50 prefix = "= ";
51 }
52 }
81f2bb1f
LT
53 printf("%s%s", prefix, sha1_to_hex(commit->object.sha1));
54 if (show_parents) {
55 struct commit_list *parents = commit->parents;
56 while (parents) {
57 printf(" %s", sha1_to_hex(parents->item->object.sha1));
58 parents = parents->next;
59 }
60 }
61 putchar('\n');
62 if (verbose_header) {
000182ea
LT
63 static char pretty_header[16384];
64 pretty_print_commit(commit_format, commit->buffer, ~0, pretty_header, sizeof(pretty_header));
65 printf("%s%c", pretty_header, hdr_termination);
7620d39f
LT
66 }
67 fflush(stdout);
a3437b8c
JS
68}
69
70static int filter_commit(struct commit * commit)
71{
5e749e25
JS
72 if (merge_order && stop_traversal && commit->object.flags & BOUNDARY)
73 return STOP;
51b1e171 74 if (commit->object.flags & (UNINTERESTING|SHOWN))
a3437b8c
JS
75 return CONTINUE;
76 if (min_age != -1 && (commit->date > min_age))
77 return CONTINUE;
5e749e25
JS
78 if (max_age != -1 && (commit->date < max_age)) {
79 if (!merge_order)
80 return STOP;
81 else {
82 stop_traversal = 1;
83 return CONTINUE;
84 }
85 }
a3437b8c
JS
86 if (max_count != -1 && !max_count--)
87 return STOP;
a3437b8c
JS
88 return DO;
89}
90
91static int process_commit(struct commit * commit)
92{
93 int action=filter_commit(commit);
94
95 if (action == STOP) {
96 return STOP;
97 }
98
99 if (action == CONTINUE) {
100 return CONTINUE;
81f2bb1f 101 }
a3437b8c
JS
102
103 show_commit(commit);
104
105 return CONTINUE;
81f2bb1f
LT
106}
107
9ce43d1c 108static struct object_list **add_object(struct object *obj, struct object_list **p, const char *name)
9de48752
LT
109{
110 struct object_list *entry = xmalloc(sizeof(*entry));
111 entry->item = obj;
36f8d174 112 entry->next = *p;
9ce43d1c 113 entry->name = name;
9de48752
LT
114 *p = entry;
115 return &entry->next;
116}
117
9ce43d1c 118static struct object_list **process_blob(struct blob *blob, struct object_list **p, const char *name)
9de48752
LT
119{
120 struct object *obj = &blob->object;
121
122 if (!blob_objects)
123 return p;
124 if (obj->flags & (UNINTERESTING | SEEN))
125 return p;
126 obj->flags |= SEEN;
9ce43d1c 127 return add_object(obj, p, name);
9de48752
LT
128}
129
9ce43d1c 130static struct object_list **process_tree(struct tree *tree, struct object_list **p, const char *name)
9de48752
LT
131{
132 struct object *obj = &tree->object;
133 struct tree_entry_list *entry;
134
135 if (!tree_objects)
136 return p;
137 if (obj->flags & (UNINTERESTING | SEEN))
138 return p;
139 if (parse_tree(tree) < 0)
140 die("bad tree object %s", sha1_to_hex(obj->sha1));
141 obj->flags |= SEEN;
9ce43d1c 142 p = add_object(obj, p, name);
9de48752
LT
143 for (entry = tree->entries ; entry ; entry = entry->next) {
144 if (entry->directory)
9ce43d1c 145 p = process_tree(entry->item.tree, p, entry->name);
9de48752 146 else
9ce43d1c 147 p = process_blob(entry->item.blob, p, entry->name);
9de48752
LT
148 }
149 return p;
150}
151
36f8d174
LT
152static struct object_list *pending_objects = NULL;
153
81f2bb1f
LT
154static void show_commit_list(struct commit_list *list)
155{
36f8d174 156 struct object_list *objects = NULL, **p = &objects, *pending;
81f2bb1f
LT
157 while (list) {
158 struct commit *commit = pop_most_recent_commit(&list, SEEN);
159
9ce43d1c 160 p = process_tree(commit->tree, p, "");
a3437b8c 161 if (process_commit(commit) == STOP)
81f2bb1f 162 break;
81f2bb1f 163 }
36f8d174
LT
164 for (pending = pending_objects; pending; pending = pending->next) {
165 struct object *obj = pending->item;
166 const char *name = pending->name;
167 if (obj->flags & (UNINTERESTING | SEEN))
168 continue;
169 if (obj->type == tag_type) {
170 obj->flags |= SEEN;
171 p = add_object(obj, p, name);
172 continue;
173 }
174 if (obj->type == tree_type) {
175 p = process_tree((struct tree *)obj, p, name);
176 continue;
177 }
178 if (obj->type == blob_type) {
179 p = process_blob((struct blob *)obj, p, name);
180 continue;
181 }
182 die("unknown pending object %s (%s)", sha1_to_hex(obj->sha1), name);
183 }
9de48752 184 while (objects) {
9ce43d1c 185 printf("%s %s\n", sha1_to_hex(objects->item->sha1), objects->name);
9de48752
LT
186 objects = objects->next;
187 }
188}
189
190static void mark_blob_uninteresting(struct blob *blob)
191{
192 if (!blob_objects)
193 return;
194 if (blob->object.flags & UNINTERESTING)
195 return;
196 blob->object.flags |= UNINTERESTING;
197}
198
199static void mark_tree_uninteresting(struct tree *tree)
200{
201 struct object *obj = &tree->object;
202 struct tree_entry_list *entry;
203
204 if (!tree_objects)
205 return;
206 if (obj->flags & UNINTERESTING)
207 return;
208 obj->flags |= UNINTERESTING;
209 if (parse_tree(tree) < 0)
210 die("bad tree %s", sha1_to_hex(obj->sha1));
211 entry = tree->entries;
212 while (entry) {
213 if (entry->directory)
214 mark_tree_uninteresting(entry->item.tree);
215 else
216 mark_blob_uninteresting(entry->item.blob);
217 entry = entry->next;
218 }
81f2bb1f
LT
219}
220
8906300f
LT
221static void mark_parents_uninteresting(struct commit *commit)
222{
223 struct commit_list *parents = commit->parents;
224
9de48752
LT
225 if (tree_objects)
226 mark_tree_uninteresting(commit->tree);
8906300f
LT
227 while (parents) {
228 struct commit *commit = parents->item;
229 commit->object.flags |= UNINTERESTING;
230 parents = parents->next;
231 }
232}
233
234static int everybody_uninteresting(struct commit_list *list)
235{
236 while (list) {
237 struct commit *commit = list->item;
238 list = list->next;
239 if (commit->object.flags & UNINTERESTING)
240 continue;
241 return 0;
242 }
243 return 1;
244}
245
8b3a1e05
LT
246/*
247 * This is a truly stupid algorithm, but it's only
248 * used for bisection, and we just don't care enough.
249 *
250 * We care just barely enough to avoid recursing for
251 * non-merge entries.
252 */
253static int count_distance(struct commit_list *entry)
254{
255 int nr = 0;
256
257 while (entry) {
258 struct commit *commit = entry->item;
259 struct commit_list *p;
260
261 if (commit->object.flags & (UNINTERESTING | COUNTED))
262 break;
263 nr++;
264 commit->object.flags |= COUNTED;
265 p = commit->parents;
266 entry = p;
267 if (p) {
268 p = p->next;
269 while (p) {
270 nr += count_distance(p);
271 p = p->next;
272 }
273 }
274 }
275 return nr;
276}
277
3d958064 278static void clear_distance(struct commit_list *list)
8b3a1e05
LT
279{
280 while (list) {
281 struct commit *commit = list->item;
282 commit->object.flags &= ~COUNTED;
283 list = list->next;
284 }
285}
286
287static struct commit_list *find_bisection(struct commit_list *list)
288{
289 int nr, closest;
290 struct commit_list *p, *best;
291
292 nr = 0;
293 p = list;
294 while (p) {
295 nr++;
296 p = p->next;
297 }
298 closest = 0;
299 best = list;
300
301 p = list;
302 while (p) {
303 int distance = count_distance(p);
304 clear_distance(list);
305 if (nr - distance < distance)
306 distance = nr - distance;
307 if (distance > closest) {
308 best = p;
309 closest = distance;
310 }
311 p = p->next;
312 }
313 if (best)
314 best->next = NULL;
315 return best;
316}
317
6da4016a 318static struct commit_list *limit_list(struct commit_list *list)
3b42a63c
LT
319{
320 struct commit_list *newlist = NULL;
321 struct commit_list **p = &newlist;
36f8d174 322 while (list) {
3b42a63c
LT
323 struct commit *commit = pop_most_recent_commit(&list, SEEN);
324 struct object *obj = &commit->object;
325
12d2a187
LT
326 if (unpacked && has_sha1_pack(obj->sha1))
327 obj->flags |= UNINTERESTING;
337cb3fb 328 if (obj->flags & UNINTERESTING) {
3b42a63c
LT
329 mark_parents_uninteresting(commit);
330 if (everybody_uninteresting(list))
331 break;
332 continue;
333 }
334 p = &commit_list_insert(commit, p)->next;
36f8d174 335 }
8b3a1e05
LT
336 if (bisect_list)
337 newlist = find_bisection(newlist);
3b42a63c
LT
338 return newlist;
339}
340
36f8d174
LT
341static void add_pending_object(struct object *obj, const char *name)
342{
343 add_object(obj, &pending_objects, name);
344}
345
3c90f03d
LT
346static struct commit *get_commit_reference(const char *name, unsigned int flags)
347{
348 unsigned char sha1[20];
36f8d174 349 struct object *object;
3c90f03d
LT
350
351 if (get_sha1(name, sha1))
352 usage(rev_list_usage);
36f8d174
LT
353 object = parse_object(sha1);
354 if (!object)
355 die("bad object %s", name);
356
357 /*
358 * Tag object? Look what it points to..
359 */
360 if (object->type == tag_type) {
361 struct tag *tag = (struct tag *) object;
362 object->flags |= flags;
363 if (tag_objects && !(object->flags & UNINTERESTING))
364 add_pending_object(object, tag->tag);
365 object = tag->tagged;
366 }
367
368 /*
369 * Commit object? Just return it, we'll do all the complex
370 * reachability crud.
371 */
372 if (object->type == commit_type) {
373 struct commit *commit = (struct commit *)object;
374 object->flags |= flags;
375 if (parse_commit(commit) < 0)
376 die("unable to parse commit %s", name);
377 return commit;
378 }
379
380 /*
381 * Tree object? Either mark it uniniteresting, or add it
382 * to the list of objects to look at later..
383 */
384 if (object->type == tree_type) {
385 struct tree *tree = (struct tree *)object;
386 if (!tree_objects)
960bba0d 387 return NULL;
36f8d174
LT
388 if (flags & UNINTERESTING) {
389 mark_tree_uninteresting(tree);
390 return NULL;
391 }
392 add_pending_object(object, "");
393 return NULL;
394 }
395
396 /*
397 * Blob object? You know the drill by now..
398 */
399 if (object->type == blob_type) {
400 struct blob *blob = (struct blob *)object;
401 if (!blob_objects)
960bba0d 402 return NULL;
36f8d174
LT
403 if (flags & UNINTERESTING) {
404 mark_blob_uninteresting(blob);
405 return NULL;
406 }
407 add_pending_object(object, "");
408 return NULL;
409 }
410 die("%s is unknown object", name);
3c90f03d
LT
411}
412
64745109
LT
413int main(int argc, char **argv)
414{
64745109 415 struct commit_list *list = NULL;
7e21c29b 416 struct commit_list *(*insert)(struct commit *, struct commit_list **);
337cb3fb 417 int i, limited = 0;
64745109 418
7e21c29b 419 insert = insert_by_date;
fcfda02b 420 for (i = 1 ; i < argc; i++) {
337cb3fb 421 int flags;
fcfda02b 422 char *arg = argv[i];
337cb3fb 423 struct commit *commit;
fcfda02b
KS
424
425 if (!strncmp(arg, "--max-count=", 12)) {
426 max_count = atoi(arg + 12);
a6f68d47
LT
427 continue;
428 }
429 if (!strncmp(arg, "--max-age=", 10)) {
fcfda02b 430 max_age = atoi(arg + 10);
a6f68d47
LT
431 continue;
432 }
433 if (!strncmp(arg, "--min-age=", 10)) {
fcfda02b 434 min_age = atoi(arg + 10);
a6f68d47 435 continue;
fcfda02b 436 }
a6f68d47
LT
437 if (!strcmp(arg, "--header")) {
438 verbose_header = 1;
439 continue;
440 }
000182ea
LT
441 if (!strncmp(arg, "--pretty", 8)) {
442 commit_format = get_commit_format(arg+8);
9d97aa64 443 verbose_header = 1;
9d97aa64
LT
444 hdr_termination = '\n';
445 prefix = "commit ";
446 continue;
447 }
97658004
LT
448 if (!strcmp(arg, "--parents")) {
449 show_parents = 1;
450 continue;
451 }
8b3a1e05
LT
452 if (!strcmp(arg, "--bisect")) {
453 bisect_list = 1;
454 continue;
455 }
9de48752 456 if (!strcmp(arg, "--objects")) {
3c90f03d 457 tag_objects = 1;
9de48752
LT
458 tree_objects = 1;
459 blob_objects = 1;
460 continue;
461 }
12d2a187
LT
462 if (!strcmp(arg, "--unpacked")) {
463 unpacked = 1;
464 limited = 1;
465 continue;
466 }
12ba7eaf 467 if (!strcmp(arg, "--merge-order")) {
a3437b8c 468 merge_order = 1;
7e21c29b 469 insert = commit_list_insert;
a3437b8c
JS
470 continue;
471 }
12ba7eaf 472 if (!strcmp(arg, "--show-breaks")) {
a3437b8c
JS
473 show_breaks = 1;
474 continue;
475 }
a6f68d47 476
337cb3fb
LT
477 flags = 0;
478 if (*arg == '^') {
479 flags = UNINTERESTING;
480 arg++;
481 limited = 1;
482 }
3c90f03d 483 if (show_breaks && !merge_order)
a6f68d47 484 usage(rev_list_usage);
3c90f03d
LT
485 commit = get_commit_reference(arg, flags);
486 if (!commit)
487 continue;
7e21c29b 488 insert(commit, &list);
fcfda02b
KS
489 }
490
a3437b8c 491 if (!merge_order) {
17ebe977 492 if (limited)
a3437b8c
JS
493 list = limit_list(list);
494 show_commit_list(list);
a3437b8c 495 } else {
a3437b8c
JS
496 if (sort_list_in_merge_order(list, &process_commit)) {
497 die("merge order sort failed\n");
498 }
a3437b8c 499 }
8906300f 500
64745109
LT
501 return 0;
502}