10 #include "list-objects.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"
31 " formatting output:\n"
33 " --objects | --objects-edge\n"
35 " --header | --pretty\n"
36 " --abbrev=nr | --no-abbrev\n"
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)
53 printf("%lu ", commit->date);
55 fputs(header_prefix, stdout);
56 if (commit->object.flags & BOUNDARY)
58 else if (revs.left_right) {
59 if (commit->object.flags & SYMMETRIC_LEFT)
64 if (revs.abbrev_commit && revs.abbrev)
65 fputs(find_unique_abbrev(commit->object.sha1, revs.abbrev),
68 fputs(sha1_to_hex(commit->object.sha1), stdout);
70 struct commit_list *parents = commit->parents;
72 struct object *o = &(parents->item->object);
73 parents = parents->next;
74 if (o->flags & TMP_MARK)
76 printf(" %s", sha1_to_hex(o->sha1));
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;
85 parents = parents->next)
86 parents->item->object.flags &= ~TMP_MARK;
88 if (revs.commit_format == CMIT_FMT_ONELINE)
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);
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');
116 printf("%s %.*s\n", sha1_to_hex(p->item->sha1),
117 (int) (ep - p->name),
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
136 static int count_distance(struct commit_list *entry)
141 struct commit *commit = entry->item;
142 struct commit_list *p;
144 if (commit->object.flags & (UNINTERESTING | COUNTED))
146 if (!revs.prune_fn || (commit->object.flags & TREECHANGE))
148 commit->object.flags |= COUNTED;
154 nr += count_distance(p);
163 static void clear_distance(struct commit_list *list)
166 struct commit *commit = list->item;
167 commit->object.flags &= ~COUNTED;
172 static struct commit_list *find_bisection(struct commit_list *list,
173 int *reaches, int *all)
176 struct commit_list *p, *best;
181 if (!revs.prune_fn || (p->item->object.flags & TREECHANGE))
189 for (p = list; p; p = p->next) {
192 if (revs.prune_fn && !(p->item->object.flags & TREECHANGE))
195 distance = reach = count_distance(p);
196 clear_distance(list);
197 if (nr - distance < distance)
198 distance = nr - distance;
199 if (distance > closest) {
210 static inline int commit_interesting(struct commit_list *elem)
212 unsigned flags = elem->item->object.flags;
213 if (flags & UNINTERESTING)
215 return (!revs.prune_fn || (flags & TREECHANGE));
218 static inline int weight(struct commit_list *elem)
220 return *((int*)(elem->item->util));
223 static inline void weight_set(struct commit_list *elem, int weight)
225 *((int*)(elem->item->util)) = weight;
228 static int count_interesting_parents(struct commit_list *elem)
231 if (!elem->item->parents)
233 for (elem = elem->item->parents; elem; elem = elem->next) {
234 if (commit_interesting(elem))
240 static inline int halfway(struct commit_list *p, int distance, int nr)
243 * Don't short-cut something we are not going to return!
245 if (revs.prune_fn && !(p->item->object.flags & TREECHANGE))
249 * 2 and 3 are halfway of 5.
250 * 3 is halfway of 6 but 2 and 4 are not.
253 switch (distance - nr) {
254 case -1: case 0: case 1:
261 static struct commit_list *find_bisection_2(struct commit_list *list,
262 int *reaches, int *all)
264 int n, nr, counted, distance;
265 struct commit_list *p, *best;
268 for (nr = 0, p = list; p; p = p->next) {
269 if (commit_interesting(p))
273 weights = xcalloc(nr, sizeof(int*));
276 for (n = 0, p = list; p; p = p->next) {
277 if (!commit_interesting(p))
279 if (commit_interesting(p)) {
281 * positive weight is the number of interesting
282 * commits it can reach, including itself.
283 * weight = 0 means it has one parent and
284 * its distance is unknown.
285 * weight < 0 means it has more than one
286 * parent and its distance is unknown.
288 p->item->util = &weights[n++];
289 switch (count_interesting_parents(p)) {
305 * If you have only one parent in the resulting set
306 * then you can reach one commit more than that parent
307 * can reach. So we do not have to run the expensive
308 * count_distance() for single strand of pearls.
310 * However, if you have more than one parents, you cannot
311 * just add their distance and one for yourself, since
312 * they usually reach the same ancestor and you would
313 * end up counting them twice that way.
315 * So we will first count distance of merges the usual
316 * way, and then fill the blanks using cheaper algorithm.
318 for (p = list; p; p = p->next) {
319 if (!commit_interesting(p))
324 distance = count_distance(p);
326 weight_set(p, distance);
328 /* Does it happen to be at exactly half-way? */
329 if (halfway(p, distance, nr)) {
338 while (counted < nr) {
339 for (p = list; p; p = p->next) {
340 struct commit_list *q;
342 if (!commit_interesting(p) || 0 < weight(p))
344 for (q = p->item->parents; q; q = q->next)
345 if (commit_interesting(q) && 0 < weight(q))
349 weight_set(p, weight(q)+1);
352 /* Does it happen to be at exactly half-way? */
353 distance = weight(p);
354 if (halfway(p, distance, nr)) {
363 /* Then find the best one */
366 for (p = list; p; p = p->next) {
367 if (!commit_interesting(p))
369 distance = weight(p);
370 if (nr - distance < distance)
371 distance = nr - distance;
372 if (distance > counted) {
375 *reaches = weight(p);
384 static void read_revisions_from_stdin(struct rev_info *revs)
388 while (fgets(line, sizeof(line), stdin) != NULL) {
389 int len = strlen(line);
390 if (line[len - 1] == '\n')
395 die("options not supported in --stdin mode");
396 if (handle_revision_arg(line, revs, 0, 1))
397 die("bad revision '%s'", line);
401 int cmd_rev_list(int argc, const char **argv, const char *prefix)
403 struct commit_list *list;
405 int read_from_stdin = 0;
406 int bisect_show_vars = 0;
408 git_config(git_default_config);
409 init_revisions(&revs, prefix);
411 revs.commit_format = CMIT_FMT_UNSPECIFIED;
412 argc = setup_revisions(argc, argv, &revs, NULL);
414 for (i = 1 ; i < argc; i++) {
415 const char *arg = argv[i];
417 if (!strcmp(arg, "--header")) {
418 revs.verbose_header = 1;
421 if (!strcmp(arg, "--timestamp")) {
425 if (!strcmp(arg, "--bisect")) {
429 if (!strcmp(arg, "--bisect-vars")) {
431 bisect_show_vars = 1;
434 if (!strcmp(arg, "--stdin")) {
435 if (read_from_stdin++)
436 die("--stdin given twice?");
437 read_revisions_from_stdin(&revs);
440 usage(rev_list_usage);
443 if (revs.commit_format != CMIT_FMT_UNSPECIFIED) {
444 /* The command line has a --pretty */
445 hdr_termination = '\n';
446 if (revs.commit_format == CMIT_FMT_ONELINE)
449 header_prefix = "commit ";
451 else if (revs.verbose_header)
452 /* Only --header was specified */
453 revs.commit_format = CMIT_FMT_RAW;
458 (!(revs.tag_objects||revs.tree_objects||revs.blob_objects) &&
459 !revs.pending.nr)) ||
461 usage(rev_list_usage);
463 save_commit_buffer = revs.verbose_header || revs.grep_filter;
464 track_object_refs = 0;
468 prepare_revision_walk(&revs);
469 if (revs.tree_objects)
470 mark_edges_uninteresting(revs.commits, &revs, show_edge);
473 int reaches = reaches, all = all;
476 revs.commits = find_bisection_2(revs.commits,
479 revs.commits = find_bisection(revs.commits,
481 if (bisect_show_vars) {
486 * revs.commits can reach "reaches" commits among
487 * "all" commits. If it is good, then there are
488 * (all-reaches) commits left to be bisected.
489 * On the other hand, if it is bad, then the set
490 * to bisect is "reaches".
491 * A bisect set of size N has (N-1) commits further
492 * to test, as we already know one bad one.
497 printf("bisect_rev=%s\n"
502 sha1_to_hex(revs.commits->item->object.sha1),
511 traverse_commit_list(&revs, show_commit, show_object);