GIT 0.99.8
[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)
bce62866 11#define SHOWN (1u << 3)
8906300f 12
a6f68d47 13static const char rev_list_usage[] =
54c6870e 14 "git-rev-list [OPTION] commit-id <commit-id>\n"
a6f68d47
LT
15 " --max-count=nr\n"
16 " --max-age=epoch\n"
17 " --min-age=epoch\n"
76cd8eb6 18 " --parents\n"
12d2a187
LT
19 " --bisect\n"
20 " --objects\n"
21 " --unpacked\n"
9d97aa64 22 " --header\n"
a3437b8c 23 " --pretty\n"
76cd8eb6
JS
24 " --no-merges\n"
25 " --merge-order [ --show-breaks ]\n"
26 " --topo-order";
a6f68d47 27
12d2a187 28static int unpacked = 0;
8b3a1e05 29static int bisect_list = 0;
3c90f03d 30static int tag_objects = 0;
9de48752
LT
31static int tree_objects = 0;
32static int blob_objects = 0;
81f2bb1f
LT
33static int verbose_header = 0;
34static int show_parents = 0;
81f2bb1f 35static int hdr_termination = 0;
d998a089 36static const char *commit_prefix = "";
81f2bb1f
LT
37static unsigned long max_age = -1;
38static unsigned long min_age = -1;
39static int max_count = -1;
000182ea 40static enum cmit_fmt commit_format = CMIT_FMT_RAW;
a3437b8c
JS
41static int merge_order = 0;
42static int show_breaks = 0;
5e749e25 43static int stop_traversal = 0;
d2d02a49 44static int topo_order = 0;
76cd8eb6 45static int no_merges = 0;
81f2bb1f
LT
46
47static void show_commit(struct commit *commit)
48{
51b1e171 49 commit->object.flags |= SHOWN;
a3437b8c 50 if (show_breaks) {
d998a089 51 commit_prefix = "| ";
a3437b8c 52 if (commit->object.flags & DISCONTINUITY) {
d998a089 53 commit_prefix = "^ ";
a3437b8c 54 } else if (commit->object.flags & BOUNDARY) {
d998a089 55 commit_prefix = "= ";
a3437b8c
JS
56 }
57 }
d998a089 58 printf("%s%s", commit_prefix, sha1_to_hex(commit->object.sha1));
81f2bb1f
LT
59 if (show_parents) {
60 struct commit_list *parents = commit->parents;
61 while (parents) {
62 printf(" %s", sha1_to_hex(parents->item->object.sha1));
63 parents = parents->next;
64 }
65 }
d87449c5
JH
66 if (commit_format == CMIT_FMT_ONELINE)
67 putchar(' ');
68 else
69 putchar('\n');
70
81f2bb1f 71 if (verbose_header) {
000182ea
LT
72 static char pretty_header[16384];
73 pretty_print_commit(commit_format, commit->buffer, ~0, pretty_header, sizeof(pretty_header));
74 printf("%s%c", pretty_header, hdr_termination);
7620d39f
LT
75 }
76 fflush(stdout);
a3437b8c
JS
77}
78
79static int filter_commit(struct commit * commit)
80{
d2775a81 81 if (stop_traversal && (commit->object.flags & BOUNDARY))
5e749e25 82 return STOP;
51b1e171 83 if (commit->object.flags & (UNINTERESTING|SHOWN))
a3437b8c
JS
84 return CONTINUE;
85 if (min_age != -1 && (commit->date > min_age))
86 return CONTINUE;
5e749e25 87 if (max_age != -1 && (commit->date < max_age)) {
d2775a81 88 stop_traversal=1;
27cfe2e2 89 return CONTINUE;
5e749e25 90 }
a3437b8c
JS
91 if (max_count != -1 && !max_count--)
92 return STOP;
76cd8eb6
JS
93 if (no_merges && (commit->parents && commit->parents->next))
94 return CONTINUE;
a3437b8c
JS
95 return DO;
96}
97
98static int process_commit(struct commit * commit)
99{
100 int action=filter_commit(commit);
101
102 if (action == STOP) {
103 return STOP;
104 }
105
106 if (action == CONTINUE) {
107 return CONTINUE;
81f2bb1f 108 }
a3437b8c
JS
109
110 show_commit(commit);
111
112 return CONTINUE;
81f2bb1f
LT
113}
114
9ce43d1c 115static struct object_list **add_object(struct object *obj, struct object_list **p, const char *name)
9de48752
LT
116{
117 struct object_list *entry = xmalloc(sizeof(*entry));
118 entry->item = obj;
36f8d174 119 entry->next = *p;
9ce43d1c 120 entry->name = name;
9de48752
LT
121 *p = entry;
122 return &entry->next;
123}
124
9ce43d1c 125static struct object_list **process_blob(struct blob *blob, struct object_list **p, const char *name)
9de48752
LT
126{
127 struct object *obj = &blob->object;
128
129 if (!blob_objects)
130 return p;
131 if (obj->flags & (UNINTERESTING | SEEN))
132 return p;
133 obj->flags |= SEEN;
9ce43d1c 134 return add_object(obj, p, name);
9de48752
LT
135}
136
9ce43d1c 137static struct object_list **process_tree(struct tree *tree, struct object_list **p, const char *name)
9de48752
LT
138{
139 struct object *obj = &tree->object;
140 struct tree_entry_list *entry;
141
142 if (!tree_objects)
143 return p;
144 if (obj->flags & (UNINTERESTING | SEEN))
145 return p;
146 if (parse_tree(tree) < 0)
147 die("bad tree object %s", sha1_to_hex(obj->sha1));
148 obj->flags |= SEEN;
9ce43d1c 149 p = add_object(obj, p, name);
b0d8923e
LT
150 entry = tree->entries;
151 tree->entries = NULL;
152 while (entry) {
153 struct tree_entry_list *next = entry->next;
9de48752 154 if (entry->directory)
9ce43d1c 155 p = process_tree(entry->item.tree, p, entry->name);
9de48752 156 else
9ce43d1c 157 p = process_blob(entry->item.blob, p, entry->name);
b0d8923e
LT
158 free(entry);
159 entry = next;
9de48752
LT
160 }
161 return p;
162}
163
36f8d174
LT
164static struct object_list *pending_objects = NULL;
165
81f2bb1f
LT
166static void show_commit_list(struct commit_list *list)
167{
36f8d174 168 struct object_list *objects = NULL, **p = &objects, *pending;
81f2bb1f
LT
169 while (list) {
170 struct commit *commit = pop_most_recent_commit(&list, SEEN);
171
9ce43d1c 172 p = process_tree(commit->tree, p, "");
a3437b8c 173 if (process_commit(commit) == STOP)
81f2bb1f 174 break;
81f2bb1f 175 }
36f8d174
LT
176 for (pending = pending_objects; pending; pending = pending->next) {
177 struct object *obj = pending->item;
178 const char *name = pending->name;
179 if (obj->flags & (UNINTERESTING | SEEN))
180 continue;
181 if (obj->type == tag_type) {
182 obj->flags |= SEEN;
183 p = add_object(obj, p, name);
184 continue;
185 }
186 if (obj->type == tree_type) {
187 p = process_tree((struct tree *)obj, p, name);
188 continue;
189 }
190 if (obj->type == blob_type) {
191 p = process_blob((struct blob *)obj, p, name);
192 continue;
193 }
194 die("unknown pending object %s (%s)", sha1_to_hex(obj->sha1), name);
195 }
9de48752 196 while (objects) {
9ce43d1c 197 printf("%s %s\n", sha1_to_hex(objects->item->sha1), objects->name);
9de48752
LT
198 objects = objects->next;
199 }
200}
201
202static void mark_blob_uninteresting(struct blob *blob)
203{
204 if (!blob_objects)
205 return;
206 if (blob->object.flags & UNINTERESTING)
207 return;
208 blob->object.flags |= UNINTERESTING;
209}
210
211static void mark_tree_uninteresting(struct tree *tree)
212{
213 struct object *obj = &tree->object;
214 struct tree_entry_list *entry;
215
216 if (!tree_objects)
217 return;
218 if (obj->flags & UNINTERESTING)
219 return;
220 obj->flags |= UNINTERESTING;
454fbbcd
LT
221 if (!has_sha1_file(obj->sha1))
222 return;
9de48752
LT
223 if (parse_tree(tree) < 0)
224 die("bad tree %s", sha1_to_hex(obj->sha1));
225 entry = tree->entries;
b0d8923e 226 tree->entries = NULL;
9de48752 227 while (entry) {
b0d8923e 228 struct tree_entry_list *next = entry->next;
9de48752
LT
229 if (entry->directory)
230 mark_tree_uninteresting(entry->item.tree);
231 else
232 mark_blob_uninteresting(entry->item.blob);
b0d8923e
LT
233 free(entry);
234 entry = next;
9de48752 235 }
81f2bb1f
LT
236}
237
8906300f
LT
238static void mark_parents_uninteresting(struct commit *commit)
239{
240 struct commit_list *parents = commit->parents;
241
242 while (parents) {
243 struct commit *commit = parents->item;
244 commit->object.flags |= UNINTERESTING;
454fbbcd 245
6c3b84c8
LT
246 /*
247 * Normally we haven't parsed the parent
248 * yet, so we won't have a parent of a parent
249 * here. However, it may turn out that we've
250 * reached this commit some other way (where it
251 * wasn't uninteresting), in which case we need
252 * to mark its parents recursively too..
253 */
254 if (commit->parents)
255 mark_parents_uninteresting(commit);
256
454fbbcd
LT
257 /*
258 * A missing commit is ok iff its parent is marked
259 * uninteresting.
260 *
261 * We just mark such a thing parsed, so that when
262 * it is popped next time around, we won't be trying
263 * to parse it and get an error.
264 */
265 if (!has_sha1_file(commit->object.sha1))
266 commit->object.parsed = 1;
8906300f
LT
267 parents = parents->next;
268 }
269}
270
4311d328 271static int everybody_uninteresting(struct commit_list *orig)
8906300f 272{
4311d328 273 struct commit_list *list = orig;
8906300f
LT
274 while (list) {
275 struct commit *commit = list->item;
276 list = list->next;
277 if (commit->object.flags & UNINTERESTING)
278 continue;
279 return 0;
280 }
281 return 1;
282}
283
8b3a1e05
LT
284/*
285 * This is a truly stupid algorithm, but it's only
286 * used for bisection, and we just don't care enough.
287 *
288 * We care just barely enough to avoid recursing for
289 * non-merge entries.
290 */
291static int count_distance(struct commit_list *entry)
292{
293 int nr = 0;
294
295 while (entry) {
296 struct commit *commit = entry->item;
297 struct commit_list *p;
298
299 if (commit->object.flags & (UNINTERESTING | COUNTED))
300 break;
301 nr++;
302 commit->object.flags |= COUNTED;
303 p = commit->parents;
304 entry = p;
305 if (p) {
306 p = p->next;
307 while (p) {
308 nr += count_distance(p);
309 p = p->next;
310 }
311 }
312 }
313 return nr;
314}
315
3d958064 316static void clear_distance(struct commit_list *list)
8b3a1e05
LT
317{
318 while (list) {
319 struct commit *commit = list->item;
320 commit->object.flags &= ~COUNTED;
321 list = list->next;
322 }
323}
324
325static struct commit_list *find_bisection(struct commit_list *list)
326{
327 int nr, closest;
328 struct commit_list *p, *best;
329
330 nr = 0;
331 p = list;
332 while (p) {
333 nr++;
334 p = p->next;
335 }
336 closest = 0;
337 best = list;
338
339 p = list;
340 while (p) {
341 int distance = count_distance(p);
342 clear_distance(list);
343 if (nr - distance < distance)
344 distance = nr - distance;
345 if (distance > closest) {
346 best = p;
347 closest = distance;
348 }
349 p = p->next;
350 }
351 if (best)
352 best->next = NULL;
353 return best;
354}
355
5bdbaaa4
LT
356static void mark_edges_uninteresting(struct commit_list *list)
357{
358 for ( ; list; list = list->next) {
359 struct commit_list *parents = list->item->parents;
360
361 for ( ; parents; parents = parents->next) {
362 struct commit *commit = parents->item;
363 if (commit->object.flags & UNINTERESTING)
364 mark_tree_uninteresting(commit->tree);
365 }
366 }
367}
368
6da4016a 369static struct commit_list *limit_list(struct commit_list *list)
3b42a63c
LT
370{
371 struct commit_list *newlist = NULL;
372 struct commit_list **p = &newlist;
36f8d174 373 while (list) {
3b42a63c
LT
374 struct commit *commit = pop_most_recent_commit(&list, SEEN);
375 struct object *obj = &commit->object;
376
27cfe2e2
LT
377 if (max_age != -1 && (commit->date < max_age))
378 obj->flags |= UNINTERESTING;
12d2a187
LT
379 if (unpacked && has_sha1_pack(obj->sha1))
380 obj->flags |= UNINTERESTING;
337cb3fb 381 if (obj->flags & UNINTERESTING) {
3b42a63c
LT
382 mark_parents_uninteresting(commit);
383 if (everybody_uninteresting(list))
384 break;
385 continue;
386 }
27cfe2e2
LT
387 if (min_age != -1 && (commit->date > min_age))
388 continue;
3b42a63c 389 p = &commit_list_insert(commit, p)->next;
36f8d174 390 }
5bdbaaa4
LT
391 if (tree_objects)
392 mark_edges_uninteresting(newlist);
8b3a1e05
LT
393 if (bisect_list)
394 newlist = find_bisection(newlist);
3b42a63c
LT
395 return newlist;
396}
397
36f8d174
LT
398static void add_pending_object(struct object *obj, const char *name)
399{
400 add_object(obj, &pending_objects, name);
401}
402
3c90f03d
LT
403static struct commit *get_commit_reference(const char *name, unsigned int flags)
404{
405 unsigned char sha1[20];
36f8d174 406 struct object *object;
3c90f03d
LT
407
408 if (get_sha1(name, sha1))
409 usage(rev_list_usage);
36f8d174
LT
410 object = parse_object(sha1);
411 if (!object)
412 die("bad object %s", name);
413
414 /*
415 * Tag object? Look what it points to..
416 */
013aab82 417 while (object->type == tag_type) {
36f8d174
LT
418 struct tag *tag = (struct tag *) object;
419 object->flags |= flags;
420 if (tag_objects && !(object->flags & UNINTERESTING))
421 add_pending_object(object, tag->tag);
013aab82 422 object = parse_object(tag->tagged->sha1);
7f1335c7
SV
423 if (!object)
424 die("bad object %s", sha1_to_hex(tag->tagged->sha1));
36f8d174
LT
425 }
426
427 /*
428 * Commit object? Just return it, we'll do all the complex
429 * reachability crud.
430 */
431 if (object->type == commit_type) {
432 struct commit *commit = (struct commit *)object;
433 object->flags |= flags;
434 if (parse_commit(commit) < 0)
435 die("unable to parse commit %s", name);
454fbbcd
LT
436 if (flags & UNINTERESTING)
437 mark_parents_uninteresting(commit);
36f8d174
LT
438 return commit;
439 }
440
441 /*
442 * Tree object? Either mark it uniniteresting, or add it
443 * to the list of objects to look at later..
444 */
445 if (object->type == tree_type) {
446 struct tree *tree = (struct tree *)object;
447 if (!tree_objects)
960bba0d 448 return NULL;
36f8d174
LT
449 if (flags & UNINTERESTING) {
450 mark_tree_uninteresting(tree);
451 return NULL;
452 }
453 add_pending_object(object, "");
454 return NULL;
455 }
456
457 /*
458 * Blob object? You know the drill by now..
459 */
460 if (object->type == blob_type) {
461 struct blob *blob = (struct blob *)object;
462 if (!blob_objects)
960bba0d 463 return NULL;
36f8d174
LT
464 if (flags & UNINTERESTING) {
465 mark_blob_uninteresting(blob);
466 return NULL;
467 }
468 add_pending_object(object, "");
469 return NULL;
470 }
471 die("%s is unknown object", name);
3c90f03d
LT
472}
473
1215879c
JH
474static void handle_one_commit(struct commit *com, struct commit_list **lst)
475{
476 if (!com || com->object.flags & SEEN)
477 return;
478 com->object.flags |= SEEN;
479 commit_list_insert(com, lst);
480}
481
482
64745109
LT
483int main(int argc, char **argv)
484{
64745109 485 struct commit_list *list = NULL;
337cb3fb 486 int i, limited = 0;
64745109 487
d998a089 488 setup_git_directory();
fcfda02b 489 for (i = 1 ; i < argc; i++) {
337cb3fb 490 int flags;
fcfda02b 491 char *arg = argv[i];
1215879c 492 char *dotdot;
337cb3fb 493 struct commit *commit;
fcfda02b
KS
494
495 if (!strncmp(arg, "--max-count=", 12)) {
496 max_count = atoi(arg + 12);
a6f68d47
LT
497 continue;
498 }
499 if (!strncmp(arg, "--max-age=", 10)) {
fcfda02b 500 max_age = atoi(arg + 10);
27cfe2e2 501 limited = 1;
a6f68d47
LT
502 continue;
503 }
504 if (!strncmp(arg, "--min-age=", 10)) {
fcfda02b 505 min_age = atoi(arg + 10);
27cfe2e2 506 limited = 1;
a6f68d47 507 continue;
fcfda02b 508 }
a6f68d47
LT
509 if (!strcmp(arg, "--header")) {
510 verbose_header = 1;
511 continue;
512 }
000182ea
LT
513 if (!strncmp(arg, "--pretty", 8)) {
514 commit_format = get_commit_format(arg+8);
9d97aa64 515 verbose_header = 1;
9d97aa64 516 hdr_termination = '\n';
d87449c5 517 if (commit_format == CMIT_FMT_ONELINE)
d998a089 518 commit_prefix = "";
d87449c5 519 else
d998a089 520 commit_prefix = "commit ";
9d97aa64
LT
521 continue;
522 }
76cd8eb6
JS
523 if (!strncmp(arg, "--no-merges", 11)) {
524 no_merges = 1;
525 continue;
526 }
97658004
LT
527 if (!strcmp(arg, "--parents")) {
528 show_parents = 1;
529 continue;
530 }
8b3a1e05
LT
531 if (!strcmp(arg, "--bisect")) {
532 bisect_list = 1;
533 continue;
534 }
9de48752 535 if (!strcmp(arg, "--objects")) {
3c90f03d 536 tag_objects = 1;
9de48752
LT
537 tree_objects = 1;
538 blob_objects = 1;
539 continue;
540 }
12d2a187
LT
541 if (!strcmp(arg, "--unpacked")) {
542 unpacked = 1;
543 limited = 1;
544 continue;
545 }
12ba7eaf 546 if (!strcmp(arg, "--merge-order")) {
a3437b8c
JS
547 merge_order = 1;
548 continue;
549 }
12ba7eaf 550 if (!strcmp(arg, "--show-breaks")) {
a3437b8c
JS
551 show_breaks = 1;
552 continue;
553 }
d2d02a49
LT
554 if (!strcmp(arg, "--topo-order")) {
555 topo_order = 1;
e6c3505b 556 limited = 1;
d2d02a49
LT
557 continue;
558 }
a6f68d47 559
1215879c
JH
560 if (show_breaks && !merge_order)
561 usage(rev_list_usage);
562
337cb3fb 563 flags = 0;
1215879c
JH
564 dotdot = strstr(arg, "..");
565 if (dotdot) {
566 char *next = dotdot + 2;
567 struct commit *exclude = NULL;
568 struct commit *include = NULL;
569 *dotdot = 0;
2a7055ae
LT
570 if (!*next)
571 next = "HEAD";
1215879c
JH
572 exclude = get_commit_reference(arg, UNINTERESTING);
573 include = get_commit_reference(next, 0);
574 if (exclude && include) {
575 limited = 1;
576 handle_one_commit(exclude, &list);
577 handle_one_commit(include, &list);
578 continue;
579 }
2a7055ae 580 *dotdot = '.';
1215879c 581 }
337cb3fb
LT
582 if (*arg == '^') {
583 flags = UNINTERESTING;
584 arg++;
585 limited = 1;
586 }
3c90f03d 587 commit = get_commit_reference(arg, flags);
1215879c 588 handle_one_commit(commit, &list);
fcfda02b
KS
589 }
590
60ab26de 591 save_commit_buffer = verbose_header;
8805ccac 592 track_object_refs = 0;
60ab26de 593
a3437b8c 594 if (!merge_order) {
a7336ae5 595 sort_by_date(&list);
17ebe977 596 if (limited)
a3437b8c 597 list = limit_list(list);
d2d02a49
LT
598 if (topo_order)
599 sort_in_topological_order(&list);
a3437b8c 600 show_commit_list(list);
a3437b8c 601 } else {
dd53c7ab 602#ifndef NO_OPENSSL
a3437b8c 603 if (sort_list_in_merge_order(list, &process_commit)) {
dd53c7ab 604 die("merge order sort failed\n");
a3437b8c 605 }
dd53c7ab
PB
606#else
607 die("merge order sort unsupported, OpenSSL not linked");
608#endif
a3437b8c 609 }
8906300f 610
64745109
LT
611 return 0;
612}