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