[PATCH] cvs2git and file permissions
[git/mingw.git] / rev-list.c
blobc7ebd2f03cd578ea0f21c3e4037e955a11b2c165
1 #include "cache.h"
2 #include "commit.h"
3 #include "epoch.h"
5 #define SEEN (1u << 0)
6 #define INTERESTING (1u << 1)
7 #define COUNTED (1u << 2)
9 static const char rev_list_usage[] =
10 "usage: git-rev-list [OPTION] commit-id <commit-id>\n"
11 " --max-count=nr\n"
12 " --max-age=epoch\n"
13 " --min-age=epoch\n"
14 " --header\n"
15 " --pretty\n"
16 " --merge-order [ --show-breaks ]";
18 static int bisect_list = 0;
19 static int verbose_header = 0;
20 static int show_parents = 0;
21 static int hdr_termination = 0;
22 static const char *prefix = "";
23 static unsigned long max_age = -1;
24 static unsigned long min_age = -1;
25 static int max_count = -1;
26 static enum cmit_fmt commit_format = CMIT_FMT_RAW;
27 static int merge_order = 0;
28 static int show_breaks = 0;
30 static void show_commit(struct commit *commit)
32 if (show_breaks) {
33 prefix = "| ";
34 if (commit->object.flags & DISCONTINUITY) {
35 prefix = "^ ";
36 } else if (commit->object.flags & BOUNDARY) {
37 prefix = "= ";
40 printf("%s%s", prefix, sha1_to_hex(commit->object.sha1));
41 if (show_parents) {
42 struct commit_list *parents = commit->parents;
43 while (parents) {
44 printf(" %s", sha1_to_hex(parents->item->object.sha1));
45 parents = parents->next;
48 putchar('\n');
49 if (verbose_header) {
50 static char pretty_header[16384];
51 pretty_print_commit(commit_format, commit->buffer, ~0, pretty_header, sizeof(pretty_header));
52 printf("%s%c", pretty_header, hdr_termination);
56 static int filter_commit(struct commit * commit)
58 if (commit->object.flags & UNINTERESTING)
59 return CONTINUE;
60 if (min_age != -1 && (commit->date > min_age))
61 return CONTINUE;
62 if (max_age != -1 && (commit->date < max_age))
63 return STOP;
64 if (max_count != -1 && !max_count--)
65 return STOP;
67 return DO;
70 static int process_commit(struct commit * commit)
72 int action=filter_commit(commit);
74 if (action == STOP) {
75 return STOP;
78 if (action == CONTINUE) {
79 return CONTINUE;
82 show_commit(commit);
84 return CONTINUE;
87 static void show_commit_list(struct commit_list *list)
89 while (list) {
90 struct commit *commit = pop_most_recent_commit(&list, SEEN);
92 if (process_commit(commit) == STOP)
93 break;
97 static void mark_parents_uninteresting(struct commit *commit)
99 struct commit_list *parents = commit->parents;
101 while (parents) {
102 struct commit *commit = parents->item;
103 commit->object.flags |= UNINTERESTING;
104 parents = parents->next;
108 static int everybody_uninteresting(struct commit_list *list)
110 while (list) {
111 struct commit *commit = list->item;
112 list = list->next;
113 if (commit->object.flags & UNINTERESTING)
114 continue;
115 return 0;
117 return 1;
121 * This is a truly stupid algorithm, but it's only
122 * used for bisection, and we just don't care enough.
124 * We care just barely enough to avoid recursing for
125 * non-merge entries.
127 static int count_distance(struct commit_list *entry)
129 int nr = 0;
131 while (entry) {
132 struct commit *commit = entry->item;
133 struct commit_list *p;
135 if (commit->object.flags & (UNINTERESTING | COUNTED))
136 break;
137 nr++;
138 commit->object.flags |= COUNTED;
139 p = commit->parents;
140 entry = p;
141 if (p) {
142 p = p->next;
143 while (p) {
144 nr += count_distance(p);
145 p = p->next;
149 return nr;
152 static int clear_distance(struct commit_list *list)
154 while (list) {
155 struct commit *commit = list->item;
156 commit->object.flags &= ~COUNTED;
157 list = list->next;
161 static struct commit_list *find_bisection(struct commit_list *list)
163 int nr, closest;
164 struct commit_list *p, *best;
166 nr = 0;
167 p = list;
168 while (p) {
169 nr++;
170 p = p->next;
172 closest = 0;
173 best = list;
175 p = list;
176 while (p) {
177 int distance = count_distance(p);
178 clear_distance(list);
179 if (nr - distance < distance)
180 distance = nr - distance;
181 if (distance > closest) {
182 best = p;
183 closest = distance;
185 p = p->next;
187 if (best)
188 best->next = NULL;
189 return best;
192 struct commit_list *limit_list(struct commit_list *list)
194 struct commit_list *newlist = NULL;
195 struct commit_list **p = &newlist;
196 do {
197 struct commit *commit = pop_most_recent_commit(&list, SEEN);
198 struct object *obj = &commit->object;
200 if (obj->flags & UNINTERESTING) {
201 mark_parents_uninteresting(commit);
202 if (everybody_uninteresting(list))
203 break;
204 continue;
206 p = &commit_list_insert(commit, p)->next;
207 } while (list);
208 if (bisect_list)
209 newlist = find_bisection(newlist);
210 return newlist;
213 static enum cmit_fmt get_commit_format(const char *arg)
215 if (!*arg)
216 return CMIT_FMT_DEFAULT;
217 if (!strcmp(arg, "=raw"))
218 return CMIT_FMT_RAW;
219 if (!strcmp(arg, "=medium"))
220 return CMIT_FMT_MEDIUM;
221 if (!strcmp(arg, "=short"))
222 return CMIT_FMT_SHORT;
223 usage(rev_list_usage);
227 int main(int argc, char **argv)
229 struct commit_list *list = NULL;
230 int i, limited = 0;
232 for (i = 1 ; i < argc; i++) {
233 int flags;
234 char *arg = argv[i];
235 unsigned char sha1[20];
236 struct commit *commit;
238 if (!strncmp(arg, "--max-count=", 12)) {
239 max_count = atoi(arg + 12);
240 continue;
242 if (!strncmp(arg, "--max-age=", 10)) {
243 max_age = atoi(arg + 10);
244 continue;
246 if (!strncmp(arg, "--min-age=", 10)) {
247 min_age = atoi(arg + 10);
248 continue;
250 if (!strcmp(arg, "--header")) {
251 verbose_header = 1;
252 continue;
254 if (!strncmp(arg, "--pretty", 8)) {
255 commit_format = get_commit_format(arg+8);
256 verbose_header = 1;
257 hdr_termination = '\n';
258 prefix = "commit ";
259 continue;
261 if (!strcmp(arg, "--parents")) {
262 show_parents = 1;
263 continue;
265 if (!strcmp(arg, "--bisect")) {
266 bisect_list = 1;
267 continue;
269 if (!strncmp(arg, "--merge-order", 13)) {
270 merge_order = 1;
271 continue;
273 if (!strncmp(arg, "--show-breaks", 13)) {
274 show_breaks = 1;
275 continue;
278 flags = 0;
279 if (*arg == '^') {
280 flags = UNINTERESTING;
281 arg++;
282 limited = 1;
284 if (get_sha1(arg, sha1) || (show_breaks && !merge_order))
285 usage(rev_list_usage);
286 commit = lookup_commit_reference(sha1);
287 if (!commit || parse_commit(commit) < 0)
288 die("bad commit object %s", arg);
289 commit->object.flags |= flags;
290 commit_list_insert(commit, &list);
293 if (!list)
294 usage(rev_list_usage);
296 if (!merge_order) {
297 if (limited)
298 list = limit_list(list);
299 show_commit_list(list);
300 } else {
301 if (sort_list_in_merge_order(list, &process_commit)) {
302 die("merge order sort failed\n");
306 return 0;