10 #include "list-objects.h"
15 /* bits #0-15 in revision.h */
17 #define COUNTED (1u<<16)
19 static const char rev_list_usage
[] =
20 "git rev-list [OPTION] <commit-id>... [ -- paths... ]\n"
38 " formatting output:\n"
41 " --objects | --objects-edge\n"
43 " --header | --pretty\n"
44 " --abbrev=nr | --no-abbrev\n"
53 static struct rev_info revs
;
55 static int bisect_list
;
56 static int show_timestamp
;
57 static int hdr_termination
;
58 static const char *header_prefix
;
60 static void finish_commit(struct commit
*commit
);
61 static void show_commit(struct commit
*commit
)
63 graph_show_commit(revs
.graph
);
66 printf("%lu ", commit
->date
);
68 fputs(header_prefix
, stdout
);
71 if (commit
->object
.flags
& BOUNDARY
)
73 else if (commit
->object
.flags
& UNINTERESTING
)
75 else if (revs
.left_right
) {
76 if (commit
->object
.flags
& SYMMETRIC_LEFT
)
82 if (revs
.abbrev_commit
&& revs
.abbrev
)
83 fputs(find_unique_abbrev(commit
->object
.sha1
, revs
.abbrev
),
86 fputs(sha1_to_hex(commit
->object
.sha1
), stdout
);
87 if (revs
.print_parents
) {
88 struct commit_list
*parents
= commit
->parents
;
90 printf(" %s", sha1_to_hex(parents
->item
->object
.sha1
));
91 parents
= parents
->next
;
94 if (revs
.children
.name
) {
95 struct commit_list
*children
;
97 children
= lookup_decoration(&revs
.children
, &commit
->object
);
99 printf(" %s", sha1_to_hex(children
->item
->object
.sha1
));
100 children
= children
->next
;
103 show_decorations(commit
);
104 if (revs
.commit_format
== CMIT_FMT_ONELINE
)
109 if (revs
.verbose_header
&& commit
->buffer
) {
111 strbuf_init(&buf
, 0);
112 pretty_print_commit(revs
.commit_format
, commit
,
113 &buf
, revs
.abbrev
, NULL
, NULL
,
117 if (revs
.commit_format
!= CMIT_FMT_ONELINE
)
118 graph_show_oneline(revs
.graph
);
120 graph_show_commit_msg(revs
.graph
, &buf
);
123 * Add a newline after the commit message.
125 * Usually, this newline produces a blank
126 * padding line between entries, in which case
127 * we need to add graph padding on this line.
129 * However, the commit message may not end in a
130 * newline. In this case the newline simply
131 * ends the last line of the commit message,
132 * and we don't need any graph output. (This
133 * always happens with CMIT_FMT_ONELINE, and it
134 * happens with CMIT_FMT_USERFORMAT when the
135 * format doesn't explicitly end in a newline.)
137 if (buf
.len
&& buf
.buf
[buf
.len
- 1] == '\n')
138 graph_show_padding(revs
.graph
);
142 * If the message buffer is empty, just show
143 * the rest of the graph output for this
146 if (graph_show_remainder(revs
.graph
))
151 printf("%s%c", buf
.buf
, hdr_termination
);
153 strbuf_release(&buf
);
155 if (graph_show_remainder(revs
.graph
))
158 maybe_flush_or_die(stdout
, "stdout");
159 finish_commit(commit
);
162 static void finish_commit(struct commit
*commit
)
164 if (commit
->parents
) {
165 free_commit_list(commit
->parents
);
166 commit
->parents
= NULL
;
168 free(commit
->buffer
);
169 commit
->buffer
= NULL
;
172 static void finish_object(struct object_array_entry
*p
)
174 if (p
->item
->type
== OBJ_BLOB
&& !has_sha1_file(p
->item
->sha1
))
175 die("missing blob object '%s'", sha1_to_hex(p
->item
->sha1
));
178 static void show_object(struct object_array_entry
*p
)
180 /* An object with name "foo\n0000000..." can be used to
181 * confuse downstream git-pack-objects very badly.
183 const char *ep
= strchr(p
->name
, '\n');
187 printf("%s %.*s\n", sha1_to_hex(p
->item
->sha1
),
188 (int) (ep
- p
->name
),
192 printf("%s %s\n", sha1_to_hex(p
->item
->sha1
), p
->name
);
195 static void show_edge(struct commit
*commit
)
197 printf("-%s\n", sha1_to_hex(commit
->object
.sha1
));
201 * This is a truly stupid algorithm, but it's only
202 * used for bisection, and we just don't care enough.
204 * We care just barely enough to avoid recursing for
207 static int count_distance(struct commit_list
*entry
)
212 struct commit
*commit
= entry
->item
;
213 struct commit_list
*p
;
215 if (commit
->object
.flags
& (UNINTERESTING
| COUNTED
))
217 if (!(commit
->object
.flags
& TREESAME
))
219 commit
->object
.flags
|= COUNTED
;
225 nr
+= count_distance(p
);
234 static void clear_distance(struct commit_list
*list
)
237 struct commit
*commit
= list
->item
;
238 commit
->object
.flags
&= ~COUNTED
;
243 #define DEBUG_BISECT 0
245 static inline int weight(struct commit_list
*elem
)
247 return *((int*)(elem
->item
->util
));
250 static inline void weight_set(struct commit_list
*elem
, int weight
)
252 *((int*)(elem
->item
->util
)) = weight
;
255 static int count_interesting_parents(struct commit
*commit
)
257 struct commit_list
*p
;
260 for (count
= 0, p
= commit
->parents
; p
; p
= p
->next
) {
261 if (p
->item
->object
.flags
& UNINTERESTING
)
268 static inline int halfway(struct commit_list
*p
, int nr
)
271 * Don't short-cut something we are not going to return!
273 if (p
->item
->object
.flags
& TREESAME
)
278 * 2 and 3 are halfway of 5.
279 * 3 is halfway of 6 but 2 and 4 are not.
281 switch (2 * weight(p
) - nr
) {
282 case -1: case 0: case 1:
290 #define show_list(a,b,c,d) do { ; } while (0)
292 static void show_list(const char *debug
, int counted
, int nr
,
293 struct commit_list
*list
)
295 struct commit_list
*p
;
297 fprintf(stderr
, "%s (%d/%d)\n", debug
, counted
, nr
);
299 for (p
= list
; p
; p
= p
->next
) {
300 struct commit_list
*pp
;
301 struct commit
*commit
= p
->item
;
302 unsigned flags
= commit
->object
.flags
;
303 enum object_type type
;
305 char *buf
= read_sha1_file(commit
->object
.sha1
, &type
, &size
);
308 fprintf(stderr
, "%c%c%c ",
309 (flags
& TREESAME
) ? ' ' : 'T',
310 (flags
& UNINTERESTING
) ? 'U' : ' ',
311 (flags
& COUNTED
) ? 'C' : ' ');
313 fprintf(stderr
, "%3d", weight(p
));
315 fprintf(stderr
, "---");
316 fprintf(stderr
, " %.*s", 8, sha1_to_hex(commit
->object
.sha1
));
317 for (pp
= commit
->parents
; pp
; pp
= pp
->next
)
318 fprintf(stderr
, " %.*s", 8,
319 sha1_to_hex(pp
->item
->object
.sha1
));
321 sp
= strstr(buf
, "\n\n");
324 for (ep
= sp
; *ep
&& *ep
!= '\n'; ep
++)
326 fprintf(stderr
, " %.*s", (int)(ep
- sp
), sp
);
328 fprintf(stderr
, "\n");
331 #endif /* DEBUG_BISECT */
333 static struct commit_list
*best_bisection(struct commit_list
*list
, int nr
)
335 struct commit_list
*p
, *best
;
336 int best_distance
= -1;
339 for (p
= list
; p
; p
= p
->next
) {
341 unsigned flags
= p
->item
->object
.flags
;
343 if (flags
& TREESAME
)
345 distance
= weight(p
);
346 if (nr
- distance
< distance
)
347 distance
= nr
- distance
;
348 if (distance
> best_distance
) {
350 best_distance
= distance
;
358 struct commit
*commit
;
362 static int compare_commit_dist(const void *a_
, const void *b_
)
364 struct commit_dist
*a
, *b
;
366 a
= (struct commit_dist
*)a_
;
367 b
= (struct commit_dist
*)b_
;
368 if (a
->distance
!= b
->distance
)
369 return b
->distance
- a
->distance
; /* desc sort */
370 return hashcmp(a
->commit
->object
.sha1
, b
->commit
->object
.sha1
);
373 static struct commit_list
*best_bisection_sorted(struct commit_list
*list
, int nr
)
375 struct commit_list
*p
;
376 struct commit_dist
*array
= xcalloc(nr
, sizeof(*array
));
379 for (p
= list
, cnt
= 0; p
; p
= p
->next
) {
381 unsigned flags
= p
->item
->object
.flags
;
383 if (flags
& TREESAME
)
385 distance
= weight(p
);
386 if (nr
- distance
< distance
)
387 distance
= nr
- distance
;
388 array
[cnt
].commit
= p
->item
;
389 array
[cnt
].distance
= distance
;
392 qsort(array
, cnt
, sizeof(*array
), compare_commit_dist
);
393 for (p
= list
, i
= 0; i
< cnt
; i
++) {
394 struct name_decoration
*r
= xmalloc(sizeof(*r
) + 100);
395 struct object
*obj
= &(array
[i
].commit
->object
);
397 sprintf(r
->name
, "dist=%d", array
[i
].distance
);
398 r
->next
= add_decoration(&name_decoration
, obj
, r
);
399 p
->item
= array
[i
].commit
;
409 * zero or positive weight is the number of interesting commits it can
410 * reach, including itself. Especially, weight = 0 means it does not
411 * reach any tree-changing commits (e.g. just above uninteresting one
412 * but traversal is with pathspec).
414 * weight = -1 means it has one parent and its distance is yet to
417 * weight = -2 means it has more than one parent and its distance is
418 * unknown. After running count_distance() first, they will get zero
419 * or positive distance.
421 static struct commit_list
*do_find_bisection(struct commit_list
*list
,
422 int nr
, int *weights
,
426 struct commit_list
*p
;
430 for (n
= 0, p
= list
; p
; p
= p
->next
) {
431 struct commit
*commit
= p
->item
;
432 unsigned flags
= commit
->object
.flags
;
434 p
->item
->util
= &weights
[n
++];
435 switch (count_interesting_parents(commit
)) {
437 if (!(flags
& TREESAME
)) {
440 show_list("bisection 2 count one",
444 * otherwise, it is known not to reach any
445 * tree-changing commit and gets weight 0.
457 show_list("bisection 2 initialize", counted
, nr
, list
);
460 * If you have only one parent in the resulting set
461 * then you can reach one commit more than that parent
462 * can reach. So we do not have to run the expensive
463 * count_distance() for single strand of pearls.
465 * However, if you have more than one parents, you cannot
466 * just add their distance and one for yourself, since
467 * they usually reach the same ancestor and you would
468 * end up counting them twice that way.
470 * So we will first count distance of merges the usual
471 * way, and then fill the blanks using cheaper algorithm.
473 for (p
= list
; p
; p
= p
->next
) {
474 if (p
->item
->object
.flags
& UNINTERESTING
)
478 weight_set(p
, count_distance(p
));
479 clear_distance(list
);
481 /* Does it happen to be at exactly half-way? */
482 if (!find_all
&& halfway(p
, nr
))
487 show_list("bisection 2 count_distance", counted
, nr
, list
);
489 while (counted
< nr
) {
490 for (p
= list
; p
; p
= p
->next
) {
491 struct commit_list
*q
;
492 unsigned flags
= p
->item
->object
.flags
;
496 for (q
= p
->item
->parents
; q
; q
= q
->next
) {
497 if (q
->item
->object
.flags
& UNINTERESTING
)
506 * weight for p is unknown but q is known.
507 * add one for p itself if p is to be counted,
508 * otherwise inherit it from q directly.
510 if (!(flags
& TREESAME
)) {
511 weight_set(p
, weight(q
)+1);
513 show_list("bisection 2 count one",
517 weight_set(p
, weight(q
));
519 /* Does it happen to be at exactly half-way? */
520 if (!find_all
&& halfway(p
, nr
))
525 show_list("bisection 2 counted all", counted
, nr
, list
);
528 return best_bisection(list
, nr
);
530 return best_bisection_sorted(list
, nr
);
533 static struct commit_list
*find_bisection(struct commit_list
*list
,
534 int *reaches
, int *all
,
538 struct commit_list
*p
, *best
, *next
, *last
;
541 show_list("bisection 2 entry", 0, 0, list
);
544 * Count the number of total and tree-changing items on the
545 * list, while reversing the list.
547 for (nr
= on_list
= 0, last
= NULL
, p
= list
;
550 unsigned flags
= p
->item
->object
.flags
;
553 if (flags
& UNINTERESTING
)
557 if (!(flags
& TREESAME
))
562 show_list("bisection 2 sorted", 0, nr
, list
);
565 weights
= xcalloc(on_list
, sizeof(*weights
));
567 /* Do the real work of finding bisection commit. */
568 best
= do_find_bisection(list
, nr
, weights
, find_all
);
572 *reaches
= weight(best
);
578 int cmd_rev_list(int argc
, const char **argv
, const char *prefix
)
580 struct commit_list
*list
;
582 int read_from_stdin
= 0;
583 int bisect_show_vars
= 0;
584 int bisect_find_all
= 0;
587 git_config(git_default_config
, NULL
);
588 init_revisions(&revs
, prefix
);
590 revs
.commit_format
= CMIT_FMT_UNSPECIFIED
;
591 argc
= setup_revisions(argc
, argv
, &revs
, NULL
);
593 quiet
= DIFF_OPT_TST(&revs
.diffopt
, QUIET
);
594 for (i
= 1 ; i
< argc
; i
++) {
595 const char *arg
= argv
[i
];
597 if (!strcmp(arg
, "--header")) {
598 revs
.verbose_header
= 1;
601 if (!strcmp(arg
, "--timestamp")) {
605 if (!strcmp(arg
, "--bisect")) {
609 if (!strcmp(arg
, "--bisect-all")) {
614 if (!strcmp(arg
, "--bisect-vars")) {
616 bisect_show_vars
= 1;
619 if (!strcmp(arg
, "--stdin")) {
620 if (read_from_stdin
++)
621 die("--stdin given twice?");
622 read_revisions_from_stdin(&revs
);
625 usage(rev_list_usage
);
628 if (revs
.commit_format
!= CMIT_FMT_UNSPECIFIED
) {
629 /* The command line has a --pretty */
630 hdr_termination
= '\n';
631 if (revs
.commit_format
== CMIT_FMT_ONELINE
)
634 header_prefix
= "commit ";
636 else if (revs
.verbose_header
)
637 /* Only --header was specified */
638 revs
.commit_format
= CMIT_FMT_RAW
;
643 (!(revs
.tag_objects
||revs
.tree_objects
||revs
.blob_objects
) &&
644 !revs
.pending
.nr
)) ||
646 usage(rev_list_usage
);
648 save_commit_buffer
= revs
.verbose_header
|| revs
.grep_filter
;
652 if (prepare_revision_walk(&revs
))
653 die("revision walk setup failed");
654 if (revs
.tree_objects
)
655 mark_edges_uninteresting(revs
.commits
, &revs
, show_edge
);
658 int reaches
= reaches
, all
= all
;
660 revs
.commits
= find_bisection(revs
.commits
, &reaches
, &all
,
662 if (bisect_show_vars
) {
668 * revs.commits can reach "reaches" commits among
669 * "all" commits. If it is good, then there are
670 * (all-reaches) commits left to be bisected.
671 * On the other hand, if it is bad, then the set
672 * to bisect is "reaches".
673 * A bisect set of size N has (N-1) commits further
674 * to test, as we already know one bad one.
679 strcpy(hex
, sha1_to_hex(revs
.commits
->item
->object
.sha1
));
681 if (bisect_find_all
) {
682 traverse_commit_list(&revs
, show_commit
, show_object
);
686 printf("bisect_rev=%s\n"
700 traverse_commit_list(&revs
,
701 quiet
? finish_commit
: show_commit
,
702 quiet
? finish_object
: show_object
);