10 #include "list-objects.h"
13 /* bits #0-15 in revision.h */
15 #define COUNTED (1u<<16)
17 static const char rev_list_usage
[] =
18 "git-rev-list [OPTION] <commit-id>... [ -- paths... ]\n"
31 " formatting output:\n"
33 " --objects | --objects-edge\n"
35 " --header | --pretty\n"
36 " --abbrev=nr | --no-abbrev\n"
43 static struct rev_info revs
;
45 static int bisect_list
;
46 static int show_timestamp
;
47 static int hdr_termination
;
48 static const char *header_prefix
;
50 static void show_commit(struct commit
*commit
)
53 printf("%lu ", commit
->date
);
55 fputs(header_prefix
, stdout
);
56 if (commit
->object
.flags
& BOUNDARY
)
58 else if (revs
.left_right
) {
59 if (commit
->object
.flags
& SYMMETRIC_LEFT
)
64 if (revs
.abbrev_commit
&& revs
.abbrev
)
65 fputs(find_unique_abbrev(commit
->object
.sha1
, revs
.abbrev
),
68 fputs(sha1_to_hex(commit
->object
.sha1
), stdout
);
70 struct commit_list
*parents
= commit
->parents
;
72 struct object
*o
= &(parents
->item
->object
);
73 parents
= parents
->next
;
74 if (o
->flags
& TMP_MARK
)
76 printf(" %s", sha1_to_hex(o
->sha1
));
79 /* TMP_MARK is a general purpose flag that can
80 * be used locally, but the user should clean
81 * things up after it is done with them.
83 for (parents
= commit
->parents
;
85 parents
= parents
->next
)
86 parents
->item
->object
.flags
&= ~TMP_MARK
;
88 if (revs
.commit_format
== CMIT_FMT_ONELINE
)
93 if (revs
.verbose_header
) {
94 static char pretty_header
[16384];
95 pretty_print_commit(revs
.commit_format
, commit
, ~0,
96 pretty_header
, sizeof(pretty_header
),
97 revs
.abbrev
, NULL
, NULL
, revs
.relative_date
);
98 printf("%s%c", pretty_header
, hdr_termination
);
101 if (commit
->parents
) {
102 free_commit_list(commit
->parents
);
103 commit
->parents
= NULL
;
105 free(commit
->buffer
);
106 commit
->buffer
= NULL
;
109 static void show_object(struct object_array_entry
*p
)
111 /* An object with name "foo\n0000000..." can be used to
112 * confuse downstream git-pack-objects very badly.
114 const char *ep
= strchr(p
->name
, '\n');
116 printf("%s %.*s\n", sha1_to_hex(p
->item
->sha1
),
117 (int) (ep
- p
->name
),
121 printf("%s %s\n", sha1_to_hex(p
->item
->sha1
), p
->name
);
124 static void show_edge(struct commit
*commit
)
126 printf("-%s\n", sha1_to_hex(commit
->object
.sha1
));
130 * This is a truly stupid algorithm, but it's only
131 * used for bisection, and we just don't care enough.
133 * We care just barely enough to avoid recursing for
136 static int count_distance(struct commit_list
*entry
)
141 struct commit
*commit
= entry
->item
;
142 struct commit_list
*p
;
144 if (commit
->object
.flags
& (UNINTERESTING
| COUNTED
))
146 if (!revs
.prune_fn
|| (commit
->object
.flags
& TREECHANGE
))
148 commit
->object
.flags
|= COUNTED
;
154 nr
+= count_distance(p
);
163 static void clear_distance(struct commit_list
*list
)
166 struct commit
*commit
= list
->item
;
167 commit
->object
.flags
&= ~COUNTED
;
172 #define DEBUG_BISECT 0
174 static inline int weight(struct commit_list
*elem
)
176 return *((int*)(elem
->item
->util
));
179 static inline void weight_set(struct commit_list
*elem
, int weight
)
181 *((int*)(elem
->item
->util
)) = weight
;
184 static int count_interesting_parents(struct commit
*commit
)
186 struct commit_list
*p
;
189 for (count
= 0, p
= commit
->parents
; p
; p
= p
->next
) {
190 if (p
->item
->object
.flags
& UNINTERESTING
)
197 static inline int halfway(struct commit_list
*p
, int distance
, int nr
)
200 * Don't short-cut something we are not going to return!
202 if (revs
.prune_fn
&& !(p
->item
->object
.flags
& TREECHANGE
))
207 * 2 and 3 are halfway of 5.
208 * 3 is halfway of 6 but 2 and 4 are not.
211 switch (distance
- nr
) {
212 case -1: case 0: case 1:
220 #define show_list(a,b,c,d) do { ; } while (0)
222 static void show_list(const char *debug
, int counted
, int nr
,
223 struct commit_list
*list
)
225 struct commit_list
*p
;
227 fprintf(stderr
, "%s (%d/%d)\n", debug
, counted
, nr
);
229 for (p
= list
; p
; p
= p
->next
) {
230 struct commit_list
*pp
;
231 struct commit
*commit
= p
->item
;
232 unsigned flags
= commit
->object
.flags
;
233 enum object_type type
;
235 char *buf
= read_sha1_file(commit
->object
.sha1
, &type
, &size
);
238 fprintf(stderr
, "%c%c%c ",
239 (flags
& TREECHANGE
) ? 'T' : ' ',
240 (flags
& UNINTERESTING
) ? 'U' : ' ',
241 (flags
& COUNTED
) ? 'C' : ' ');
243 fprintf(stderr
, "%3d", weight(p
));
245 fprintf(stderr
, "---");
246 fprintf(stderr
, " %.*s", 8, sha1_to_hex(commit
->object
.sha1
));
247 for (pp
= commit
->parents
; pp
; pp
= pp
->next
)
248 fprintf(stderr
, " %.*s", 8,
249 sha1_to_hex(pp
->item
->object
.sha1
));
251 sp
= strstr(buf
, "\n\n");
254 for (ep
= sp
; *ep
&& *ep
!= '\n'; ep
++)
256 fprintf(stderr
, " %.*s", (int)(ep
- sp
), sp
);
258 fprintf(stderr
, "\n");
261 #endif /* DEBUG_BISECT */
264 * zero or positive weight is the number of interesting commits it can
265 * reach, including itself. Especially, weight = 0 means it does not
266 * reach any tree-changing commits (e.g. just above uninteresting one
267 * but traversal is with pathspec).
269 * weight = -1 means it has one parent and its distance is yet to
272 * weight = -2 means it has more than one parent and its distance is
273 * unknown. After running count_distance() first, they will get zero
274 * or positive distance.
277 static struct commit_list
*find_bisection(struct commit_list
*list
,
278 int *reaches
, int *all
)
280 int n
, nr
, on_list
, counted
, distance
;
281 struct commit_list
*p
, *best
, *next
, *last
;
284 show_list("bisection 2 entry", 0, 0, list
);
287 * Count the number of total and tree-changing items on the
288 * list, while reversing the list.
290 for (nr
= on_list
= 0, last
= NULL
, p
= list
;
293 unsigned flags
= p
->item
->object
.flags
;
296 if (flags
& UNINTERESTING
)
300 if (!revs
.prune_fn
|| (flags
& TREECHANGE
))
305 show_list("bisection 2 sorted", 0, nr
, list
);
308 weights
= xcalloc(on_list
, sizeof(int*));
311 for (n
= 0, p
= list
; p
; p
= p
->next
) {
312 struct commit
*commit
= p
->item
;
313 unsigned flags
= commit
->object
.flags
;
315 p
->item
->util
= &weights
[n
++];
316 switch (count_interesting_parents(commit
)) {
318 if (!revs
.prune_fn
|| (flags
& TREECHANGE
)) {
321 show_list("bisection 2 count one",
325 * otherwise, it is known not to reach any
326 * tree-changing commit and gets weight 0.
338 show_list("bisection 2 initialize", counted
, nr
, list
);
341 * If you have only one parent in the resulting set
342 * then you can reach one commit more than that parent
343 * can reach. So we do not have to run the expensive
344 * count_distance() for single strand of pearls.
346 * However, if you have more than one parents, you cannot
347 * just add their distance and one for yourself, since
348 * they usually reach the same ancestor and you would
349 * end up counting them twice that way.
351 * So we will first count distance of merges the usual
352 * way, and then fill the blanks using cheaper algorithm.
354 for (p
= list
; p
; p
= p
->next
) {
355 if (p
->item
->object
.flags
& UNINTERESTING
)
360 distance
= count_distance(p
);
361 clear_distance(list
);
362 weight_set(p
, distance
);
364 /* Does it happen to be at exactly half-way? */
365 if (halfway(p
, distance
, nr
)) {
374 show_list("bisection 2 count_distance", counted
, nr
, list
);
376 while (counted
< nr
) {
377 for (p
= list
; p
; p
= p
->next
) {
378 struct commit_list
*q
;
379 unsigned flags
= p
->item
->object
.flags
;
383 for (q
= p
->item
->parents
; q
; q
= q
->next
) {
384 if (q
->item
->object
.flags
& UNINTERESTING
)
393 * weight for p is unknown but q is known.
394 * add one for p itself if p is to be counted,
395 * otherwise inherit it from q directly.
397 if (!revs
.prune_fn
|| (flags
& TREECHANGE
)) {
398 weight_set(p
, weight(q
)+1);
400 show_list("bisection 2 count one",
404 weight_set(p
, weight(q
));
406 /* Does it happen to be at exactly half-way? */
407 distance
= weight(p
);
408 if (halfway(p
, distance
, nr
)) {
417 show_list("bisection 2 counted all", counted
, nr
, list
);
419 /* Then find the best one */
422 for (p
= list
; p
; p
= p
->next
) {
423 unsigned flags
= p
->item
->object
.flags
;
425 if (revs
.prune_fn
&& !(flags
& TREECHANGE
))
427 distance
= weight(p
);
428 if (nr
- distance
< distance
)
429 distance
= nr
- distance
;
430 if (distance
> counted
) {
433 *reaches
= weight(p
);
442 static void read_revisions_from_stdin(struct rev_info
*revs
)
446 while (fgets(line
, sizeof(line
), stdin
) != NULL
) {
447 int len
= strlen(line
);
448 if (line
[len
- 1] == '\n')
453 die("options not supported in --stdin mode");
454 if (handle_revision_arg(line
, revs
, 0, 1))
455 die("bad revision '%s'", line
);
459 int cmd_rev_list(int argc
, const char **argv
, const char *prefix
)
461 struct commit_list
*list
;
463 int read_from_stdin
= 0;
464 int bisect_show_vars
= 0;
466 git_config(git_default_config
);
467 init_revisions(&revs
, prefix
);
469 revs
.commit_format
= CMIT_FMT_UNSPECIFIED
;
470 argc
= setup_revisions(argc
, argv
, &revs
, NULL
);
472 for (i
= 1 ; i
< argc
; i
++) {
473 const char *arg
= argv
[i
];
475 if (!strcmp(arg
, "--header")) {
476 revs
.verbose_header
= 1;
479 if (!strcmp(arg
, "--timestamp")) {
483 if (!strcmp(arg
, "--bisect")) {
487 if (!strcmp(arg
, "--bisect-vars")) {
489 bisect_show_vars
= 1;
492 if (!strcmp(arg
, "--stdin")) {
493 if (read_from_stdin
++)
494 die("--stdin given twice?");
495 read_revisions_from_stdin(&revs
);
498 usage(rev_list_usage
);
501 if (revs
.commit_format
!= CMIT_FMT_UNSPECIFIED
) {
502 /* The command line has a --pretty */
503 hdr_termination
= '\n';
504 if (revs
.commit_format
== CMIT_FMT_ONELINE
)
507 header_prefix
= "commit ";
509 else if (revs
.verbose_header
)
510 /* Only --header was specified */
511 revs
.commit_format
= CMIT_FMT_RAW
;
516 (!(revs
.tag_objects
||revs
.tree_objects
||revs
.blob_objects
) &&
517 !revs
.pending
.nr
)) ||
519 usage(rev_list_usage
);
521 save_commit_buffer
= revs
.verbose_header
|| revs
.grep_filter
;
522 track_object_refs
= 0;
526 prepare_revision_walk(&revs
);
527 if (revs
.tree_objects
)
528 mark_edges_uninteresting(revs
.commits
, &revs
, show_edge
);
531 int reaches
= reaches
, all
= all
;
533 revs
.commits
= find_bisection(revs
.commits
, &reaches
, &all
);
534 if (bisect_show_vars
) {
539 * revs.commits can reach "reaches" commits among
540 * "all" commits. If it is good, then there are
541 * (all-reaches) commits left to be bisected.
542 * On the other hand, if it is bad, then the set
543 * to bisect is "reaches".
544 * A bisect set of size N has (N-1) commits further
545 * to test, as we already know one bad one.
550 printf("bisect_rev=%s\n"
555 sha1_to_hex(revs
.commits
->item
->object
.sha1
),
564 traverse_commit_list(&revs
, show_commit
, show_object
);