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