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