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"
32 " formatting output:\n"
34 " --objects | --objects-edge\n"
36 " --header | --pretty\n"
37 " --abbrev=nr | --no-abbrev\n"
46 static struct rev_info revs
;
48 static int bisect_list
;
49 static int show_timestamp
;
50 static int hdr_termination
;
51 static const char *header_prefix
;
53 static void show_commit(struct commit
*commit
)
56 printf("%lu ", commit
->date
);
58 fputs(header_prefix
, stdout
);
59 if (commit
->object
.flags
& BOUNDARY
)
61 else if (revs
.left_right
) {
62 if (commit
->object
.flags
& SYMMETRIC_LEFT
)
67 if (revs
.abbrev_commit
&& revs
.abbrev
)
68 fputs(find_unique_abbrev(commit
->object
.sha1
, revs
.abbrev
),
71 fputs(sha1_to_hex(commit
->object
.sha1
), stdout
);
73 struct commit_list
*parents
= commit
->parents
;
75 printf(" %s", sha1_to_hex(parents
->item
->object
.sha1
));
76 parents
= parents
->next
;
79 show_decorations(commit
);
80 if (revs
.commit_format
== CMIT_FMT_ONELINE
)
85 if (revs
.verbose_header
) {
88 pretty_print_commit(revs
.commit_format
, commit
,
89 &buf
, revs
.abbrev
, NULL
, NULL
,
92 printf("%s%c", buf
.buf
, hdr_termination
);
95 maybe_flush_or_die(stdout
, "stdout");
96 if (commit
->parents
) {
97 free_commit_list(commit
->parents
);
98 commit
->parents
= NULL
;
100 free(commit
->buffer
);
101 commit
->buffer
= NULL
;
104 static void show_object(struct object_array_entry
*p
)
106 /* An object with name "foo\n0000000..." can be used to
107 * confuse downstream git-pack-objects very badly.
109 const char *ep
= strchr(p
->name
, '\n');
111 if (p
->item
->type
== OBJ_BLOB
&& !has_sha1_file(p
->item
->sha1
))
112 die("missing blob object '%s'", sha1_to_hex(p
->item
->sha1
));
115 printf("%s %.*s\n", sha1_to_hex(p
->item
->sha1
),
116 (int) (ep
- p
->name
),
120 printf("%s %s\n", sha1_to_hex(p
->item
->sha1
), p
->name
);
123 static void show_edge(struct commit
*commit
)
125 printf("-%s\n", sha1_to_hex(commit
->object
.sha1
));
129 * This is a truly stupid algorithm, but it's only
130 * used for bisection, and we just don't care enough.
132 * We care just barely enough to avoid recursing for
135 static int count_distance(struct commit_list
*entry
)
140 struct commit
*commit
= entry
->item
;
141 struct commit_list
*p
;
143 if (commit
->object
.flags
& (UNINTERESTING
| COUNTED
))
145 if (!revs
.prune_fn
|| (commit
->object
.flags
& TREECHANGE
))
147 commit
->object
.flags
|= COUNTED
;
153 nr
+= count_distance(p
);
162 static void clear_distance(struct commit_list
*list
)
165 struct commit
*commit
= list
->item
;
166 commit
->object
.flags
&= ~COUNTED
;
171 #define DEBUG_BISECT 0
173 static inline int weight(struct commit_list
*elem
)
175 return *((int*)(elem
->item
->util
));
178 static inline void weight_set(struct commit_list
*elem
, int weight
)
180 *((int*)(elem
->item
->util
)) = weight
;
183 static int count_interesting_parents(struct commit
*commit
)
185 struct commit_list
*p
;
188 for (count
= 0, p
= commit
->parents
; p
; p
= p
->next
) {
189 if (p
->item
->object
.flags
& UNINTERESTING
)
196 static inline int halfway(struct commit_list
*p
, int nr
)
199 * Don't short-cut something we are not going to return!
201 if (revs
.prune_fn
&& !(p
->item
->object
.flags
& TREECHANGE
))
206 * 2 and 3 are halfway of 5.
207 * 3 is halfway of 6 but 2 and 4 are not.
209 switch (2 * weight(p
) - nr
) {
210 case -1: case 0: case 1:
218 #define show_list(a,b,c,d) do { ; } while (0)
220 static void show_list(const char *debug
, int counted
, int nr
,
221 struct commit_list
*list
)
223 struct commit_list
*p
;
225 fprintf(stderr
, "%s (%d/%d)\n", debug
, counted
, nr
);
227 for (p
= list
; p
; p
= p
->next
) {
228 struct commit_list
*pp
;
229 struct commit
*commit
= p
->item
;
230 unsigned flags
= commit
->object
.flags
;
231 enum object_type type
;
233 char *buf
= read_sha1_file(commit
->object
.sha1
, &type
, &size
);
236 fprintf(stderr
, "%c%c%c ",
237 (flags
& TREECHANGE
) ? 'T' : ' ',
238 (flags
& UNINTERESTING
) ? 'U' : ' ',
239 (flags
& COUNTED
) ? 'C' : ' ');
241 fprintf(stderr
, "%3d", weight(p
));
243 fprintf(stderr
, "---");
244 fprintf(stderr
, " %.*s", 8, sha1_to_hex(commit
->object
.sha1
));
245 for (pp
= commit
->parents
; pp
; pp
= pp
->next
)
246 fprintf(stderr
, " %.*s", 8,
247 sha1_to_hex(pp
->item
->object
.sha1
));
249 sp
= strstr(buf
, "\n\n");
252 for (ep
= sp
; *ep
&& *ep
!= '\n'; ep
++)
254 fprintf(stderr
, " %.*s", (int)(ep
- sp
), sp
);
256 fprintf(stderr
, "\n");
259 #endif /* DEBUG_BISECT */
261 static struct commit_list
*best_bisection(struct commit_list
*list
, int nr
)
263 struct commit_list
*p
, *best
;
264 int best_distance
= -1;
267 for (p
= list
; p
; p
= p
->next
) {
269 unsigned flags
= p
->item
->object
.flags
;
271 if (revs
.prune_fn
&& !(flags
& TREECHANGE
))
273 distance
= weight(p
);
274 if (nr
- distance
< distance
)
275 distance
= nr
- distance
;
276 if (distance
> best_distance
) {
278 best_distance
= distance
;
286 struct commit
*commit
;
290 static int compare_commit_dist(const void *a_
, const void *b_
)
292 struct commit_dist
*a
, *b
;
294 a
= (struct commit_dist
*)a_
;
295 b
= (struct commit_dist
*)b_
;
296 if (a
->distance
!= b
->distance
)
297 return b
->distance
- a
->distance
; /* desc sort */
298 return hashcmp(a
->commit
->object
.sha1
, b
->commit
->object
.sha1
);
301 static struct commit_list
*best_bisection_sorted(struct commit_list
*list
, int nr
)
303 struct commit_list
*p
;
304 struct commit_dist
*array
= xcalloc(nr
, sizeof(*array
));
307 for (p
= list
, cnt
= 0; p
; p
= p
->next
) {
309 unsigned flags
= p
->item
->object
.flags
;
311 if (revs
.prune_fn
&& !(flags
& TREECHANGE
))
313 distance
= weight(p
);
314 if (nr
- distance
< distance
)
315 distance
= nr
- distance
;
316 array
[cnt
].commit
= p
->item
;
317 array
[cnt
].distance
= distance
;
320 qsort(array
, cnt
, sizeof(*array
), compare_commit_dist
);
321 for (p
= list
, i
= 0; i
< cnt
; i
++) {
322 struct name_decoration
*r
= xmalloc(sizeof(*r
) + 100);
323 struct object
*obj
= &(array
[i
].commit
->object
);
325 sprintf(r
->name
, "dist=%d", array
[i
].distance
);
326 r
->next
= add_decoration(&name_decoration
, obj
, r
);
327 p
->item
= array
[i
].commit
;
337 * zero or positive weight is the number of interesting commits it can
338 * reach, including itself. Especially, weight = 0 means it does not
339 * reach any tree-changing commits (e.g. just above uninteresting one
340 * but traversal is with pathspec).
342 * weight = -1 means it has one parent and its distance is yet to
345 * weight = -2 means it has more than one parent and its distance is
346 * unknown. After running count_distance() first, they will get zero
347 * or positive distance.
349 static struct commit_list
*do_find_bisection(struct commit_list
*list
,
350 int nr
, int *weights
,
354 struct commit_list
*p
;
358 for (n
= 0, p
= list
; p
; p
= p
->next
) {
359 struct commit
*commit
= p
->item
;
360 unsigned flags
= commit
->object
.flags
;
362 p
->item
->util
= &weights
[n
++];
363 switch (count_interesting_parents(commit
)) {
365 if (!revs
.prune_fn
|| (flags
& TREECHANGE
)) {
368 show_list("bisection 2 count one",
372 * otherwise, it is known not to reach any
373 * tree-changing commit and gets weight 0.
385 show_list("bisection 2 initialize", counted
, nr
, list
);
388 * If you have only one parent in the resulting set
389 * then you can reach one commit more than that parent
390 * can reach. So we do not have to run the expensive
391 * count_distance() for single strand of pearls.
393 * However, if you have more than one parents, you cannot
394 * just add their distance and one for yourself, since
395 * they usually reach the same ancestor and you would
396 * end up counting them twice that way.
398 * So we will first count distance of merges the usual
399 * way, and then fill the blanks using cheaper algorithm.
401 for (p
= list
; p
; p
= p
->next
) {
402 if (p
->item
->object
.flags
& UNINTERESTING
)
406 weight_set(p
, count_distance(p
));
407 clear_distance(list
);
409 /* Does it happen to be at exactly half-way? */
410 if (!find_all
&& halfway(p
, nr
))
415 show_list("bisection 2 count_distance", counted
, nr
, list
);
417 while (counted
< nr
) {
418 for (p
= list
; p
; p
= p
->next
) {
419 struct commit_list
*q
;
420 unsigned flags
= p
->item
->object
.flags
;
424 for (q
= p
->item
->parents
; q
; q
= q
->next
) {
425 if (q
->item
->object
.flags
& UNINTERESTING
)
434 * weight for p is unknown but q is known.
435 * add one for p itself if p is to be counted,
436 * otherwise inherit it from q directly.
438 if (!revs
.prune_fn
|| (flags
& TREECHANGE
)) {
439 weight_set(p
, weight(q
)+1);
441 show_list("bisection 2 count one",
445 weight_set(p
, weight(q
));
447 /* Does it happen to be at exactly half-way? */
448 if (!find_all
&& halfway(p
, nr
))
453 show_list("bisection 2 counted all", counted
, nr
, list
);
456 return best_bisection(list
, nr
);
458 return best_bisection_sorted(list
, nr
);
461 static struct commit_list
*find_bisection(struct commit_list
*list
,
462 int *reaches
, int *all
,
466 struct commit_list
*p
, *best
, *next
, *last
;
469 show_list("bisection 2 entry", 0, 0, list
);
472 * Count the number of total and tree-changing items on the
473 * list, while reversing the list.
475 for (nr
= on_list
= 0, last
= NULL
, p
= list
;
478 unsigned flags
= p
->item
->object
.flags
;
481 if (flags
& UNINTERESTING
)
485 if (!revs
.prune_fn
|| (flags
& TREECHANGE
))
490 show_list("bisection 2 sorted", 0, nr
, list
);
493 weights
= xcalloc(on_list
, sizeof(*weights
));
495 /* Do the real work of finding bisection commit. */
496 best
= do_find_bisection(list
, nr
, weights
, find_all
);
500 *reaches
= weight(best
);
506 static void read_revisions_from_stdin(struct rev_info
*revs
)
510 while (fgets(line
, sizeof(line
), stdin
) != NULL
) {
511 int len
= strlen(line
);
512 if (line
[len
- 1] == '\n')
517 die("options not supported in --stdin mode");
518 if (handle_revision_arg(line
, revs
, 0, 1))
519 die("bad revision '%s'", line
);
523 int cmd_rev_list(int argc
, const char **argv
, const char *prefix
)
525 struct commit_list
*list
;
527 int read_from_stdin
= 0;
528 int bisect_show_vars
= 0;
529 int bisect_find_all
= 0;
531 git_config(git_default_config
);
532 init_revisions(&revs
, prefix
);
534 revs
.commit_format
= CMIT_FMT_UNSPECIFIED
;
535 argc
= setup_revisions(argc
, argv
, &revs
, NULL
);
537 for (i
= 1 ; i
< argc
; i
++) {
538 const char *arg
= argv
[i
];
540 if (!strcmp(arg
, "--header")) {
541 revs
.verbose_header
= 1;
544 if (!strcmp(arg
, "--timestamp")) {
548 if (!strcmp(arg
, "--bisect")) {
552 if (!strcmp(arg
, "--bisect-all")) {
557 if (!strcmp(arg
, "--bisect-vars")) {
559 bisect_show_vars
= 1;
562 if (!strcmp(arg
, "--stdin")) {
563 if (read_from_stdin
++)
564 die("--stdin given twice?");
565 read_revisions_from_stdin(&revs
);
568 usage(rev_list_usage
);
571 if (revs
.commit_format
!= CMIT_FMT_UNSPECIFIED
) {
572 /* The command line has a --pretty */
573 hdr_termination
= '\n';
574 if (revs
.commit_format
== CMIT_FMT_ONELINE
)
577 header_prefix
= "commit ";
579 else if (revs
.verbose_header
)
580 /* Only --header was specified */
581 revs
.commit_format
= CMIT_FMT_RAW
;
586 (!(revs
.tag_objects
||revs
.tree_objects
||revs
.blob_objects
) &&
587 !revs
.pending
.nr
)) ||
589 usage(rev_list_usage
);
591 save_commit_buffer
= revs
.verbose_header
|| revs
.grep_filter
;
592 track_object_refs
= 0;
596 prepare_revision_walk(&revs
);
597 if (revs
.tree_objects
)
598 mark_edges_uninteresting(revs
.commits
, &revs
, show_edge
);
601 int reaches
= reaches
, all
= all
;
603 revs
.commits
= find_bisection(revs
.commits
, &reaches
, &all
,
605 if (bisect_show_vars
) {
611 * revs.commits can reach "reaches" commits among
612 * "all" commits. If it is good, then there are
613 * (all-reaches) commits left to be bisected.
614 * On the other hand, if it is bad, then the set
615 * to bisect is "reaches".
616 * A bisect set of size N has (N-1) commits further
617 * to test, as we already know one bad one.
622 strcpy(hex
, sha1_to_hex(revs
.commits
->item
->object
.sha1
));
624 if (bisect_find_all
) {
625 traverse_commit_list(&revs
, show_commit
, show_object
);
629 printf("bisect_rev=%s\n"
643 traverse_commit_list(&revs
, show_commit
, show_object
);