Merge branch 'maint'
[git] / builtin-rev-list.c
1 #include "cache.h"
2 #include "commit.h"
3 #include "diff.h"
4 #include "revision.h"
5 #include "list-objects.h"
6 #include "builtin.h"
7 #include "log-tree.h"
8 #include "graph.h"
9 #include "bisect.h"
10
11 static const char rev_list_usage[] =
12 "git rev-list [OPTION] <commit-id>... [ -- paths... ]\n"
13 "  limiting output:\n"
14 "    --max-count=nr\n"
15 "    --max-age=epoch\n"
16 "    --min-age=epoch\n"
17 "    --sparse\n"
18 "    --no-merges\n"
19 "    --remove-empty\n"
20 "    --all\n"
21 "    --branches\n"
22 "    --tags\n"
23 "    --remotes\n"
24 "    --stdin\n"
25 "    --quiet\n"
26 "  ordering output:\n"
27 "    --topo-order\n"
28 "    --date-order\n"
29 "    --reverse\n"
30 "  formatting output:\n"
31 "    --parents\n"
32 "    --children\n"
33 "    --objects | --objects-edge\n"
34 "    --unpacked\n"
35 "    --header | --pretty\n"
36 "    --abbrev=nr | --no-abbrev\n"
37 "    --abbrev-commit\n"
38 "    --left-right\n"
39 "  special purpose:\n"
40 "    --bisect\n"
41 "    --bisect-vars\n"
42 "    --bisect-all"
43 ;
44
45 static void finish_commit(struct commit *commit, void *data);
46 static void show_commit(struct commit *commit, void *data)
47 {
48         struct rev_list_info *info = data;
49         struct rev_info *revs = info->revs;
50
51         graph_show_commit(revs->graph);
52
53         if (info->show_timestamp)
54                 printf("%lu ", commit->date);
55         if (info->header_prefix)
56                 fputs(info->header_prefix, stdout);
57
58         if (!revs->graph) {
59                 if (commit->object.flags & BOUNDARY)
60                         putchar('-');
61                 else if (commit->object.flags & UNINTERESTING)
62                         putchar('^');
63                 else if (revs->left_right) {
64                         if (commit->object.flags & SYMMETRIC_LEFT)
65                                 putchar('<');
66                         else
67                                 putchar('>');
68                 }
69         }
70         if (revs->abbrev_commit && revs->abbrev)
71                 fputs(find_unique_abbrev(commit->object.sha1, revs->abbrev),
72                       stdout);
73         else
74                 fputs(sha1_to_hex(commit->object.sha1), stdout);
75         if (revs->print_parents) {
76                 struct commit_list *parents = commit->parents;
77                 while (parents) {
78                         printf(" %s", sha1_to_hex(parents->item->object.sha1));
79                         parents = parents->next;
80                 }
81         }
82         if (revs->children.name) {
83                 struct commit_list *children;
84
85                 children = lookup_decoration(&revs->children, &commit->object);
86                 while (children) {
87                         printf(" %s", sha1_to_hex(children->item->object.sha1));
88                         children = children->next;
89                 }
90         }
91         show_decorations(revs, commit);
92         if (revs->commit_format == CMIT_FMT_ONELINE)
93                 putchar(' ');
94         else
95                 putchar('\n');
96
97         if (revs->verbose_header && commit->buffer) {
98                 struct strbuf buf = STRBUF_INIT;
99                 struct pretty_print_context ctx = {0};
100                 ctx.abbrev = revs->abbrev;
101                 ctx.date_mode = revs->date_mode;
102                 pretty_print_commit(revs->commit_format, commit, &buf, &ctx);
103                 if (revs->graph) {
104                         if (buf.len) {
105                                 if (revs->commit_format != CMIT_FMT_ONELINE)
106                                         graph_show_oneline(revs->graph);
107
108                                 graph_show_commit_msg(revs->graph, &buf);
109
110                                 /*
111                                  * Add a newline after the commit message.
112                                  *
113                                  * Usually, this newline produces a blank
114                                  * padding line between entries, in which case
115                                  * we need to add graph padding on this line.
116                                  *
117                                  * However, the commit message may not end in a
118                                  * newline.  In this case the newline simply
119                                  * ends the last line of the commit message,
120                                  * and we don't need any graph output.  (This
121                                  * always happens with CMIT_FMT_ONELINE, and it
122                                  * happens with CMIT_FMT_USERFORMAT when the
123                                  * format doesn't explicitly end in a newline.)
124                                  */
125                                 if (buf.len && buf.buf[buf.len - 1] == '\n')
126                                         graph_show_padding(revs->graph);
127                                 putchar('\n');
128                         } else {
129                                 /*
130                                  * If the message buffer is empty, just show
131                                  * the rest of the graph output for this
132                                  * commit.
133                                  */
134                                 if (graph_show_remainder(revs->graph))
135                                         putchar('\n');
136                         }
137                 } else {
138                         if (buf.len)
139                                 printf("%s%c", buf.buf, info->hdr_termination);
140                 }
141                 strbuf_release(&buf);
142         } else {
143                 if (graph_show_remainder(revs->graph))
144                         putchar('\n');
145         }
146         maybe_flush_or_die(stdout, "stdout");
147         finish_commit(commit, data);
148 }
149
150 static void finish_commit(struct commit *commit, void *data)
151 {
152         if (commit->parents) {
153                 free_commit_list(commit->parents);
154                 commit->parents = NULL;
155         }
156         free(commit->buffer);
157         commit->buffer = NULL;
158 }
159
160 static void finish_object(struct object *obj, const struct name_path *path, const char *name)
161 {
162         if (obj->type == OBJ_BLOB && !has_sha1_file(obj->sha1))
163                 die("missing blob object '%s'", sha1_to_hex(obj->sha1));
164 }
165
166 static void show_object(struct object *obj, const struct name_path *path, const char *component)
167 {
168         char *name = path_name(path, component);
169         /* An object with name "foo\n0000000..." can be used to
170          * confuse downstream "git pack-objects" very badly.
171          */
172         const char *ep = strchr(name, '\n');
173
174         finish_object(obj, path, name);
175         if (ep) {
176                 printf("%s %.*s\n", sha1_to_hex(obj->sha1),
177                        (int) (ep - name),
178                        name);
179         }
180         else
181                 printf("%s %s\n", sha1_to_hex(obj->sha1), name);
182         free(name);
183 }
184
185 static void show_edge(struct commit *commit)
186 {
187         printf("-%s\n", sha1_to_hex(commit->object.sha1));
188 }
189
190 static inline int log2i(int n)
191 {
192         int log2 = 0;
193
194         for (; n > 1; n >>= 1)
195                 log2++;
196
197         return log2;
198 }
199
200 static inline int exp2i(int n)
201 {
202         return 1 << n;
203 }
204
205 /*
206  * Estimate the number of bisect steps left (after the current step)
207  *
208  * For any x between 0 included and 2^n excluded, the probability for
209  * n - 1 steps left looks like:
210  *
211  * P(2^n + x) == (2^n - x) / (2^n + x)
212  *
213  * and P(2^n + x) < 0.5 means 2^n < 3x
214  */
215 int estimate_bisect_steps(int all)
216 {
217         int n, x, e;
218
219         if (all < 3)
220                 return 0;
221
222         n = log2i(all);
223         e = exp2i(n);
224         x = all - e;
225
226         return (e < 3 * x) ? n : n - 1;
227 }
228
229 void print_commit_list(struct commit_list *list,
230                        const char *format_cur,
231                        const char *format_last)
232 {
233         for ( ; list; list = list->next) {
234                 const char *format = list->next ? format_cur : format_last;
235                 printf(format, sha1_to_hex(list->item->object.sha1));
236         }
237 }
238
239 static void show_tried_revs(struct commit_list *tried)
240 {
241         printf("bisect_tried='");
242         print_commit_list(tried, "%s|", "%s");
243         printf("'\n");
244 }
245
246 static void print_var_str(const char *var, const char *val)
247 {
248         printf("%s='%s'\n", var, val);
249 }
250
251 static void print_var_int(const char *var, int val)
252 {
253         printf("%s=%d\n", var, val);
254 }
255
256 int show_bisect_vars(struct rev_list_info *info, int reaches, int all)
257 {
258         int cnt, flags = info->bisect_show_flags;
259         char hex[41] = "";
260         struct commit_list *tried;
261         struct rev_info *revs = info->revs;
262
263         if (!revs->commits && !(flags & BISECT_SHOW_TRIED))
264                 return 1;
265
266         revs->commits = filter_skipped(revs->commits, &tried,
267                                        flags & BISECT_SHOW_ALL,
268                                        NULL, NULL);
269
270         /*
271          * revs->commits can reach "reaches" commits among
272          * "all" commits.  If it is good, then there are
273          * (all-reaches) commits left to be bisected.
274          * On the other hand, if it is bad, then the set
275          * to bisect is "reaches".
276          * A bisect set of size N has (N-1) commits further
277          * to test, as we already know one bad one.
278          */
279         cnt = all - reaches;
280         if (cnt < reaches)
281                 cnt = reaches;
282
283         if (revs->commits)
284                 strcpy(hex, sha1_to_hex(revs->commits->item->object.sha1));
285
286         if (flags & BISECT_SHOW_ALL) {
287                 traverse_commit_list(revs, show_commit, show_object, info);
288                 printf("------\n");
289         }
290
291         if (flags & BISECT_SHOW_TRIED)
292                 show_tried_revs(tried);
293
294         print_var_str("bisect_rev", hex);
295         print_var_int("bisect_nr", cnt - 1);
296         print_var_int("bisect_good", all - reaches - 1);
297         print_var_int("bisect_bad", reaches - 1);
298         print_var_int("bisect_all", all);
299         print_var_int("bisect_steps", estimate_bisect_steps(all));
300
301         return 0;
302 }
303
304 int cmd_rev_list(int argc, const char **argv, const char *prefix)
305 {
306         struct rev_info revs;
307         struct rev_list_info info;
308         int i;
309         int bisect_list = 0;
310         int bisect_show_vars = 0;
311         int bisect_find_all = 0;
312         int quiet = 0;
313
314         git_config(git_default_config, NULL);
315         init_revisions(&revs, prefix);
316         revs.abbrev = 0;
317         revs.commit_format = CMIT_FMT_UNSPECIFIED;
318         argc = setup_revisions(argc, argv, &revs, NULL);
319
320         memset(&info, 0, sizeof(info));
321         info.revs = &revs;
322         if (revs.bisect)
323                 bisect_list = 1;
324
325         quiet = DIFF_OPT_TST(&revs.diffopt, QUICK);
326         for (i = 1 ; i < argc; i++) {
327                 const char *arg = argv[i];
328
329                 if (!strcmp(arg, "--header")) {
330                         revs.verbose_header = 1;
331                         continue;
332                 }
333                 if (!strcmp(arg, "--timestamp")) {
334                         info.show_timestamp = 1;
335                         continue;
336                 }
337                 if (!strcmp(arg, "--bisect")) {
338                         bisect_list = 1;
339                         continue;
340                 }
341                 if (!strcmp(arg, "--bisect-all")) {
342                         bisect_list = 1;
343                         bisect_find_all = 1;
344                         info.bisect_show_flags = BISECT_SHOW_ALL;
345                         revs.show_decorations = 1;
346                         continue;
347                 }
348                 if (!strcmp(arg, "--bisect-vars")) {
349                         bisect_list = 1;
350                         bisect_show_vars = 1;
351                         continue;
352                 }
353                 usage(rev_list_usage);
354
355         }
356         if (revs.commit_format != CMIT_FMT_UNSPECIFIED) {
357                 /* The command line has a --pretty  */
358                 info.hdr_termination = '\n';
359                 if (revs.commit_format == CMIT_FMT_ONELINE)
360                         info.header_prefix = "";
361                 else
362                         info.header_prefix = "commit ";
363         }
364         else if (revs.verbose_header)
365                 /* Only --header was specified */
366                 revs.commit_format = CMIT_FMT_RAW;
367
368         if ((!revs.commits &&
369              (!(revs.tag_objects||revs.tree_objects||revs.blob_objects) &&
370               !revs.pending.nr)) ||
371             revs.diff)
372                 usage(rev_list_usage);
373
374         save_commit_buffer = revs.verbose_header ||
375                 revs.grep_filter.pattern_list;
376         if (bisect_list)
377                 revs.limited = 1;
378
379         if (prepare_revision_walk(&revs))
380                 die("revision walk setup failed");
381         if (revs.tree_objects)
382                 mark_edges_uninteresting(revs.commits, &revs, show_edge);
383
384         if (bisect_list) {
385                 int reaches = reaches, all = all;
386
387                 revs.commits = find_bisection(revs.commits, &reaches, &all,
388                                               bisect_find_all);
389
390                 if (bisect_show_vars)
391                         return show_bisect_vars(&info, reaches, all);
392         }
393
394         traverse_commit_list(&revs,
395                              quiet ? finish_commit : show_commit,
396                              quiet ? finish_object : show_object,
397                              &info);
398
399         return 0;
400 }