git-rev-list: add --bisect-vars option.
[git/gitweb.git] / builtin-rev-list.c
blob723e4d419c64d3782b3612a1d1a7975e1f006146
1 #include "cache.h"
2 #include "refs.h"
3 #include "tag.h"
4 #include "commit.h"
5 #include "tree.h"
6 #include "blob.h"
7 #include "tree-walk.h"
8 #include "diff.h"
9 #include "revision.h"
10 #include "list-objects.h"
11 #include "builtin.h"
13 /* bits #0-15 in revision.h */
15 #define COUNTED (1u<<16)
17 static const char rev_list_usage[] =
18 "git-rev-list [OPTION] <commit-id>... [ -- paths... ]\n"
19 " limiting output:\n"
20 " --max-count=nr\n"
21 " --max-age=epoch\n"
22 " --min-age=epoch\n"
23 " --sparse\n"
24 " --no-merges\n"
25 " --remove-empty\n"
26 " --all\n"
27 " --stdin\n"
28 " ordering output:\n"
29 " --topo-order\n"
30 " --date-order\n"
31 " formatting output:\n"
32 " --parents\n"
33 " --objects | --objects-edge\n"
34 " --unpacked\n"
35 " --header | --pretty\n"
36 " --abbrev=nr | --no-abbrev\n"
37 " --abbrev-commit\n"
38 " special purpose:\n"
39 " --bisect\n"
40 " --bisect-vars"
43 static struct rev_info revs;
45 static int bisect_list;
46 static int show_timestamp;
47 static int hdr_termination;
48 static const char *header_prefix;
50 static void show_commit(struct commit *commit)
52 if (show_timestamp)
53 printf("%lu ", commit->date);
54 if (header_prefix)
55 fputs(header_prefix, stdout);
56 if (commit->object.flags & BOUNDARY)
57 putchar('-');
58 else if (revs.left_right) {
59 if (commit->object.flags & SYMMETRIC_LEFT)
60 putchar('<');
61 else
62 putchar('>');
64 if (revs.abbrev_commit && revs.abbrev)
65 fputs(find_unique_abbrev(commit->object.sha1, revs.abbrev),
66 stdout);
67 else
68 fputs(sha1_to_hex(commit->object.sha1), stdout);
69 if (revs.parents) {
70 struct commit_list *parents = commit->parents;
71 while (parents) {
72 struct object *o = &(parents->item->object);
73 parents = parents->next;
74 if (o->flags & TMP_MARK)
75 continue;
76 printf(" %s", sha1_to_hex(o->sha1));
77 o->flags |= TMP_MARK;
79 /* TMP_MARK is a general purpose flag that can
80 * be used locally, but the user should clean
81 * things up after it is done with them.
83 for (parents = commit->parents;
84 parents;
85 parents = parents->next)
86 parents->item->object.flags &= ~TMP_MARK;
88 if (revs.commit_format == CMIT_FMT_ONELINE)
89 putchar(' ');
90 else
91 putchar('\n');
93 if (revs.verbose_header) {
94 static char pretty_header[16384];
95 pretty_print_commit(revs.commit_format, commit, ~0,
96 pretty_header, sizeof(pretty_header),
97 revs.abbrev, NULL, NULL, revs.relative_date);
98 printf("%s%c", pretty_header, hdr_termination);
100 fflush(stdout);
101 if (commit->parents) {
102 free_commit_list(commit->parents);
103 commit->parents = NULL;
105 free(commit->buffer);
106 commit->buffer = NULL;
109 static void show_object(struct object_array_entry *p)
111 /* An object with name "foo\n0000000..." can be used to
112 * confuse downstream git-pack-objects very badly.
114 const char *ep = strchr(p->name, '\n');
115 if (ep) {
116 printf("%s %.*s\n", sha1_to_hex(p->item->sha1),
117 (int) (ep - p->name),
118 p->name);
120 else
121 printf("%s %s\n", sha1_to_hex(p->item->sha1), p->name);
124 static void show_edge(struct commit *commit)
126 printf("-%s\n", sha1_to_hex(commit->object.sha1));
130 * This is a truly stupid algorithm, but it's only
131 * used for bisection, and we just don't care enough.
133 * We care just barely enough to avoid recursing for
134 * non-merge entries.
136 static int count_distance(struct commit_list *entry)
138 int nr = 0;
140 while (entry) {
141 struct commit *commit = entry->item;
142 struct commit_list *p;
144 if (commit->object.flags & (UNINTERESTING | COUNTED))
145 break;
146 if (!revs.prune_fn || (commit->object.flags & TREECHANGE))
147 nr++;
148 commit->object.flags |= COUNTED;
149 p = commit->parents;
150 entry = p;
151 if (p) {
152 p = p->next;
153 while (p) {
154 nr += count_distance(p);
155 p = p->next;
160 return nr;
163 static void clear_distance(struct commit_list *list)
165 while (list) {
166 struct commit *commit = list->item;
167 commit->object.flags &= ~COUNTED;
168 list = list->next;
172 static struct commit_list *find_bisection(struct commit_list *list,
173 int *reaches, int *all)
175 int nr, closest;
176 struct commit_list *p, *best;
178 nr = 0;
179 p = list;
180 while (p) {
181 if (!revs.prune_fn || (p->item->object.flags & TREECHANGE))
182 nr++;
183 p = p->next;
185 *all = nr;
186 closest = 0;
187 best = list;
189 for (p = list; p; p = p->next) {
190 int distance, reach;
192 if (revs.prune_fn && !(p->item->object.flags & TREECHANGE))
193 continue;
195 distance = reach = count_distance(p);
196 clear_distance(list);
197 if (nr - distance < distance)
198 distance = nr - distance;
199 if (distance > closest) {
200 best = p;
201 *reaches = reach;
202 closest = distance;
205 if (best)
206 best->next = NULL;
207 return best;
210 static void read_revisions_from_stdin(struct rev_info *revs)
212 char line[1000];
214 while (fgets(line, sizeof(line), stdin) != NULL) {
215 int len = strlen(line);
216 if (line[len - 1] == '\n')
217 line[--len] = 0;
218 if (!len)
219 break;
220 if (line[0] == '-')
221 die("options not supported in --stdin mode");
222 if (handle_revision_arg(line, revs, 0, 1))
223 die("bad revision '%s'", line);
227 int cmd_rev_list(int argc, const char **argv, const char *prefix)
229 struct commit_list *list;
230 int i;
231 int read_from_stdin = 0;
232 int bisect_show_vars = 0;
234 git_config(git_default_config);
235 init_revisions(&revs, prefix);
236 revs.abbrev = 0;
237 revs.commit_format = CMIT_FMT_UNSPECIFIED;
238 argc = setup_revisions(argc, argv, &revs, NULL);
240 for (i = 1 ; i < argc; i++) {
241 const char *arg = argv[i];
243 if (!strcmp(arg, "--header")) {
244 revs.verbose_header = 1;
245 continue;
247 if (!strcmp(arg, "--timestamp")) {
248 show_timestamp = 1;
249 continue;
251 if (!strcmp(arg, "--bisect")) {
252 bisect_list = 1;
253 continue;
255 if (!strcmp(arg, "--bisect-vars")) {
256 bisect_list = 1;
257 bisect_show_vars = 1;
258 continue;
260 if (!strcmp(arg, "--stdin")) {
261 if (read_from_stdin++)
262 die("--stdin given twice?");
263 read_revisions_from_stdin(&revs);
264 continue;
266 usage(rev_list_usage);
269 if (revs.commit_format != CMIT_FMT_UNSPECIFIED) {
270 /* The command line has a --pretty */
271 hdr_termination = '\n';
272 if (revs.commit_format == CMIT_FMT_ONELINE)
273 header_prefix = "";
274 else
275 header_prefix = "commit ";
277 else if (revs.verbose_header)
278 /* Only --header was specified */
279 revs.commit_format = CMIT_FMT_RAW;
281 list = revs.commits;
283 if ((!list &&
284 (!(revs.tag_objects||revs.tree_objects||revs.blob_objects) &&
285 !revs.pending.nr)) ||
286 revs.diff)
287 usage(rev_list_usage);
289 save_commit_buffer = revs.verbose_header || revs.grep_filter;
290 track_object_refs = 0;
291 if (bisect_list)
292 revs.limited = 1;
294 prepare_revision_walk(&revs);
295 if (revs.tree_objects)
296 mark_edges_uninteresting(revs.commits, &revs, show_edge);
298 if (bisect_list) {
299 int reaches = reaches, all = all;
301 revs.commits = find_bisection(revs.commits,
302 &reaches, &all);
303 if (bisect_show_vars) {
304 int cnt;
305 if (!revs.commits)
306 return 1;
308 * revs.commits can reach "reaches" commits among
309 * "all" commits. If it is good, then there are
310 * (all-reaches) commits left to be bisected.
311 * On the other hand, if it is bad, then the set
312 * to bisect is "reaches".
313 * A bisect set of size N has (N-1) commits further
314 * to test, as we already know one bad one.
316 cnt = all-reaches;
317 if (cnt < reaches)
318 cnt = reaches;
319 printf("bisect_rev=%s\n"
320 "bisect_nr=%d\n"
321 "bisect_good=%d\n"
322 "bisect_bad=%d\n"
323 "bisect_all=%d\n",
324 sha1_to_hex(revs.commits->item->object.sha1),
325 cnt - 1,
326 all - reaches - 1,
327 reaches - 1,
328 all);
329 return 0;
333 traverse_commit_list(&revs, show_commit, show_object);
335 return 0;