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