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
);
68 if (commit
->object
.flags
& BOUNDARY
)
70 else if (commit
->object
.flags
& UNINTERESTING
)
72 else if (revs
.left_right
) {
73 if (commit
->object
.flags
& SYMMETRIC_LEFT
)
78 if (revs
.abbrev_commit
&& revs
.abbrev
)
79 fputs(find_unique_abbrev(commit
->object
.sha1
, revs
.abbrev
),
82 fputs(sha1_to_hex(commit
->object
.sha1
), stdout
);
83 if (revs
.print_parents
) {
84 struct commit_list
*parents
= commit
->parents
;
86 printf(" %s", sha1_to_hex(parents
->item
->object
.sha1
));
87 parents
= parents
->next
;
90 show_decorations(commit
);
91 if (revs
.commit_format
== CMIT_FMT_ONELINE
)
96 if (revs
.verbose_header
&& commit
->buffer
) {
99 pretty_print_commit(revs
.commit_format
, commit
,
100 &buf
, revs
.abbrev
, NULL
, NULL
,
104 if (revs
.commit_format
!= CMIT_FMT_ONELINE
)
105 graph_show_oneline(revs
.graph
);
107 graph_show_commit_msg(revs
.graph
, &buf
);
110 * Add a newline after the commit message.
112 * Usually, this newline produces a blank
113 * padding line between entries, in which case
114 * we need to add graph padding on this line.
116 * However, the commit message may not end in a
117 * newline. In this case the newline simply
118 * ends the last line of the commit message,
119 * and we don't need any graph output. (This
120 * always happens with CMIT_FMT_ONELINE, and it
121 * happens with CMIT_FMT_USERFORMAT when the
122 * format doesn't explicitly end in a newline.)
124 if (buf
.len
&& buf
.buf
[buf
.len
- 1] == '\n')
125 graph_show_padding(revs
.graph
);
129 * If the message buffer is empty, just show
130 * the rest of the graph output for this
133 if (graph_show_remainder(revs
.graph
))
138 printf("%s%c", buf
.buf
, hdr_termination
);
140 strbuf_release(&buf
);
142 if (graph_show_remainder(revs
.graph
))
145 maybe_flush_or_die(stdout
, "stdout");
146 finish_commit(commit
);
149 static void finish_commit(struct commit
*commit
)
151 if (commit
->parents
) {
152 free_commit_list(commit
->parents
);
153 commit
->parents
= NULL
;
155 free(commit
->buffer
);
156 commit
->buffer
= NULL
;
159 static void finish_object(struct object_array_entry
*p
)
161 if (p
->item
->type
== OBJ_BLOB
&& !has_sha1_file(p
->item
->sha1
))
162 die("missing blob object '%s'", sha1_to_hex(p
->item
->sha1
));
165 static void show_object(struct object_array_entry
*p
)
167 /* An object with name "foo\n0000000..." can be used to
168 * confuse downstream git-pack-objects very badly.
170 const char *ep
= strchr(p
->name
, '\n');
174 printf("%s %.*s\n", sha1_to_hex(p
->item
->sha1
),
175 (int) (ep
- p
->name
),
179 printf("%s %s\n", sha1_to_hex(p
->item
->sha1
), p
->name
);
182 static void show_edge(struct commit
*commit
)
184 printf("-%s\n", sha1_to_hex(commit
->object
.sha1
));
188 * This is a truly stupid algorithm, but it's only
189 * used for bisection, and we just don't care enough.
191 * We care just barely enough to avoid recursing for
194 static int count_distance(struct commit_list
*entry
)
199 struct commit
*commit
= entry
->item
;
200 struct commit_list
*p
;
202 if (commit
->object
.flags
& (UNINTERESTING
| COUNTED
))
204 if (!(commit
->object
.flags
& TREESAME
))
206 commit
->object
.flags
|= COUNTED
;
212 nr
+= count_distance(p
);
221 static void clear_distance(struct commit_list
*list
)
224 struct commit
*commit
= list
->item
;
225 commit
->object
.flags
&= ~COUNTED
;
230 #define DEBUG_BISECT 0
232 static inline int weight(struct commit_list
*elem
)
234 return *((int*)(elem
->item
->util
));
237 static inline void weight_set(struct commit_list
*elem
, int weight
)
239 *((int*)(elem
->item
->util
)) = weight
;
242 static int count_interesting_parents(struct commit
*commit
)
244 struct commit_list
*p
;
247 for (count
= 0, p
= commit
->parents
; p
; p
= p
->next
) {
248 if (p
->item
->object
.flags
& UNINTERESTING
)
255 static inline int halfway(struct commit_list
*p
, int nr
)
258 * Don't short-cut something we are not going to return!
260 if (p
->item
->object
.flags
& TREESAME
)
265 * 2 and 3 are halfway of 5.
266 * 3 is halfway of 6 but 2 and 4 are not.
268 switch (2 * weight(p
) - nr
) {
269 case -1: case 0: case 1:
277 #define show_list(a,b,c,d) do { ; } while (0)
279 static void show_list(const char *debug
, int counted
, int nr
,
280 struct commit_list
*list
)
282 struct commit_list
*p
;
284 fprintf(stderr
, "%s (%d/%d)\n", debug
, counted
, nr
);
286 for (p
= list
; p
; p
= p
->next
) {
287 struct commit_list
*pp
;
288 struct commit
*commit
= p
->item
;
289 unsigned flags
= commit
->object
.flags
;
290 enum object_type type
;
292 char *buf
= read_sha1_file(commit
->object
.sha1
, &type
, &size
);
295 fprintf(stderr
, "%c%c%c ",
296 (flags
& TREESAME
) ? ' ' : 'T',
297 (flags
& UNINTERESTING
) ? 'U' : ' ',
298 (flags
& COUNTED
) ? 'C' : ' ');
300 fprintf(stderr
, "%3d", weight(p
));
302 fprintf(stderr
, "---");
303 fprintf(stderr
, " %.*s", 8, sha1_to_hex(commit
->object
.sha1
));
304 for (pp
= commit
->parents
; pp
; pp
= pp
->next
)
305 fprintf(stderr
, " %.*s", 8,
306 sha1_to_hex(pp
->item
->object
.sha1
));
308 sp
= strstr(buf
, "\n\n");
311 for (ep
= sp
; *ep
&& *ep
!= '\n'; ep
++)
313 fprintf(stderr
, " %.*s", (int)(ep
- sp
), sp
);
315 fprintf(stderr
, "\n");
318 #endif /* DEBUG_BISECT */
320 static struct commit_list
*best_bisection(struct commit_list
*list
, int nr
)
322 struct commit_list
*p
, *best
;
323 int best_distance
= -1;
326 for (p
= list
; p
; p
= p
->next
) {
328 unsigned flags
= p
->item
->object
.flags
;
330 if (flags
& TREESAME
)
332 distance
= weight(p
);
333 if (nr
- distance
< distance
)
334 distance
= nr
- distance
;
335 if (distance
> best_distance
) {
337 best_distance
= distance
;
345 struct commit
*commit
;
349 static int compare_commit_dist(const void *a_
, const void *b_
)
351 struct commit_dist
*a
, *b
;
353 a
= (struct commit_dist
*)a_
;
354 b
= (struct commit_dist
*)b_
;
355 if (a
->distance
!= b
->distance
)
356 return b
->distance
- a
->distance
; /* desc sort */
357 return hashcmp(a
->commit
->object
.sha1
, b
->commit
->object
.sha1
);
360 static struct commit_list
*best_bisection_sorted(struct commit_list
*list
, int nr
)
362 struct commit_list
*p
;
363 struct commit_dist
*array
= xcalloc(nr
, sizeof(*array
));
366 for (p
= list
, cnt
= 0; p
; p
= p
->next
) {
368 unsigned flags
= p
->item
->object
.flags
;
370 if (flags
& TREESAME
)
372 distance
= weight(p
);
373 if (nr
- distance
< distance
)
374 distance
= nr
- distance
;
375 array
[cnt
].commit
= p
->item
;
376 array
[cnt
].distance
= distance
;
379 qsort(array
, cnt
, sizeof(*array
), compare_commit_dist
);
380 for (p
= list
, i
= 0; i
< cnt
; i
++) {
381 struct name_decoration
*r
= xmalloc(sizeof(*r
) + 100);
382 struct object
*obj
= &(array
[i
].commit
->object
);
384 sprintf(r
->name
, "dist=%d", array
[i
].distance
);
385 r
->next
= add_decoration(&name_decoration
, obj
, r
);
386 p
->item
= array
[i
].commit
;
396 * zero or positive weight is the number of interesting commits it can
397 * reach, including itself. Especially, weight = 0 means it does not
398 * reach any tree-changing commits (e.g. just above uninteresting one
399 * but traversal is with pathspec).
401 * weight = -1 means it has one parent and its distance is yet to
404 * weight = -2 means it has more than one parent and its distance is
405 * unknown. After running count_distance() first, they will get zero
406 * or positive distance.
408 static struct commit_list
*do_find_bisection(struct commit_list
*list
,
409 int nr
, int *weights
,
413 struct commit_list
*p
;
417 for (n
= 0, p
= list
; p
; p
= p
->next
) {
418 struct commit
*commit
= p
->item
;
419 unsigned flags
= commit
->object
.flags
;
421 p
->item
->util
= &weights
[n
++];
422 switch (count_interesting_parents(commit
)) {
424 if (!(flags
& TREESAME
)) {
427 show_list("bisection 2 count one",
431 * otherwise, it is known not to reach any
432 * tree-changing commit and gets weight 0.
444 show_list("bisection 2 initialize", counted
, nr
, list
);
447 * If you have only one parent in the resulting set
448 * then you can reach one commit more than that parent
449 * can reach. So we do not have to run the expensive
450 * count_distance() for single strand of pearls.
452 * However, if you have more than one parents, you cannot
453 * just add their distance and one for yourself, since
454 * they usually reach the same ancestor and you would
455 * end up counting them twice that way.
457 * So we will first count distance of merges the usual
458 * way, and then fill the blanks using cheaper algorithm.
460 for (p
= list
; p
; p
= p
->next
) {
461 if (p
->item
->object
.flags
& UNINTERESTING
)
465 weight_set(p
, count_distance(p
));
466 clear_distance(list
);
468 /* Does it happen to be at exactly half-way? */
469 if (!find_all
&& halfway(p
, nr
))
474 show_list("bisection 2 count_distance", counted
, nr
, list
);
476 while (counted
< nr
) {
477 for (p
= list
; p
; p
= p
->next
) {
478 struct commit_list
*q
;
479 unsigned flags
= p
->item
->object
.flags
;
483 for (q
= p
->item
->parents
; q
; q
= q
->next
) {
484 if (q
->item
->object
.flags
& UNINTERESTING
)
493 * weight for p is unknown but q is known.
494 * add one for p itself if p is to be counted,
495 * otherwise inherit it from q directly.
497 if (!(flags
& TREESAME
)) {
498 weight_set(p
, weight(q
)+1);
500 show_list("bisection 2 count one",
504 weight_set(p
, weight(q
));
506 /* Does it happen to be at exactly half-way? */
507 if (!find_all
&& halfway(p
, nr
))
512 show_list("bisection 2 counted all", counted
, nr
, list
);
515 return best_bisection(list
, nr
);
517 return best_bisection_sorted(list
, nr
);
520 static struct commit_list
*find_bisection(struct commit_list
*list
,
521 int *reaches
, int *all
,
525 struct commit_list
*p
, *best
, *next
, *last
;
528 show_list("bisection 2 entry", 0, 0, list
);
531 * Count the number of total and tree-changing items on the
532 * list, while reversing the list.
534 for (nr
= on_list
= 0, last
= NULL
, p
= list
;
537 unsigned flags
= p
->item
->object
.flags
;
540 if (flags
& UNINTERESTING
)
544 if (!(flags
& TREESAME
))
549 show_list("bisection 2 sorted", 0, nr
, list
);
552 weights
= xcalloc(on_list
, sizeof(*weights
));
554 /* Do the real work of finding bisection commit. */
555 best
= do_find_bisection(list
, nr
, weights
, find_all
);
559 *reaches
= weight(best
);
565 static void read_revisions_from_stdin(struct rev_info
*revs
)
569 while (fgets(line
, sizeof(line
), stdin
) != NULL
) {
570 int len
= strlen(line
);
571 if (len
&& line
[len
- 1] == '\n')
576 die("options not supported in --stdin mode");
577 if (handle_revision_arg(line
, revs
, 0, 1))
578 die("bad revision '%s'", line
);
582 int cmd_rev_list(int argc
, const char **argv
, const char *prefix
)
584 struct commit_list
*list
;
586 int read_from_stdin
= 0;
587 int bisect_show_vars
= 0;
588 int bisect_find_all
= 0;
591 git_config(git_default_config
);
592 init_revisions(&revs
, prefix
);
594 revs
.commit_format
= CMIT_FMT_UNSPECIFIED
;
595 argc
= setup_revisions(argc
, argv
, &revs
, NULL
);
597 for (i
= 1 ; i
< argc
; i
++) {
598 const char *arg
= argv
[i
];
600 if (!strcmp(arg
, "--header")) {
601 revs
.verbose_header
= 1;
604 if (!strcmp(arg
, "--timestamp")) {
608 if (!strcmp(arg
, "--bisect")) {
612 if (!strcmp(arg
, "--bisect-all")) {
617 if (!strcmp(arg
, "--bisect-vars")) {
619 bisect_show_vars
= 1;
622 if (!strcmp(arg
, "--stdin")) {
623 if (read_from_stdin
++)
624 die("--stdin given twice?");
625 read_revisions_from_stdin(&revs
);
628 if (!strcmp(arg
, "--quiet")) {
632 usage(rev_list_usage
);
635 if (revs
.commit_format
!= CMIT_FMT_UNSPECIFIED
) {
636 /* The command line has a --pretty */
637 hdr_termination
= '\n';
638 if (revs
.commit_format
== CMIT_FMT_ONELINE
)
641 header_prefix
= "commit ";
643 else if (revs
.verbose_header
)
644 /* Only --header was specified */
645 revs
.commit_format
= CMIT_FMT_RAW
;
650 (!(revs
.tag_objects
||revs
.tree_objects
||revs
.blob_objects
) &&
651 !revs
.pending
.nr
)) ||
653 usage(rev_list_usage
);
655 save_commit_buffer
= revs
.verbose_header
|| revs
.grep_filter
;
659 if (prepare_revision_walk(&revs
))
660 die("revision walk setup failed");
661 if (revs
.tree_objects
)
662 mark_edges_uninteresting(revs
.commits
, &revs
, show_edge
);
665 int reaches
= reaches
, all
= all
;
667 revs
.commits
= find_bisection(revs
.commits
, &reaches
, &all
,
669 if (bisect_show_vars
) {
675 * revs.commits can reach "reaches" commits among
676 * "all" commits. If it is good, then there are
677 * (all-reaches) commits left to be bisected.
678 * On the other hand, if it is bad, then the set
679 * to bisect is "reaches".
680 * A bisect set of size N has (N-1) commits further
681 * to test, as we already know one bad one.
686 strcpy(hex
, sha1_to_hex(revs
.commits
->item
->object
.sha1
));
688 if (bisect_find_all
) {
689 traverse_commit_list(&revs
, show_commit
, show_object
);
693 printf("bisect_rev=%s\n"
707 traverse_commit_list(&revs
,
708 quiet
? finish_commit
: show_commit
,
709 quiet
? finish_object
: show_object
);