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"
37 " formatting output:\n"
39 " --objects | --objects-edge\n"
41 " --header | --pretty\n"
42 " --abbrev=nr | --no-abbrev\n"
51 static struct rev_info revs
;
53 static int bisect_list
;
54 static int show_timestamp
;
55 static int hdr_termination
;
56 static const char *header_prefix
;
58 static void finish_commit(struct commit
*commit
);
59 static void show_commit(struct commit
*commit
)
62 printf("%lu ", commit
->date
);
64 fputs(header_prefix
, stdout
);
65 if (commit
->object
.flags
& BOUNDARY
)
67 else if (commit
->object
.flags
& UNINTERESTING
)
69 else if (revs
.left_right
) {
70 if (commit
->object
.flags
& SYMMETRIC_LEFT
)
75 if (revs
.abbrev_commit
&& revs
.abbrev
)
76 fputs(find_unique_abbrev(commit
->object
.sha1
, revs
.abbrev
),
79 fputs(sha1_to_hex(commit
->object
.sha1
), stdout
);
81 struct commit_list
*parents
= commit
->parents
;
83 printf(" %s", sha1_to_hex(parents
->item
->object
.sha1
));
84 parents
= parents
->next
;
87 show_decorations(commit
);
88 if (revs
.commit_format
== CMIT_FMT_ONELINE
)
93 if (revs
.verbose_header
&& commit
->buffer
) {
96 pretty_print_commit(revs
.commit_format
, commit
,
97 &buf
, revs
.abbrev
, NULL
, NULL
,
100 printf("%s%c", buf
.buf
, hdr_termination
);
101 strbuf_release(&buf
);
103 maybe_flush_or_die(stdout
, "stdout");
104 finish_commit(commit
);
107 static void finish_commit(struct commit
*commit
)
109 if (commit
->parents
) {
110 free_commit_list(commit
->parents
);
111 commit
->parents
= NULL
;
113 free(commit
->buffer
);
114 commit
->buffer
= NULL
;
117 static void finish_object(struct object_array_entry
*p
)
119 if (p
->item
->type
== OBJ_BLOB
&& !has_sha1_file(p
->item
->sha1
))
120 die("missing blob object '%s'", sha1_to_hex(p
->item
->sha1
));
123 static void show_object(struct object_array_entry
*p
)
125 /* An object with name "foo\n0000000..." can be used to
126 * confuse downstream git-pack-objects very badly.
128 const char *ep
= strchr(p
->name
, '\n');
132 printf("%s %.*s\n", sha1_to_hex(p
->item
->sha1
),
133 (int) (ep
- p
->name
),
137 printf("%s %s\n", sha1_to_hex(p
->item
->sha1
), p
->name
);
140 static void show_edge(struct commit
*commit
)
142 printf("-%s\n", sha1_to_hex(commit
->object
.sha1
));
146 * This is a truly stupid algorithm, but it's only
147 * used for bisection, and we just don't care enough.
149 * We care just barely enough to avoid recursing for
152 static int count_distance(struct commit_list
*entry
)
157 struct commit
*commit
= entry
->item
;
158 struct commit_list
*p
;
160 if (commit
->object
.flags
& (UNINTERESTING
| COUNTED
))
162 if (!(commit
->object
.flags
& TREESAME
))
164 commit
->object
.flags
|= COUNTED
;
170 nr
+= count_distance(p
);
179 static void clear_distance(struct commit_list
*list
)
182 struct commit
*commit
= list
->item
;
183 commit
->object
.flags
&= ~COUNTED
;
188 #define DEBUG_BISECT 0
190 static inline int weight(struct commit_list
*elem
)
192 return *((int*)(elem
->item
->util
));
195 static inline void weight_set(struct commit_list
*elem
, int weight
)
197 *((int*)(elem
->item
->util
)) = weight
;
200 static int count_interesting_parents(struct commit
*commit
)
202 struct commit_list
*p
;
205 for (count
= 0, p
= commit
->parents
; p
; p
= p
->next
) {
206 if (p
->item
->object
.flags
& UNINTERESTING
)
213 static inline int halfway(struct commit_list
*p
, int nr
)
216 * Don't short-cut something we are not going to return!
218 if (p
->item
->object
.flags
& TREESAME
)
223 * 2 and 3 are halfway of 5.
224 * 3 is halfway of 6 but 2 and 4 are not.
226 switch (2 * weight(p
) - nr
) {
227 case -1: case 0: case 1:
235 #define show_list(a,b,c,d) do { ; } while (0)
237 static void show_list(const char *debug
, int counted
, int nr
,
238 struct commit_list
*list
)
240 struct commit_list
*p
;
242 fprintf(stderr
, "%s (%d/%d)\n", debug
, counted
, nr
);
244 for (p
= list
; p
; p
= p
->next
) {
245 struct commit_list
*pp
;
246 struct commit
*commit
= p
->item
;
247 unsigned flags
= commit
->object
.flags
;
248 enum object_type type
;
250 char *buf
= read_sha1_file(commit
->object
.sha1
, &type
, &size
);
253 fprintf(stderr
, "%c%c%c ",
254 (flags
& TREESAME
) ? ' ' : 'T',
255 (flags
& UNINTERESTING
) ? 'U' : ' ',
256 (flags
& COUNTED
) ? 'C' : ' ');
258 fprintf(stderr
, "%3d", weight(p
));
260 fprintf(stderr
, "---");
261 fprintf(stderr
, " %.*s", 8, sha1_to_hex(commit
->object
.sha1
));
262 for (pp
= commit
->parents
; pp
; pp
= pp
->next
)
263 fprintf(stderr
, " %.*s", 8,
264 sha1_to_hex(pp
->item
->object
.sha1
));
266 sp
= strstr(buf
, "\n\n");
269 for (ep
= sp
; *ep
&& *ep
!= '\n'; ep
++)
271 fprintf(stderr
, " %.*s", (int)(ep
- sp
), sp
);
273 fprintf(stderr
, "\n");
276 #endif /* DEBUG_BISECT */
278 static struct commit_list
*best_bisection(struct commit_list
*list
, int nr
)
280 struct commit_list
*p
, *best
;
281 int best_distance
= -1;
284 for (p
= list
; p
; p
= p
->next
) {
286 unsigned flags
= p
->item
->object
.flags
;
288 if (flags
& TREESAME
)
290 distance
= weight(p
);
291 if (nr
- distance
< distance
)
292 distance
= nr
- distance
;
293 if (distance
> best_distance
) {
295 best_distance
= distance
;
303 struct commit
*commit
;
307 static int compare_commit_dist(const void *a_
, const void *b_
)
309 struct commit_dist
*a
, *b
;
311 a
= (struct commit_dist
*)a_
;
312 b
= (struct commit_dist
*)b_
;
313 if (a
->distance
!= b
->distance
)
314 return b
->distance
- a
->distance
; /* desc sort */
315 return hashcmp(a
->commit
->object
.sha1
, b
->commit
->object
.sha1
);
318 static struct commit_list
*best_bisection_sorted(struct commit_list
*list
, int nr
)
320 struct commit_list
*p
;
321 struct commit_dist
*array
= xcalloc(nr
, sizeof(*array
));
324 for (p
= list
, cnt
= 0; p
; p
= p
->next
) {
326 unsigned flags
= p
->item
->object
.flags
;
328 if (flags
& TREESAME
)
330 distance
= weight(p
);
331 if (nr
- distance
< distance
)
332 distance
= nr
- distance
;
333 array
[cnt
].commit
= p
->item
;
334 array
[cnt
].distance
= distance
;
337 qsort(array
, cnt
, sizeof(*array
), compare_commit_dist
);
338 for (p
= list
, i
= 0; i
< cnt
; i
++) {
339 struct name_decoration
*r
= xmalloc(sizeof(*r
) + 100);
340 struct object
*obj
= &(array
[i
].commit
->object
);
342 sprintf(r
->name
, "dist=%d", array
[i
].distance
);
343 r
->next
= add_decoration(&name_decoration
, obj
, r
);
344 p
->item
= array
[i
].commit
;
354 * zero or positive weight is the number of interesting commits it can
355 * reach, including itself. Especially, weight = 0 means it does not
356 * reach any tree-changing commits (e.g. just above uninteresting one
357 * but traversal is with pathspec).
359 * weight = -1 means it has one parent and its distance is yet to
362 * weight = -2 means it has more than one parent and its distance is
363 * unknown. After running count_distance() first, they will get zero
364 * or positive distance.
366 static struct commit_list
*do_find_bisection(struct commit_list
*list
,
367 int nr
, int *weights
,
371 struct commit_list
*p
;
375 for (n
= 0, p
= list
; p
; p
= p
->next
) {
376 struct commit
*commit
= p
->item
;
377 unsigned flags
= commit
->object
.flags
;
379 p
->item
->util
= &weights
[n
++];
380 switch (count_interesting_parents(commit
)) {
382 if (!(flags
& TREESAME
)) {
385 show_list("bisection 2 count one",
389 * otherwise, it is known not to reach any
390 * tree-changing commit and gets weight 0.
402 show_list("bisection 2 initialize", counted
, nr
, list
);
405 * If you have only one parent in the resulting set
406 * then you can reach one commit more than that parent
407 * can reach. So we do not have to run the expensive
408 * count_distance() for single strand of pearls.
410 * However, if you have more than one parents, you cannot
411 * just add their distance and one for yourself, since
412 * they usually reach the same ancestor and you would
413 * end up counting them twice that way.
415 * So we will first count distance of merges the usual
416 * way, and then fill the blanks using cheaper algorithm.
418 for (p
= list
; p
; p
= p
->next
) {
419 if (p
->item
->object
.flags
& UNINTERESTING
)
423 weight_set(p
, count_distance(p
));
424 clear_distance(list
);
426 /* Does it happen to be at exactly half-way? */
427 if (!find_all
&& halfway(p
, nr
))
432 show_list("bisection 2 count_distance", counted
, nr
, list
);
434 while (counted
< nr
) {
435 for (p
= list
; p
; p
= p
->next
) {
436 struct commit_list
*q
;
437 unsigned flags
= p
->item
->object
.flags
;
441 for (q
= p
->item
->parents
; q
; q
= q
->next
) {
442 if (q
->item
->object
.flags
& UNINTERESTING
)
451 * weight for p is unknown but q is known.
452 * add one for p itself if p is to be counted,
453 * otherwise inherit it from q directly.
455 if (!(flags
& TREESAME
)) {
456 weight_set(p
, weight(q
)+1);
458 show_list("bisection 2 count one",
462 weight_set(p
, weight(q
));
464 /* Does it happen to be at exactly half-way? */
465 if (!find_all
&& halfway(p
, nr
))
470 show_list("bisection 2 counted all", counted
, nr
, list
);
473 return best_bisection(list
, nr
);
475 return best_bisection_sorted(list
, nr
);
478 static struct commit_list
*find_bisection(struct commit_list
*list
,
479 int *reaches
, int *all
,
483 struct commit_list
*p
, *best
, *next
, *last
;
486 show_list("bisection 2 entry", 0, 0, list
);
489 * Count the number of total and tree-changing items on the
490 * list, while reversing the list.
492 for (nr
= on_list
= 0, last
= NULL
, p
= list
;
495 unsigned flags
= p
->item
->object
.flags
;
498 if (flags
& UNINTERESTING
)
502 if (!(flags
& TREESAME
))
507 show_list("bisection 2 sorted", 0, nr
, list
);
510 weights
= xcalloc(on_list
, sizeof(*weights
));
512 /* Do the real work of finding bisection commit. */
513 best
= do_find_bisection(list
, nr
, weights
, find_all
);
517 *reaches
= weight(best
);
523 static void read_revisions_from_stdin(struct rev_info
*revs
)
527 while (fgets(line
, sizeof(line
), stdin
) != NULL
) {
528 int len
= strlen(line
);
529 if (len
&& line
[len
- 1] == '\n')
534 die("options not supported in --stdin mode");
535 if (handle_revision_arg(line
, revs
, 0, 1))
536 die("bad revision '%s'", line
);
540 int cmd_rev_list(int argc
, const char **argv
, const char *prefix
)
542 struct commit_list
*list
;
544 int read_from_stdin
= 0;
545 int bisect_show_vars
= 0;
546 int bisect_find_all
= 0;
549 git_config(git_default_config
);
550 init_revisions(&revs
, prefix
);
552 revs
.commit_format
= CMIT_FMT_UNSPECIFIED
;
553 argc
= setup_revisions(argc
, argv
, &revs
, NULL
);
555 for (i
= 1 ; i
< argc
; i
++) {
556 const char *arg
= argv
[i
];
558 if (!strcmp(arg
, "--header")) {
559 revs
.verbose_header
= 1;
562 if (!strcmp(arg
, "--timestamp")) {
566 if (!strcmp(arg
, "--bisect")) {
570 if (!strcmp(arg
, "--bisect-all")) {
575 if (!strcmp(arg
, "--bisect-vars")) {
577 bisect_show_vars
= 1;
580 if (!strcmp(arg
, "--stdin")) {
581 if (read_from_stdin
++)
582 die("--stdin given twice?");
583 read_revisions_from_stdin(&revs
);
586 if (!strcmp(arg
, "--quiet")) {
590 usage(rev_list_usage
);
593 if (revs
.commit_format
!= CMIT_FMT_UNSPECIFIED
) {
594 /* The command line has a --pretty */
595 hdr_termination
= '\n';
596 if (revs
.commit_format
== CMIT_FMT_ONELINE
)
599 header_prefix
= "commit ";
601 else if (revs
.verbose_header
)
602 /* Only --header was specified */
603 revs
.commit_format
= CMIT_FMT_RAW
;
608 (!(revs
.tag_objects
||revs
.tree_objects
||revs
.blob_objects
) &&
609 !revs
.pending
.nr
)) ||
611 usage(rev_list_usage
);
613 save_commit_buffer
= revs
.verbose_header
|| revs
.grep_filter
;
617 if (prepare_revision_walk(&revs
))
618 die("revision walk setup failed");
619 if (revs
.tree_objects
)
620 mark_edges_uninteresting(revs
.commits
, &revs
, show_edge
);
623 int reaches
= reaches
, all
= all
;
625 revs
.commits
= find_bisection(revs
.commits
, &reaches
, &all
,
627 if (bisect_show_vars
) {
633 * revs.commits can reach "reaches" commits among
634 * "all" commits. If it is good, then there are
635 * (all-reaches) commits left to be bisected.
636 * On the other hand, if it is bad, then the set
637 * to bisect is "reaches".
638 * A bisect set of size N has (N-1) commits further
639 * to test, as we already know one bad one.
644 strcpy(hex
, sha1_to_hex(revs
.commits
->item
->object
.sha1
));
646 if (bisect_find_all
) {
647 traverse_commit_list(&revs
, show_commit
, show_object
);
651 printf("bisect_rev=%s\n"
665 traverse_commit_list(&revs
,
666 quiet
? finish_commit
: show_commit
,
667 quiet
? finish_object
: show_object
);