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
) {
96 unsigned long buflen
= 0;
97 pretty_print_commit(revs
.commit_format
, commit
, ~0,
99 revs
.abbrev
, NULL
, NULL
, revs
.date_mode
);
100 printf("%s%c", buf
, hdr_termination
);
104 if (commit
->parents
) {
105 free_commit_list(commit
->parents
);
106 commit
->parents
= NULL
;
108 free(commit
->buffer
);
109 commit
->buffer
= NULL
;
112 static void show_object(struct object_array_entry
*p
)
114 /* An object with name "foo\n0000000..." can be used to
115 * confuse downstream git-pack-objects very badly.
117 const char *ep
= strchr(p
->name
, '\n');
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 printf("%s %.*s\n", sha1_to_hex(p
->item
->sha1
),
124 (int) (ep
- p
->name
),
128 printf("%s %s\n", sha1_to_hex(p
->item
->sha1
), p
->name
);
131 static void show_edge(struct commit
*commit
)
133 printf("-%s\n", sha1_to_hex(commit
->object
.sha1
));
137 * This is a truly stupid algorithm, but it's only
138 * used for bisection, and we just don't care enough.
140 * We care just barely enough to avoid recursing for
143 static int count_distance(struct commit_list
*entry
)
148 struct commit
*commit
= entry
->item
;
149 struct commit_list
*p
;
151 if (commit
->object
.flags
& (UNINTERESTING
| COUNTED
))
153 if (!revs
.prune_fn
|| (commit
->object
.flags
& TREECHANGE
))
155 commit
->object
.flags
|= COUNTED
;
161 nr
+= count_distance(p
);
170 static void clear_distance(struct commit_list
*list
)
173 struct commit
*commit
= list
->item
;
174 commit
->object
.flags
&= ~COUNTED
;
179 #define DEBUG_BISECT 0
181 static inline int weight(struct commit_list
*elem
)
183 return *((int*)(elem
->item
->util
));
186 static inline void weight_set(struct commit_list
*elem
, int weight
)
188 *((int*)(elem
->item
->util
)) = weight
;
191 static int count_interesting_parents(struct commit
*commit
)
193 struct commit_list
*p
;
196 for (count
= 0, p
= commit
->parents
; p
; p
= p
->next
) {
197 if (p
->item
->object
.flags
& UNINTERESTING
)
204 static inline int halfway(struct commit_list
*p
, int distance
, int nr
)
207 * Don't short-cut something we are not going to return!
209 if (revs
.prune_fn
&& !(p
->item
->object
.flags
& TREECHANGE
))
214 * 2 and 3 are halfway of 5.
215 * 3 is halfway of 6 but 2 and 4 are not.
218 switch (distance
- nr
) {
219 case -1: case 0: case 1:
227 #define show_list(a,b,c,d) do { ; } while (0)
229 static void show_list(const char *debug
, int counted
, int nr
,
230 struct commit_list
*list
)
232 struct commit_list
*p
;
234 fprintf(stderr
, "%s (%d/%d)\n", debug
, counted
, nr
);
236 for (p
= list
; p
; p
= p
->next
) {
237 struct commit_list
*pp
;
238 struct commit
*commit
= p
->item
;
239 unsigned flags
= commit
->object
.flags
;
240 enum object_type type
;
242 char *buf
= read_sha1_file(commit
->object
.sha1
, &type
, &size
);
245 fprintf(stderr
, "%c%c%c ",
246 (flags
& TREECHANGE
) ? 'T' : ' ',
247 (flags
& UNINTERESTING
) ? 'U' : ' ',
248 (flags
& COUNTED
) ? 'C' : ' ');
250 fprintf(stderr
, "%3d", weight(p
));
252 fprintf(stderr
, "---");
253 fprintf(stderr
, " %.*s", 8, sha1_to_hex(commit
->object
.sha1
));
254 for (pp
= commit
->parents
; pp
; pp
= pp
->next
)
255 fprintf(stderr
, " %.*s", 8,
256 sha1_to_hex(pp
->item
->object
.sha1
));
258 sp
= strstr(buf
, "\n\n");
261 for (ep
= sp
; *ep
&& *ep
!= '\n'; ep
++)
263 fprintf(stderr
, " %.*s", (int)(ep
- sp
), sp
);
265 fprintf(stderr
, "\n");
268 #endif /* DEBUG_BISECT */
271 * zero or positive weight is the number of interesting commits it can
272 * reach, including itself. Especially, weight = 0 means it does not
273 * reach any tree-changing commits (e.g. just above uninteresting one
274 * but traversal is with pathspec).
276 * weight = -1 means it has one parent and its distance is yet to
279 * weight = -2 means it has more than one parent and its distance is
280 * unknown. After running count_distance() first, they will get zero
281 * or positive distance.
284 static struct commit_list
*find_bisection(struct commit_list
*list
,
285 int *reaches
, int *all
)
287 int n
, nr
, on_list
, counted
, distance
;
288 struct commit_list
*p
, *best
, *next
, *last
;
291 show_list("bisection 2 entry", 0, 0, list
);
294 * Count the number of total and tree-changing items on the
295 * list, while reversing the list.
297 for (nr
= on_list
= 0, last
= NULL
, p
= list
;
300 unsigned flags
= p
->item
->object
.flags
;
303 if (flags
& UNINTERESTING
)
307 if (!revs
.prune_fn
|| (flags
& TREECHANGE
))
312 show_list("bisection 2 sorted", 0, nr
, list
);
315 weights
= xcalloc(on_list
, sizeof(int*));
318 for (n
= 0, p
= list
; p
; p
= p
->next
) {
319 struct commit
*commit
= p
->item
;
320 unsigned flags
= commit
->object
.flags
;
322 p
->item
->util
= &weights
[n
++];
323 switch (count_interesting_parents(commit
)) {
325 if (!revs
.prune_fn
|| (flags
& TREECHANGE
)) {
328 show_list("bisection 2 count one",
332 * otherwise, it is known not to reach any
333 * tree-changing commit and gets weight 0.
345 show_list("bisection 2 initialize", counted
, nr
, list
);
348 * If you have only one parent in the resulting set
349 * then you can reach one commit more than that parent
350 * can reach. So we do not have to run the expensive
351 * count_distance() for single strand of pearls.
353 * However, if you have more than one parents, you cannot
354 * just add their distance and one for yourself, since
355 * they usually reach the same ancestor and you would
356 * end up counting them twice that way.
358 * So we will first count distance of merges the usual
359 * way, and then fill the blanks using cheaper algorithm.
361 for (p
= list
; p
; p
= p
->next
) {
362 if (p
->item
->object
.flags
& UNINTERESTING
)
367 distance
= count_distance(p
);
368 clear_distance(list
);
369 weight_set(p
, distance
);
371 /* Does it happen to be at exactly half-way? */
372 if (halfway(p
, distance
, nr
)) {
381 show_list("bisection 2 count_distance", counted
, nr
, list
);
383 while (counted
< nr
) {
384 for (p
= list
; p
; p
= p
->next
) {
385 struct commit_list
*q
;
386 unsigned flags
= p
->item
->object
.flags
;
390 for (q
= p
->item
->parents
; q
; q
= q
->next
) {
391 if (q
->item
->object
.flags
& UNINTERESTING
)
400 * weight for p is unknown but q is known.
401 * add one for p itself if p is to be counted,
402 * otherwise inherit it from q directly.
404 if (!revs
.prune_fn
|| (flags
& TREECHANGE
)) {
405 weight_set(p
, weight(q
)+1);
407 show_list("bisection 2 count one",
411 weight_set(p
, weight(q
));
413 /* Does it happen to be at exactly half-way? */
414 distance
= weight(p
);
415 if (halfway(p
, distance
, nr
)) {
424 show_list("bisection 2 counted all", counted
, nr
, list
);
426 /* Then find the best one */
429 for (p
= list
; p
; p
= p
->next
) {
430 unsigned flags
= p
->item
->object
.flags
;
432 if (revs
.prune_fn
&& !(flags
& TREECHANGE
))
434 distance
= weight(p
);
435 if (nr
- distance
< distance
)
436 distance
= nr
- distance
;
437 if (distance
> counted
) {
440 *reaches
= weight(p
);
449 static void read_revisions_from_stdin(struct rev_info
*revs
)
453 while (fgets(line
, sizeof(line
), stdin
) != NULL
) {
454 int len
= strlen(line
);
455 if (line
[len
- 1] == '\n')
460 die("options not supported in --stdin mode");
461 if (handle_revision_arg(line
, revs
, 0, 1))
462 die("bad revision '%s'", line
);
466 int cmd_rev_list(int argc
, const char **argv
, const char *prefix
)
468 struct commit_list
*list
;
470 int read_from_stdin
= 0;
471 int bisect_show_vars
= 0;
473 git_config(git_default_config
);
474 init_revisions(&revs
, prefix
);
476 revs
.commit_format
= CMIT_FMT_UNSPECIFIED
;
477 argc
= setup_revisions(argc
, argv
, &revs
, NULL
);
479 for (i
= 1 ; i
< argc
; i
++) {
480 const char *arg
= argv
[i
];
482 if (!strcmp(arg
, "--header")) {
483 revs
.verbose_header
= 1;
486 if (!strcmp(arg
, "--timestamp")) {
490 if (!strcmp(arg
, "--bisect")) {
494 if (!strcmp(arg
, "--bisect-vars")) {
496 bisect_show_vars
= 1;
499 if (!strcmp(arg
, "--stdin")) {
500 if (read_from_stdin
++)
501 die("--stdin given twice?");
502 read_revisions_from_stdin(&revs
);
505 usage(rev_list_usage
);
508 if (revs
.commit_format
!= CMIT_FMT_UNSPECIFIED
) {
509 /* The command line has a --pretty */
510 hdr_termination
= '\n';
511 if (revs
.commit_format
== CMIT_FMT_ONELINE
)
514 header_prefix
= "commit ";
516 else if (revs
.verbose_header
)
517 /* Only --header was specified */
518 revs
.commit_format
= CMIT_FMT_RAW
;
523 (!(revs
.tag_objects
||revs
.tree_objects
||revs
.blob_objects
) &&
524 !revs
.pending
.nr
)) ||
526 usage(rev_list_usage
);
528 save_commit_buffer
= revs
.verbose_header
|| revs
.grep_filter
;
529 track_object_refs
= 0;
533 prepare_revision_walk(&revs
);
534 if (revs
.tree_objects
)
535 mark_edges_uninteresting(revs
.commits
, &revs
, show_edge
);
538 int reaches
= reaches
, all
= all
;
540 revs
.commits
= find_bisection(revs
.commits
, &reaches
, &all
);
541 if (bisect_show_vars
) {
546 * revs.commits can reach "reaches" commits among
547 * "all" commits. If it is good, then there are
548 * (all-reaches) commits left to be bisected.
549 * On the other hand, if it is bad, then the set
550 * to bisect is "reaches".
551 * A bisect set of size N has (N-1) commits further
552 * to test, as we already know one bad one.
557 printf("bisect_rev=%s\n"
562 sha1_to_hex(revs
.commits
->item
->object
.sha1
),
571 traverse_commit_list(&revs
, show_commit
, show_object
);