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"
44 static struct rev_info revs
;
46 static int bisect_list
;
47 static int show_timestamp
;
48 static int hdr_termination
;
49 static const char *header_prefix
;
51 static void show_commit(struct commit
*commit
)
54 printf("%lu ", commit
->date
);
56 fputs(header_prefix
, stdout
);
57 if (commit
->object
.flags
& BOUNDARY
)
59 else if (revs
.left_right
) {
60 if (commit
->object
.flags
& SYMMETRIC_LEFT
)
65 if (revs
.abbrev_commit
&& revs
.abbrev
)
66 fputs(find_unique_abbrev(commit
->object
.sha1
, revs
.abbrev
),
69 fputs(sha1_to_hex(commit
->object
.sha1
), stdout
);
71 struct commit_list
*parents
= commit
->parents
;
73 struct object
*o
= &(parents
->item
->object
);
74 parents
= parents
->next
;
75 if (o
->flags
& TMP_MARK
)
77 printf(" %s", sha1_to_hex(o
->sha1
));
80 /* TMP_MARK is a general purpose flag that can
81 * be used locally, but the user should clean
82 * things up after it is done with them.
84 for (parents
= commit
->parents
;
86 parents
= parents
->next
)
87 parents
->item
->object
.flags
&= ~TMP_MARK
;
89 if (revs
.commit_format
== CMIT_FMT_ONELINE
)
94 if (revs
.verbose_header
) {
95 static char pretty_header
[16384];
96 pretty_print_commit(revs
.commit_format
, commit
, ~0,
97 pretty_header
, sizeof(pretty_header
),
98 revs
.abbrev
, NULL
, NULL
, revs
.relative_date
);
99 printf("%s%c", pretty_header
, hdr_termination
);
102 if (commit
->parents
) {
103 free_commit_list(commit
->parents
);
104 commit
->parents
= NULL
;
106 free(commit
->buffer
);
107 commit
->buffer
= NULL
;
110 static void show_object(struct object_array_entry
*p
)
112 /* An object with name "foo\n0000000..." can be used to
113 * confuse downstream git-pack-objects very badly.
115 const char *ep
= strchr(p
->name
, '\n');
117 if (p
->item
->type
== OBJ_BLOB
&& !has_sha1_file(p
->item
->sha1
))
118 die("missing blob object '%s'", sha1_to_hex(p
->item
->sha1
));
121 printf("%s %.*s\n", sha1_to_hex(p
->item
->sha1
),
122 (int) (ep
- p
->name
),
126 printf("%s %s\n", sha1_to_hex(p
->item
->sha1
), p
->name
);
129 static void show_edge(struct commit
*commit
)
131 printf("-%s\n", sha1_to_hex(commit
->object
.sha1
));
135 * This is a truly stupid algorithm, but it's only
136 * used for bisection, and we just don't care enough.
138 * We care just barely enough to avoid recursing for
141 static int count_distance(struct commit_list
*entry
)
146 struct commit
*commit
= entry
->item
;
147 struct commit_list
*p
;
149 if (commit
->object
.flags
& (UNINTERESTING
| COUNTED
))
151 if (!revs
.prune_fn
|| (commit
->object
.flags
& TREECHANGE
))
153 commit
->object
.flags
|= COUNTED
;
159 nr
+= count_distance(p
);
168 static void clear_distance(struct commit_list
*list
)
171 struct commit
*commit
= list
->item
;
172 commit
->object
.flags
&= ~COUNTED
;
177 #define DEBUG_BISECT 0
179 static inline int weight(struct commit_list
*elem
)
181 return *((int*)(elem
->item
->util
));
184 static inline void weight_set(struct commit_list
*elem
, int weight
)
186 *((int*)(elem
->item
->util
)) = weight
;
189 static int count_interesting_parents(struct commit
*commit
)
191 struct commit_list
*p
;
194 for (count
= 0, p
= commit
->parents
; p
; p
= p
->next
) {
195 if (p
->item
->object
.flags
& UNINTERESTING
)
202 static inline int halfway(struct commit_list
*p
, int distance
, int nr
)
205 * Don't short-cut something we are not going to return!
207 if (revs
.prune_fn
&& !(p
->item
->object
.flags
& TREECHANGE
))
212 * 2 and 3 are halfway of 5.
213 * 3 is halfway of 6 but 2 and 4 are not.
216 switch (distance
- nr
) {
217 case -1: case 0: case 1:
225 #define show_list(a,b,c,d) do { ; } while (0)
227 static void show_list(const char *debug
, int counted
, int nr
,
228 struct commit_list
*list
)
230 struct commit_list
*p
;
232 fprintf(stderr
, "%s (%d/%d)\n", debug
, counted
, nr
);
234 for (p
= list
; p
; p
= p
->next
) {
235 struct commit_list
*pp
;
236 struct commit
*commit
= p
->item
;
237 unsigned flags
= commit
->object
.flags
;
238 enum object_type type
;
240 char *buf
= read_sha1_file(commit
->object
.sha1
, &type
, &size
);
243 fprintf(stderr
, "%c%c%c ",
244 (flags
& TREECHANGE
) ? 'T' : ' ',
245 (flags
& UNINTERESTING
) ? 'U' : ' ',
246 (flags
& COUNTED
) ? 'C' : ' ');
248 fprintf(stderr
, "%3d", weight(p
));
250 fprintf(stderr
, "---");
251 fprintf(stderr
, " %.*s", 8, sha1_to_hex(commit
->object
.sha1
));
252 for (pp
= commit
->parents
; pp
; pp
= pp
->next
)
253 fprintf(stderr
, " %.*s", 8,
254 sha1_to_hex(pp
->item
->object
.sha1
));
256 sp
= strstr(buf
, "\n\n");
259 for (ep
= sp
; *ep
&& *ep
!= '\n'; ep
++)
261 fprintf(stderr
, " %.*s", (int)(ep
- sp
), sp
);
263 fprintf(stderr
, "\n");
266 #endif /* DEBUG_BISECT */
269 * zero or positive weight is the number of interesting commits it can
270 * reach, including itself. Especially, weight = 0 means it does not
271 * reach any tree-changing commits (e.g. just above uninteresting one
272 * but traversal is with pathspec).
274 * weight = -1 means it has one parent and its distance is yet to
277 * weight = -2 means it has more than one parent and its distance is
278 * unknown. After running count_distance() first, they will get zero
279 * or positive distance.
282 static struct commit_list
*find_bisection(struct commit_list
*list
,
283 int *reaches
, int *all
)
285 int n
, nr
, on_list
, counted
, distance
;
286 struct commit_list
*p
, *best
, *next
, *last
;
289 show_list("bisection 2 entry", 0, 0, list
);
292 * Count the number of total and tree-changing items on the
293 * list, while reversing the list.
295 for (nr
= on_list
= 0, last
= NULL
, p
= list
;
298 unsigned flags
= p
->item
->object
.flags
;
301 if (flags
& UNINTERESTING
)
305 if (!revs
.prune_fn
|| (flags
& TREECHANGE
))
310 show_list("bisection 2 sorted", 0, nr
, list
);
313 weights
= xcalloc(on_list
, sizeof(int*));
316 for (n
= 0, p
= list
; p
; p
= p
->next
) {
317 struct commit
*commit
= p
->item
;
318 unsigned flags
= commit
->object
.flags
;
320 p
->item
->util
= &weights
[n
++];
321 switch (count_interesting_parents(commit
)) {
323 if (!revs
.prune_fn
|| (flags
& TREECHANGE
)) {
326 show_list("bisection 2 count one",
330 * otherwise, it is known not to reach any
331 * tree-changing commit and gets weight 0.
343 show_list("bisection 2 initialize", counted
, nr
, list
);
346 * If you have only one parent in the resulting set
347 * then you can reach one commit more than that parent
348 * can reach. So we do not have to run the expensive
349 * count_distance() for single strand of pearls.
351 * However, if you have more than one parents, you cannot
352 * just add their distance and one for yourself, since
353 * they usually reach the same ancestor and you would
354 * end up counting them twice that way.
356 * So we will first count distance of merges the usual
357 * way, and then fill the blanks using cheaper algorithm.
359 for (p
= list
; p
; p
= p
->next
) {
360 if (p
->item
->object
.flags
& UNINTERESTING
)
365 distance
= count_distance(p
);
366 clear_distance(list
);
367 weight_set(p
, distance
);
369 /* Does it happen to be at exactly half-way? */
370 if (halfway(p
, distance
, nr
)) {
379 show_list("bisection 2 count_distance", counted
, nr
, list
);
381 while (counted
< nr
) {
382 for (p
= list
; p
; p
= p
->next
) {
383 struct commit_list
*q
;
384 unsigned flags
= p
->item
->object
.flags
;
388 for (q
= p
->item
->parents
; q
; q
= q
->next
) {
389 if (q
->item
->object
.flags
& UNINTERESTING
)
398 * weight for p is unknown but q is known.
399 * add one for p itself if p is to be counted,
400 * otherwise inherit it from q directly.
402 if (!revs
.prune_fn
|| (flags
& TREECHANGE
)) {
403 weight_set(p
, weight(q
)+1);
405 show_list("bisection 2 count one",
409 weight_set(p
, weight(q
));
411 /* Does it happen to be at exactly half-way? */
412 distance
= weight(p
);
413 if (halfway(p
, distance
, nr
)) {
422 show_list("bisection 2 counted all", counted
, nr
, list
);
424 /* Then find the best one */
427 for (p
= list
; p
; p
= p
->next
) {
428 unsigned flags
= p
->item
->object
.flags
;
430 if (revs
.prune_fn
&& !(flags
& TREECHANGE
))
432 distance
= weight(p
);
433 if (nr
- distance
< distance
)
434 distance
= nr
- distance
;
435 if (distance
> counted
) {
438 *reaches
= weight(p
);
447 static void read_revisions_from_stdin(struct rev_info
*revs
)
451 while (fgets(line
, sizeof(line
), stdin
) != NULL
) {
452 int len
= strlen(line
);
453 if (line
[len
- 1] == '\n')
458 die("options not supported in --stdin mode");
459 if (handle_revision_arg(line
, revs
, 0, 1))
460 die("bad revision '%s'", line
);
464 int cmd_rev_list(int argc
, const char **argv
, const char *prefix
)
466 struct commit_list
*list
;
468 int read_from_stdin
= 0;
469 int bisect_show_vars
= 0;
471 git_config(git_default_config
);
472 init_revisions(&revs
, prefix
);
474 revs
.commit_format
= CMIT_FMT_UNSPECIFIED
;
475 argc
= setup_revisions(argc
, argv
, &revs
, NULL
);
477 for (i
= 1 ; i
< argc
; i
++) {
478 const char *arg
= argv
[i
];
480 if (!strcmp(arg
, "--header")) {
481 revs
.verbose_header
= 1;
484 if (!strcmp(arg
, "--timestamp")) {
488 if (!strcmp(arg
, "--bisect")) {
492 if (!strcmp(arg
, "--bisect-vars")) {
494 bisect_show_vars
= 1;
497 if (!strcmp(arg
, "--stdin")) {
498 if (read_from_stdin
++)
499 die("--stdin given twice?");
500 read_revisions_from_stdin(&revs
);
503 usage(rev_list_usage
);
506 if (revs
.commit_format
!= CMIT_FMT_UNSPECIFIED
) {
507 /* The command line has a --pretty */
508 hdr_termination
= '\n';
509 if (revs
.commit_format
== CMIT_FMT_ONELINE
)
512 header_prefix
= "commit ";
514 else if (revs
.verbose_header
)
515 /* Only --header was specified */
516 revs
.commit_format
= CMIT_FMT_RAW
;
521 (!(revs
.tag_objects
||revs
.tree_objects
||revs
.blob_objects
) &&
522 !revs
.pending
.nr
)) ||
524 usage(rev_list_usage
);
526 save_commit_buffer
= revs
.verbose_header
|| revs
.grep_filter
;
527 track_object_refs
= 0;
531 prepare_revision_walk(&revs
);
532 if (revs
.tree_objects
)
533 mark_edges_uninteresting(revs
.commits
, &revs
, show_edge
);
536 int reaches
= reaches
, all
= all
;
538 revs
.commits
= find_bisection(revs
.commits
, &reaches
, &all
);
539 if (bisect_show_vars
) {
544 * revs.commits can reach "reaches" commits among
545 * "all" commits. If it is good, then there are
546 * (all-reaches) commits left to be bisected.
547 * On the other hand, if it is bad, then the set
548 * to bisect is "reaches".
549 * A bisect set of size N has (N-1) commits further
550 * to test, as we already know one bad one.
555 printf("bisect_rev=%s\n"
560 sha1_to_hex(revs
.commits
->item
->object
.sha1
),
569 traverse_commit_list(&revs
, show_commit
, show_object
);