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
);
69 if (commit
->object
.flags
& BOUNDARY
)
71 else if (commit
->object
.flags
& UNINTERESTING
)
73 else if (revs
.left_right
) {
74 if (commit
->object
.flags
& SYMMETRIC_LEFT
)
79 if (revs
.abbrev_commit
&& revs
.abbrev
)
80 fputs(find_unique_abbrev(commit
->object
.sha1
, revs
.abbrev
),
83 fputs(sha1_to_hex(commit
->object
.sha1
), stdout
);
84 if (revs
.print_parents
) {
85 struct commit_list
*parents
= commit
->parents
;
87 printf(" %s", sha1_to_hex(parents
->item
->object
.sha1
));
88 parents
= parents
->next
;
91 if (revs
.children
.name
) {
92 struct commit_list
*children
;
94 children
= lookup_decoration(&revs
.children
, &commit
->object
);
96 printf(" %s", sha1_to_hex(children
->item
->object
.sha1
));
97 children
= children
->next
;
100 show_decorations(commit
);
101 if (revs
.commit_format
== CMIT_FMT_ONELINE
)
106 if (revs
.verbose_header
&& commit
->buffer
) {
108 strbuf_init(&buf
, 0);
109 pretty_print_commit(revs
.commit_format
, commit
,
110 &buf
, revs
.abbrev
, NULL
, NULL
,
114 if (revs
.commit_format
!= CMIT_FMT_ONELINE
)
115 graph_show_oneline(revs
.graph
);
117 graph_show_commit_msg(revs
.graph
, &buf
);
120 * Add a newline after the commit message.
122 * Usually, this newline produces a blank
123 * padding line between entries, in which case
124 * we need to add graph padding on this line.
126 * However, the commit message may not end in a
127 * newline. In this case the newline simply
128 * ends the last line of the commit message,
129 * and we don't need any graph output. (This
130 * always happens with CMIT_FMT_ONELINE, and it
131 * happens with CMIT_FMT_USERFORMAT when the
132 * format doesn't explicitly end in a newline.)
134 if (buf
.len
&& buf
.buf
[buf
.len
- 1] == '\n')
135 graph_show_padding(revs
.graph
);
139 * If the message buffer is empty, just show
140 * the rest of the graph output for this
143 if (graph_show_remainder(revs
.graph
))
148 printf("%s%c", buf
.buf
, hdr_termination
);
150 strbuf_release(&buf
);
152 if (graph_show_remainder(revs
.graph
))
155 maybe_flush_or_die(stdout
, "stdout");
156 finish_commit(commit
);
159 static void finish_commit(struct commit
*commit
)
161 if (commit
->parents
) {
162 free_commit_list(commit
->parents
);
163 commit
->parents
= NULL
;
165 free(commit
->buffer
);
166 commit
->buffer
= NULL
;
169 static void finish_object(struct object_array_entry
*p
)
171 if (p
->item
->type
== OBJ_BLOB
&& !has_sha1_file(p
->item
->sha1
))
172 die("missing blob object '%s'", sha1_to_hex(p
->item
->sha1
));
175 static void show_object(struct object_array_entry
*p
)
177 /* An object with name "foo\n0000000..." can be used to
178 * confuse downstream git-pack-objects very badly.
180 const char *ep
= strchr(p
->name
, '\n');
184 printf("%s %.*s\n", sha1_to_hex(p
->item
->sha1
),
185 (int) (ep
- p
->name
),
189 printf("%s %s\n", sha1_to_hex(p
->item
->sha1
), p
->name
);
192 static void show_edge(struct commit
*commit
)
194 printf("-%s\n", sha1_to_hex(commit
->object
.sha1
));
198 * This is a truly stupid algorithm, but it's only
199 * used for bisection, and we just don't care enough.
201 * We care just barely enough to avoid recursing for
204 static int count_distance(struct commit_list
*entry
)
209 struct commit
*commit
= entry
->item
;
210 struct commit_list
*p
;
212 if (commit
->object
.flags
& (UNINTERESTING
| COUNTED
))
214 if (!(commit
->object
.flags
& TREESAME
))
216 commit
->object
.flags
|= COUNTED
;
222 nr
+= count_distance(p
);
231 static void clear_distance(struct commit_list
*list
)
234 struct commit
*commit
= list
->item
;
235 commit
->object
.flags
&= ~COUNTED
;
240 #define DEBUG_BISECT 0
242 static inline int weight(struct commit_list
*elem
)
244 return *((int*)(elem
->item
->util
));
247 static inline void weight_set(struct commit_list
*elem
, int weight
)
249 *((int*)(elem
->item
->util
)) = weight
;
252 static int count_interesting_parents(struct commit
*commit
)
254 struct commit_list
*p
;
257 for (count
= 0, p
= commit
->parents
; p
; p
= p
->next
) {
258 if (p
->item
->object
.flags
& UNINTERESTING
)
265 static inline int halfway(struct commit_list
*p
, int nr
)
268 * Don't short-cut something we are not going to return!
270 if (p
->item
->object
.flags
& TREESAME
)
275 * 2 and 3 are halfway of 5.
276 * 3 is halfway of 6 but 2 and 4 are not.
278 switch (2 * weight(p
) - nr
) {
279 case -1: case 0: case 1:
287 #define show_list(a,b,c,d) do { ; } while (0)
289 static void show_list(const char *debug
, int counted
, int nr
,
290 struct commit_list
*list
)
292 struct commit_list
*p
;
294 fprintf(stderr
, "%s (%d/%d)\n", debug
, counted
, nr
);
296 for (p
= list
; p
; p
= p
->next
) {
297 struct commit_list
*pp
;
298 struct commit
*commit
= p
->item
;
299 unsigned flags
= commit
->object
.flags
;
300 enum object_type type
;
302 char *buf
= read_sha1_file(commit
->object
.sha1
, &type
, &size
);
305 fprintf(stderr
, "%c%c%c ",
306 (flags
& TREESAME
) ? ' ' : 'T',
307 (flags
& UNINTERESTING
) ? 'U' : ' ',
308 (flags
& COUNTED
) ? 'C' : ' ');
310 fprintf(stderr
, "%3d", weight(p
));
312 fprintf(stderr
, "---");
313 fprintf(stderr
, " %.*s", 8, sha1_to_hex(commit
->object
.sha1
));
314 for (pp
= commit
->parents
; pp
; pp
= pp
->next
)
315 fprintf(stderr
, " %.*s", 8,
316 sha1_to_hex(pp
->item
->object
.sha1
));
318 sp
= strstr(buf
, "\n\n");
321 for (ep
= sp
; *ep
&& *ep
!= '\n'; ep
++)
323 fprintf(stderr
, " %.*s", (int)(ep
- sp
), sp
);
325 fprintf(stderr
, "\n");
328 #endif /* DEBUG_BISECT */
330 static struct commit_list
*best_bisection(struct commit_list
*list
, int nr
)
332 struct commit_list
*p
, *best
;
333 int best_distance
= -1;
336 for (p
= list
; p
; p
= p
->next
) {
338 unsigned flags
= p
->item
->object
.flags
;
340 if (flags
& TREESAME
)
342 distance
= weight(p
);
343 if (nr
- distance
< distance
)
344 distance
= nr
- distance
;
345 if (distance
> best_distance
) {
347 best_distance
= distance
;
355 struct commit
*commit
;
359 static int compare_commit_dist(const void *a_
, const void *b_
)
361 struct commit_dist
*a
, *b
;
363 a
= (struct commit_dist
*)a_
;
364 b
= (struct commit_dist
*)b_
;
365 if (a
->distance
!= b
->distance
)
366 return b
->distance
- a
->distance
; /* desc sort */
367 return hashcmp(a
->commit
->object
.sha1
, b
->commit
->object
.sha1
);
370 static struct commit_list
*best_bisection_sorted(struct commit_list
*list
, int nr
)
372 struct commit_list
*p
;
373 struct commit_dist
*array
= xcalloc(nr
, sizeof(*array
));
376 for (p
= list
, cnt
= 0; p
; p
= p
->next
) {
378 unsigned flags
= p
->item
->object
.flags
;
380 if (flags
& TREESAME
)
382 distance
= weight(p
);
383 if (nr
- distance
< distance
)
384 distance
= nr
- distance
;
385 array
[cnt
].commit
= p
->item
;
386 array
[cnt
].distance
= distance
;
389 qsort(array
, cnt
, sizeof(*array
), compare_commit_dist
);
390 for (p
= list
, i
= 0; i
< cnt
; i
++) {
391 struct name_decoration
*r
= xmalloc(sizeof(*r
) + 100);
392 struct object
*obj
= &(array
[i
].commit
->object
);
394 sprintf(r
->name
, "dist=%d", array
[i
].distance
);
395 r
->next
= add_decoration(&name_decoration
, obj
, r
);
396 p
->item
= array
[i
].commit
;
406 * zero or positive weight is the number of interesting commits it can
407 * reach, including itself. Especially, weight = 0 means it does not
408 * reach any tree-changing commits (e.g. just above uninteresting one
409 * but traversal is with pathspec).
411 * weight = -1 means it has one parent and its distance is yet to
414 * weight = -2 means it has more than one parent and its distance is
415 * unknown. After running count_distance() first, they will get zero
416 * or positive distance.
418 static struct commit_list
*do_find_bisection(struct commit_list
*list
,
419 int nr
, int *weights
,
423 struct commit_list
*p
;
427 for (n
= 0, p
= list
; p
; p
= p
->next
) {
428 struct commit
*commit
= p
->item
;
429 unsigned flags
= commit
->object
.flags
;
431 p
->item
->util
= &weights
[n
++];
432 switch (count_interesting_parents(commit
)) {
434 if (!(flags
& TREESAME
)) {
437 show_list("bisection 2 count one",
441 * otherwise, it is known not to reach any
442 * tree-changing commit and gets weight 0.
454 show_list("bisection 2 initialize", counted
, nr
, list
);
457 * If you have only one parent in the resulting set
458 * then you can reach one commit more than that parent
459 * can reach. So we do not have to run the expensive
460 * count_distance() for single strand of pearls.
462 * However, if you have more than one parents, you cannot
463 * just add their distance and one for yourself, since
464 * they usually reach the same ancestor and you would
465 * end up counting them twice that way.
467 * So we will first count distance of merges the usual
468 * way, and then fill the blanks using cheaper algorithm.
470 for (p
= list
; p
; p
= p
->next
) {
471 if (p
->item
->object
.flags
& UNINTERESTING
)
475 weight_set(p
, count_distance(p
));
476 clear_distance(list
);
478 /* Does it happen to be at exactly half-way? */
479 if (!find_all
&& halfway(p
, nr
))
484 show_list("bisection 2 count_distance", counted
, nr
, list
);
486 while (counted
< nr
) {
487 for (p
= list
; p
; p
= p
->next
) {
488 struct commit_list
*q
;
489 unsigned flags
= p
->item
->object
.flags
;
493 for (q
= p
->item
->parents
; q
; q
= q
->next
) {
494 if (q
->item
->object
.flags
& UNINTERESTING
)
503 * weight for p is unknown but q is known.
504 * add one for p itself if p is to be counted,
505 * otherwise inherit it from q directly.
507 if (!(flags
& TREESAME
)) {
508 weight_set(p
, weight(q
)+1);
510 show_list("bisection 2 count one",
514 weight_set(p
, weight(q
));
516 /* Does it happen to be at exactly half-way? */
517 if (!find_all
&& halfway(p
, nr
))
522 show_list("bisection 2 counted all", counted
, nr
, list
);
525 return best_bisection(list
, nr
);
527 return best_bisection_sorted(list
, nr
);
530 static struct commit_list
*find_bisection(struct commit_list
*list
,
531 int *reaches
, int *all
,
535 struct commit_list
*p
, *best
, *next
, *last
;
538 show_list("bisection 2 entry", 0, 0, list
);
541 * Count the number of total and tree-changing items on the
542 * list, while reversing the list.
544 for (nr
= on_list
= 0, last
= NULL
, p
= list
;
547 unsigned flags
= p
->item
->object
.flags
;
550 if (flags
& UNINTERESTING
)
554 if (!(flags
& TREESAME
))
559 show_list("bisection 2 sorted", 0, nr
, list
);
562 weights
= xcalloc(on_list
, sizeof(*weights
));
564 /* Do the real work of finding bisection commit. */
565 best
= do_find_bisection(list
, nr
, weights
, find_all
);
569 *reaches
= weight(best
);
575 static void read_revisions_from_stdin(struct rev_info
*revs
)
579 while (fgets(line
, sizeof(line
), stdin
) != NULL
) {
580 int len
= strlen(line
);
581 if (len
&& line
[len
- 1] == '\n')
586 die("options not supported in --stdin mode");
587 if (handle_revision_arg(line
, revs
, 0, 1))
588 die("bad revision '%s'", line
);
592 int cmd_rev_list(int argc
, const char **argv
, const char *prefix
)
594 struct commit_list
*list
;
596 int read_from_stdin
= 0;
597 int bisect_show_vars
= 0;
598 int bisect_find_all
= 0;
601 git_config(git_default_config
);
602 init_revisions(&revs
, prefix
);
604 revs
.commit_format
= CMIT_FMT_UNSPECIFIED
;
605 argc
= setup_revisions(argc
, argv
, &revs
, NULL
);
607 for (i
= 1 ; i
< argc
; i
++) {
608 const char *arg
= argv
[i
];
610 if (!strcmp(arg
, "--header")) {
611 revs
.verbose_header
= 1;
614 if (!strcmp(arg
, "--timestamp")) {
618 if (!strcmp(arg
, "--bisect")) {
622 if (!strcmp(arg
, "--bisect-all")) {
627 if (!strcmp(arg
, "--bisect-vars")) {
629 bisect_show_vars
= 1;
632 if (!strcmp(arg
, "--stdin")) {
633 if (read_from_stdin
++)
634 die("--stdin given twice?");
635 read_revisions_from_stdin(&revs
);
638 if (!strcmp(arg
, "--quiet")) {
642 usage(rev_list_usage
);
645 if (revs
.commit_format
!= CMIT_FMT_UNSPECIFIED
) {
646 /* The command line has a --pretty */
647 hdr_termination
= '\n';
648 if (revs
.commit_format
== CMIT_FMT_ONELINE
)
651 header_prefix
= "commit ";
653 else if (revs
.verbose_header
)
654 /* Only --header was specified */
655 revs
.commit_format
= CMIT_FMT_RAW
;
660 (!(revs
.tag_objects
||revs
.tree_objects
||revs
.blob_objects
) &&
661 !revs
.pending
.nr
)) ||
663 usage(rev_list_usage
);
665 save_commit_buffer
= revs
.verbose_header
|| revs
.grep_filter
;
669 if (prepare_revision_walk(&revs
))
670 die("revision walk setup failed");
671 if (revs
.tree_objects
)
672 mark_edges_uninteresting(revs
.commits
, &revs
, show_edge
);
675 int reaches
= reaches
, all
= all
;
677 revs
.commits
= find_bisection(revs
.commits
, &reaches
, &all
,
679 if (bisect_show_vars
) {
685 * revs.commits can reach "reaches" commits among
686 * "all" commits. If it is good, then there are
687 * (all-reaches) commits left to be bisected.
688 * On the other hand, if it is bad, then the set
689 * to bisect is "reaches".
690 * A bisect set of size N has (N-1) commits further
691 * to test, as we already know one bad one.
696 strcpy(hex
, sha1_to_hex(revs
.commits
->item
->object
.sha1
));
698 if (bisect_find_all
) {
699 traverse_commit_list(&revs
, show_commit
, show_object
);
703 printf("bisect_rev=%s\n"
717 traverse_commit_list(&revs
,
718 quiet
? finish_commit
: show_commit
,
719 quiet
? finish_object
: show_object
);