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