Make rev-list flush the stdio buffers after each rev.
[git.git] / rev-list.c
1 #include "cache.h"
2 #include "tag.h"
3 #include "commit.h"
4 #include "tree.h"
5 #include "blob.h"
6 #include "epoch.h"
7
8 #define SEEN            (1u << 0)
9 #define INTERESTING     (1u << 1)
10 #define COUNTED         (1u << 2)
11 #define SHOWN           (LAST_EPOCH_FLAG << 2)
12
13 static const char rev_list_usage[] =
14         "usage: git-rev-list [OPTION] commit-id <commit-id>\n"
15                       "  --max-count=nr\n"
16                       "  --max-age=epoch\n"
17                       "  --min-age=epoch\n"
18                       "  --bisect\n"
19                       "  --objects\n"
20                       "  --unpacked\n"
21                       "  --header\n"
22                       "  --pretty\n"
23                       "  --merge-order [ --show-breaks ]";
24
25 static int unpacked = 0;
26 static int bisect_list = 0;
27 static int tag_objects = 0;
28 static int tree_objects = 0;
29 static int blob_objects = 0;
30 static int verbose_header = 0;
31 static int show_parents = 0;
32 static int hdr_termination = 0;
33 static const char *prefix = "";
34 static unsigned long max_age = -1;
35 static unsigned long min_age = -1;
36 static int max_count = -1;
37 static enum cmit_fmt commit_format = CMIT_FMT_RAW;
38 static int merge_order = 0;
39 static int show_breaks = 0;
40 static int stop_traversal = 0;
41
42 static void show_commit(struct commit *commit)
43 {
44         commit->object.flags |= SHOWN;
45         if (show_breaks) {
46                 prefix = "| ";
47                 if (commit->object.flags & DISCONTINUITY) {
48                         prefix = "^ ";     
49                 } else if (commit->object.flags & BOUNDARY) {
50                         prefix = "= ";
51                 } 
52         }                       
53         printf("%s%s", prefix, sha1_to_hex(commit->object.sha1));
54         if (show_parents) {
55                 struct commit_list *parents = commit->parents;
56                 while (parents) {
57                         printf(" %s", sha1_to_hex(parents->item->object.sha1));
58                         parents = parents->next;
59                 }
60         }
61         putchar('\n');
62         if (verbose_header) {
63                 static char pretty_header[16384];
64                 pretty_print_commit(commit_format, commit->buffer, ~0, pretty_header, sizeof(pretty_header));
65                 printf("%s%c", pretty_header, hdr_termination);
66         }
67         fflush(stdout);
68 }
69
70 static int filter_commit(struct commit * commit)
71 {
72         if (merge_order && stop_traversal && commit->object.flags & BOUNDARY)
73                 return STOP;
74         if (commit->object.flags & (UNINTERESTING|SHOWN))
75                 return CONTINUE;
76         if (min_age != -1 && (commit->date > min_age))
77                 return CONTINUE;
78         if (max_age != -1 && (commit->date < max_age)) {
79                 if (!merge_order)
80                         return STOP;
81                 else {
82                         stop_traversal = 1;
83                         return CONTINUE;
84                 }
85         }
86         if (max_count != -1 && !max_count--)
87                 return STOP;
88         return DO;
89 }
90
91 static int process_commit(struct commit * commit)
92 {
93         int action=filter_commit(commit);
94
95         if (action == STOP) {
96                 return STOP;
97         }
98
99         if (action == CONTINUE) {
100                 return CONTINUE;
101         }
102
103         show_commit(commit);
104
105         return CONTINUE;
106 }
107
108 static struct object_list **add_object(struct object *obj, struct object_list **p, const char *name)
109 {
110         struct object_list *entry = xmalloc(sizeof(*entry));
111         entry->item = obj;
112         entry->next = *p;
113         entry->name = name;
114         *p = entry;
115         return &entry->next;
116 }
117
118 static struct object_list **process_blob(struct blob *blob, struct object_list **p, const char *name)
119 {
120         struct object *obj = &blob->object;
121
122         if (!blob_objects)
123                 return p;
124         if (obj->flags & (UNINTERESTING | SEEN))
125                 return p;
126         obj->flags |= SEEN;
127         return add_object(obj, p, name);
128 }
129
130 static struct object_list **process_tree(struct tree *tree, struct object_list **p, const char *name)
131 {
132         struct object *obj = &tree->object;
133         struct tree_entry_list *entry;
134
135         if (!tree_objects)
136                 return p;
137         if (obj->flags & (UNINTERESTING | SEEN))
138                 return p;
139         if (parse_tree(tree) < 0)
140                 die("bad tree object %s", sha1_to_hex(obj->sha1));
141         obj->flags |= SEEN;
142         p = add_object(obj, p, name);
143         for (entry = tree->entries ; entry ; entry = entry->next) {
144                 if (entry->directory)
145                         p = process_tree(entry->item.tree, p, entry->name);
146                 else
147                         p = process_blob(entry->item.blob, p, entry->name);
148         }
149         return p;
150 }
151
152 static struct object_list *pending_objects = NULL;
153
154 static void show_commit_list(struct commit_list *list)
155 {
156         struct object_list *objects = NULL, **p = &objects, *pending;
157         while (list) {
158                 struct commit *commit = pop_most_recent_commit(&list, SEEN);
159
160                 p = process_tree(commit->tree, p, "");
161                 if (process_commit(commit) == STOP)
162                         break;
163         }
164         for (pending = pending_objects; pending; pending = pending->next) {
165                 struct object *obj = pending->item;
166                 const char *name = pending->name;
167                 if (obj->flags & (UNINTERESTING | SEEN))
168                         continue;
169                 if (obj->type == tag_type) {
170                         obj->flags |= SEEN;
171                         p = add_object(obj, p, name);
172                         continue;
173                 }
174                 if (obj->type == tree_type) {
175                         p = process_tree((struct tree *)obj, p, name);
176                         continue;
177                 }
178                 if (obj->type == blob_type) {
179                         p = process_blob((struct blob *)obj, p, name);
180                         continue;
181                 }
182                 die("unknown pending object %s (%s)", sha1_to_hex(obj->sha1), name);
183         }
184         while (objects) {
185                 printf("%s %s\n", sha1_to_hex(objects->item->sha1), objects->name);
186                 objects = objects->next;
187         }
188 }
189
190 static void mark_blob_uninteresting(struct blob *blob)
191 {
192         if (!blob_objects)
193                 return;
194         if (blob->object.flags & UNINTERESTING)
195                 return;
196         blob->object.flags |= UNINTERESTING;
197 }
198
199 static void mark_tree_uninteresting(struct tree *tree)
200 {
201         struct object *obj = &tree->object;
202         struct tree_entry_list *entry;
203
204         if (!tree_objects)
205                 return;
206         if (obj->flags & UNINTERESTING)
207                 return;
208         obj->flags |= UNINTERESTING;
209         if (parse_tree(tree) < 0)
210                 die("bad tree %s", sha1_to_hex(obj->sha1));
211         entry = tree->entries;
212         while (entry) {
213                 if (entry->directory)
214                         mark_tree_uninteresting(entry->item.tree);
215                 else
216                         mark_blob_uninteresting(entry->item.blob);
217                 entry = entry->next;
218         }
219 }
220
221 static void mark_parents_uninteresting(struct commit *commit)
222 {
223         struct commit_list *parents = commit->parents;
224
225         if (tree_objects)
226                 mark_tree_uninteresting(commit->tree);
227         while (parents) {
228                 struct commit *commit = parents->item;
229                 commit->object.flags |= UNINTERESTING;
230                 parents = parents->next;
231         }
232 }
233
234 static int everybody_uninteresting(struct commit_list *list)
235 {
236         while (list) {
237                 struct commit *commit = list->item;
238                 list = list->next;
239                 if (commit->object.flags & UNINTERESTING)
240                         continue;
241                 return 0;
242         }
243         return 1;
244 }
245
246 /*
247  * This is a truly stupid algorithm, but it's only
248  * used for bisection, and we just don't care enough.
249  *
250  * We care just barely enough to avoid recursing for
251  * non-merge entries.
252  */
253 static int count_distance(struct commit_list *entry)
254 {
255         int nr = 0;
256
257         while (entry) {
258                 struct commit *commit = entry->item;
259                 struct commit_list *p;
260
261                 if (commit->object.flags & (UNINTERESTING | COUNTED))
262                         break;
263                 nr++;
264                 commit->object.flags |= COUNTED;
265                 p = commit->parents;
266                 entry = p;
267                 if (p) {
268                         p = p->next;
269                         while (p) {
270                                 nr += count_distance(p);
271                                 p = p->next;
272                         }
273                 }
274         }
275         return nr;
276 }
277
278 static void clear_distance(struct commit_list *list)
279 {
280         while (list) {
281                 struct commit *commit = list->item;
282                 commit->object.flags &= ~COUNTED;
283                 list = list->next;
284         }
285 }
286
287 static struct commit_list *find_bisection(struct commit_list *list)
288 {
289         int nr, closest;
290         struct commit_list *p, *best;
291
292         nr = 0;
293         p = list;
294         while (p) {
295                 nr++;
296                 p = p->next;
297         }
298         closest = 0;
299         best = list;
300
301         p = list;
302         while (p) {
303                 int distance = count_distance(p);
304                 clear_distance(list);
305                 if (nr - distance < distance)
306                         distance = nr - distance;
307                 if (distance > closest) {
308                         best = p;
309                         closest = distance;
310                 }
311                 p = p->next;
312         }
313         if (best)
314                 best->next = NULL;
315         return best;
316 }
317
318 static struct commit_list *limit_list(struct commit_list *list)
319 {
320         struct commit_list *newlist = NULL;
321         struct commit_list **p = &newlist;
322         while (list) {
323                 struct commit *commit = pop_most_recent_commit(&list, SEEN);
324                 struct object *obj = &commit->object;
325
326                 if (unpacked && has_sha1_pack(obj->sha1))
327                         obj->flags |= UNINTERESTING;
328                 if (obj->flags & UNINTERESTING) {
329                         mark_parents_uninteresting(commit);
330                         if (everybody_uninteresting(list))
331                                 break;
332                         continue;
333                 }
334                 p = &commit_list_insert(commit, p)->next;
335         }
336         if (bisect_list)
337                 newlist = find_bisection(newlist);
338         return newlist;
339 }
340
341 static void add_pending_object(struct object *obj, const char *name)
342 {
343         add_object(obj, &pending_objects, name);
344 }
345
346 static struct commit *get_commit_reference(const char *name, unsigned int flags)
347 {
348         unsigned char sha1[20];
349         struct object *object;
350
351         if (get_sha1(name, sha1))
352                 usage(rev_list_usage);
353         object = parse_object(sha1);
354         if (!object)
355                 die("bad object %s", name);
356
357         /*
358          * Tag object? Look what it points to..
359          */
360         if (object->type == tag_type) {
361                 struct tag *tag = (struct tag *) object;
362                 object->flags |= flags;
363                 if (tag_objects && !(object->flags & UNINTERESTING))
364                         add_pending_object(object, tag->tag);
365                 object = tag->tagged;
366         }
367
368         /*
369          * Commit object? Just return it, we'll do all the complex
370          * reachability crud.
371          */
372         if (object->type == commit_type) {
373                 struct commit *commit = (struct commit *)object;
374                 object->flags |= flags;
375                 if (parse_commit(commit) < 0)
376                         die("unable to parse commit %s", name);
377                 return commit;
378         }
379
380         /*
381          * Tree object? Either mark it uniniteresting, or add it
382          * to the list of objects to look at later..
383          */
384         if (object->type == tree_type) {
385                 struct tree *tree = (struct tree *)object;
386                 if (!tree_objects)
387                         return NULL;
388                 if (flags & UNINTERESTING) {
389                         mark_tree_uninteresting(tree);
390                         return NULL;
391                 }
392                 add_pending_object(object, "");
393                 return NULL;
394         }
395
396         /*
397          * Blob object? You know the drill by now..
398          */
399         if (object->type == blob_type) {
400                 struct blob *blob = (struct blob *)object;
401                 if (!blob_objects)
402                         return NULL;
403                 if (flags & UNINTERESTING) {
404                         mark_blob_uninteresting(blob);
405                         return NULL;
406                 }
407                 add_pending_object(object, "");
408                 return NULL;
409         }
410         die("%s is unknown object", name);
411 }
412
413 int main(int argc, char **argv)
414 {
415         struct commit_list *list = NULL;
416         int i, limited = 0;
417
418         for (i = 1 ; i < argc; i++) {
419                 int flags;
420                 char *arg = argv[i];
421                 struct commit *commit;
422
423                 if (!strncmp(arg, "--max-count=", 12)) {
424                         max_count = atoi(arg + 12);
425                         continue;
426                 }
427                 if (!strncmp(arg, "--max-age=", 10)) {
428                         max_age = atoi(arg + 10);
429                         continue;
430                 }
431                 if (!strncmp(arg, "--min-age=", 10)) {
432                         min_age = atoi(arg + 10);
433                         continue;
434                 }
435                 if (!strcmp(arg, "--header")) {
436                         verbose_header = 1;
437                         continue;
438                 }
439                 if (!strncmp(arg, "--pretty", 8)) {
440                         commit_format = get_commit_format(arg+8);
441                         verbose_header = 1;
442                         hdr_termination = '\n';
443                         prefix = "commit ";
444                         continue;
445                 }
446                 if (!strcmp(arg, "--parents")) {
447                         show_parents = 1;
448                         continue;
449                 }
450                 if (!strcmp(arg, "--bisect")) {
451                         bisect_list = 1;
452                         continue;
453                 }
454                 if (!strcmp(arg, "--objects")) {
455                         tag_objects = 1;
456                         tree_objects = 1;
457                         blob_objects = 1;
458                         continue;
459                 }
460                 if (!strcmp(arg, "--unpacked")) {
461                         unpacked = 1;
462                         limited = 1;
463                         continue;
464                 }
465                 if (!strncmp(arg, "--merge-order", 13)) {
466                         merge_order = 1;
467                         continue;
468                 }
469                 if (!strncmp(arg, "--show-breaks", 13)) {
470                         show_breaks = 1;
471                         continue;
472                 }
473
474                 flags = 0;
475                 if (*arg == '^') {
476                         flags = UNINTERESTING;
477                         arg++;
478                         limited = 1;
479                 }
480                 if (show_breaks && !merge_order)
481                         usage(rev_list_usage);
482                 commit = get_commit_reference(arg, flags);
483                 if (!commit)
484                         continue;
485                 commit_list_insert(commit, &list);
486         }
487
488         if (!merge_order) {             
489                 if (limited)
490                         list = limit_list(list);
491                 show_commit_list(list);
492         } else {
493                 if (sort_list_in_merge_order(list, &process_commit)) {
494                           die("merge order sort failed\n");
495                 }
496         }
497
498         return 0;
499 }