[PATCH] Pull misc objects
[git/dkf.git] / rev-list.c
blob16920ef69e844697628ca3775403f86cc1c13945
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)
8 #define SHOWN (LAST_EPOCH_FLAG << 2)
10 static const char rev_list_usage[] =
11 "usage: git-rev-list [OPTION] commit-id <commit-id>\n"
12 " --max-count=nr\n"
13 " --max-age=epoch\n"
14 " --min-age=epoch\n"
15 " --header\n"
16 " --pretty\n"
17 " --merge-order [ --show-breaks ]";
19 static int bisect_list = 0;
20 static int verbose_header = 0;
21 static int show_parents = 0;
22 static int hdr_termination = 0;
23 static const char *prefix = "";
24 static unsigned long max_age = -1;
25 static unsigned long min_age = -1;
26 static int max_count = -1;
27 static enum cmit_fmt commit_format = CMIT_FMT_RAW;
28 static int merge_order = 0;
29 static int show_breaks = 0;
30 static int stop_traversal = 0;
32 static void show_commit(struct commit *commit)
34 commit->object.flags |= SHOWN;
35 if (show_breaks) {
36 prefix = "| ";
37 if (commit->object.flags & DISCONTINUITY) {
38 prefix = "^ ";
39 } else if (commit->object.flags & BOUNDARY) {
40 prefix = "= ";
43 printf("%s%s", prefix, sha1_to_hex(commit->object.sha1));
44 if (show_parents) {
45 struct commit_list *parents = commit->parents;
46 while (parents) {
47 printf(" %s", sha1_to_hex(parents->item->object.sha1));
48 parents = parents->next;
51 putchar('\n');
52 if (verbose_header) {
53 static char pretty_header[16384];
54 pretty_print_commit(commit_format, commit->buffer, ~0, pretty_header, sizeof(pretty_header));
55 printf("%s%c", pretty_header, hdr_termination);
59 static int filter_commit(struct commit * commit)
61 if (merge_order && stop_traversal && commit->object.flags & BOUNDARY)
62 return STOP;
63 if (commit->object.flags & (UNINTERESTING|SHOWN))
64 return CONTINUE;
65 if (min_age != -1 && (commit->date > min_age))
66 return CONTINUE;
67 if (max_age != -1 && (commit->date < max_age)) {
68 if (!merge_order)
69 return STOP;
70 else {
71 stop_traversal = 1;
72 return CONTINUE;
75 if (max_count != -1 && !max_count--)
76 return STOP;
77 return DO;
80 static int process_commit(struct commit * commit)
82 int action=filter_commit(commit);
84 if (action == STOP) {
85 return STOP;
88 if (action == CONTINUE) {
89 return CONTINUE;
92 show_commit(commit);
94 return CONTINUE;
97 static void show_commit_list(struct commit_list *list)
99 while (list) {
100 struct commit *commit = pop_most_recent_commit(&list, SEEN);
102 if (process_commit(commit) == STOP)
103 break;
107 static void mark_parents_uninteresting(struct commit *commit)
109 struct commit_list *parents = commit->parents;
111 while (parents) {
112 struct commit *commit = parents->item;
113 commit->object.flags |= UNINTERESTING;
114 parents = parents->next;
118 static int everybody_uninteresting(struct commit_list *list)
120 while (list) {
121 struct commit *commit = list->item;
122 list = list->next;
123 if (commit->object.flags & UNINTERESTING)
124 continue;
125 return 0;
127 return 1;
131 * This is a truly stupid algorithm, but it's only
132 * used for bisection, and we just don't care enough.
134 * We care just barely enough to avoid recursing for
135 * non-merge entries.
137 static int count_distance(struct commit_list *entry)
139 int nr = 0;
141 while (entry) {
142 struct commit *commit = entry->item;
143 struct commit_list *p;
145 if (commit->object.flags & (UNINTERESTING | COUNTED))
146 break;
147 nr++;
148 commit->object.flags |= COUNTED;
149 p = commit->parents;
150 entry = p;
151 if (p) {
152 p = p->next;
153 while (p) {
154 nr += count_distance(p);
155 p = p->next;
159 return nr;
162 static void clear_distance(struct commit_list *list)
164 while (list) {
165 struct commit *commit = list->item;
166 commit->object.flags &= ~COUNTED;
167 list = list->next;
171 static struct commit_list *find_bisection(struct commit_list *list)
173 int nr, closest;
174 struct commit_list *p, *best;
176 nr = 0;
177 p = list;
178 while (p) {
179 nr++;
180 p = p->next;
182 closest = 0;
183 best = list;
185 p = list;
186 while (p) {
187 int distance = count_distance(p);
188 clear_distance(list);
189 if (nr - distance < distance)
190 distance = nr - distance;
191 if (distance > closest) {
192 best = p;
193 closest = distance;
195 p = p->next;
197 if (best)
198 best->next = NULL;
199 return best;
202 struct commit_list *limit_list(struct commit_list *list)
204 struct commit_list *newlist = NULL;
205 struct commit_list **p = &newlist;
206 do {
207 struct commit *commit = pop_most_recent_commit(&list, SEEN);
208 struct object *obj = &commit->object;
210 if (obj->flags & UNINTERESTING) {
211 mark_parents_uninteresting(commit);
212 if (everybody_uninteresting(list))
213 break;
214 continue;
216 p = &commit_list_insert(commit, p)->next;
217 } while (list);
218 if (bisect_list)
219 newlist = find_bisection(newlist);
220 return newlist;
223 static enum cmit_fmt get_commit_format(const char *arg)
225 if (!*arg)
226 return CMIT_FMT_DEFAULT;
227 if (!strcmp(arg, "=raw"))
228 return CMIT_FMT_RAW;
229 if (!strcmp(arg, "=medium"))
230 return CMIT_FMT_MEDIUM;
231 if (!strcmp(arg, "=short"))
232 return CMIT_FMT_SHORT;
233 usage(rev_list_usage);
237 int main(int argc, char **argv)
239 struct commit_list *list = NULL;
240 int i, limited = 0;
242 for (i = 1 ; i < argc; i++) {
243 int flags;
244 char *arg = argv[i];
245 unsigned char sha1[20];
246 struct commit *commit;
248 if (!strncmp(arg, "--max-count=", 12)) {
249 max_count = atoi(arg + 12);
250 continue;
252 if (!strncmp(arg, "--max-age=", 10)) {
253 max_age = atoi(arg + 10);
254 continue;
256 if (!strncmp(arg, "--min-age=", 10)) {
257 min_age = atoi(arg + 10);
258 continue;
260 if (!strcmp(arg, "--header")) {
261 verbose_header = 1;
262 continue;
264 if (!strncmp(arg, "--pretty", 8)) {
265 commit_format = get_commit_format(arg+8);
266 verbose_header = 1;
267 hdr_termination = '\n';
268 prefix = "commit ";
269 continue;
271 if (!strcmp(arg, "--parents")) {
272 show_parents = 1;
273 continue;
275 if (!strcmp(arg, "--bisect")) {
276 bisect_list = 1;
277 continue;
279 if (!strncmp(arg, "--merge-order", 13)) {
280 merge_order = 1;
281 continue;
283 if (!strncmp(arg, "--show-breaks", 13)) {
284 show_breaks = 1;
285 continue;
288 flags = 0;
289 if (*arg == '^') {
290 flags = UNINTERESTING;
291 arg++;
292 limited = 1;
294 if (get_sha1(arg, sha1) || (show_breaks && !merge_order))
295 usage(rev_list_usage);
296 commit = lookup_commit_reference(sha1);
297 if (!commit || parse_commit(commit) < 0)
298 die("bad commit object %s", arg);
299 commit->object.flags |= flags;
300 commit_list_insert(commit, &list);
303 if (!list)
304 usage(rev_list_usage);
306 if (!merge_order) {
307 if (limited)
308 list = limit_list(list);
309 show_commit_list(list);
310 } else {
311 if (sort_list_in_merge_order(list, &process_commit)) {
312 die("merge order sort failed\n");
316 return 0;