6b35b61ec25136c0c7249d217e2f47f663da3199
[git.git] / rev-list.c
1 #include "cache.h"
2 #include "commit.h"
3 #include "tree.h"
4 #include "blob.h"
5 #include "epoch.h"
6
7 #define SEEN            (1u << 0)
8 #define INTERESTING     (1u << 1)
9 #define COUNTED         (1u << 2)
10 #define SHOWN           (LAST_EPOCH_FLAG << 2)
11
12 static const char rev_list_usage[] =
13         "usage: git-rev-list [OPTION] commit-id <commit-id>\n"
14                       "  --max-count=nr\n"
15                       "  --max-age=epoch\n"
16                       "  --min-age=epoch\n"
17                       "  --header\n"
18                       "  --pretty\n"
19                       "  --merge-order [ --show-breaks ]";
20
21 static int bisect_list = 0;
22 static int tree_objects = 0;
23 static int blob_objects = 0;
24 static int verbose_header = 0;
25 static int show_parents = 0;
26 static int hdr_termination = 0;
27 static const char *prefix = "";
28 static unsigned long max_age = -1;
29 static unsigned long min_age = -1;
30 static int max_count = -1;
31 static enum cmit_fmt commit_format = CMIT_FMT_RAW;
32 static int merge_order = 0;
33 static int show_breaks = 0;
34 static int stop_traversal = 0;
35
36 static void show_commit(struct commit *commit)
37 {
38         commit->object.flags |= SHOWN;
39         if (show_breaks) {
40                 prefix = "| ";
41                 if (commit->object.flags & DISCONTINUITY) {
42                         prefix = "^ ";     
43                 } else if (commit->object.flags & BOUNDARY) {
44                         prefix = "= ";
45                 } 
46         }                       
47         printf("%s%s", prefix, sha1_to_hex(commit->object.sha1));
48         if (show_parents) {
49                 struct commit_list *parents = commit->parents;
50                 while (parents) {
51                         printf(" %s", sha1_to_hex(parents->item->object.sha1));
52                         parents = parents->next;
53                 }
54         }
55         putchar('\n');
56         if (verbose_header) {
57                 static char pretty_header[16384];
58                 pretty_print_commit(commit_format, commit->buffer, ~0, pretty_header, sizeof(pretty_header));
59                 printf("%s%c", pretty_header, hdr_termination);
60         }       
61 }
62
63 static int filter_commit(struct commit * commit)
64 {
65         if (merge_order && stop_traversal && commit->object.flags & BOUNDARY)
66                 return STOP;
67         if (commit->object.flags & (UNINTERESTING|SHOWN))
68                 return CONTINUE;
69         if (min_age != -1 && (commit->date > min_age))
70                 return CONTINUE;
71         if (max_age != -1 && (commit->date < max_age)) {
72                 if (!merge_order)
73                         return STOP;
74                 else {
75                         stop_traversal = 1;
76                         return CONTINUE;
77                 }
78         }
79         if (max_count != -1 && !max_count--)
80                 return STOP;
81         return DO;
82 }
83
84 static int process_commit(struct commit * commit)
85 {
86         int action=filter_commit(commit);
87
88         if (action == STOP) {
89                 return STOP;
90         }
91
92         if (action == CONTINUE) {
93                 return CONTINUE;
94         }
95
96         show_commit(commit);
97
98         return CONTINUE;
99 }
100
101 static struct object_list **add_object(struct object *obj, struct object_list **p)
102 {
103         struct object_list *entry = xmalloc(sizeof(*entry));
104         entry->item = obj;
105         entry->next = NULL;
106         *p = entry;
107         return &entry->next;
108 }
109
110 static struct object_list **process_blob(struct blob *blob, struct object_list **p)
111 {
112         struct object *obj = &blob->object;
113
114         if (!blob_objects)
115                 return p;
116         if (obj->flags & (UNINTERESTING | SEEN))
117                 return p;
118         obj->flags |= SEEN;
119         return add_object(obj, p);
120 }
121
122 static struct object_list **process_tree(struct tree *tree, struct object_list **p)
123 {
124         struct object *obj = &tree->object;
125         struct tree_entry_list *entry;
126
127         if (!tree_objects)
128                 return p;
129         if (obj->flags & (UNINTERESTING | SEEN))
130                 return p;
131         if (parse_tree(tree) < 0)
132                 die("bad tree object %s", sha1_to_hex(obj->sha1));
133         obj->flags |= SEEN;
134         p = add_object(obj, p);
135         for (entry = tree->entries ; entry ; entry = entry->next) {
136                 if (entry->directory)
137                         p = process_tree(entry->item.tree, p);
138                 else
139                         p = process_blob(entry->item.blob, p);
140         }
141         return p;
142 }
143
144 static void show_commit_list(struct commit_list *list)
145 {
146         struct object_list *objects = NULL, **p = &objects;
147         while (list) {
148                 struct commit *commit = pop_most_recent_commit(&list, SEEN);
149
150                 p = process_tree(commit->tree, p);
151                 if (process_commit(commit) == STOP)
152                         break;
153         }
154         while (objects) {
155                 puts(sha1_to_hex(objects->item->sha1));
156                 objects = objects->next;
157         }
158 }
159
160 static void mark_blob_uninteresting(struct blob *blob)
161 {
162         if (!blob_objects)
163                 return;
164         if (blob->object.flags & UNINTERESTING)
165                 return;
166         blob->object.flags |= UNINTERESTING;
167 }
168
169 static void mark_tree_uninteresting(struct tree *tree)
170 {
171         struct object *obj = &tree->object;
172         struct tree_entry_list *entry;
173
174         if (!tree_objects)
175                 return;
176         if (obj->flags & UNINTERESTING)
177                 return;
178         obj->flags |= UNINTERESTING;
179         if (parse_tree(tree) < 0)
180                 die("bad tree %s", sha1_to_hex(obj->sha1));
181         entry = tree->entries;
182         while (entry) {
183                 if (entry->directory)
184                         mark_tree_uninteresting(entry->item.tree);
185                 else
186                         mark_blob_uninteresting(entry->item.blob);
187                 entry = entry->next;
188         }
189 }
190
191 static void mark_parents_uninteresting(struct commit *commit)
192 {
193         struct commit_list *parents = commit->parents;
194
195         if (tree_objects)
196                 mark_tree_uninteresting(commit->tree);
197         while (parents) {
198                 struct commit *commit = parents->item;
199                 commit->object.flags |= UNINTERESTING;
200                 parents = parents->next;
201         }
202 }
203
204 static int everybody_uninteresting(struct commit_list *list)
205 {
206         while (list) {
207                 struct commit *commit = list->item;
208                 list = list->next;
209                 if (commit->object.flags & UNINTERESTING)
210                         continue;
211                 return 0;
212         }
213         return 1;
214 }
215
216 /*
217  * This is a truly stupid algorithm, but it's only
218  * used for bisection, and we just don't care enough.
219  *
220  * We care just barely enough to avoid recursing for
221  * non-merge entries.
222  */
223 static int count_distance(struct commit_list *entry)
224 {
225         int nr = 0;
226
227         while (entry) {
228                 struct commit *commit = entry->item;
229                 struct commit_list *p;
230
231                 if (commit->object.flags & (UNINTERESTING | COUNTED))
232                         break;
233                 nr++;
234                 commit->object.flags |= COUNTED;
235                 p = commit->parents;
236                 entry = p;
237                 if (p) {
238                         p = p->next;
239                         while (p) {
240                                 nr += count_distance(p);
241                                 p = p->next;
242                         }
243                 }
244         }
245         return nr;
246 }
247
248 static void clear_distance(struct commit_list *list)
249 {
250         while (list) {
251                 struct commit *commit = list->item;
252                 commit->object.flags &= ~COUNTED;
253                 list = list->next;
254         }
255 }
256
257 static struct commit_list *find_bisection(struct commit_list *list)
258 {
259         int nr, closest;
260         struct commit_list *p, *best;
261
262         nr = 0;
263         p = list;
264         while (p) {
265                 nr++;
266                 p = p->next;
267         }
268         closest = 0;
269         best = list;
270
271         p = list;
272         while (p) {
273                 int distance = count_distance(p);
274                 clear_distance(list);
275                 if (nr - distance < distance)
276                         distance = nr - distance;
277                 if (distance > closest) {
278                         best = p;
279                         closest = distance;
280                 }
281                 p = p->next;
282         }
283         if (best)
284                 best->next = NULL;
285         return best;
286 }
287
288 struct commit_list *limit_list(struct commit_list *list)
289 {
290         struct commit_list *newlist = NULL;
291         struct commit_list **p = &newlist;
292         do {
293                 struct commit *commit = pop_most_recent_commit(&list, SEEN);
294                 struct object *obj = &commit->object;
295
296                 if (obj->flags & UNINTERESTING) {
297                         mark_parents_uninteresting(commit);
298                         if (everybody_uninteresting(list))
299                                 break;
300                         continue;
301                 }
302                 p = &commit_list_insert(commit, p)->next;
303         } while (list);
304         if (bisect_list)
305                 newlist = find_bisection(newlist);
306         return newlist;
307 }
308
309 static enum cmit_fmt get_commit_format(const char *arg)
310 {
311         if (!*arg)
312                 return CMIT_FMT_DEFAULT;
313         if (!strcmp(arg, "=raw"))
314                 return CMIT_FMT_RAW;
315         if (!strcmp(arg, "=medium"))
316                 return CMIT_FMT_MEDIUM;
317         if (!strcmp(arg, "=short"))
318                 return CMIT_FMT_SHORT;
319         usage(rev_list_usage);  
320 }                       
321
322
323 int main(int argc, char **argv)
324 {
325         struct commit_list *list = NULL;
326         int i, limited = 0;
327
328         for (i = 1 ; i < argc; i++) {
329                 int flags;
330                 char *arg = argv[i];
331                 unsigned char sha1[20];
332                 struct commit *commit;
333
334                 if (!strncmp(arg, "--max-count=", 12)) {
335                         max_count = atoi(arg + 12);
336                         continue;
337                 }
338                 if (!strncmp(arg, "--max-age=", 10)) {
339                         max_age = atoi(arg + 10);
340                         continue;
341                 }
342                 if (!strncmp(arg, "--min-age=", 10)) {
343                         min_age = atoi(arg + 10);
344                         continue;
345                 }
346                 if (!strcmp(arg, "--header")) {
347                         verbose_header = 1;
348                         continue;
349                 }
350                 if (!strncmp(arg, "--pretty", 8)) {
351                         commit_format = get_commit_format(arg+8);
352                         verbose_header = 1;
353                         hdr_termination = '\n';
354                         prefix = "commit ";
355                         continue;
356                 }
357                 if (!strcmp(arg, "--parents")) {
358                         show_parents = 1;
359                         continue;
360                 }
361                 if (!strcmp(arg, "--bisect")) {
362                         bisect_list = 1;
363                         continue;
364                 }
365                 if (!strcmp(arg, "--objects")) {
366                         tree_objects = 1;
367                         blob_objects = 1;
368                         continue;
369                 }
370                 if (!strncmp(arg, "--merge-order", 13)) {
371                         merge_order = 1;
372                         continue;
373                 }
374                 if (!strncmp(arg, "--show-breaks", 13)) {
375                         show_breaks = 1;
376                         continue;
377                 }
378
379                 flags = 0;
380                 if (*arg == '^') {
381                         flags = UNINTERESTING;
382                         arg++;
383                         limited = 1;
384                 }
385                 if (get_sha1(arg, sha1) || (show_breaks && !merge_order))
386                         usage(rev_list_usage);
387                 commit = lookup_commit_reference(sha1);
388                 if (!commit || parse_commit(commit) < 0)
389                         die("bad commit object %s", arg);
390                 commit->object.flags |= flags;
391                 commit_list_insert(commit, &list);
392         }
393
394         if (!list)
395                 usage(rev_list_usage);
396
397         if (!merge_order) {             
398                 if (limited)
399                         list = limit_list(list);
400                 show_commit_list(list);
401         } else {
402                 if (sort_list_in_merge_order(list, &process_commit)) {
403                           die("merge order sort failed\n");
404                 }
405         }
406
407         return 0;
408 }