10 #include "list-objects.h"
14 /* bits #0-15 in revision.h */
16 #define COUNTED (1u<<16)
18 static const char rev_list_usage
[] =
19 "git-rev-list [OPTION] <commit-id>... [ -- paths... ]\n"
33 " formatting output:\n"
35 " --objects | --objects-edge\n"
37 " --header | --pretty\n"
38 " --abbrev=nr | --no-abbrev\n"
47 static struct rev_info revs
;
49 static int bisect_list
;
50 static int show_timestamp
;
51 static int hdr_termination
;
52 static const char *header_prefix
;
54 static void finish_commit(struct commit
*commit
);
55 static void show_commit(struct commit
*commit
)
58 printf("%lu ", commit
->date
);
60 fputs(header_prefix
, stdout
);
61 if (commit
->object
.flags
& BOUNDARY
)
63 else if (revs
.left_right
) {
64 if (commit
->object
.flags
& SYMMETRIC_LEFT
)
69 if (revs
.abbrev_commit
&& revs
.abbrev
)
70 fputs(find_unique_abbrev(commit
->object
.sha1
, revs
.abbrev
),
73 fputs(sha1_to_hex(commit
->object
.sha1
), stdout
);
75 struct commit_list
*parents
= commit
->parents
;
77 printf(" %s", sha1_to_hex(parents
->item
->object
.sha1
));
78 parents
= parents
->next
;
81 show_decorations(commit
);
82 if (revs
.commit_format
== CMIT_FMT_ONELINE
)
87 if (revs
.verbose_header
) {
90 pretty_print_commit(revs
.commit_format
, commit
,
91 &buf
, revs
.abbrev
, NULL
, NULL
,
94 printf("%s%c", buf
.buf
, hdr_termination
);
97 maybe_flush_or_die(stdout
, "stdout");
98 finish_commit(commit
);
101 static void finish_commit(struct commit
*commit
)
103 if (commit
->parents
) {
104 free_commit_list(commit
->parents
);
105 commit
->parents
= NULL
;
107 free(commit
->buffer
);
108 commit
->buffer
= NULL
;
111 static void finish_object(struct object_array_entry
*p
)
113 if (p
->item
->type
== OBJ_BLOB
&& !has_sha1_file(p
->item
->sha1
))
114 die("missing blob object '%s'", sha1_to_hex(p
->item
->sha1
));
117 static void show_object(struct object_array_entry
*p
)
119 /* An object with name "foo\n0000000..." can be used to
120 * confuse downstream git-pack-objects very badly.
122 const char *ep
= strchr(p
->name
, '\n');
126 printf("%s %.*s\n", sha1_to_hex(p
->item
->sha1
),
127 (int) (ep
- p
->name
),
131 printf("%s %s\n", sha1_to_hex(p
->item
->sha1
), p
->name
);
134 static void show_edge(struct commit
*commit
)
136 printf("-%s\n", sha1_to_hex(commit
->object
.sha1
));
140 * This is a truly stupid algorithm, but it's only
141 * used for bisection, and we just don't care enough.
143 * We care just barely enough to avoid recursing for
146 static int count_distance(struct commit_list
*entry
)
151 struct commit
*commit
= entry
->item
;
152 struct commit_list
*p
;
154 if (commit
->object
.flags
& (UNINTERESTING
| COUNTED
))
156 if (!(commit
->object
.flags
& TREESAME
))
158 commit
->object
.flags
|= COUNTED
;
164 nr
+= count_distance(p
);
173 static void clear_distance(struct commit_list
*list
)
176 struct commit
*commit
= list
->item
;
177 commit
->object
.flags
&= ~COUNTED
;
182 #define DEBUG_BISECT 0
184 static inline int weight(struct commit_list
*elem
)
186 return *((int*)(elem
->item
->util
));
189 static inline void weight_set(struct commit_list
*elem
, int weight
)
191 *((int*)(elem
->item
->util
)) = weight
;
194 static int count_interesting_parents(struct commit
*commit
)
196 struct commit_list
*p
;
199 for (count
= 0, p
= commit
->parents
; p
; p
= p
->next
) {
200 if (p
->item
->object
.flags
& UNINTERESTING
)
207 static inline int halfway(struct commit_list
*p
, int nr
)
210 * Don't short-cut something we are not going to return!
212 if (p
->item
->object
.flags
& TREESAME
)
217 * 2 and 3 are halfway of 5.
218 * 3 is halfway of 6 but 2 and 4 are not.
220 switch (2 * weight(p
) - nr
) {
221 case -1: case 0: case 1:
229 #define show_list(a,b,c,d) do { ; } while (0)
231 static void show_list(const char *debug
, int counted
, int nr
,
232 struct commit_list
*list
)
234 struct commit_list
*p
;
236 fprintf(stderr
, "%s (%d/%d)\n", debug
, counted
, nr
);
238 for (p
= list
; p
; p
= p
->next
) {
239 struct commit_list
*pp
;
240 struct commit
*commit
= p
->item
;
241 unsigned flags
= commit
->object
.flags
;
242 enum object_type type
;
244 char *buf
= read_sha1_file(commit
->object
.sha1
, &type
, &size
);
247 fprintf(stderr
, "%c%c%c ",
248 (flags
& TREESAME
) ? ' ' : 'T',
249 (flags
& UNINTERESTING
) ? 'U' : ' ',
250 (flags
& COUNTED
) ? 'C' : ' ');
252 fprintf(stderr
, "%3d", weight(p
));
254 fprintf(stderr
, "---");
255 fprintf(stderr
, " %.*s", 8, sha1_to_hex(commit
->object
.sha1
));
256 for (pp
= commit
->parents
; pp
; pp
= pp
->next
)
257 fprintf(stderr
, " %.*s", 8,
258 sha1_to_hex(pp
->item
->object
.sha1
));
260 sp
= strstr(buf
, "\n\n");
263 for (ep
= sp
; *ep
&& *ep
!= '\n'; ep
++)
265 fprintf(stderr
, " %.*s", (int)(ep
- sp
), sp
);
267 fprintf(stderr
, "\n");
270 #endif /* DEBUG_BISECT */
272 static struct commit_list
*best_bisection(struct commit_list
*list
, int nr
)
274 struct commit_list
*p
, *best
;
275 int best_distance
= -1;
278 for (p
= list
; p
; p
= p
->next
) {
280 unsigned flags
= p
->item
->object
.flags
;
282 if (flags
& TREESAME
)
284 distance
= weight(p
);
285 if (nr
- distance
< distance
)
286 distance
= nr
- distance
;
287 if (distance
> best_distance
) {
289 best_distance
= distance
;
297 struct commit
*commit
;
301 static int compare_commit_dist(const void *a_
, const void *b_
)
303 struct commit_dist
*a
, *b
;
305 a
= (struct commit_dist
*)a_
;
306 b
= (struct commit_dist
*)b_
;
307 if (a
->distance
!= b
->distance
)
308 return b
->distance
- a
->distance
; /* desc sort */
309 return hashcmp(a
->commit
->object
.sha1
, b
->commit
->object
.sha1
);
312 static struct commit_list
*best_bisection_sorted(struct commit_list
*list
, int nr
)
314 struct commit_list
*p
;
315 struct commit_dist
*array
= xcalloc(nr
, sizeof(*array
));
318 for (p
= list
, cnt
= 0; p
; p
= p
->next
) {
320 unsigned flags
= p
->item
->object
.flags
;
322 if (flags
& TREESAME
)
324 distance
= weight(p
);
325 if (nr
- distance
< distance
)
326 distance
= nr
- distance
;
327 array
[cnt
].commit
= p
->item
;
328 array
[cnt
].distance
= distance
;
331 qsort(array
, cnt
, sizeof(*array
), compare_commit_dist
);
332 for (p
= list
, i
= 0; i
< cnt
; i
++) {
333 struct name_decoration
*r
= xmalloc(sizeof(*r
) + 100);
334 struct object
*obj
= &(array
[i
].commit
->object
);
336 sprintf(r
->name
, "dist=%d", array
[i
].distance
);
337 r
->next
= add_decoration(&name_decoration
, obj
, r
);
338 p
->item
= array
[i
].commit
;
348 * zero or positive weight is the number of interesting commits it can
349 * reach, including itself. Especially, weight = 0 means it does not
350 * reach any tree-changing commits (e.g. just above uninteresting one
351 * but traversal is with pathspec).
353 * weight = -1 means it has one parent and its distance is yet to
356 * weight = -2 means it has more than one parent and its distance is
357 * unknown. After running count_distance() first, they will get zero
358 * or positive distance.
360 static struct commit_list
*do_find_bisection(struct commit_list
*list
,
361 int nr
, int *weights
,
365 struct commit_list
*p
;
369 for (n
= 0, p
= list
; p
; p
= p
->next
) {
370 struct commit
*commit
= p
->item
;
371 unsigned flags
= commit
->object
.flags
;
373 p
->item
->util
= &weights
[n
++];
374 switch (count_interesting_parents(commit
)) {
376 if (!(flags
& TREESAME
)) {
379 show_list("bisection 2 count one",
383 * otherwise, it is known not to reach any
384 * tree-changing commit and gets weight 0.
396 show_list("bisection 2 initialize", counted
, nr
, list
);
399 * If you have only one parent in the resulting set
400 * then you can reach one commit more than that parent
401 * can reach. So we do not have to run the expensive
402 * count_distance() for single strand of pearls.
404 * However, if you have more than one parents, you cannot
405 * just add their distance and one for yourself, since
406 * they usually reach the same ancestor and you would
407 * end up counting them twice that way.
409 * So we will first count distance of merges the usual
410 * way, and then fill the blanks using cheaper algorithm.
412 for (p
= list
; p
; p
= p
->next
) {
413 if (p
->item
->object
.flags
& UNINTERESTING
)
417 weight_set(p
, count_distance(p
));
418 clear_distance(list
);
420 /* Does it happen to be at exactly half-way? */
421 if (!find_all
&& halfway(p
, nr
))
426 show_list("bisection 2 count_distance", counted
, nr
, list
);
428 while (counted
< nr
) {
429 for (p
= list
; p
; p
= p
->next
) {
430 struct commit_list
*q
;
431 unsigned flags
= p
->item
->object
.flags
;
435 for (q
= p
->item
->parents
; q
; q
= q
->next
) {
436 if (q
->item
->object
.flags
& UNINTERESTING
)
445 * weight for p is unknown but q is known.
446 * add one for p itself if p is to be counted,
447 * otherwise inherit it from q directly.
449 if (!(flags
& TREESAME
)) {
450 weight_set(p
, weight(q
)+1);
452 show_list("bisection 2 count one",
456 weight_set(p
, weight(q
));
458 /* Does it happen to be at exactly half-way? */
459 if (!find_all
&& halfway(p
, nr
))
464 show_list("bisection 2 counted all", counted
, nr
, list
);
467 return best_bisection(list
, nr
);
469 return best_bisection_sorted(list
, nr
);
472 static struct commit_list
*find_bisection(struct commit_list
*list
,
473 int *reaches
, int *all
,
477 struct commit_list
*p
, *best
, *next
, *last
;
480 show_list("bisection 2 entry", 0, 0, list
);
483 * Count the number of total and tree-changing items on the
484 * list, while reversing the list.
486 for (nr
= on_list
= 0, last
= NULL
, p
= list
;
489 unsigned flags
= p
->item
->object
.flags
;
492 if (flags
& UNINTERESTING
)
496 if (!(flags
& TREESAME
))
501 show_list("bisection 2 sorted", 0, nr
, list
);
504 weights
= xcalloc(on_list
, sizeof(*weights
));
506 /* Do the real work of finding bisection commit. */
507 best
= do_find_bisection(list
, nr
, weights
, find_all
);
511 *reaches
= weight(best
);
517 static void read_revisions_from_stdin(struct rev_info
*revs
)
521 while (fgets(line
, sizeof(line
), stdin
) != NULL
) {
522 int len
= strlen(line
);
523 if (len
&& line
[len
- 1] == '\n')
528 die("options not supported in --stdin mode");
529 if (handle_revision_arg(line
, revs
, 0, 1))
530 die("bad revision '%s'", line
);
534 int cmd_rev_list(int argc
, const char **argv
, const char *prefix
)
536 struct commit_list
*list
;
538 int read_from_stdin
= 0;
539 int bisect_show_vars
= 0;
540 int bisect_find_all
= 0;
543 git_config(git_default_config
);
544 init_revisions(&revs
, prefix
);
546 revs
.commit_format
= CMIT_FMT_UNSPECIFIED
;
547 argc
= setup_revisions(argc
, argv
, &revs
, NULL
);
549 for (i
= 1 ; i
< argc
; i
++) {
550 const char *arg
= argv
[i
];
552 if (!strcmp(arg
, "--header")) {
553 revs
.verbose_header
= 1;
556 if (!strcmp(arg
, "--timestamp")) {
560 if (!strcmp(arg
, "--bisect")) {
564 if (!strcmp(arg
, "--bisect-all")) {
569 if (!strcmp(arg
, "--bisect-vars")) {
571 bisect_show_vars
= 1;
574 if (!strcmp(arg
, "--stdin")) {
575 if (read_from_stdin
++)
576 die("--stdin given twice?");
577 read_revisions_from_stdin(&revs
);
580 if (!strcmp(arg
, "--quiet")) {
584 usage(rev_list_usage
);
587 if (revs
.commit_format
!= CMIT_FMT_UNSPECIFIED
) {
588 /* The command line has a --pretty */
589 hdr_termination
= '\n';
590 if (revs
.commit_format
== CMIT_FMT_ONELINE
)
593 header_prefix
= "commit ";
595 else if (revs
.verbose_header
)
596 /* Only --header was specified */
597 revs
.commit_format
= CMIT_FMT_RAW
;
602 (!(revs
.tag_objects
||revs
.tree_objects
||revs
.blob_objects
) &&
603 !revs
.pending
.nr
)) ||
605 usage(rev_list_usage
);
607 save_commit_buffer
= revs
.verbose_header
|| revs
.grep_filter
;
608 track_object_refs
= 0;
612 if (prepare_revision_walk(&revs
))
613 die("revision walk setup failed");
614 if (revs
.tree_objects
)
615 mark_edges_uninteresting(revs
.commits
, &revs
, show_edge
);
618 int reaches
= reaches
, all
= all
;
620 revs
.commits
= find_bisection(revs
.commits
, &reaches
, &all
,
622 if (bisect_show_vars
) {
628 * revs.commits can reach "reaches" commits among
629 * "all" commits. If it is good, then there are
630 * (all-reaches) commits left to be bisected.
631 * On the other hand, if it is bad, then the set
632 * to bisect is "reaches".
633 * A bisect set of size N has (N-1) commits further
634 * to test, as we already know one bad one.
639 strcpy(hex
, sha1_to_hex(revs
.commits
->item
->object
.sha1
));
641 if (bisect_find_all
) {
642 traverse_commit_list(&revs
, show_commit
, show_object
);
646 printf("bisect_rev=%s\n"
660 traverse_commit_list(&revs
,
661 quiet
? finish_commit
: show_commit
,
662 quiet
? finish_object
: show_object
);