First cut at libifying revlist generation
This really just splits things up partially, and creates the interface to set things up by parsing the command line. No real code changes so far, although the parsing of filenames is a bit stricter. In particular, if there is a "--", then we do not accept any filenames before it, and if there isn't any "--", then we check that _all_ paths listed are valid, not just the first one. The new argument parsing automatically also gives us "--default" and "--not" handling as in git-rev-parse. Signed-off-by: Linus Torvalds <torvalds@osdl.org> Signed-off-by: Junio C Hamano <junkio@cox.net>
Linus Torvalds committed
Feb 25, 2006 at 16:19 UTC
ae563542bf10fa8c33abd2a354e4b28aca4264d7
6 files changed
+464
-357
Makefile
+2
-2
@@ -192,7 +192,7 @@ LIB_FILE=libgit.a
192
LIB_H = \
193
blob.h cache.h commit.h count-delta.h csum-file.h delta.h \
194
diff.h epoch.h object.h pack.h pkt-line.h quote.h refs.h \
195
- run-command.h strbuf.h tag.h tree.h git-compat-util.h
195
+ run-command.h strbuf.h tag.h tree.h git-compat-util.h revision.h
196
197
DIFF_OBJS = \
198
diff.o diffcore-break.o diffcore-order.o diffcore-pathspec.o \
@@ -205,7 +205,7 @@ LIB_OBJS = \
205
quote.o read-cache.o refs.o run-command.o \
206
server-info.o setup.o sha1_file.o sha1_name.o strbuf.o \
207
tag.o tree.o usage.o config.o environment.o ctype.o copy.o \
208
- fetch-clone.o \
208
+ fetch-clone.o revision.o \
209
$(DIFF_OBJS)
210
211
LIBS = $(LIB_FILE)
epoch.c
+1
@@ -15,6 +15,7 @@
15
16
#include "cache.h"
17
#include "commit.h"
18
+#include "revision.h"
19
#include "epoch.h"
20
21
struct fraction {
epoch.h
-1
@@ -11,7 +11,6 @@ typedef int (*emitter_func) (struct commit *);
11
int sort_list_in_merge_order(struct commit_list *list, emitter_func emitter);
12
13
/* Low bits are used by rev-list */
14
-#define UNINTERESTING (1u<<10)
14
#define BOUNDARY (1u<<11)
15
#define VISITED (1u<<12)
16
#define DISCONTINUITY (1u<<13)
rev-list.c
+43
-354
@@ -6,9 +6,10 @@
6
#include "blob.h"
7
#include "epoch.h"
8
#include "diff.h"
9
+#include "revision.h"
10
+
11
+/* bits #0 and #1 in revision.h */
12
10
-#define SEEN (1u << 0)
11
-#define INTERESTING (1u << 1)
13
#define COUNTED (1u << 2)
14
#define SHOWN (1u << 3)
15
#define TREECHANGE (1u << 4)
@@ -38,60 +39,20 @@ static const char rev_list_usage[] =
39
" --bisect"
40
;
41
41
-static int dense = 1;
42
+struct rev_info revs;
43
+
44
static int unpacked = 0;
45
static int bisect_list = 0;
44
-static int tag_objects = 0;
45
-static int tree_objects = 0;
46
-static int blob_objects = 0;
47
-static int edge_hint = 0;
46
static int verbose_header = 0;
47
static int abbrev = DEFAULT_ABBREV;
48
static int show_parents = 0;
49
static int hdr_termination = 0;
50
static const char *commit_prefix = "";
53
-static unsigned long max_age = -1;
54
-static unsigned long min_age = -1;
55
-static int max_count = -1;
51
static enum cmit_fmt commit_format = CMIT_FMT_RAW;
52
static int merge_order = 0;
53
static int show_breaks = 0;
54
static int stop_traversal = 0;
60
-static int topo_order = 0;
61
-static int lifo = 1;
55
static int no_merges = 0;
63
-static const char **paths = NULL;
64
-static int remove_empty_trees = 0;
65
-
66
-struct name_path {
67
- struct name_path *up;
68
- int elem_len;
69
- const char *elem;
70
-};
71
-
72
-static char *path_name(struct name_path *path, const char *name)
73
-{
74
- struct name_path *p;
75
- char *n, *m;
76
- int nlen = strlen(name);
77
- int len = nlen + 1;
78
-
79
- for (p = path; p; p = p->up) {
80
- if (p->elem_len)
81
- len += p->elem_len + 1;
82
- }
83
- n = xmalloc(len);
84
- m = n + len - (nlen + 1);
85
- strcpy(m, name);
86
- for (p = path; p; p = p->up) {
87
- if (p->elem_len) {
88
- m -= p->elem_len + 1;
89
- memcpy(m, p->elem, p->elem_len);
90
- m[p->elem_len] = '/';
91
- }
92
- }
93
- return n;
94
-}
56
57
static void show_commit(struct commit *commit)
58
{
@@ -168,15 +129,15 @@ static int filter_commit(struct commit * commit)
129
return STOP;
130
if (commit->object.flags & (UNINTERESTING|SHOWN))
131
return CONTINUE;
171
- if (min_age != -1 && (commit->date > min_age))
132
+ if (revs.min_age != -1 && (commit->date > revs.min_age))
133
return CONTINUE;
173
- if (max_age != -1 && (commit->date < max_age)) {
134
+ if (revs.max_age != -1 && (commit->date < revs.max_age)) {
135
stop_traversal=1;
136
return CONTINUE;
137
}
138
if (no_merges && (commit->parents && commit->parents->next))
139
return CONTINUE;
179
- if (paths && dense) {
140
+ if (revs.paths && revs.dense) {
141
if (!(commit->object.flags & TREECHANGE))
142
return CONTINUE;
143
rewrite_parents(commit);
@@ -196,7 +157,7 @@ static int process_commit(struct commit * commit)
157
return CONTINUE;
158
}
159
199
- if (max_count != -1 && !max_count--)
160
+ if (revs.max_count != -1 && !revs.max_count--)
161
return STOP;
162
163
show_commit(commit);
@@ -204,19 +165,6 @@ static int process_commit(struct commit * commit)
165
return CONTINUE;
166
}
167
207
-static struct object_list **add_object(struct object *obj,
208
- struct object_list **p,
209
- struct name_path *path,
210
- const char *name)
211
-{
212
- struct object_list *entry = xmalloc(sizeof(*entry));
213
- entry->item = obj;
214
- entry->next = *p;
215
- entry->name = path_name(path, name);
216
- *p = entry;
217
- return &entry->next;
218
-}
219
-
168
static struct object_list **process_blob(struct blob *blob,
169
struct object_list **p,
170
struct name_path *path,
@@ -224,7 +172,7 @@ static struct object_list **process_blob(struct blob *blob,
172
{
173
struct object *obj = &blob->object;
174
227
- if (!blob_objects)
175
+ if (!revs.blob_objects)
176
return p;
177
if (obj->flags & (UNINTERESTING | SEEN))
178
return p;
@@ -241,7 +189,7 @@ static struct object_list **process_tree(struct tree *tree,
189
struct tree_entry_list *entry;
190
struct name_path me;
191
244
- if (!tree_objects)
192
+ if (!revs.tree_objects)
193
return p;
194
if (obj->flags & (UNINTERESTING | SEEN))
195
return p;
@@ -314,75 +262,6 @@ static void show_commit_list(struct commit_list *list)
262
}
263
}
264
317
-static void mark_blob_uninteresting(struct blob *blob)
318
-{
319
- if (!blob_objects)
320
- return;
321
- if (blob->object.flags & UNINTERESTING)
322
- return;
323
- blob->object.flags |= UNINTERESTING;
324
-}
325
-
326
-static void mark_tree_uninteresting(struct tree *tree)
327
-{
328
- struct object *obj = &tree->object;
329
- struct tree_entry_list *entry;
330
-
331
- if (!tree_objects)
332
- return;
333
- if (obj->flags & UNINTERESTING)
334
- return;
335
- obj->flags |= UNINTERESTING;
336
- if (!has_sha1_file(obj->sha1))
337
- return;
338
- if (parse_tree(tree) < 0)
339
- die("bad tree %s", sha1_to_hex(obj->sha1));
340
- entry = tree->entries;
341
- tree->entries = NULL;
342
- while (entry) {
343
- struct tree_entry_list *next = entry->next;
344
- if (entry->directory)
345
- mark_tree_uninteresting(entry->item.tree);
346
- else
347
- mark_blob_uninteresting(entry->item.blob);
348
- free(entry);
349
- entry = next;
350
- }
351
-}
352
-
353
-static void mark_parents_uninteresting(struct commit *commit)
354
-{
355
- struct commit_list *parents = commit->parents;
356
-
357
- while (parents) {
358
- struct commit *commit = parents->item;
359
- commit->object.flags |= UNINTERESTING;
360
-
361
- /*
362
- * Normally we haven't parsed the parent
363
- * yet, so we won't have a parent of a parent
364
- * here. However, it may turn out that we've
365
- * reached this commit some other way (where it
366
- * wasn't uninteresting), in which case we need
367
- * to mark its parents recursively too..
368
- */
369
- if (commit->parents)
370
- mark_parents_uninteresting(commit);
371
-
372
- /*
373
- * A missing commit is ok iff its parent is marked
374
- * uninteresting.
375
- *
376
- * We just mark such a thing parsed, so that when
377
- * it is popped next time around, we won't be trying
378
- * to parse it and get an error.
379
- */
380
- if (!has_sha1_file(commit->object.sha1))
381
- commit->object.parsed = 1;
382
- parents = parents->next;
383
- }
384
-}
385
-
265
static int everybody_uninteresting(struct commit_list *orig)
266
{
267
struct commit_list *list = orig;
@@ -413,7 +292,7 @@ static int count_distance(struct commit_list *entry)
292
293
if (commit->object.flags & (UNINTERESTING | COUNTED))
294
break;
416
- if (!paths || (commit->object.flags & TREECHANGE))
295
+ if (!revs.paths || (commit->object.flags & TREECHANGE))
296
nr++;
297
commit->object.flags |= COUNTED;
298
p = commit->parents;
@@ -447,7 +326,7 @@ static struct commit_list *find_bisection(struct commit_list *list)
326
nr = 0;
327
p = list;
328
while (p) {
450
- if (!paths || (p->item->object.flags & TREECHANGE))
329
+ if (!revs.paths || (p->item->object.flags & TREECHANGE))
330
nr++;
331
p = p->next;
332
}
@@ -457,7 +336,7 @@ static struct commit_list *find_bisection(struct commit_list *list)
336
for (p = list; p; p = p->next) {
337
int distance;
338
460
- if (paths && !(p->item->object.flags & TREECHANGE))
339
+ if (revs.paths && !(p->item->object.flags & TREECHANGE))
340
continue;
341
342
distance = count_distance(p);
@@ -483,7 +362,7 @@ static void mark_edge_parents_uninteresting(struct commit *commit)
362
if (!(parent->object.flags & UNINTERESTING))
363
continue;
364
mark_tree_uninteresting(parent->tree);
486
- if (edge_hint && !(parent->object.flags & SHOWN)) {
365
+ if (revs.edge_hint && !(parent->object.flags & SHOWN)) {
366
parent->object.flags |= SHOWN;
367
printf("-%s\n", sha1_to_hex(parent->object.sha1));
368
}
@@ -613,7 +492,7 @@ static void try_to_simplify_commit(struct commit *commit)
492
return;
493
494
case TREE_NEW:
616
- if (remove_empty_trees && same_tree_as_empty(p->tree)) {
495
+ if (revs.remove_empty_trees && same_tree_as_empty(p->tree)) {
496
*pp = parent->next;
497
continue;
498
}
@@ -664,7 +543,7 @@ static void add_parents_to_list(struct commit *commit, struct commit_list **list
543
* simplify the commit history and find the parent
544
* that has no differences in the path set if one exists.
545
*/
667
- if (paths)
546
+ if (revs.paths)
547
try_to_simplify_commit(commit);
548
549
parent = commit->parents;
@@ -693,7 +572,7 @@ static struct commit_list *limit_list(struct commit_list *list)
572
list = list->next;
573
free(entry);
574
696
- if (max_age != -1 && (commit->date < max_age))
575
+ if (revs.max_age != -1 && (commit->date < revs.max_age))
576
obj->flags |= UNINTERESTING;
577
if (unpacked && has_sha1_pack(obj->sha1))
578
obj->flags |= UNINTERESTING;
@@ -704,155 +583,40 @@ static struct commit_list *limit_list(struct commit_list *list)
583
break;
584
continue;
585
}
707
- if (min_age != -1 && (commit->date > min_age))
586
+ if (revs.min_age != -1 && (commit->date > revs.min_age))
587
continue;
588
p = &commit_list_insert(commit, p)->next;
589
}
711
- if (tree_objects)
590
+ if (revs.tree_objects)
591
mark_edges_uninteresting(newlist);
592
if (bisect_list)
593
newlist = find_bisection(newlist);
594
return newlist;
595
}
596
718
-static void add_pending_object(struct object *obj, const char *name)
719
-{
720
- add_object(obj, &pending_objects, NULL, name);
721
-}
722
-
723
-static struct commit *get_commit_reference(const char *name, const unsigned char *sha1, unsigned int flags)
724
-{
725
- struct object *object;
726
-
727
- object = parse_object(sha1);
728
- if (!object)
729
- die("bad object %s", name);
730
-
731
- /*
732
- * Tag object? Look what it points to..
733
- */
734
- while (object->type == tag_type) {
735
- struct tag *tag = (struct tag *) object;
736
- object->flags |= flags;
737
- if (tag_objects && !(object->flags & UNINTERESTING))
738
- add_pending_object(object, tag->tag);
739
- object = parse_object(tag->tagged->sha1);
740
- if (!object)
741
- die("bad object %s", sha1_to_hex(tag->tagged->sha1));
742
- }
743
-
744
- /*
745
- * Commit object? Just return it, we'll do all the complex
746
- * reachability crud.
747
- */
748
- if (object->type == commit_type) {
749
- struct commit *commit = (struct commit *)object;
750
- object->flags |= flags;
751
- if (parse_commit(commit) < 0)
752
- die("unable to parse commit %s", name);
753
- if (flags & UNINTERESTING)
754
- mark_parents_uninteresting(commit);
755
- return commit;
756
- }
757
-
758
- /*
759
- * Tree object? Either mark it uniniteresting, or add it
760
- * to the list of objects to look at later..
761
- */
762
- if (object->type == tree_type) {
763
- struct tree *tree = (struct tree *)object;
764
- if (!tree_objects)
765
- return NULL;
766
- if (flags & UNINTERESTING) {
767
- mark_tree_uninteresting(tree);
768
- return NULL;
769
- }
770
- add_pending_object(object, "");
771
- return NULL;
772
- }
773
-
774
- /*
775
- * Blob object? You know the drill by now..
776
- */
777
- if (object->type == blob_type) {
778
- struct blob *blob = (struct blob *)object;
779
- if (!blob_objects)
780
- return NULL;
781
- if (flags & UNINTERESTING) {
782
- mark_blob_uninteresting(blob);
783
- return NULL;
784
- }
785
- add_pending_object(object, "");
786
- return NULL;
787
- }
788
- die("%s is unknown object", name);
789
-}
790
-
791
-static void handle_one_commit(struct commit *com, struct commit_list **lst)
792
-{
793
- if (!com || com->object.flags & SEEN)
794
- return;
795
- com->object.flags |= SEEN;
796
- commit_list_insert(com, lst);
797
-}
798
-
799
-/* for_each_ref() callback does not allow user data -- Yuck. */
800
-static struct commit_list **global_lst;
801
-
802
-static int include_one_commit(const char *path, const unsigned char *sha1)
803
-{
804
- struct commit *com = get_commit_reference(path, sha1, 0);
805
- handle_one_commit(com, global_lst);
806
- return 0;
807
-}
808
-
809
-static void handle_all(struct commit_list **lst)
810
-{
811
- global_lst = lst;
812
- for_each_ref(include_one_commit);
813
- global_lst = NULL;
814
-}
815
-
597
int main(int argc, const char **argv)
598
{
818
- const char *prefix = setup_git_directory();
819
- struct commit_list *list = NULL;
599
+ struct commit_list *list;
600
int i, limited = 0;
601
602
+ argc = setup_revisions(argc, argv, &revs);
603
+
604
for (i = 1 ; i < argc; i++) {
823
- int flags;
605
const char *arg = argv[i];
825
- char *dotdot;
826
- struct commit *commit;
827
- unsigned char sha1[20];
606
607
/* accept -<digit>, like traditilnal "head" */
608
if ((*arg == '-') && isdigit(arg[1])) {
831
- max_count = atoi(arg + 1);
609
+ revs.max_count = atoi(arg + 1);
610
continue;
611
}
612
if (!strcmp(arg, "-n")) {
613
if (++i >= argc)
614
die("-n requires an argument");
837
- max_count = atoi(argv[i]);
615
+ revs.max_count = atoi(argv[i]);
616
continue;
617
}
618
if (!strncmp(arg,"-n",2)) {
841
- max_count = atoi(arg + 2);
842
- continue;
843
- }
844
- if (!strncmp(arg, "--max-count=", 12)) {
845
- max_count = atoi(arg + 12);
846
- continue;
847
- }
848
- if (!strncmp(arg, "--max-age=", 10)) {
849
- max_age = atoi(arg + 10);
850
- limited = 1;
851
- continue;
852
- }
853
- if (!strncmp(arg, "--min-age=", 10)) {
854
- min_age = atoi(arg + 10);
855
- limited = 1;
619
+ revs.max_count = atoi(arg + 2);
620
continue;
621
}
622
if (!strcmp(arg, "--header")) {
@@ -893,23 +657,6 @@ int main(int argc, const char **argv)
657
bisect_list = 1;
658
continue;
659
}
896
- if (!strcmp(arg, "--all")) {
897
- handle_all(&list);
898
- continue;
899
- }
900
- if (!strcmp(arg, "--objects")) {
901
- tag_objects = 1;
902
- tree_objects = 1;
903
- blob_objects = 1;
904
- continue;
905
- }
906
- if (!strcmp(arg, "--objects-edge")) {
907
- tag_objects = 1;
908
- tree_objects = 1;
909
- blob_objects = 1;
910
- edge_hint = 1;
911
- continue;
912
- }
660
if (!strcmp(arg, "--unpacked")) {
661
unpacked = 1;
662
limited = 1;
@@ -923,100 +670,42 @@ int main(int argc, const char **argv)
670
show_breaks = 1;
671
continue;
672
}
926
- if (!strcmp(arg, "--topo-order")) {
927
- topo_order = 1;
928
- lifo = 1;
929
- limited = 1;
930
- continue;
931
- }
932
- if (!strcmp(arg, "--date-order")) {
933
- topo_order = 1;
934
- lifo = 0;
935
- limited = 1;
936
- continue;
937
- }
938
- if (!strcmp(arg, "--dense")) {
939
- dense = 1;
940
- continue;
941
- }
942
- if (!strcmp(arg, "--sparse")) {
943
- dense = 0;
944
- continue;
945
- }
946
- if (!strcmp(arg, "--remove-empty")) {
947
- remove_empty_trees = 1;
948
- continue;
949
- }
950
- if (!strcmp(arg, "--")) {
951
- i++;
952
- break;
953
- }
673
+ usage(rev_list_usage);
674
955
- if (show_breaks && !merge_order)
956
- usage(rev_list_usage);
957
-
958
- flags = 0;
959
- dotdot = strstr(arg, "..");
960
- if (dotdot) {
961
- unsigned char from_sha1[20];
962
- char *next = dotdot + 2;
963
- *dotdot = 0;
964
- if (!*next)
965
- next = "HEAD";
966
- if (!get_sha1(arg, from_sha1) && !get_sha1(next, sha1)) {
967
- struct commit *exclude;
968
- struct commit *include;
969
-
970
- exclude = get_commit_reference(arg, from_sha1, UNINTERESTING);
971
- include = get_commit_reference(next, sha1, 0);
972
- if (!exclude || !include)
973
- die("Invalid revision range %s..%s", arg, next);
974
- limited = 1;
975
- handle_one_commit(exclude, &list);
976
- handle_one_commit(include, &list);
977
- continue;
978
- }
979
- *dotdot = '.';
980
- }
981
- if (*arg == '^') {
982
- flags = UNINTERESTING;
983
- arg++;
984
- limited = 1;
985
- }
986
- if (get_sha1(arg, sha1) < 0) {
987
- struct stat st;
988
- if (lstat(arg, &st) < 0)
989
- die("'%s': %s", arg, strerror(errno));
990
- break;
991
- }
992
- commit = get_commit_reference(arg, sha1, flags);
993
- handle_one_commit(commit, &list);
675
}
676
677
+ list = revs.commits;
678
+ if (list && list->next)
679
+ limited = 1;
680
+
681
+ if (revs.topo_order)
682
+ limited = 1;
683
+
684
if (!list &&
997
- (!(tag_objects||tree_objects||blob_objects) && !pending_objects))
685
+ (!(revs.tag_objects||revs.tree_objects||revs.blob_objects) && !revs.pending_objects))
686
usage(rev_list_usage);
687
1000
- paths = get_pathspec(prefix, argv + i);
1001
- if (paths) {
688
+ if (revs.paths) {
689
limited = 1;
1003
- diff_tree_setup_paths(paths);
690
+ diff_tree_setup_paths(revs.paths);
691
}
692
+ if (revs.max_age || revs.min_age)
693
+ limited = 1;
694
695
save_commit_buffer = verbose_header;
696
track_object_refs = 0;
697
698
if (!merge_order) {
699
sort_by_date(&list);
1011
- if (list && !limited && max_count == 1 &&
1012
- !tag_objects && !tree_objects && !blob_objects) {
700
+ if (list && !limited && revs.max_count == 1 &&
701
+ !revs.tag_objects && !revs.tree_objects && !revs.blob_objects) {
702
show_commit(list->item);
703
return 0;
704
}
705
if (limited)
706
list = limit_list(list);
1018
- if (topo_order)
1019
- sort_in_topological_order(&list, lifo);
707
+ if (revs.topo_order)
708
+ sort_in_topological_order(&list, revs.lifo);
709
show_commit_list(list);
710
} else {
711
#ifndef NO_OPENSSL
revision.c
new
+370
@@ -0,0 +1,370 @@
1
+#include "cache.h"
2
+#include "tag.h"
3
+#include "blob.h"
4
+#include "tree.h"
5
+#include "commit.h"
6
+#include "refs.h"
7
+#include "revision.h"
8
+
9
+static char *path_name(struct name_path *path, const char *name)
10
+{
11
+ struct name_path *p;
12
+ char *n, *m;
13
+ int nlen = strlen(name);
14
+ int len = nlen + 1;
15
+
16
+ for (p = path; p; p = p->up) {
17
+ if (p->elem_len)
18
+ len += p->elem_len + 1;
19
+ }
20
+ n = xmalloc(len);
21
+ m = n + len - (nlen + 1);
22
+ strcpy(m, name);
23
+ for (p = path; p; p = p->up) {
24
+ if (p->elem_len) {
25
+ m -= p->elem_len + 1;
26
+ memcpy(m, p->elem, p->elem_len);
27
+ m[p->elem_len] = '/';
28
+ }
29
+ }
30
+ return n;
31
+}
32
+
33
+struct object_list **add_object(struct object *obj,
34
+ struct object_list **p,
35
+ struct name_path *path,
36
+ const char *name)
37
+{
38
+ struct object_list *entry = xmalloc(sizeof(*entry));
39
+ entry->item = obj;
40
+ entry->next = *p;
41
+ entry->name = path_name(path, name);
42
+ *p = entry;
43
+ return &entry->next;
44
+}
45
+
46
+static void mark_blob_uninteresting(struct blob *blob)
47
+{
48
+ if (blob->object.flags & UNINTERESTING)
49
+ return;
50
+ blob->object.flags |= UNINTERESTING;
51
+}
52
+
53
+void mark_tree_uninteresting(struct tree *tree)
54
+{
55
+ struct object *obj = &tree->object;
56
+ struct tree_entry_list *entry;
57
+
58
+ if (obj->flags & UNINTERESTING)
59
+ return;
60
+ obj->flags |= UNINTERESTING;
61
+ if (!has_sha1_file(obj->sha1))
62
+ return;
63
+ if (parse_tree(tree) < 0)
64
+ die("bad tree %s", sha1_to_hex(obj->sha1));
65
+ entry = tree->entries;
66
+ tree->entries = NULL;
67
+ while (entry) {
68
+ struct tree_entry_list *next = entry->next;
69
+ if (entry->directory)
70
+ mark_tree_uninteresting(entry->item.tree);
71
+ else
72
+ mark_blob_uninteresting(entry->item.blob);
73
+ free(entry);
74
+ entry = next;
75
+ }
76
+}
77
+
78
+void mark_parents_uninteresting(struct commit *commit)
79
+{
80
+ struct commit_list *parents = commit->parents;
81
+
82
+ while (parents) {
83
+ struct commit *commit = parents->item;
84
+ commit->object.flags |= UNINTERESTING;
85
+
86
+ /*
87
+ * Normally we haven't parsed the parent
88
+ * yet, so we won't have a parent of a parent
89
+ * here. However, it may turn out that we've
90
+ * reached this commit some other way (where it
91
+ * wasn't uninteresting), in which case we need
92
+ * to mark its parents recursively too..
93
+ */
94
+ if (commit->parents)
95
+ mark_parents_uninteresting(commit);
96
+
97
+ /*
98
+ * A missing commit is ok iff its parent is marked
99
+ * uninteresting.
100
+ *
101
+ * We just mark such a thing parsed, so that when
102
+ * it is popped next time around, we won't be trying
103
+ * to parse it and get an error.
104
+ */
105
+ if (!has_sha1_file(commit->object.sha1))
106
+ commit->object.parsed = 1;
107
+ parents = parents->next;
108
+ }
109
+}
110
+
111
+static void add_pending_object(struct rev_info *revs, struct object *obj, const char *name)
112
+{
113
+ add_object(obj, &revs->pending_objects, NULL, name);
114
+}
115
+
116
+static struct commit *get_commit_reference(struct rev_info *revs, const char *name, const unsigned char *sha1, unsigned int flags)
117
+{
118
+ struct object *object;
119
+
120
+ object = parse_object(sha1);
121
+ if (!object)
122
+ die("bad object %s", name);
123
+
124
+ /*
125
+ * Tag object? Look what it points to..
126
+ */
127
+ while (object->type == tag_type) {
128
+ struct tag *tag = (struct tag *) object;
129
+ object->flags |= flags;
130
+ if (revs->tag_objects && !(object->flags & UNINTERESTING))
131
+ add_pending_object(revs, object, tag->tag);
132
+ object = parse_object(tag->tagged->sha1);
133
+ if (!object)
134
+ die("bad object %s", sha1_to_hex(tag->tagged->sha1));
135
+ }
136
+
137
+ /*
138
+ * Commit object? Just return it, we'll do all the complex
139
+ * reachability crud.
140
+ */
141
+ if (object->type == commit_type) {
142
+ struct commit *commit = (struct commit *)object;
143
+ object->flags |= flags;
144
+ if (parse_commit(commit) < 0)
145
+ die("unable to parse commit %s", name);
146
+ if (flags & UNINTERESTING)
147
+ mark_parents_uninteresting(commit);
148
+ return commit;
149
+ }
150
+
151
+ /*
152
+ * Tree object? Either mark it uniniteresting, or add it
153
+ * to the list of objects to look at later..
154
+ */
155
+ if (object->type == tree_type) {
156
+ struct tree *tree = (struct tree *)object;
157
+ if (!revs->tree_objects)
158
+ return NULL;
159
+ if (flags & UNINTERESTING) {
160
+ mark_tree_uninteresting(tree);
161
+ return NULL;
162
+ }
163
+ add_pending_object(revs, object, "");
164
+ return NULL;
165
+ }
166
+
167
+ /*
168
+ * Blob object? You know the drill by now..
169
+ */
170
+ if (object->type == blob_type) {
171
+ struct blob *blob = (struct blob *)object;
172
+ if (!revs->blob_objects)
173
+ return NULL;
174
+ if (flags & UNINTERESTING) {
175
+ mark_blob_uninteresting(blob);
176
+ return NULL;
177
+ }
178
+ add_pending_object(revs, object, "");
179
+ return NULL;
180
+ }
181
+ die("%s is unknown object", name);
182
+}
183
+
184
+static void add_one_commit(struct commit *commit, struct rev_info *revs)
185
+{
186
+ if (!commit || (commit->object.flags & SEEN))
187
+ return;
188
+ commit->object.flags |= SEEN;
189
+ commit_list_insert(commit, &revs->commits);
190
+}
191
+
192
+static int all_flags;
193
+static struct rev_info *all_revs;
194
+
195
+static int handle_one_ref(const char *path, const unsigned char *sha1)
196
+{
197
+ struct commit *commit = get_commit_reference(all_revs, path, sha1, all_flags);
198
+ add_one_commit(commit, all_revs);
199
+ return 0;
200
+}
201
+
202
+static void handle_all(struct rev_info *revs, unsigned flags)
203
+{
204
+ all_revs = revs;
205
+ all_flags = flags;
206
+ for_each_ref(handle_one_ref);
207
+}
208
+
209
+/*
210
+ * Parse revision information, filling in the "rev_info" structure,
211
+ * and removing the used arguments from the argument list.
212
+ *
213
+ * Returns the number of arguments left ("new argc").
214
+ */
215
+int setup_revisions(int argc, const char **argv, struct rev_info *revs)
216
+{
217
+ int i, flags, seen_dashdash;
218
+ const char *def = NULL;
219
+ const char **unrecognized = argv+1;
220
+ int left = 1;
221
+
222
+ memset(revs, 0, sizeof(*revs));
223
+ revs->lifo = 1;
224
+ revs->dense = 1;
225
+ revs->prefix = setup_git_directory();
226
+ revs->max_age = -1;
227
+ revs->min_age = -1;
228
+ revs->max_count = -1;
229
+
230
+ /* First, search for "--" */
231
+ seen_dashdash = 0;
232
+ for (i = 1; i < argc; i++) {
233
+ const char *arg = argv[i];
234
+ if (strcmp(arg, "--"))
235
+ continue;
236
+ argv[i] = NULL;
237
+ argc = i;
238
+ revs->paths = get_pathspec(revs->prefix, argv + i + 1);
239
+ seen_dashdash = 1;
240
+ break;
241
+ }
242
+
243
+ flags = 0;
244
+ for (i = 1; i < argc; i++) {
245
+ struct commit *commit;
246
+ const char *arg = argv[i];
247
+ unsigned char sha1[20];
248
+ char *dotdot;
249
+ int local_flags;
250
+
251
+ if (*arg == '-') {
252
+ if (!strncmp(arg, "--max-count=", 12)) {
253
+ revs->max_count = atoi(arg + 12);
254
+ continue;
255
+ }
256
+ if (!strncmp(arg, "--max-age=", 10)) {
257
+ revs->max_age = atoi(arg + 10);
258
+ continue;
259
+ }
260
+ if (!strncmp(arg, "--min-age=", 10)) {
261
+ revs->min_age = atoi(arg + 10);
262
+ continue;
263
+ }
264
+ if (!strcmp(arg, "--all")) {
265
+ handle_all(revs, flags);
266
+ continue;
267
+ }
268
+ if (!strcmp(arg, "--not")) {
269
+ flags ^= UNINTERESTING;
270
+ continue;
271
+ }
272
+ if (!strcmp(arg, "--default")) {
273
+ if (++i >= argc)
274
+ die("bad --default argument");
275
+ def = argv[i];
276
+ continue;
277
+ }
278
+ if (!strcmp(arg, "--topo-order")) {
279
+ revs->topo_order = 1;
280
+ continue;
281
+ }
282
+ if (!strcmp(arg, "--date-order")) {
283
+ revs->lifo = 0;
284
+ revs->topo_order = 1;
285
+ continue;
286
+ }
287
+ if (!strcmp(arg, "--dense")) {
288
+ revs->dense = 1;
289
+ continue;
290
+ }
291
+ if (!strcmp(arg, "--sparse")) {
292
+ revs->dense = 0;
293
+ continue;
294
+ }
295
+ if (!strcmp(arg, "--remove-empty")) {
296
+ revs->remove_empty_trees = 1;
297
+ continue;
298
+ }
299
+ if (!strcmp(arg, "--objects")) {
300
+ revs->tag_objects = 1;
301
+ revs->tree_objects = 1;
302
+ revs->blob_objects = 1;
303
+ continue;
304
+ }
305
+ if (!strcmp(arg, "--objects-edge")) {
306
+ revs->tag_objects = 1;
307
+ revs->tree_objects = 1;
308
+ revs->blob_objects = 1;
309
+ revs->edge_hint = 1;
310
+ continue;
311
+ }
312
+ *unrecognized++ = arg;
313
+ left++;
314
+ continue;
315
+ }
316
+ dotdot = strstr(arg, "..");
317
+ if (dotdot) {
318
+ unsigned char from_sha1[20];
319
+ char *next = dotdot + 2;
320
+ *dotdot = 0;
321
+ if (!*next)
322
+ next = "HEAD";
323
+ if (!get_sha1(arg, from_sha1) && !get_sha1(next, sha1)) {
324
+ struct commit *exclude;
325
+ struct commit *include;
326
+
327
+ exclude = get_commit_reference(revs, arg, from_sha1, flags ^ UNINTERESTING);
328
+ include = get_commit_reference(revs, next, sha1, flags);
329
+ if (!exclude || !include)
330
+ die("Invalid revision range %s..%s", arg, next);
331
+ add_one_commit(exclude, revs);
332
+ add_one_commit(include, revs);
333
+ continue;
334
+ }
335
+ *dotdot = '.';
336
+ }
337
+ local_flags = 0;
338
+ if (*arg == '^') {
339
+ local_flags = UNINTERESTING;
340
+ arg++;
341
+ }
342
+ if (get_sha1(arg, sha1) < 0) {
343
+ struct stat st;
344
+ int j;
345
+
346
+ if (seen_dashdash || local_flags)
347
+ die("bad revision '%s'", arg);
348
+
349
+ /* If we didn't have a "--", all filenames must exist */
350
+ for (j = i; j < argc; j++) {
351
+ if (lstat(argv[j], &st) < 0)
352
+ die("'%s': %s", arg, strerror(errno));
353
+ }
354
+ revs->paths = get_pathspec(revs->prefix, argv + i);
355
+ break;
356
+ }
357
+ commit = get_commit_reference(revs, arg, sha1, flags ^ local_flags);
358
+ add_one_commit(commit, revs);
359
+ }
360
+ if (def && !revs->commits) {
361
+ unsigned char sha1[20];
362
+ struct commit *commit;
363
+ if (get_sha1(def, sha1) < 0)
364
+ die("bad default revision '%s'", def);
365
+ commit = get_commit_reference(revs, def, sha1, 0);
366
+ add_one_commit(commit, revs);
367
+ }
368
+ *unrecognized = NULL;
369
+ return left;
370
+}
revision.h
new
+48
@@ -0,0 +1,48 @@
1
+#ifndef REVISION_H
2
+#define REVISION_H
3
+
4
+#define SEEN (1u<<0)
5
+#define UNINTERESTING (1u<<1)
6
+
7
+struct rev_info {
8
+ /* Starting list */
9
+ struct commit_list *commits;
10
+ struct object_list *pending_objects;
11
+
12
+ /* Basic information */
13
+ const char *prefix;
14
+ const char **paths;
15
+
16
+ /* Traversal flags */
17
+ unsigned int dense:1,
18
+ remove_empty_trees:1,
19
+ lifo:1,
20
+ topo_order:1,
21
+ tag_objects:1,
22
+ tree_objects:1,
23
+ blob_objects:1,
24
+ edge_hint:1;
25
+
26
+ /* special limits */
27
+ int max_count;
28
+ unsigned long max_age;
29
+ unsigned long min_age;
30
+};
31
+
32
+/* revision.c */
33
+extern int setup_revisions(int argc, const char **argv, struct rev_info *revs);
34
+extern void mark_parents_uninteresting(struct commit *commit);
35
+extern void mark_tree_uninteresting(struct tree *tree);
36
+
37
+struct name_path {
38
+ struct name_path *up;
39
+ int elem_len;
40
+ const char *elem;
41
+};
42
+
43
+extern struct object_list **add_object(struct object *obj,
44
+ struct object_list **p,
45
+ struct name_path *path,
46
+ const char *name);
47
+
48
+#endif