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"
40 " --objects | --objects-edge\n"
42 " --header | --pretty\n"
43 " --abbrev=nr | --no-abbrev\n"
52 static struct rev_info revs
;
54 static int bisect_list
;
55 static int show_timestamp
;
56 static int hdr_termination
;
57 static const char *header_prefix
;
59 static void finish_commit(struct commit
*commit
);
60 static void show_commit(struct commit
*commit
)
62 graph_show_commit(revs
.graph
);
65 printf("%lu ", commit
->date
);
67 fputs(header_prefix
, stdout
);
70 if (commit
->object
.flags
& BOUNDARY
)
72 else if (commit
->object
.flags
& UNINTERESTING
)
74 else if (revs
.left_right
) {
75 if (commit
->object
.flags
& SYMMETRIC_LEFT
)
81 if (revs
.abbrev_commit
&& revs
.abbrev
)
82 fputs(find_unique_abbrev(commit
->object
.sha1
, revs
.abbrev
),
85 fputs(sha1_to_hex(commit
->object
.sha1
), stdout
);
86 if (revs
.print_parents
) {
87 struct commit_list
*parents
= commit
->parents
;
89 printf(" %s", sha1_to_hex(parents
->item
->object
.sha1
));
90 parents
= parents
->next
;
93 show_decorations(commit
);
94 if (revs
.commit_format
== CMIT_FMT_ONELINE
)
99 if (revs
.verbose_header
&& commit
->buffer
) {
101 strbuf_init(&buf
, 0);
102 pretty_print_commit(revs
.commit_format
, commit
,
103 &buf
, revs
.abbrev
, NULL
, NULL
,
107 if (revs
.commit_format
!= CMIT_FMT_ONELINE
)
108 graph_show_oneline(revs
.graph
);
110 graph_show_commit_msg(revs
.graph
, &buf
);
113 * Add a newline after the commit message.
115 * Usually, this newline produces a blank
116 * padding line between entries, in which case
117 * we need to add graph padding on this line.
119 * However, the commit message may not end in a
120 * newline. In this case the newline simply
121 * ends the last line of the commit message,
122 * and we don't need any graph output. (This
123 * always happens with CMIT_FMT_ONELINE, and it
124 * happens with CMIT_FMT_USERFORMAT when the
125 * format doesn't explicitly end in a newline.)
127 if (buf
.len
&& buf
.buf
[buf
.len
- 1] == '\n')
128 graph_show_padding(revs
.graph
);
132 * If the message buffer is empty, just show
133 * the rest of the graph output for this
136 if (graph_show_remainder(revs
.graph
))
141 printf("%s%c", buf
.buf
, hdr_termination
);
143 strbuf_release(&buf
);
145 if (graph_show_remainder(revs
.graph
))
148 maybe_flush_or_die(stdout
, "stdout");
149 finish_commit(commit
);
152 static void finish_commit(struct commit
*commit
)
154 if (commit
->parents
) {
155 free_commit_list(commit
->parents
);
156 commit
->parents
= NULL
;
158 free(commit
->buffer
);
159 commit
->buffer
= NULL
;
162 static void finish_object(struct object_array_entry
*p
)
164 if (p
->item
->type
== OBJ_BLOB
&& !has_sha1_file(p
->item
->sha1
))
165 die("missing blob object '%s'", sha1_to_hex(p
->item
->sha1
));
168 static void show_object(struct object_array_entry
*p
)
170 /* An object with name "foo\n0000000..." can be used to
171 * confuse downstream git-pack-objects very badly.
173 const char *ep
= strchr(p
->name
, '\n');
177 printf("%s %.*s\n", sha1_to_hex(p
->item
->sha1
),
178 (int) (ep
- p
->name
),
182 printf("%s %s\n", sha1_to_hex(p
->item
->sha1
), p
->name
);
185 static void show_edge(struct commit
*commit
)
187 printf("-%s\n", sha1_to_hex(commit
->object
.sha1
));
191 * This is a truly stupid algorithm, but it's only
192 * used for bisection, and we just don't care enough.
194 * We care just barely enough to avoid recursing for
197 static int count_distance(struct commit_list
*entry
)
202 struct commit
*commit
= entry
->item
;
203 struct commit_list
*p
;
205 if (commit
->object
.flags
& (UNINTERESTING
| COUNTED
))
207 if (!(commit
->object
.flags
& TREESAME
))
209 commit
->object
.flags
|= COUNTED
;
215 nr
+= count_distance(p
);
224 static void clear_distance(struct commit_list
*list
)
227 struct commit
*commit
= list
->item
;
228 commit
->object
.flags
&= ~COUNTED
;
233 #define DEBUG_BISECT 0
235 static inline int weight(struct commit_list
*elem
)
237 return *((int*)(elem
->item
->util
));
240 static inline void weight_set(struct commit_list
*elem
, int weight
)
242 *((int*)(elem
->item
->util
)) = weight
;
245 static int count_interesting_parents(struct commit
*commit
)
247 struct commit_list
*p
;
250 for (count
= 0, p
= commit
->parents
; p
; p
= p
->next
) {
251 if (p
->item
->object
.flags
& UNINTERESTING
)
258 static inline int halfway(struct commit_list
*p
, int nr
)
261 * Don't short-cut something we are not going to return!
263 if (p
->item
->object
.flags
& TREESAME
)
268 * 2 and 3 are halfway of 5.
269 * 3 is halfway of 6 but 2 and 4 are not.
271 switch (2 * weight(p
) - nr
) {
272 case -1: case 0: case 1:
280 #define show_list(a,b,c,d) do { ; } while (0)
282 static void show_list(const char *debug
, int counted
, int nr
,
283 struct commit_list
*list
)
285 struct commit_list
*p
;
287 fprintf(stderr
, "%s (%d/%d)\n", debug
, counted
, nr
);
289 for (p
= list
; p
; p
= p
->next
) {
290 struct commit_list
*pp
;
291 struct commit
*commit
= p
->item
;
292 unsigned flags
= commit
->object
.flags
;
293 enum object_type type
;
295 char *buf
= read_sha1_file(commit
->object
.sha1
, &type
, &size
);
298 fprintf(stderr
, "%c%c%c ",
299 (flags
& TREESAME
) ? ' ' : 'T',
300 (flags
& UNINTERESTING
) ? 'U' : ' ',
301 (flags
& COUNTED
) ? 'C' : ' ');
303 fprintf(stderr
, "%3d", weight(p
));
305 fprintf(stderr
, "---");
306 fprintf(stderr
, " %.*s", 8, sha1_to_hex(commit
->object
.sha1
));
307 for (pp
= commit
->parents
; pp
; pp
= pp
->next
)
308 fprintf(stderr
, " %.*s", 8,
309 sha1_to_hex(pp
->item
->object
.sha1
));
311 sp
= strstr(buf
, "\n\n");
314 for (ep
= sp
; *ep
&& *ep
!= '\n'; ep
++)
316 fprintf(stderr
, " %.*s", (int)(ep
- sp
), sp
);
318 fprintf(stderr
, "\n");
321 #endif /* DEBUG_BISECT */
323 static struct commit_list
*best_bisection(struct commit_list
*list
, int nr
)
325 struct commit_list
*p
, *best
;
326 int best_distance
= -1;
329 for (p
= list
; p
; p
= p
->next
) {
331 unsigned flags
= p
->item
->object
.flags
;
333 if (flags
& TREESAME
)
335 distance
= weight(p
);
336 if (nr
- distance
< distance
)
337 distance
= nr
- distance
;
338 if (distance
> best_distance
) {
340 best_distance
= distance
;
348 struct commit
*commit
;
352 static int compare_commit_dist(const void *a_
, const void *b_
)
354 struct commit_dist
*a
, *b
;
356 a
= (struct commit_dist
*)a_
;
357 b
= (struct commit_dist
*)b_
;
358 if (a
->distance
!= b
->distance
)
359 return b
->distance
- a
->distance
; /* desc sort */
360 return hashcmp(a
->commit
->object
.sha1
, b
->commit
->object
.sha1
);
363 static struct commit_list
*best_bisection_sorted(struct commit_list
*list
, int nr
)
365 struct commit_list
*p
;
366 struct commit_dist
*array
= xcalloc(nr
, sizeof(*array
));
369 for (p
= list
, cnt
= 0; p
; p
= p
->next
) {
371 unsigned flags
= p
->item
->object
.flags
;
373 if (flags
& TREESAME
)
375 distance
= weight(p
);
376 if (nr
- distance
< distance
)
377 distance
= nr
- distance
;
378 array
[cnt
].commit
= p
->item
;
379 array
[cnt
].distance
= distance
;
382 qsort(array
, cnt
, sizeof(*array
), compare_commit_dist
);
383 for (p
= list
, i
= 0; i
< cnt
; i
++) {
384 struct name_decoration
*r
= xmalloc(sizeof(*r
) + 100);
385 struct object
*obj
= &(array
[i
].commit
->object
);
387 sprintf(r
->name
, "dist=%d", array
[i
].distance
);
388 r
->next
= add_decoration(&name_decoration
, obj
, r
);
389 p
->item
= array
[i
].commit
;
399 * zero or positive weight is the number of interesting commits it can
400 * reach, including itself. Especially, weight = 0 means it does not
401 * reach any tree-changing commits (e.g. just above uninteresting one
402 * but traversal is with pathspec).
404 * weight = -1 means it has one parent and its distance is yet to
407 * weight = -2 means it has more than one parent and its distance is
408 * unknown. After running count_distance() first, they will get zero
409 * or positive distance.
411 static struct commit_list
*do_find_bisection(struct commit_list
*list
,
412 int nr
, int *weights
,
416 struct commit_list
*p
;
420 for (n
= 0, p
= list
; p
; p
= p
->next
) {
421 struct commit
*commit
= p
->item
;
422 unsigned flags
= commit
->object
.flags
;
424 p
->item
->util
= &weights
[n
++];
425 switch (count_interesting_parents(commit
)) {
427 if (!(flags
& TREESAME
)) {
430 show_list("bisection 2 count one",
434 * otherwise, it is known not to reach any
435 * tree-changing commit and gets weight 0.
447 show_list("bisection 2 initialize", counted
, nr
, list
);
450 * If you have only one parent in the resulting set
451 * then you can reach one commit more than that parent
452 * can reach. So we do not have to run the expensive
453 * count_distance() for single strand of pearls.
455 * However, if you have more than one parents, you cannot
456 * just add their distance and one for yourself, since
457 * they usually reach the same ancestor and you would
458 * end up counting them twice that way.
460 * So we will first count distance of merges the usual
461 * way, and then fill the blanks using cheaper algorithm.
463 for (p
= list
; p
; p
= p
->next
) {
464 if (p
->item
->object
.flags
& UNINTERESTING
)
468 weight_set(p
, count_distance(p
));
469 clear_distance(list
);
471 /* Does it happen to be at exactly half-way? */
472 if (!find_all
&& halfway(p
, nr
))
477 show_list("bisection 2 count_distance", counted
, nr
, list
);
479 while (counted
< nr
) {
480 for (p
= list
; p
; p
= p
->next
) {
481 struct commit_list
*q
;
482 unsigned flags
= p
->item
->object
.flags
;
486 for (q
= p
->item
->parents
; q
; q
= q
->next
) {
487 if (q
->item
->object
.flags
& UNINTERESTING
)
496 * weight for p is unknown but q is known.
497 * add one for p itself if p is to be counted,
498 * otherwise inherit it from q directly.
500 if (!(flags
& TREESAME
)) {
501 weight_set(p
, weight(q
)+1);
503 show_list("bisection 2 count one",
507 weight_set(p
, weight(q
));
509 /* Does it happen to be at exactly half-way? */
510 if (!find_all
&& halfway(p
, nr
))
515 show_list("bisection 2 counted all", counted
, nr
, list
);
518 return best_bisection(list
, nr
);
520 return best_bisection_sorted(list
, nr
);
523 static struct commit_list
*find_bisection(struct commit_list
*list
,
524 int *reaches
, int *all
,
528 struct commit_list
*p
, *best
, *next
, *last
;
531 show_list("bisection 2 entry", 0, 0, list
);
534 * Count the number of total and tree-changing items on the
535 * list, while reversing the list.
537 for (nr
= on_list
= 0, last
= NULL
, p
= list
;
540 unsigned flags
= p
->item
->object
.flags
;
543 if (flags
& UNINTERESTING
)
547 if (!(flags
& TREESAME
))
552 show_list("bisection 2 sorted", 0, nr
, list
);
555 weights
= xcalloc(on_list
, sizeof(*weights
));
557 /* Do the real work of finding bisection commit. */
558 best
= do_find_bisection(list
, nr
, weights
, find_all
);
562 *reaches
= weight(best
);
568 static void read_revisions_from_stdin(struct rev_info
*revs
)
572 while (fgets(line
, sizeof(line
), stdin
) != NULL
) {
573 int len
= strlen(line
);
574 if (len
&& line
[len
- 1] == '\n')
579 die("options not supported in --stdin mode");
580 if (handle_revision_arg(line
, revs
, 0, 1))
581 die("bad revision '%s'", line
);
585 int cmd_rev_list(int argc
, const char **argv
, const char *prefix
)
587 struct commit_list
*list
;
589 int read_from_stdin
= 0;
590 int bisect_show_vars
= 0;
591 int bisect_find_all
= 0;
594 git_config(git_default_config
, NULL
);
595 init_revisions(&revs
, prefix
);
597 revs
.commit_format
= CMIT_FMT_UNSPECIFIED
;
598 argc
= setup_revisions(argc
, argv
, &revs
, NULL
);
600 for (i
= 1 ; i
< argc
; i
++) {
601 const char *arg
= argv
[i
];
603 if (!strcmp(arg
, "--header")) {
604 revs
.verbose_header
= 1;
607 if (!strcmp(arg
, "--timestamp")) {
611 if (!strcmp(arg
, "--bisect")) {
615 if (!strcmp(arg
, "--bisect-all")) {
620 if (!strcmp(arg
, "--bisect-vars")) {
622 bisect_show_vars
= 1;
625 if (!strcmp(arg
, "--stdin")) {
626 if (read_from_stdin
++)
627 die("--stdin given twice?");
628 read_revisions_from_stdin(&revs
);
631 if (!strcmp(arg
, "--quiet")) {
635 usage(rev_list_usage
);
638 if (revs
.commit_format
!= CMIT_FMT_UNSPECIFIED
) {
639 /* The command line has a --pretty */
640 hdr_termination
= '\n';
641 if (revs
.commit_format
== CMIT_FMT_ONELINE
)
644 header_prefix
= "commit ";
646 else if (revs
.verbose_header
)
647 /* Only --header was specified */
648 revs
.commit_format
= CMIT_FMT_RAW
;
653 (!(revs
.tag_objects
||revs
.tree_objects
||revs
.blob_objects
) &&
654 !revs
.pending
.nr
)) ||
656 usage(rev_list_usage
);
658 save_commit_buffer
= revs
.verbose_header
|| revs
.grep_filter
;
662 if (prepare_revision_walk(&revs
))
663 die("revision walk setup failed");
664 if (revs
.tree_objects
)
665 mark_edges_uninteresting(revs
.commits
, &revs
, show_edge
);
668 int reaches
= reaches
, all
= all
;
670 revs
.commits
= find_bisection(revs
.commits
, &reaches
, &all
,
672 if (bisect_show_vars
) {
678 * revs.commits can reach "reaches" commits among
679 * "all" commits. If it is good, then there are
680 * (all-reaches) commits left to be bisected.
681 * On the other hand, if it is bad, then the set
682 * to bisect is "reaches".
683 * A bisect set of size N has (N-1) commits further
684 * to test, as we already know one bad one.
689 strcpy(hex
, sha1_to_hex(revs
.commits
->item
->object
.sha1
));
691 if (bisect_find_all
) {
692 traverse_commit_list(&revs
, show_commit
, show_object
);
696 printf("bisect_rev=%s\n"
710 traverse_commit_list(&revs
,
711 quiet
? finish_commit
: show_commit
,
712 quiet
? finish_object
: show_object
);