]>
Commit | Line | Data |
---|---|---|
64745109 LT |
1 | #include "cache.h" |
2 | #include "commit.h" | |
3 | ||
8906300f LT |
4 | #define SEEN (1u << 0) |
5 | #define INTERESTING (1u << 1) | |
6 | #define UNINTERESTING (1u << 2) | |
7 | ||
a6f68d47 LT |
8 | static const char rev_list_usage[] = |
9 | "usage: git-rev-list [OPTION] commit-id <commit-id>\n" | |
10 | " --max-count=nr\n" | |
11 | " --max-age=epoch\n" | |
12 | " --min-age=epoch\n" | |
9d97aa64 LT |
13 | " --header\n" |
14 | " --pretty"; | |
a6f68d47 | 15 | |
8906300f LT |
16 | static void mark_parents_uninteresting(struct commit *commit) |
17 | { | |
18 | struct commit_list *parents = commit->parents; | |
19 | ||
20 | while (parents) { | |
21 | struct commit *commit = parents->item; | |
22 | commit->object.flags |= UNINTERESTING; | |
23 | parents = parents->next; | |
24 | } | |
25 | } | |
26 | ||
27 | static int everybody_uninteresting(struct commit_list *list) | |
28 | { | |
29 | while (list) { | |
30 | struct commit *commit = list->item; | |
31 | list = list->next; | |
32 | if (commit->object.flags & UNINTERESTING) | |
33 | continue; | |
34 | return 0; | |
35 | } | |
36 | return 1; | |
37 | } | |
38 | ||
64745109 LT |
39 | int main(int argc, char **argv) |
40 | { | |
a6f68d47 LT |
41 | int nr_sha; |
42 | unsigned char sha1[2][20]; | |
64745109 | 43 | struct commit_list *list = NULL; |
a6f68d47 | 44 | struct commit *commit, *end; |
9d97aa64 LT |
45 | int i, verbose_header = 0, show_parents = 0, pretty_print = 0; |
46 | int hdr_termination = 0; | |
47 | const char *prefix = ""; | |
fcfda02b KS |
48 | unsigned long max_age = -1; |
49 | unsigned long min_age = -1; | |
50 | int max_count = -1; | |
64745109 | 51 | |
a6f68d47 | 52 | nr_sha = 0; |
fcfda02b KS |
53 | for (i = 1 ; i < argc; i++) { |
54 | char *arg = argv[i]; | |
55 | ||
56 | if (!strncmp(arg, "--max-count=", 12)) { | |
57 | max_count = atoi(arg + 12); | |
a6f68d47 LT |
58 | continue; |
59 | } | |
60 | if (!strncmp(arg, "--max-age=", 10)) { | |
fcfda02b | 61 | max_age = atoi(arg + 10); |
a6f68d47 LT |
62 | continue; |
63 | } | |
64 | if (!strncmp(arg, "--min-age=", 10)) { | |
fcfda02b | 65 | min_age = atoi(arg + 10); |
a6f68d47 | 66 | continue; |
fcfda02b | 67 | } |
a6f68d47 LT |
68 | if (!strcmp(arg, "--header")) { |
69 | verbose_header = 1; | |
70 | continue; | |
71 | } | |
9d97aa64 LT |
72 | if (!strcmp(arg, "--pretty")) { |
73 | verbose_header = 1; | |
74 | pretty_print = 1; | |
75 | hdr_termination = '\n'; | |
76 | prefix = "commit "; | |
77 | continue; | |
78 | } | |
97658004 LT |
79 | if (!strcmp(arg, "--parents")) { |
80 | show_parents = 1; | |
81 | continue; | |
82 | } | |
a6f68d47 LT |
83 | |
84 | if (nr_sha > 2 || get_sha1(arg, sha1[nr_sha])) | |
85 | usage(rev_list_usage); | |
86 | nr_sha++; | |
fcfda02b KS |
87 | } |
88 | ||
a6f68d47 LT |
89 | if (!nr_sha) |
90 | usage(rev_list_usage); | |
64745109 | 91 | |
a6f68d47 | 92 | commit = lookup_commit_reference(sha1[0]); |
64745109 | 93 | if (!commit || parse_commit(commit) < 0) |
a6f68d47 LT |
94 | die("bad starting commit object"); |
95 | ||
96 | end = NULL; | |
97 | if (nr_sha > 1) { | |
98 | end = lookup_commit_reference(sha1[1]); | |
99 | if (!end || parse_commit(end) < 0) | |
100 | die("bad ending commit object"); | |
101 | } | |
64745109 LT |
102 | |
103 | commit_list_insert(commit, &list); | |
8906300f LT |
104 | if (end) { |
105 | struct commit_list *newlist = NULL; | |
106 | struct commit_list **p = &newlist; | |
107 | do { | |
108 | struct commit *commit = pop_most_recent_commit(&list, SEEN); | |
109 | struct object *obj = &commit->object; | |
fcfda02b | 110 | |
8906300f LT |
111 | if (commit == end || (obj->flags & UNINTERESTING)) { |
112 | mark_parents_uninteresting(commit); | |
113 | if (everybody_uninteresting(list)) | |
114 | break; | |
115 | continue; | |
116 | } | |
117 | p = &commit_list_insert(commit, p)->next; | |
118 | } while (list); | |
119 | list = newlist; | |
120 | } | |
121 | ||
122 | while (list) { | |
123 | struct commit *commit = pop_most_recent_commit(&list, SEEN); | |
124 | ||
125 | if (commit->object.flags & UNINTERESTING) | |
126 | continue; | |
fcfda02b KS |
127 | if (min_age != -1 && (commit->date > min_age)) |
128 | continue; | |
129 | if (max_age != -1 && (commit->date < max_age)) | |
130 | break; | |
131 | if (max_count != -1 && !max_count--) | |
132 | break; | |
9d97aa64 | 133 | printf("%s%s", prefix, sha1_to_hex(commit->object.sha1)); |
97658004 LT |
134 | if (show_parents) { |
135 | struct commit_list *parents = commit->parents; | |
136 | while (parents) { | |
137 | printf(" %s", sha1_to_hex(parents->item->object.sha1)); | |
138 | parents = parents->next; | |
139 | } | |
140 | } | |
141 | putchar('\n'); | |
9d97aa64 LT |
142 | if (verbose_header) { |
143 | const char *buf = commit->buffer; | |
144 | if (pretty_print) { | |
145 | static char pretty_header[16384]; | |
146 | pretty_print_commit(commit->buffer, ~0, pretty_header, sizeof(pretty_header)); | |
147 | buf = pretty_header; | |
148 | } | |
149 | printf("%s%c", buf, hdr_termination); | |
150 | } | |
8906300f | 151 | } |
64745109 LT |
152 | return 0; |
153 | } |