]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/rev-list.c
Git 1.7.5
[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"
5fb61b8d 6#include "builtin.h"
50e62a8e 7#include "log-tree.h"
7fefda5c 8#include "graph.h"
a2ad79ce 9#include "bisect.h"
8906300f 10
a6f68d47 11static const char rev_list_usage[] =
1b1dd23f 12"git rev-list [OPTION] <commit-id>... [ -- paths... ]\n"
69e0c256 13" limiting output:\n"
62b4698e
ŠN
14" --max-count=<n>\n"
15" --max-age=<epoch>\n"
16" --min-age=<epoch>\n"
69e0c256
JH
17" --sparse\n"
18" --no-merges\n"
ad5aeede
MG
19" --min-parents=<n>\n"
20" --no-min-parents\n"
21" --max-parents=<n>\n"
22" --no-max-parents\n"
93b74bca 23" --remove-empty\n"
69e0c256 24" --all\n"
a5aa930d
UKK
25" --branches\n"
26" --tags\n"
27" --remotes\n"
42cabc34 28" --stdin\n"
27350891 29" --quiet\n"
69e0c256 30" ordering output:\n"
69e0c256 31" --topo-order\n"
4c8725f1 32" --date-order\n"
7ccd3667 33" --reverse\n"
69e0c256
JH
34" formatting output:\n"
35" --parents\n"
72276a3e 36" --children\n"
c6496575 37" --objects | --objects-edge\n"
69e0c256
JH
38" --unpacked\n"
39" --header | --pretty\n"
62b4698e 40" --abbrev=<n> | --no-abbrev\n"
5c51c985 41" --abbrev-commit\n"
b24bace5 42" --left-right\n"
69e0c256 43" special purpose:\n"
457f08a0 44" --bisect\n"
50e62a8e
CC
45" --bisect-vars\n"
46" --bisect-all"
69e0c256 47;
a6f68d47 48
11c211fa
CC
49static void finish_commit(struct commit *commit, void *data);
50static void show_commit(struct commit *commit, void *data)
81f2bb1f 51{
d797257e
CC
52 struct rev_list_info *info = data;
53 struct rev_info *revs = info->revs;
11c211fa
CC
54
55 graph_show_commit(revs->graph);
7fefda5c 56
f69c5018
TR
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
d797257e 66 if (info->show_timestamp)
dc68c4ff 67 printf("%lu ", commit->date);
d797257e
CC
68 if (info->header_prefix)
69 fputs(info->header_prefix, stdout);
7528f27d 70
1df2d656
MG
71 if (!revs->graph)
72 fputs(get_revision_mark(revs, commit), stdout);
11c211fa
CC
73 if (revs->abbrev_commit && revs->abbrev)
74 fputs(find_unique_abbrev(commit->object.sha1, revs->abbrev),
7594c4b2 75 stdout);
5c51c985
JH
76 else
77 fputs(sha1_to_hex(commit->object.sha1), stdout);
11c211fa 78 if (revs->print_parents) {
81f2bb1f
LT
79 struct commit_list *parents = commit->parents;
80 while (parents) {
1ed84157 81 printf(" %s", sha1_to_hex(parents->item->object.sha1));
81f2bb1f
LT
82 parents = parents->next;
83 }
84 }
11c211fa 85 if (revs->children.name) {
72276a3e
JH
86 struct commit_list *children;
87
11c211fa 88 children = lookup_decoration(&revs->children, &commit->object);
72276a3e
JH
89 while (children) {
90 printf(" %s", sha1_to_hex(children->item->object.sha1));
91 children = children->next;
92 }
93 }
11c211fa
CC
94 show_decorations(revs, commit);
95 if (revs->commit_format == CMIT_FMT_ONELINE)
d87449c5
JH
96 putchar(' ');
97 else
98 putchar('\n');
99
11c211fa 100 if (revs->verbose_header && commit->buffer) {
f285a2d7 101 struct strbuf buf = STRBUF_INIT;
dd2e794a
TR
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);
11c211fa 106 if (revs->graph) {
7fefda5c 107 if (buf.len) {
11c211fa
CC
108 if (revs->commit_format != CMIT_FMT_ONELINE)
109 graph_show_oneline(revs->graph);
7fefda5c 110
11c211fa 111 graph_show_commit_msg(revs->graph, &buf);
7fefda5c
AS
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')
11c211fa 129 graph_show_padding(revs->graph);
7fefda5c
AS
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 */
11c211fa 137 if (graph_show_remainder(revs->graph))
7fefda5c 138 putchar('\n');
1fb5fdd2
EFL
139 if (revs->commit_format == CMIT_FMT_ONELINE)
140 putchar('\n');
7fefda5c
AS
141 }
142 } else {
1fb5fdd2 143 if (revs->commit_format != CMIT_FMT_USERFORMAT ||
9130ac9f
JK
144 buf.len) {
145 fwrite(buf.buf, 1, buf.len, stdout);
146 putchar(info->hdr_termination);
147 }
7fefda5c 148 }
674d1727 149 strbuf_release(&buf);
7fefda5c 150 } else {
11c211fa 151 if (graph_show_remainder(revs->graph))
7fefda5c 152 putchar('\n');
7620d39f 153 }
06f59e9f 154 maybe_flush_or_die(stdout, "stdout");
11c211fa 155 finish_commit(commit, data);
27350891
SP
156}
157
11c211fa 158static void finish_commit(struct commit *commit, void *data)
27350891 159{
cb115748
LT
160 if (commit->parents) {
161 free_commit_list(commit->parents);
162 commit->parents = NULL;
163 }
4cac42b1
JH
164 free(commit->buffer);
165 commit->buffer = NULL;
a3437b8c
JS
166}
167
cf2ab916 168static void finish_object(struct object *obj, const struct name_path *path, const char *name)
27350891 169{
8d2dfc49
LT
170 if (obj->type == OBJ_BLOB && !has_sha1_file(obj->sha1))
171 die("missing blob object '%s'", sha1_to_hex(obj->sha1));
27350891
SP
172}
173
cf2ab916 174static void show_object(struct object *obj, const struct name_path *path, const char *component)
9de48752 175{
cf2ab916 176 char *name = path_name(path, component);
c64ed70d 177 /* An object with name "foo\n0000000..." can be used to
f18d244a 178 * confuse downstream "git pack-objects" very badly.
c64ed70d 179 */
8d2dfc49 180 const char *ep = strchr(name, '\n');
b9849a1a 181
cf2ab916 182 finish_object(obj, path, name);
c64ed70d 183 if (ep) {
8d2dfc49
LT
184 printf("%s %.*s\n", sha1_to_hex(obj->sha1),
185 (int) (ep - name),
186 name);
9de48752 187 }
c64ed70d 188 else
8d2dfc49 189 printf("%s %s\n", sha1_to_hex(obj->sha1), name);
cf2ab916 190 free(name);
9de48752
LT
191}
192
8d1d8f83
JH
193static void show_edge(struct commit *commit)
194{
195 printf("-%s\n", sha1_to_hex(commit->object.sha1));
196}
197
9f199b15
CC
198static 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
208static 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 */
1c876546 223int estimate_bisect_steps(int all)
9f199b15
CC
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
280e65cb
CC
237void 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
38ef7507 247static void show_tried_revs(struct commit_list *tried)
95188648
CC
248{
249 printf("bisect_tried='");
280e65cb 250 print_commit_list(tried, "%s|", "%s");
38ef7507 251 printf("'\n");
95188648
CC
252}
253
38ef7507 254static void print_var_str(const char *var, const char *val)
280e65cb 255{
38ef7507 256 printf("%s='%s'\n", var, val);
280e65cb
CC
257}
258
38ef7507 259static void print_var_int(const char *var, int val)
280e65cb 260{
38ef7507 261 printf("%s=%d\n", var, val);
280e65cb
CC
262}
263
f1c92c63 264static int show_bisect_vars(struct rev_list_info *info, int reaches, int all)
9996983c 265{
38ef7507 266 int cnt, flags = info->bisect_show_flags;
280e65cb 267 char hex[41] = "";
95188648 268 struct commit_list *tried;
d797257e 269 struct rev_info *revs = info->revs;
9996983c 270
37c4c38d 271 if (!revs->commits && !(flags & BISECT_SHOW_TRIED))
9996983c
CC
272 return 1;
273
9af3589e
CC
274 revs->commits = filter_skipped(revs->commits, &tried,
275 flags & BISECT_SHOW_ALL,
276 NULL, NULL);
95188648 277
9996983c 278 /*
7428d754 279 * revs->commits can reach "reaches" commits among
9996983c
CC
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;
6a17fad7 290
95188648
CC
291 if (revs->commits)
292 strcpy(hex, sha1_to_hex(revs->commits->item->object.sha1));
9996983c 293
37c4c38d 294 if (flags & BISECT_SHOW_ALL) {
d797257e 295 traverse_commit_list(revs, show_commit, show_object, info);
9996983c
CC
296 printf("------\n");
297 }
298
37c4c38d 299 if (flags & BISECT_SHOW_TRIED)
38ef7507
CC
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));
9996983c
CC
308
309 return 0;
310}
311
a633fca0 312int cmd_rev_list(int argc, const char **argv, const char *prefix)
64745109 313{
11c211fa 314 struct rev_info revs;
d797257e 315 struct rev_list_info info;
d9a83684 316 int i;
ff62d732 317 int bisect_list = 0;
457f08a0 318 int bisect_show_vars = 0;
50e62a8e 319 int bisect_find_all = 0;
27350891 320 int quiet = 0;
64745109 321
ef90d6d4 322 git_config(git_default_config, NULL);
a633fca0 323 init_revisions(&revs, prefix);
7337b138 324 revs.abbrev = DEFAULT_ABBREV;
7594c4b2 325 revs.commit_format = CMIT_FMT_UNSPECIFIED;
a4a88b2b 326 argc = setup_revisions(argc, argv, &revs, NULL);
ae563542 327
d797257e
CC
328 memset(&info, 0, sizeof(info));
329 info.revs = &revs;
ad3f9a71
LT
330 if (revs.bisect)
331 bisect_list = 1;
d797257e 332
90b19941 333 quiet = DIFF_OPT_TST(&revs.diffopt, QUICK);
fcfda02b 334 for (i = 1 ; i < argc; i++) {
cf484544 335 const char *arg = argv[i];
fcfda02b 336
a6f68d47 337 if (!strcmp(arg, "--header")) {
7594c4b2 338 revs.verbose_header = 1;
9d97aa64
LT
339 continue;
340 }
dc68c4ff 341 if (!strcmp(arg, "--timestamp")) {
d797257e 342 info.show_timestamp = 1;
dc68c4ff
JH
343 continue;
344 }
8b3a1e05
LT
345 if (!strcmp(arg, "--bisect")) {
346 bisect_list = 1;
347 continue;
348 }
50e62a8e
CC
349 if (!strcmp(arg, "--bisect-all")) {
350 bisect_list = 1;
351 bisect_find_all = 1;
13858e57 352 info.bisect_show_flags = BISECT_SHOW_ALL;
6e46cc0d 353 revs.show_decorations = 1;
50e62a8e
CC
354 continue;
355 }
457f08a0
JH
356 if (!strcmp(arg, "--bisect-vars")) {
357 bisect_list = 1;
358 bisect_show_vars = 1;
359 continue;
360 }
ae563542 361 usage(rev_list_usage);
a6f68d47 362
fcfda02b 363 }
7594c4b2
JH
364 if (revs.commit_format != CMIT_FMT_UNSPECIFIED) {
365 /* The command line has a --pretty */
d797257e 366 info.hdr_termination = '\n';
7594c4b2 367 if (revs.commit_format == CMIT_FMT_ONELINE)
d797257e 368 info.header_prefix = "";
7594c4b2 369 else
d797257e 370 info.header_prefix = "commit ";
7594c4b2 371 }
db89665f
JH
372 else if (revs.verbose_header)
373 /* Only --header was specified */
374 revs.commit_format = CMIT_FMT_RAW;
fcfda02b 375
d797257e 376 if ((!revs.commits &&
8c1f0b44 377 (!(revs.tag_objects||revs.tree_objects||revs.blob_objects) &&
1f1e895f 378 !revs.pending.nr)) ||
8c1f0b44 379 revs.diff)
7b34c2fa
LT
380 usage(rev_list_usage);
381
80235ba7
JH
382 save_commit_buffer = (revs.verbose_header ||
383 revs.grep_filter.pattern_list ||
384 revs.grep_filter.header_list);
4e1dc640
JH
385 if (bisect_list)
386 revs.limited = 1;
9181ca2c 387
3d51e1b5
MK
388 if (prepare_revision_walk(&revs))
389 die("revision walk setup failed");
a4a88b2b 390 if (revs.tree_objects)
8d1d8f83 391 mark_edges_uninteresting(revs.commits, &revs, show_edge);
a4a88b2b 392
457f08a0
JH
393 if (bisect_list) {
394 int reaches = reaches, all = all;
395
50e62a8e
CC
396 revs.commits = find_bisection(revs.commits, &reaches, &all,
397 bisect_find_all);
95188648 398
9996983c 399 if (bisect_show_vars)
13858e57 400 return show_bisect_vars(&info, reaches, all);
457f08a0 401 }
7b34c2fa 402
27350891 403 traverse_commit_list(&revs,
11c211fa
CC
404 quiet ? finish_commit : show_commit,
405 quiet ? finish_object : show_object,
d797257e 406 &info);
8906300f 407
f69c5018
TR
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
64745109
LT
415 return 0;
416}