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"
44 static struct rev_info revs
;
46 static int bisect_list
;
47 static int show_timestamp
;
48 static int hdr_termination
;
49 static const char *header_prefix
;
51 static void show_commit(struct commit
*commit
)
54 printf("%lu ", commit
->date
);
56 fputs(header_prefix
, stdout
);
57 if (commit
->object
.flags
& BOUNDARY
)
59 else if (revs
.left_right
) {
60 if (commit
->object
.flags
& SYMMETRIC_LEFT
)
65 if (revs
.abbrev_commit
&& revs
.abbrev
)
66 fputs(find_unique_abbrev(commit
->object
.sha1
, revs
.abbrev
),
69 fputs(sha1_to_hex(commit
->object
.sha1
), stdout
);
71 struct commit_list
*parents
= commit
->parents
;
73 printf(" %s", sha1_to_hex(parents
->item
->object
.sha1
));
74 parents
= parents
->next
;
77 if (revs
.commit_format
== CMIT_FMT_ONELINE
)
82 if (revs
.verbose_header
) {
84 unsigned long buflen
= 0;
85 pretty_print_commit(revs
.commit_format
, commit
, ~0,
87 revs
.abbrev
, NULL
, NULL
, revs
.date_mode
);
88 printf("%s%c", buf
, hdr_termination
);
91 maybe_flush_or_die(stdout
, "stdout");
92 if (commit
->parents
) {
93 free_commit_list(commit
->parents
);
94 commit
->parents
= NULL
;
97 commit
->buffer
= NULL
;
100 static void show_object(struct object_array_entry
*p
)
102 /* An object with name "foo\n0000000..." can be used to
103 * confuse downstream git-pack-objects very badly.
105 const char *ep
= strchr(p
->name
, '\n');
107 if (p
->item
->type
== OBJ_BLOB
&& !has_sha1_file(p
->item
->sha1
))
108 die("missing blob object '%s'", sha1_to_hex(p
->item
->sha1
));
111 printf("%s %.*s\n", sha1_to_hex(p
->item
->sha1
),
112 (int) (ep
- p
->name
),
116 printf("%s %s\n", sha1_to_hex(p
->item
->sha1
), p
->name
);
119 static void show_edge(struct commit
*commit
)
121 printf("-%s\n", sha1_to_hex(commit
->object
.sha1
));
125 * This is a truly stupid algorithm, but it's only
126 * used for bisection, and we just don't care enough.
128 * We care just barely enough to avoid recursing for
131 static int count_distance(struct commit_list
*entry
)
136 struct commit
*commit
= entry
->item
;
137 struct commit_list
*p
;
139 if (commit
->object
.flags
& (UNINTERESTING
| COUNTED
))
141 if (!revs
.prune_fn
|| (commit
->object
.flags
& TREECHANGE
))
143 commit
->object
.flags
|= COUNTED
;
149 nr
+= count_distance(p
);
158 static void clear_distance(struct commit_list
*list
)
161 struct commit
*commit
= list
->item
;
162 commit
->object
.flags
&= ~COUNTED
;
167 #define DEBUG_BISECT 0
169 static inline int weight(struct commit_list
*elem
)
171 return *((int*)(elem
->item
->util
));
174 static inline void weight_set(struct commit_list
*elem
, int weight
)
176 *((int*)(elem
->item
->util
)) = weight
;
179 static int count_interesting_parents(struct commit
*commit
)
181 struct commit_list
*p
;
184 for (count
= 0, p
= commit
->parents
; p
; p
= p
->next
) {
185 if (p
->item
->object
.flags
& UNINTERESTING
)
192 static inline int halfway(struct commit_list
*p
, int nr
)
195 * Don't short-cut something we are not going to return!
197 if (revs
.prune_fn
&& !(p
->item
->object
.flags
& TREECHANGE
))
202 * 2 and 3 are halfway of 5.
203 * 3 is halfway of 6 but 2 and 4 are not.
205 switch (2 * weight(p
) - nr
) {
206 case -1: case 0: case 1:
214 #define show_list(a,b,c,d) do { ; } while (0)
216 static void show_list(const char *debug
, int counted
, int nr
,
217 struct commit_list
*list
)
219 struct commit_list
*p
;
221 fprintf(stderr
, "%s (%d/%d)\n", debug
, counted
, nr
);
223 for (p
= list
; p
; p
= p
->next
) {
224 struct commit_list
*pp
;
225 struct commit
*commit
= p
->item
;
226 unsigned flags
= commit
->object
.flags
;
227 enum object_type type
;
229 char *buf
= read_sha1_file(commit
->object
.sha1
, &type
, &size
);
232 fprintf(stderr
, "%c%c%c ",
233 (flags
& TREECHANGE
) ? 'T' : ' ',
234 (flags
& UNINTERESTING
) ? 'U' : ' ',
235 (flags
& COUNTED
) ? 'C' : ' ');
237 fprintf(stderr
, "%3d", weight(p
));
239 fprintf(stderr
, "---");
240 fprintf(stderr
, " %.*s", 8, sha1_to_hex(commit
->object
.sha1
));
241 for (pp
= commit
->parents
; pp
; pp
= pp
->next
)
242 fprintf(stderr
, " %.*s", 8,
243 sha1_to_hex(pp
->item
->object
.sha1
));
245 sp
= strstr(buf
, "\n\n");
248 for (ep
= sp
; *ep
&& *ep
!= '\n'; ep
++)
250 fprintf(stderr
, " %.*s", (int)(ep
- sp
), sp
);
252 fprintf(stderr
, "\n");
255 #endif /* DEBUG_BISECT */
257 static struct commit_list
*best_bisection(struct commit_list
*list
, int nr
)
259 struct commit_list
*p
, *best
;
260 int best_distance
= -1;
263 for (p
= list
; p
; p
= p
->next
) {
265 unsigned flags
= p
->item
->object
.flags
;
267 if (revs
.prune_fn
&& !(flags
& TREECHANGE
))
269 distance
= weight(p
);
270 if (nr
- distance
< distance
)
271 distance
= nr
- distance
;
272 if (distance
> best_distance
) {
274 best_distance
= distance
;
282 * zero or positive weight is the number of interesting commits it can
283 * reach, including itself. Especially, weight = 0 means it does not
284 * reach any tree-changing commits (e.g. just above uninteresting one
285 * but traversal is with pathspec).
287 * weight = -1 means it has one parent and its distance is yet to
290 * weight = -2 means it has more than one parent and its distance is
291 * unknown. After running count_distance() first, they will get zero
292 * or positive distance.
294 static struct commit_list
*do_find_bisection(struct commit_list
*list
,
295 int nr
, int *weights
)
298 struct commit_list
*p
;
302 for (n
= 0, p
= list
; p
; p
= p
->next
) {
303 struct commit
*commit
= p
->item
;
304 unsigned flags
= commit
->object
.flags
;
306 p
->item
->util
= &weights
[n
++];
307 switch (count_interesting_parents(commit
)) {
309 if (!revs
.prune_fn
|| (flags
& TREECHANGE
)) {
312 show_list("bisection 2 count one",
316 * otherwise, it is known not to reach any
317 * tree-changing commit and gets weight 0.
329 show_list("bisection 2 initialize", counted
, nr
, list
);
332 * If you have only one parent in the resulting set
333 * then you can reach one commit more than that parent
334 * can reach. So we do not have to run the expensive
335 * count_distance() for single strand of pearls.
337 * However, if you have more than one parents, you cannot
338 * just add their distance and one for yourself, since
339 * they usually reach the same ancestor and you would
340 * end up counting them twice that way.
342 * So we will first count distance of merges the usual
343 * way, and then fill the blanks using cheaper algorithm.
345 for (p
= list
; p
; p
= p
->next
) {
346 if (p
->item
->object
.flags
& UNINTERESTING
)
350 weight_set(p
, count_distance(p
));
351 clear_distance(list
);
353 /* Does it happen to be at exactly half-way? */
359 show_list("bisection 2 count_distance", counted
, nr
, list
);
361 while (counted
< nr
) {
362 for (p
= list
; p
; p
= p
->next
) {
363 struct commit_list
*q
;
364 unsigned flags
= p
->item
->object
.flags
;
368 for (q
= p
->item
->parents
; q
; q
= q
->next
) {
369 if (q
->item
->object
.flags
& UNINTERESTING
)
378 * weight for p is unknown but q is known.
379 * add one for p itself if p is to be counted,
380 * otherwise inherit it from q directly.
382 if (!revs
.prune_fn
|| (flags
& TREECHANGE
)) {
383 weight_set(p
, weight(q
)+1);
385 show_list("bisection 2 count one",
389 weight_set(p
, weight(q
));
391 /* Does it happen to be at exactly half-way? */
397 show_list("bisection 2 counted all", counted
, nr
, list
);
399 /* Then find the best one */
400 return best_bisection(list
, nr
);
403 static struct commit_list
*find_bisection(struct commit_list
*list
,
404 int *reaches
, int *all
)
407 struct commit_list
*p
, *best
, *next
, *last
;
410 show_list("bisection 2 entry", 0, 0, list
);
413 * Count the number of total and tree-changing items on the
414 * list, while reversing the list.
416 for (nr
= on_list
= 0, last
= NULL
, p
= list
;
419 unsigned flags
= p
->item
->object
.flags
;
422 if (flags
& UNINTERESTING
)
426 if (!revs
.prune_fn
|| (flags
& TREECHANGE
))
431 show_list("bisection 2 sorted", 0, nr
, list
);
434 weights
= xcalloc(on_list
, sizeof(*weights
));
436 /* Do the real work of finding bisection commit. */
437 best
= do_find_bisection(list
, nr
, weights
);
441 *reaches
= weight(best
);
448 static void read_revisions_from_stdin(struct rev_info
*revs
)
452 while (fgets(line
, sizeof(line
), stdin
) != NULL
) {
453 int len
= strlen(line
);
454 if (line
[len
- 1] == '\n')
459 die("options not supported in --stdin mode");
460 if (handle_revision_arg(line
, revs
, 0, 1))
461 die("bad revision '%s'", line
);
465 int cmd_rev_list(int argc
, const char **argv
, const char *prefix
)
467 struct commit_list
*list
;
469 int read_from_stdin
= 0;
470 int bisect_show_vars
= 0;
472 git_config(git_default_config
);
473 init_revisions(&revs
, prefix
);
475 revs
.commit_format
= CMIT_FMT_UNSPECIFIED
;
476 argc
= setup_revisions(argc
, argv
, &revs
, NULL
);
478 for (i
= 1 ; i
< argc
; i
++) {
479 const char *arg
= argv
[i
];
481 if (!strcmp(arg
, "--header")) {
482 revs
.verbose_header
= 1;
485 if (!strcmp(arg
, "--timestamp")) {
489 if (!strcmp(arg
, "--bisect")) {
493 if (!strcmp(arg
, "--bisect-vars")) {
495 bisect_show_vars
= 1;
498 if (!strcmp(arg
, "--stdin")) {
499 if (read_from_stdin
++)
500 die("--stdin given twice?");
501 read_revisions_from_stdin(&revs
);
504 usage(rev_list_usage
);
507 if (revs
.commit_format
!= CMIT_FMT_UNSPECIFIED
) {
508 /* The command line has a --pretty */
509 hdr_termination
= '\n';
510 if (revs
.commit_format
== CMIT_FMT_ONELINE
)
513 header_prefix
= "commit ";
515 else if (revs
.verbose_header
)
516 /* Only --header was specified */
517 revs
.commit_format
= CMIT_FMT_RAW
;
522 (!(revs
.tag_objects
||revs
.tree_objects
||revs
.blob_objects
) &&
523 !revs
.pending
.nr
)) ||
525 usage(rev_list_usage
);
527 save_commit_buffer
= revs
.verbose_header
|| revs
.grep_filter
;
528 track_object_refs
= 0;
532 prepare_revision_walk(&revs
);
533 if (revs
.tree_objects
)
534 mark_edges_uninteresting(revs
.commits
, &revs
, show_edge
);
537 int reaches
= reaches
, all
= all
;
539 revs
.commits
= find_bisection(revs
.commits
, &reaches
, &all
);
540 if (bisect_show_vars
) {
545 * revs.commits can reach "reaches" commits among
546 * "all" commits. If it is good, then there are
547 * (all-reaches) commits left to be bisected.
548 * On the other hand, if it is bad, then the set
549 * to bisect is "reaches".
550 * A bisect set of size N has (N-1) commits further
551 * to test, as we already know one bad one.
556 printf("bisect_rev=%s\n"
561 sha1_to_hex(revs
.commits
->item
->object
.sha1
),
570 traverse_commit_list(&revs
, show_commit
, show_object
);