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