Merge branch 'master' into next
[git] / builtin-rev-list.c
1 #include "cache.h"
2 #include "refs.h"
3 #include "tag.h"
4 #include "commit.h"
5 #include "tree.h"
6 #include "blob.h"
7 #include "tree-walk.h"
8 #include "diff.h"
9 #include "revision.h"
10 #include "builtin.h"
11
12 /* bits #0-15 in revision.h */
13
14 #define COUNTED         (1u<<16)
15
16 static const char rev_list_usage[] =
17 "git-rev-list [OPTION] <commit-id>... [ -- paths... ]\n"
18 "  limiting output:\n"
19 "    --max-count=nr\n"
20 "    --max-age=epoch\n"
21 "    --min-age=epoch\n"
22 "    --sparse\n"
23 "    --no-merges\n"
24 "    --remove-empty\n"
25 "    --all\n"
26 "  ordering output:\n"
27 "    --topo-order\n"
28 "    --date-order\n"
29 "  formatting output:\n"
30 "    --parents\n"
31 "    --objects | --objects-edge\n"
32 "    --unpacked\n"
33 "    --header | --pretty\n"
34 "    --abbrev=nr | --no-abbrev\n"
35 "    --abbrev-commit\n"
36 "  special purpose:\n"
37 "    --bisect"
38 ;
39
40 static struct rev_info revs;
41
42 static int bisect_list = 0;
43 static int show_timestamp = 0;
44 static int hdr_termination = 0;
45 static const char *header_prefix;
46
47 static void show_commit(struct commit *commit)
48 {
49         if (show_timestamp)
50                 printf("%lu ", commit->date);
51         if (header_prefix)
52                 fputs(header_prefix, stdout);
53         if (commit->object.flags & BOUNDARY)
54                 putchar('-');
55         if (revs.abbrev_commit && revs.abbrev)
56                 fputs(find_unique_abbrev(commit->object.sha1, revs.abbrev),
57                       stdout);
58         else
59                 fputs(sha1_to_hex(commit->object.sha1), stdout);
60         if (revs.parents) {
61                 struct commit_list *parents = commit->parents;
62                 while (parents) {
63                         struct object *o = &(parents->item->object);
64                         parents = parents->next;
65                         if (o->flags & TMP_MARK)
66                                 continue;
67                         printf(" %s", sha1_to_hex(o->sha1));
68                         o->flags |= TMP_MARK;
69                 }
70                 /* TMP_MARK is a general purpose flag that can
71                  * be used locally, but the user should clean
72                  * things up after it is done with them.
73                  */
74                 for (parents = commit->parents;
75                      parents;
76                      parents = parents->next)
77                         parents->item->object.flags &= ~TMP_MARK;
78         }
79         if (revs.commit_format == CMIT_FMT_ONELINE)
80                 putchar(' ');
81         else
82                 putchar('\n');
83
84         if (revs.verbose_header) {
85                 static char pretty_header[16384];
86                 pretty_print_commit(revs.commit_format, commit, ~0,
87                                     pretty_header, sizeof(pretty_header),
88                                     revs.abbrev, NULL, NULL);
89                 printf("%s%c", pretty_header, hdr_termination);
90         }
91         fflush(stdout);
92 }
93
94 static struct object_list **process_blob(struct blob *blob,
95                                          struct object_list **p,
96                                          struct name_path *path,
97                                          const char *name)
98 {
99         struct object *obj = &blob->object;
100
101         if (!revs.blob_objects)
102                 return p;
103         if (obj->flags & (UNINTERESTING | SEEN))
104                 return p;
105         obj->flags |= SEEN;
106         name = strdup(name);
107         return add_object(obj, p, path, name);
108 }
109
110 static struct object_list **process_tree(struct tree *tree,
111                                          struct object_list **p,
112                                          struct name_path *path,
113                                          const char *name)
114 {
115         struct object *obj = &tree->object;
116         struct tree_entry_list *entry;
117         struct name_path me;
118
119         if (!revs.tree_objects)
120                 return p;
121         if (obj->flags & (UNINTERESTING | SEEN))
122                 return p;
123         if (parse_tree(tree) < 0)
124                 die("bad tree object %s", sha1_to_hex(obj->sha1));
125         obj->flags |= SEEN;
126         name = strdup(name);
127         p = add_object(obj, p, path, name);
128         me.up = path;
129         me.elem = name;
130         me.elem_len = strlen(name);
131         entry = tree->entries;
132         tree->entries = NULL;
133         while (entry) {
134                 struct tree_entry_list *next = entry->next;
135                 if (entry->directory)
136                         p = process_tree(entry->item.tree, p, &me, entry->name);
137                 else
138                         p = process_blob(entry->item.blob, p, &me, entry->name);
139                 free(entry->name);
140                 free(entry);
141                 entry = next;
142         }
143         return p;
144 }
145
146 static void show_commit_list(struct rev_info *revs)
147 {
148         struct commit *commit;
149         struct object_list *objects = NULL, **p = &objects, *pending;
150
151         while ((commit = get_revision(revs)) != NULL) {
152                 p = process_tree(commit->tree, p, NULL, "");
153                 show_commit(commit);
154         }
155         for (pending = revs->pending_objects; pending; pending = pending->next) {
156                 struct object *obj = pending->item;
157                 const char *name = pending->name;
158                 if (obj->flags & (UNINTERESTING | SEEN))
159                         continue;
160                 if (obj->type == tag_type) {
161                         obj->flags |= SEEN;
162                         p = add_object(obj, p, NULL, name);
163                         continue;
164                 }
165                 if (obj->type == tree_type) {
166                         p = process_tree((struct tree *)obj, p, NULL, name);
167                         continue;
168                 }
169                 if (obj->type == blob_type) {
170                         p = process_blob((struct blob *)obj, p, NULL, name);
171                         continue;
172                 }
173                 die("unknown pending object %s (%s)", sha1_to_hex(obj->sha1), name);
174         }
175         while (objects) {
176                 /* An object with name "foo\n0000000..." can be used to
177                  * confuse downstream git-pack-objects very badly.
178                  */
179                 const char *ep = strchr(objects->name, '\n');
180                 if (ep) {
181                         printf("%s %.*s\n", sha1_to_hex(objects->item->sha1),
182                                (int) (ep - objects->name),
183                                objects->name);
184                 }
185                 else
186                         printf("%s %s\n", sha1_to_hex(objects->item->sha1), objects->name);
187                 objects = objects->next;
188         }
189 }
190
191 /*
192  * This is a truly stupid algorithm, but it's only
193  * used for bisection, and we just don't care enough.
194  *
195  * We care just barely enough to avoid recursing for
196  * non-merge entries.
197  */
198 static int count_distance(struct commit_list *entry)
199 {
200         int nr = 0;
201
202         while (entry) {
203                 struct commit *commit = entry->item;
204                 struct commit_list *p;
205
206                 if (commit->object.flags & (UNINTERESTING | COUNTED))
207                         break;
208                 if (!revs.prune_fn || (commit->object.flags & TREECHANGE))
209                         nr++;
210                 commit->object.flags |= COUNTED;
211                 p = commit->parents;
212                 entry = p;
213                 if (p) {
214                         p = p->next;
215                         while (p) {
216                                 nr += count_distance(p);
217                                 p = p->next;
218                         }
219                 }
220         }
221
222         return nr;
223 }
224
225 static void clear_distance(struct commit_list *list)
226 {
227         while (list) {
228                 struct commit *commit = list->item;
229                 commit->object.flags &= ~COUNTED;
230                 list = list->next;
231         }
232 }
233
234 static struct commit_list *find_bisection(struct commit_list *list)
235 {
236         int nr, closest;
237         struct commit_list *p, *best;
238
239         nr = 0;
240         p = list;
241         while (p) {
242                 if (!revs.prune_fn || (p->item->object.flags & TREECHANGE))
243                         nr++;
244                 p = p->next;
245         }
246         closest = 0;
247         best = list;
248
249         for (p = list; p; p = p->next) {
250                 int distance;
251
252                 if (revs.prune_fn && !(p->item->object.flags & TREECHANGE))
253                         continue;
254
255                 distance = count_distance(p);
256                 clear_distance(list);
257                 if (nr - distance < distance)
258                         distance = nr - distance;
259                 if (distance > closest) {
260                         best = p;
261                         closest = distance;
262                 }
263         }
264         if (best)
265                 best->next = NULL;
266         return best;
267 }
268
269 static void mark_edge_parents_uninteresting(struct commit *commit)
270 {
271         struct commit_list *parents;
272
273         for (parents = commit->parents; parents; parents = parents->next) {
274                 struct commit *parent = parents->item;
275                 if (!(parent->object.flags & UNINTERESTING))
276                         continue;
277                 mark_tree_uninteresting(parent->tree);
278                 if (revs.edge_hint && !(parent->object.flags & SHOWN)) {
279                         parent->object.flags |= SHOWN;
280                         printf("-%s\n", sha1_to_hex(parent->object.sha1));
281                 }
282         }
283 }
284
285 static void mark_edges_uninteresting(struct commit_list *list)
286 {
287         for ( ; list; list = list->next) {
288                 struct commit *commit = list->item;
289
290                 if (commit->object.flags & UNINTERESTING) {
291                         mark_tree_uninteresting(commit->tree);
292                         continue;
293                 }
294                 mark_edge_parents_uninteresting(commit);
295         }
296 }
297
298 int cmd_rev_list(int argc, const char **argv, char **envp)
299 {
300         struct commit_list *list;
301         int i;
302
303         init_revisions(&revs);
304         revs.abbrev = 0;
305         revs.commit_format = CMIT_FMT_UNSPECIFIED;
306         argc = setup_revisions(argc, argv, &revs, NULL);
307
308         for (i = 1 ; i < argc; i++) {
309                 const char *arg = argv[i];
310
311                 if (!strcmp(arg, "--header")) {
312                         revs.verbose_header = 1;
313                         continue;
314                 }
315                 if (!strcmp(arg, "--timestamp")) {
316                         show_timestamp = 1;
317                         continue;
318                 }
319                 if (!strcmp(arg, "--bisect")) {
320                         bisect_list = 1;
321                         continue;
322                 }
323                 usage(rev_list_usage);
324
325         }
326         if (revs.commit_format != CMIT_FMT_UNSPECIFIED) {
327                 /* The command line has a --pretty  */
328                 hdr_termination = '\n';
329                 if (revs.commit_format == CMIT_FMT_ONELINE)
330                         header_prefix = "";
331                 else
332                         header_prefix = "commit ";
333         }
334         else if (revs.verbose_header)
335                 /* Only --header was specified */
336                 revs.commit_format = CMIT_FMT_RAW;
337
338         list = revs.commits;
339
340         if ((!list &&
341              (!(revs.tag_objects||revs.tree_objects||revs.blob_objects) &&
342               !revs.pending_objects)) ||
343             revs.diff)
344                 usage(rev_list_usage);
345
346         save_commit_buffer = revs.verbose_header;
347         track_object_refs = 0;
348         if (bisect_list)
349                 revs.limited = 1;
350
351         prepare_revision_walk(&revs);
352         if (revs.tree_objects)
353                 mark_edges_uninteresting(revs.commits);
354
355         if (bisect_list)
356                 revs.commits = find_bisection(revs.commits);
357
358         show_commit_list(&revs);
359
360         return 0;
361 }