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
, revs
.date_mode
);
91 printf("%s%c", buf
.buf
, hdr_termination
);
94 maybe_flush_or_die(stdout
, "stdout");
95 if (commit
->parents
) {
96 free_commit_list(commit
->parents
);
97 commit
->parents
= NULL
;
100 commit
->buffer
= NULL
;
103 static void show_object(struct object_array_entry
*p
)
105 /* An object with name "foo\n0000000..." can be used to
106 * confuse downstream git-pack-objects very badly.
108 const char *ep
= strchr(p
->name
, '\n');
110 if (p
->item
->type
== OBJ_BLOB
&& !has_sha1_file(p
->item
->sha1
))
111 die("missing blob object '%s'", sha1_to_hex(p
->item
->sha1
));
114 printf("%s %.*s\n", sha1_to_hex(p
->item
->sha1
),
115 (int) (ep
- p
->name
),
119 printf("%s %s\n", sha1_to_hex(p
->item
->sha1
), p
->name
);
122 static void show_edge(struct commit
*commit
)
124 printf("-%s\n", sha1_to_hex(commit
->object
.sha1
));
128 * This is a truly stupid algorithm, but it's only
129 * used for bisection, and we just don't care enough.
131 * We care just barely enough to avoid recursing for
134 static int count_distance(struct commit_list
*entry
)
139 struct commit
*commit
= entry
->item
;
140 struct commit_list
*p
;
142 if (commit
->object
.flags
& (UNINTERESTING
| COUNTED
))
144 if (!revs
.prune_fn
|| (commit
->object
.flags
& TREECHANGE
))
146 commit
->object
.flags
|= COUNTED
;
152 nr
+= count_distance(p
);
161 static void clear_distance(struct commit_list
*list
)
164 struct commit
*commit
= list
->item
;
165 commit
->object
.flags
&= ~COUNTED
;
170 #define DEBUG_BISECT 0
172 static inline int weight(struct commit_list
*elem
)
174 return *((int*)(elem
->item
->util
));
177 static inline void weight_set(struct commit_list
*elem
, int weight
)
179 *((int*)(elem
->item
->util
)) = weight
;
182 static int count_interesting_parents(struct commit
*commit
)
184 struct commit_list
*p
;
187 for (count
= 0, p
= commit
->parents
; p
; p
= p
->next
) {
188 if (p
->item
->object
.flags
& UNINTERESTING
)
195 static inline int halfway(struct commit_list
*p
, int nr
)
198 * Don't short-cut something we are not going to return!
200 if (revs
.prune_fn
&& !(p
->item
->object
.flags
& TREECHANGE
))
205 * 2 and 3 are halfway of 5.
206 * 3 is halfway of 6 but 2 and 4 are not.
208 switch (2 * weight(p
) - nr
) {
209 case -1: case 0: case 1:
217 #define show_list(a,b,c,d) do { ; } while (0)
219 static void show_list(const char *debug
, int counted
, int nr
,
220 struct commit_list
*list
)
222 struct commit_list
*p
;
224 fprintf(stderr
, "%s (%d/%d)\n", debug
, counted
, nr
);
226 for (p
= list
; p
; p
= p
->next
) {
227 struct commit_list
*pp
;
228 struct commit
*commit
= p
->item
;
229 unsigned flags
= commit
->object
.flags
;
230 enum object_type type
;
232 char *buf
= read_sha1_file(commit
->object
.sha1
, &type
, &size
);
235 fprintf(stderr
, "%c%c%c ",
236 (flags
& TREECHANGE
) ? 'T' : ' ',
237 (flags
& UNINTERESTING
) ? 'U' : ' ',
238 (flags
& COUNTED
) ? 'C' : ' ');
240 fprintf(stderr
, "%3d", weight(p
));
242 fprintf(stderr
, "---");
243 fprintf(stderr
, " %.*s", 8, sha1_to_hex(commit
->object
.sha1
));
244 for (pp
= commit
->parents
; pp
; pp
= pp
->next
)
245 fprintf(stderr
, " %.*s", 8,
246 sha1_to_hex(pp
->item
->object
.sha1
));
248 sp
= strstr(buf
, "\n\n");
251 for (ep
= sp
; *ep
&& *ep
!= '\n'; ep
++)
253 fprintf(stderr
, " %.*s", (int)(ep
- sp
), sp
);
255 fprintf(stderr
, "\n");
258 #endif /* DEBUG_BISECT */
260 static struct commit_list
*best_bisection(struct commit_list
*list
, int nr
)
262 struct commit_list
*p
, *best
;
263 int best_distance
= -1;
266 for (p
= list
; p
; p
= p
->next
) {
268 unsigned flags
= p
->item
->object
.flags
;
270 if (revs
.prune_fn
&& !(flags
& TREECHANGE
))
272 distance
= weight(p
);
273 if (nr
- distance
< distance
)
274 distance
= nr
- distance
;
275 if (distance
> best_distance
) {
277 best_distance
= distance
;
285 struct commit
*commit
;
289 static int compare_commit_dist(const void *a_
, const void *b_
)
291 struct commit_dist
*a
, *b
;
293 a
= (struct commit_dist
*)a_
;
294 b
= (struct commit_dist
*)b_
;
295 if (a
->distance
!= b
->distance
)
296 return b
->distance
- a
->distance
; /* desc sort */
297 return hashcmp(a
->commit
->object
.sha1
, b
->commit
->object
.sha1
);
300 static struct commit_list
*best_bisection_sorted(struct commit_list
*list
, int nr
)
302 struct commit_list
*p
;
303 struct commit_dist
*array
= xcalloc(nr
, sizeof(*array
));
306 for (p
= list
, cnt
= 0; p
; p
= p
->next
) {
308 unsigned flags
= p
->item
->object
.flags
;
310 if (revs
.prune_fn
&& !(flags
& TREECHANGE
))
312 distance
= weight(p
);
313 if (nr
- distance
< distance
)
314 distance
= nr
- distance
;
315 array
[cnt
].commit
= p
->item
;
316 array
[cnt
].distance
= distance
;
319 qsort(array
, cnt
, sizeof(*array
), compare_commit_dist
);
320 for (p
= list
, i
= 0; i
< cnt
; i
++) {
321 struct name_decoration
*r
= xmalloc(sizeof(*r
) + 100);
322 struct object
*obj
= &(array
[i
].commit
->object
);
324 sprintf(r
->name
, "dist=%d", array
[i
].distance
);
325 r
->next
= add_decoration(&name_decoration
, obj
, r
);
326 p
->item
= array
[i
].commit
;
336 * zero or positive weight is the number of interesting commits it can
337 * reach, including itself. Especially, weight = 0 means it does not
338 * reach any tree-changing commits (e.g. just above uninteresting one
339 * but traversal is with pathspec).
341 * weight = -1 means it has one parent and its distance is yet to
344 * weight = -2 means it has more than one parent and its distance is
345 * unknown. After running count_distance() first, they will get zero
346 * or positive distance.
348 static struct commit_list
*do_find_bisection(struct commit_list
*list
,
349 int nr
, int *weights
,
353 struct commit_list
*p
;
357 for (n
= 0, p
= list
; p
; p
= p
->next
) {
358 struct commit
*commit
= p
->item
;
359 unsigned flags
= commit
->object
.flags
;
361 p
->item
->util
= &weights
[n
++];
362 switch (count_interesting_parents(commit
)) {
364 if (!revs
.prune_fn
|| (flags
& TREECHANGE
)) {
367 show_list("bisection 2 count one",
371 * otherwise, it is known not to reach any
372 * tree-changing commit and gets weight 0.
384 show_list("bisection 2 initialize", counted
, nr
, list
);
387 * If you have only one parent in the resulting set
388 * then you can reach one commit more than that parent
389 * can reach. So we do not have to run the expensive
390 * count_distance() for single strand of pearls.
392 * However, if you have more than one parents, you cannot
393 * just add their distance and one for yourself, since
394 * they usually reach the same ancestor and you would
395 * end up counting them twice that way.
397 * So we will first count distance of merges the usual
398 * way, and then fill the blanks using cheaper algorithm.
400 for (p
= list
; p
; p
= p
->next
) {
401 if (p
->item
->object
.flags
& UNINTERESTING
)
405 weight_set(p
, count_distance(p
));
406 clear_distance(list
);
408 /* Does it happen to be at exactly half-way? */
409 if (!find_all
&& halfway(p
, nr
))
414 show_list("bisection 2 count_distance", counted
, nr
, list
);
416 while (counted
< nr
) {
417 for (p
= list
; p
; p
= p
->next
) {
418 struct commit_list
*q
;
419 unsigned flags
= p
->item
->object
.flags
;
423 for (q
= p
->item
->parents
; q
; q
= q
->next
) {
424 if (q
->item
->object
.flags
& UNINTERESTING
)
433 * weight for p is unknown but q is known.
434 * add one for p itself if p is to be counted,
435 * otherwise inherit it from q directly.
437 if (!revs
.prune_fn
|| (flags
& TREECHANGE
)) {
438 weight_set(p
, weight(q
)+1);
440 show_list("bisection 2 count one",
444 weight_set(p
, weight(q
));
446 /* Does it happen to be at exactly half-way? */
447 if (!find_all
&& halfway(p
, nr
))
452 show_list("bisection 2 counted all", counted
, nr
, list
);
455 return best_bisection(list
, nr
);
457 return best_bisection_sorted(list
, nr
);
460 static struct commit_list
*find_bisection(struct commit_list
*list
,
461 int *reaches
, int *all
,
465 struct commit_list
*p
, *best
, *next
, *last
;
468 show_list("bisection 2 entry", 0, 0, list
);
471 * Count the number of total and tree-changing items on the
472 * list, while reversing the list.
474 for (nr
= on_list
= 0, last
= NULL
, p
= list
;
477 unsigned flags
= p
->item
->object
.flags
;
480 if (flags
& UNINTERESTING
)
484 if (!revs
.prune_fn
|| (flags
& TREECHANGE
))
489 show_list("bisection 2 sorted", 0, nr
, list
);
492 weights
= xcalloc(on_list
, sizeof(*weights
));
494 /* Do the real work of finding bisection commit. */
495 best
= do_find_bisection(list
, nr
, weights
, find_all
);
499 *reaches
= weight(best
);
505 static void read_revisions_from_stdin(struct rev_info
*revs
)
509 while (fgets(line
, sizeof(line
), stdin
) != NULL
) {
510 int len
= strlen(line
);
511 if (line
[len
- 1] == '\n')
516 die("options not supported in --stdin mode");
517 if (handle_revision_arg(line
, revs
, 0, 1))
518 die("bad revision '%s'", line
);
522 int cmd_rev_list(int argc
, const char **argv
, const char *prefix
)
524 struct commit_list
*list
;
526 int read_from_stdin
= 0;
527 int bisect_show_vars
= 0;
528 int bisect_find_all
= 0;
530 git_config(git_default_config
);
531 init_revisions(&revs
, prefix
);
533 revs
.commit_format
= CMIT_FMT_UNSPECIFIED
;
534 argc
= setup_revisions(argc
, argv
, &revs
, NULL
);
536 for (i
= 1 ; i
< argc
; i
++) {
537 const char *arg
= argv
[i
];
539 if (!strcmp(arg
, "--header")) {
540 revs
.verbose_header
= 1;
543 if (!strcmp(arg
, "--timestamp")) {
547 if (!strcmp(arg
, "--bisect")) {
551 if (!strcmp(arg
, "--bisect-all")) {
556 if (!strcmp(arg
, "--bisect-vars")) {
558 bisect_show_vars
= 1;
561 if (!strcmp(arg
, "--stdin")) {
562 if (read_from_stdin
++)
563 die("--stdin given twice?");
564 read_revisions_from_stdin(&revs
);
567 usage(rev_list_usage
);
570 if (revs
.commit_format
!= CMIT_FMT_UNSPECIFIED
) {
571 /* The command line has a --pretty */
572 hdr_termination
= '\n';
573 if (revs
.commit_format
== CMIT_FMT_ONELINE
)
576 header_prefix
= "commit ";
578 else if (revs
.verbose_header
)
579 /* Only --header was specified */
580 revs
.commit_format
= CMIT_FMT_RAW
;
585 (!(revs
.tag_objects
||revs
.tree_objects
||revs
.blob_objects
) &&
586 !revs
.pending
.nr
)) ||
588 usage(rev_list_usage
);
590 save_commit_buffer
= revs
.verbose_header
|| revs
.grep_filter
;
591 track_object_refs
= 0;
595 prepare_revision_walk(&revs
);
596 if (revs
.tree_objects
)
597 mark_edges_uninteresting(revs
.commits
, &revs
, show_edge
);
600 int reaches
= reaches
, all
= all
;
602 revs
.commits
= find_bisection(revs
.commits
, &reaches
, &all
,
604 if (bisect_show_vars
) {
610 * revs.commits can reach "reaches" commits among
611 * "all" commits. If it is good, then there are
612 * (all-reaches) commits left to be bisected.
613 * On the other hand, if it is bad, then the set
614 * to bisect is "reaches".
615 * A bisect set of size N has (N-1) commits further
616 * to test, as we already know one bad one.
621 strcpy(hex
, sha1_to_hex(revs
.commits
->item
->object
.sha1
));
623 if (bisect_find_all
) {
624 traverse_commit_list(&revs
, show_commit
, show_object
);
628 printf("bisect_rev=%s\n"
642 traverse_commit_list(&revs
, show_commit
, show_object
);