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