git-upload-pack: More efficient usage of the has_sha1 array
[git.git] / rev-list.c
1 #include "cache.h"
2 #include "refs.h"
3 #include "tag.h"
4 #include "commit.h"
5 #include "tree.h"
6 #include "blob.h"
7 #include "epoch.h"
8 #include "diff.h"
9
10 #define SEEN            (1u << 0)
11 #define INTERESTING     (1u << 1)
12 #define COUNTED         (1u << 2)
13 #define SHOWN           (1u << 3)
14 #define TREECHANGE      (1u << 4)
15
16 static const char rev_list_usage[] =
17         "git-rev-list [OPTION] commit-id <commit-id>\n"
18                       "  --max-count=nr\n"
19                       "  --max-age=epoch\n"
20                       "  --min-age=epoch\n"
21                       "  --parents\n"
22                       "  --bisect\n"
23                       "  --objects\n"
24                       "  --unpacked\n"
25                       "  --header\n"
26                       "  --pretty\n"
27                       "  --no-merges\n"
28                       "  --merge-order [ --show-breaks ]\n"
29                       "  --topo-order";
30
31 static int dense = 0;
32 static int unpacked = 0;
33 static int bisect_list = 0;
34 static int tag_objects = 0;
35 static int tree_objects = 0;
36 static int blob_objects = 0;
37 static int verbose_header = 0;
38 static int show_parents = 0;
39 static int hdr_termination = 0;
40 static const char *commit_prefix = "";
41 static unsigned long max_age = -1;
42 static unsigned long min_age = -1;
43 static int max_count = -1;
44 static enum cmit_fmt commit_format = CMIT_FMT_RAW;
45 static int merge_order = 0;
46 static int show_breaks = 0;
47 static int stop_traversal = 0;
48 static int topo_order = 0;
49 static int no_merges = 0;
50 static const char **paths = NULL;
51
52 static void show_commit(struct commit *commit)
53 {
54         commit->object.flags |= SHOWN;
55         if (show_breaks) {
56                 commit_prefix = "| ";
57                 if (commit->object.flags & DISCONTINUITY) {
58                         commit_prefix = "^ ";     
59                 } else if (commit->object.flags & BOUNDARY) {
60                         commit_prefix = "= ";
61                 } 
62         }                       
63         printf("%s%s", commit_prefix, sha1_to_hex(commit->object.sha1));
64         if (show_parents) {
65                 struct commit_list *parents = commit->parents;
66                 while (parents) {
67                         printf(" %s", sha1_to_hex(parents->item->object.sha1));
68                         parents = parents->next;
69                 }
70         }
71         if (commit_format == CMIT_FMT_ONELINE)
72                 putchar(' ');
73         else
74                 putchar('\n');
75
76         if (verbose_header) {
77                 static char pretty_header[16384];
78                 pretty_print_commit(commit_format, commit->buffer, ~0, pretty_header, sizeof(pretty_header));
79                 printf("%s%c", pretty_header, hdr_termination);
80         }
81         fflush(stdout);
82 }
83
84 static void rewrite_one(struct commit **pp)
85 {
86         for (;;) {
87                 struct commit *p = *pp;
88                 if (p->object.flags & (TREECHANGE | UNINTERESTING))
89                         return;
90                 /* Only single-parent commits don't have TREECHANGE */
91                 *pp = p->parents->item;
92         }
93 }
94
95 static void rewrite_parents(struct commit *commit)
96 {
97         struct commit_list *parent = commit->parents;
98         while (parent) {
99                 rewrite_one(&parent->item);
100                 parent = parent->next;
101         }
102 }
103
104 static int filter_commit(struct commit * commit)
105 {
106         if (stop_traversal && (commit->object.flags & BOUNDARY))
107                 return STOP;
108         if (commit->object.flags & (UNINTERESTING|SHOWN))
109                 return CONTINUE;
110         if (min_age != -1 && (commit->date > min_age))
111                 return CONTINUE;
112         if (max_age != -1 && (commit->date < max_age)) {
113                 stop_traversal=1;
114                 return CONTINUE;
115         }
116         if (max_count != -1 && !max_count--)
117                 return STOP;
118         if (no_merges && (commit->parents && commit->parents->next))
119                 return CONTINUE;
120         if (paths && dense) {
121                 if (!(commit->object.flags & TREECHANGE))
122                         return CONTINUE;
123                 rewrite_parents(commit);
124         }
125         return DO;
126 }
127
128 static int process_commit(struct commit * commit)
129 {
130         int action=filter_commit(commit);
131
132         if (action == STOP) {
133                 return STOP;
134         }
135
136         if (action == CONTINUE) {
137                 return CONTINUE;
138         }
139
140         show_commit(commit);
141
142         return CONTINUE;
143 }
144
145 static struct object_list **add_object(struct object *obj, struct object_list **p, const char *name)
146 {
147         struct object_list *entry = xmalloc(sizeof(*entry));
148         entry->item = obj;
149         entry->next = *p;
150         entry->name = name;
151         *p = entry;
152         return &entry->next;
153 }
154
155 static struct object_list **process_blob(struct blob *blob, struct object_list **p, const char *name)
156 {
157         struct object *obj = &blob->object;
158
159         if (!blob_objects)
160                 return p;
161         if (obj->flags & (UNINTERESTING | SEEN))
162                 return p;
163         obj->flags |= SEEN;
164         return add_object(obj, p, name);
165 }
166
167 static struct object_list **process_tree(struct tree *tree, struct object_list **p, const char *name)
168 {
169         struct object *obj = &tree->object;
170         struct tree_entry_list *entry;
171
172         if (!tree_objects)
173                 return p;
174         if (obj->flags & (UNINTERESTING | SEEN))
175                 return p;
176         if (parse_tree(tree) < 0)
177                 die("bad tree object %s", sha1_to_hex(obj->sha1));
178         obj->flags |= SEEN;
179         p = add_object(obj, p, name);
180         entry = tree->entries;
181         tree->entries = NULL;
182         while (entry) {
183                 struct tree_entry_list *next = entry->next;
184                 if (entry->directory)
185                         p = process_tree(entry->item.tree, p, entry->name);
186                 else
187                         p = process_blob(entry->item.blob, p, entry->name);
188                 free(entry);
189                 entry = next;
190         }
191         return p;
192 }
193
194 static struct object_list *pending_objects = NULL;
195
196 static void show_commit_list(struct commit_list *list)
197 {
198         struct object_list *objects = NULL, **p = &objects, *pending;
199         while (list) {
200                 struct commit *commit = pop_most_recent_commit(&list, SEEN);
201
202                 p = process_tree(commit->tree, p, "");
203                 if (process_commit(commit) == STOP)
204                         break;
205         }
206         for (pending = pending_objects; pending; pending = pending->next) {
207                 struct object *obj = pending->item;
208                 const char *name = pending->name;
209                 if (obj->flags & (UNINTERESTING | SEEN))
210                         continue;
211                 if (obj->type == tag_type) {
212                         obj->flags |= SEEN;
213                         p = add_object(obj, p, name);
214                         continue;
215                 }
216                 if (obj->type == tree_type) {
217                         p = process_tree((struct tree *)obj, p, name);
218                         continue;
219                 }
220                 if (obj->type == blob_type) {
221                         p = process_blob((struct blob *)obj, p, name);
222                         continue;
223                 }
224                 die("unknown pending object %s (%s)", sha1_to_hex(obj->sha1), name);
225         }
226         while (objects) {
227                 /* An object with name "foo\n0000000000000000000000000000000000000000"
228                  * can be used confuse downstream git-pack-objects very badly.
229                  */
230                 const char *ep = strchr(objects->name, '\n');
231                 if (ep) {
232                         printf("%s %.*s\n", sha1_to_hex(objects->item->sha1),
233                                (int) (ep - objects->name),
234                                objects->name);
235                 }
236                 else
237                         printf("%s %s\n", sha1_to_hex(objects->item->sha1), objects->name);
238                 objects = objects->next;
239         }
240 }
241
242 static void mark_blob_uninteresting(struct blob *blob)
243 {
244         if (!blob_objects)
245                 return;
246         if (blob->object.flags & UNINTERESTING)
247                 return;
248         blob->object.flags |= UNINTERESTING;
249 }
250
251 static void mark_tree_uninteresting(struct tree *tree)
252 {
253         struct object *obj = &tree->object;
254         struct tree_entry_list *entry;
255
256         if (!tree_objects)
257                 return;
258         if (obj->flags & UNINTERESTING)
259                 return;
260         obj->flags |= UNINTERESTING;
261         if (!has_sha1_file(obj->sha1))
262                 return;
263         if (parse_tree(tree) < 0)
264                 die("bad tree %s", sha1_to_hex(obj->sha1));
265         entry = tree->entries;
266         tree->entries = NULL;
267         while (entry) {
268                 struct tree_entry_list *next = entry->next;
269                 if (entry->directory)
270                         mark_tree_uninteresting(entry->item.tree);
271                 else
272                         mark_blob_uninteresting(entry->item.blob);
273                 free(entry);
274                 entry = next;
275         }
276 }
277
278 static void mark_parents_uninteresting(struct commit *commit)
279 {
280         struct commit_list *parents = commit->parents;
281
282         while (parents) {
283                 struct commit *commit = parents->item;
284                 commit->object.flags |= UNINTERESTING;
285
286                 /*
287                  * Normally we haven't parsed the parent
288                  * yet, so we won't have a parent of a parent
289                  * here. However, it may turn out that we've
290                  * reached this commit some other way (where it
291                  * wasn't uninteresting), in which case we need
292                  * to mark its parents recursively too..
293                  */
294                 if (commit->parents)
295                         mark_parents_uninteresting(commit);
296
297                 /*
298                  * A missing commit is ok iff its parent is marked 
299                  * uninteresting.
300                  *
301                  * We just mark such a thing parsed, so that when
302                  * it is popped next time around, we won't be trying
303                  * to parse it and get an error.
304                  */
305                 if (!has_sha1_file(commit->object.sha1))
306                         commit->object.parsed = 1;
307                 parents = parents->next;
308         }
309 }
310
311 static int everybody_uninteresting(struct commit_list *orig)
312 {
313         struct commit_list *list = orig;
314         while (list) {
315                 struct commit *commit = list->item;
316                 list = list->next;
317                 if (commit->object.flags & UNINTERESTING)
318                         continue;
319                 return 0;
320         }
321         return 1;
322 }
323
324 /*
325  * This is a truly stupid algorithm, but it's only
326  * used for bisection, and we just don't care enough.
327  *
328  * We care just barely enough to avoid recursing for
329  * non-merge entries.
330  */
331 static int count_distance(struct commit_list *entry)
332 {
333         int nr = 0;
334
335         while (entry) {
336                 struct commit *commit = entry->item;
337                 struct commit_list *p;
338
339                 if (commit->object.flags & (UNINTERESTING | COUNTED))
340                         break;
341                 nr++;
342                 commit->object.flags |= COUNTED;
343                 p = commit->parents;
344                 entry = p;
345                 if (p) {
346                         p = p->next;
347                         while (p) {
348                                 nr += count_distance(p);
349                                 p = p->next;
350                         }
351                 }
352         }
353         return nr;
354 }
355
356 static void clear_distance(struct commit_list *list)
357 {
358         while (list) {
359                 struct commit *commit = list->item;
360                 commit->object.flags &= ~COUNTED;
361                 list = list->next;
362         }
363 }
364
365 static struct commit_list *find_bisection(struct commit_list *list)
366 {
367         int nr, closest;
368         struct commit_list *p, *best;
369
370         nr = 0;
371         p = list;
372         while (p) {
373                 nr++;
374                 p = p->next;
375         }
376         closest = 0;
377         best = list;
378
379         p = list;
380         while (p) {
381                 int distance = count_distance(p);
382                 clear_distance(list);
383                 if (nr - distance < distance)
384                         distance = nr - distance;
385                 if (distance > closest) {
386                         best = p;
387                         closest = distance;
388                 }
389                 p = p->next;
390         }
391         if (best)
392                 best->next = NULL;
393         return best;
394 }
395
396 static void mark_edges_uninteresting(struct commit_list *list)
397 {
398         for ( ; list; list = list->next) {
399                 struct commit_list *parents = list->item->parents;
400
401                 for ( ; parents; parents = parents->next) {
402                         struct commit *commit = parents->item;
403                         if (commit->object.flags & UNINTERESTING)
404                                 mark_tree_uninteresting(commit->tree);
405                 }
406         }
407 }
408
409 static int is_different = 0;
410
411 static void file_add_remove(struct diff_options *options,
412                     int addremove, unsigned mode,
413                     const unsigned char *sha1,
414                     const char *base, const char *path)
415 {
416         is_different = 1;
417 }
418
419 static void file_change(struct diff_options *options,
420                  unsigned old_mode, unsigned new_mode,
421                  const unsigned char *old_sha1,
422                  const unsigned char *new_sha1,
423                  const char *base, const char *path)
424 {
425         is_different = 1;
426 }
427
428 static struct diff_options diff_opt = {
429         .recursive = 1,
430         .add_remove = file_add_remove,
431         .change = file_change,
432 };
433
434 static int same_tree(struct tree *t1, struct tree *t2)
435 {
436         is_different = 0;
437         if (diff_tree_sha1(t1->object.sha1, t2->object.sha1, "", &diff_opt) < 0)
438                 return 0;
439         return !is_different;
440 }
441
442 static struct commit *try_to_simplify_merge(struct commit *commit, struct commit_list *parent)
443 {
444         if (!commit->tree)
445                 return NULL;
446
447         while (parent) {
448                 struct commit *p = parent->item;
449                 parent = parent->next;
450                 parse_commit(p);
451                 if (!p->tree)
452                         continue;
453                 if (same_tree(commit->tree, p->tree))
454                         return p;
455         }
456         return NULL;
457 }
458
459 static void add_parents_to_list(struct commit *commit, struct commit_list **list)
460 {
461         struct commit_list *parent = commit->parents;
462
463         /*
464          * If the commit is uninteresting, don't try to
465          * prune parents - we want the maximal uninteresting
466          * set.
467          *
468          * Normally we haven't parsed the parent
469          * yet, so we won't have a parent of a parent
470          * here. However, it may turn out that we've
471          * reached this commit some other way (where it
472          * wasn't uninteresting), in which case we need
473          * to mark its parents recursively too..
474          */
475         if (commit->object.flags & UNINTERESTING) {
476                 while (parent) {
477                         struct commit *p = parent->item;
478                         parent = parent->next;
479                         parse_commit(p);
480                         p->object.flags |= UNINTERESTING;
481                         if (p->parents)
482                                 mark_parents_uninteresting(p);
483                         if (p->object.flags & SEEN)
484                                 continue;
485                         p->object.flags |= SEEN;
486                         insert_by_date(p, list);
487                 }
488                 return;
489         }
490
491         /*
492          * Ok, the commit wasn't uninteresting. If it
493          * is a merge, try to find the parent that has
494          * no differences in the path set if one exists.
495          */
496         if (paths && parent && parent->next) {
497                 struct commit *preferred;
498
499                 preferred = try_to_simplify_merge(commit, parent);
500                 if (preferred) {
501                         parent->item = preferred;
502                         parent->next = NULL;
503                 }
504         }
505
506         while (parent) {
507                 struct commit *p = parent->item;
508
509                 parent = parent->next;
510
511                 parse_commit(p);
512                 if (p->object.flags & SEEN)
513                         continue;
514                 p->object.flags |= SEEN;
515                 insert_by_date(p, list);
516         }
517 }
518
519 static void compress_list(struct commit_list *list)
520 {
521         while (list) {
522                 struct commit *commit = list->item;
523                 struct commit_list *parent = commit->parents;
524                 list = list->next;
525
526                 /*
527                  * Exactly one parent? Check if it leaves the tree
528                  * unchanged
529                  */
530                 if (parent && !parent->next) {
531                         struct tree *t1 = commit->tree;
532                         struct tree *t2 = parent->item->tree;
533                         if (!t1 || !t2 || same_tree(t1, t2))
534                                 continue;
535                 }
536                 commit->object.flags |= TREECHANGE;
537         }
538 }
539
540 static struct commit_list *limit_list(struct commit_list *list)
541 {
542         struct commit_list *newlist = NULL;
543         struct commit_list **p = &newlist;
544         while (list) {
545                 struct commit_list *entry = list;
546                 struct commit *commit = list->item;
547                 struct object *obj = &commit->object;
548
549                 list = list->next;
550                 free(entry);
551
552                 if (max_age != -1 && (commit->date < max_age))
553                         obj->flags |= UNINTERESTING;
554                 if (unpacked && has_sha1_pack(obj->sha1))
555                         obj->flags |= UNINTERESTING;
556                 add_parents_to_list(commit, &list);
557                 if (obj->flags & UNINTERESTING) {
558                         mark_parents_uninteresting(commit);
559                         if (everybody_uninteresting(list))
560                                 break;
561                         continue;
562                 }
563                 if (min_age != -1 && (commit->date > min_age))
564                         continue;
565                 p = &commit_list_insert(commit, p)->next;
566         }
567         if (tree_objects)
568                 mark_edges_uninteresting(newlist);
569         if (paths && dense)
570                 compress_list(newlist);
571         if (bisect_list)
572                 newlist = find_bisection(newlist);
573         return newlist;
574 }
575
576 static void add_pending_object(struct object *obj, const char *name)
577 {
578         add_object(obj, &pending_objects, name);
579 }
580
581 static struct commit *get_commit_reference(const char *name, unsigned int flags)
582 {
583         unsigned char sha1[20];
584         struct object *object;
585
586         if (get_sha1(name, sha1))
587                 usage(rev_list_usage);
588         object = parse_object(sha1);
589         if (!object)
590                 die("bad object %s", name);
591
592         /*
593          * Tag object? Look what it points to..
594          */
595         while (object->type == tag_type) {
596                 struct tag *tag = (struct tag *) object;
597                 object->flags |= flags;
598                 if (tag_objects && !(object->flags & UNINTERESTING))
599                         add_pending_object(object, tag->tag);
600                 object = parse_object(tag->tagged->sha1);
601                 if (!object)
602                         die("bad object %s", sha1_to_hex(tag->tagged->sha1));
603         }
604
605         /*
606          * Commit object? Just return it, we'll do all the complex
607          * reachability crud.
608          */
609         if (object->type == commit_type) {
610                 struct commit *commit = (struct commit *)object;
611                 object->flags |= flags;
612                 if (parse_commit(commit) < 0)
613                         die("unable to parse commit %s", name);
614                 if (flags & UNINTERESTING)
615                         mark_parents_uninteresting(commit);
616                 return commit;
617         }
618
619         /*
620          * Tree object? Either mark it uniniteresting, or add it
621          * to the list of objects to look at later..
622          */
623         if (object->type == tree_type) {
624                 struct tree *tree = (struct tree *)object;
625                 if (!tree_objects)
626                         return NULL;
627                 if (flags & UNINTERESTING) {
628                         mark_tree_uninteresting(tree);
629                         return NULL;
630                 }
631                 add_pending_object(object, "");
632                 return NULL;
633         }
634
635         /*
636          * Blob object? You know the drill by now..
637          */
638         if (object->type == blob_type) {
639                 struct blob *blob = (struct blob *)object;
640                 if (!blob_objects)
641                         return NULL;
642                 if (flags & UNINTERESTING) {
643                         mark_blob_uninteresting(blob);
644                         return NULL;
645                 }
646                 add_pending_object(object, "");
647                 return NULL;
648         }
649         die("%s is unknown object", name);
650 }
651
652 static void handle_one_commit(struct commit *com, struct commit_list **lst)
653 {
654         if (!com || com->object.flags & SEEN)
655                 return;
656         com->object.flags |= SEEN;
657         commit_list_insert(com, lst);
658 }
659
660 /* for_each_ref() callback does not allow user data -- Yuck. */
661 static struct commit_list **global_lst;
662
663 static int include_one_commit(const char *path, const unsigned char *sha1)
664 {
665         struct commit *com = get_commit_reference(path, 0);
666         handle_one_commit(com, global_lst);
667         return 0;
668 }
669
670 static void handle_all(struct commit_list **lst)
671 {
672         global_lst = lst;
673         for_each_ref(include_one_commit);
674         global_lst = NULL;
675 }
676
677 int main(int argc, const char **argv)
678 {
679         const char *prefix = setup_git_directory();
680         struct commit_list *list = NULL;
681         int i, limited = 0;
682
683         for (i = 1 ; i < argc; i++) {
684                 int flags;
685                 const char *arg = argv[i];
686                 char *dotdot;
687                 struct commit *commit;
688
689                 if (!strncmp(arg, "--max-count=", 12)) {
690                         max_count = atoi(arg + 12);
691                         continue;
692                 }
693                 if (!strncmp(arg, "--max-age=", 10)) {
694                         max_age = atoi(arg + 10);
695                         limited = 1;
696                         continue;
697                 }
698                 if (!strncmp(arg, "--min-age=", 10)) {
699                         min_age = atoi(arg + 10);
700                         limited = 1;
701                         continue;
702                 }
703                 if (!strcmp(arg, "--header")) {
704                         verbose_header = 1;
705                         continue;
706                 }
707                 if (!strncmp(arg, "--pretty", 8)) {
708                         commit_format = get_commit_format(arg+8);
709                         verbose_header = 1;
710                         hdr_termination = '\n';
711                         if (commit_format == CMIT_FMT_ONELINE)
712                                 commit_prefix = "";
713                         else
714                                 commit_prefix = "commit ";
715                         continue;
716                 }
717                 if (!strncmp(arg, "--no-merges", 11)) {
718                         no_merges = 1;
719                         continue;
720                 }
721                 if (!strcmp(arg, "--parents")) {
722                         show_parents = 1;
723                         continue;
724                 }
725                 if (!strcmp(arg, "--bisect")) {
726                         bisect_list = 1;
727                         continue;
728                 }
729                 if (!strcmp(arg, "--all")) {
730                         handle_all(&list);
731                         continue;
732                 }
733                 if (!strcmp(arg, "--objects")) {
734                         tag_objects = 1;
735                         tree_objects = 1;
736                         blob_objects = 1;
737                         continue;
738                 }
739                 if (!strcmp(arg, "--unpacked")) {
740                         unpacked = 1;
741                         limited = 1;
742                         continue;
743                 }
744                 if (!strcmp(arg, "--merge-order")) {
745                         merge_order = 1;
746                         continue;
747                 }
748                 if (!strcmp(arg, "--show-breaks")) {
749                         show_breaks = 1;
750                         continue;
751                 }
752                 if (!strcmp(arg, "--topo-order")) {
753                         topo_order = 1;
754                         limited = 1;
755                         continue;
756                 }
757                 if (!strcmp(arg, "--dense")) {
758                         dense = 1;
759                         continue;
760                 }
761                 if (!strcmp(arg, "--")) {
762                         paths = get_pathspec(prefix, argv + i + 1);
763                         if (paths) {
764                                 limited = 1;
765                                 diff_tree_setup_paths(paths);
766                         }
767                         break;
768                 }
769
770                 if (show_breaks && !merge_order)
771                         usage(rev_list_usage);
772
773                 flags = 0;
774                 dotdot = strstr(arg, "..");
775                 if (dotdot) {
776                         char *next = dotdot + 2;
777                         struct commit *exclude = NULL;
778                         struct commit *include = NULL;
779                         *dotdot = 0;
780                         if (!*next)
781                                 next = "HEAD";
782                         exclude = get_commit_reference(arg, UNINTERESTING);
783                         include = get_commit_reference(next, 0);
784                         if (exclude && include) {
785                                 limited = 1;
786                                 handle_one_commit(exclude, &list);
787                                 handle_one_commit(include, &list);
788                                 continue;
789                         }
790                         *dotdot = '.';
791                 }
792                 if (*arg == '^') {
793                         flags = UNINTERESTING;
794                         arg++;
795                         limited = 1;
796                 }
797                 commit = get_commit_reference(arg, flags);
798                 handle_one_commit(commit, &list);
799         }
800
801         save_commit_buffer = verbose_header;
802         track_object_refs = 0;
803
804         if (!merge_order) {             
805                 sort_by_date(&list);
806                 if (list && !limited && max_count == 1 &&
807                     !tag_objects && !tree_objects && !blob_objects) {
808                         show_commit(list->item);
809                         return 0;
810                 }
811                 if (limited)
812                         list = limit_list(list);
813                 if (topo_order)
814                         sort_in_topological_order(&list);
815                 show_commit_list(list);
816         } else {
817 #ifndef NO_OPENSSL
818                 if (sort_list_in_merge_order(list, &process_commit)) {
819                         die("merge order sort failed\n");
820                 }
821 #else
822                 die("merge order sort unsupported, OpenSSL not linked");
823 #endif
824         }
825
826         return 0;
827 }