6 #include "list-objects.h"
8 #include "sha1-lookup.h"
11 static unsigned char (*skipped_sha1
)[20];
12 static int skipped_sha1_nr
;
13 static int skipped_sha1_alloc
;
15 static const char **rev_argv
;
16 static int rev_argv_nr
;
17 static int rev_argv_alloc
;
19 /* bits #0-15 in revision.h */
21 #define COUNTED (1u<<16)
24 * This is a truly stupid algorithm, but it's only
25 * used for bisection, and we just don't care enough.
27 * We care just barely enough to avoid recursing for
30 static int count_distance(struct commit_list
*entry
)
35 struct commit
*commit
= entry
->item
;
36 struct commit_list
*p
;
38 if (commit
->object
.flags
& (UNINTERESTING
| COUNTED
))
40 if (!(commit
->object
.flags
& TREESAME
))
42 commit
->object
.flags
|= COUNTED
;
48 nr
+= count_distance(p
);
57 static void clear_distance(struct commit_list
*list
)
60 struct commit
*commit
= list
->item
;
61 commit
->object
.flags
&= ~COUNTED
;
66 #define DEBUG_BISECT 0
68 static inline int weight(struct commit_list
*elem
)
70 return *((int*)(elem
->item
->util
));
73 static inline void weight_set(struct commit_list
*elem
, int weight
)
75 *((int*)(elem
->item
->util
)) = weight
;
78 static int count_interesting_parents(struct commit
*commit
)
80 struct commit_list
*p
;
83 for (count
= 0, p
= commit
->parents
; p
; p
= p
->next
) {
84 if (p
->item
->object
.flags
& UNINTERESTING
)
91 static inline int halfway(struct commit_list
*p
, int nr
)
94 * Don't short-cut something we are not going to return!
96 if (p
->item
->object
.flags
& TREESAME
)
101 * 2 and 3 are halfway of 5.
102 * 3 is halfway of 6 but 2 and 4 are not.
104 switch (2 * weight(p
) - nr
) {
105 case -1: case 0: case 1:
113 #define show_list(a,b,c,d) do { ; } while (0)
115 static void show_list(const char *debug
, int counted
, int nr
,
116 struct commit_list
*list
)
118 struct commit_list
*p
;
120 fprintf(stderr
, "%s (%d/%d)\n", debug
, counted
, nr
);
122 for (p
= list
; p
; p
= p
->next
) {
123 struct commit_list
*pp
;
124 struct commit
*commit
= p
->item
;
125 unsigned flags
= commit
->object
.flags
;
126 enum object_type type
;
128 char *buf
= read_sha1_file(commit
->object
.sha1
, &type
, &size
);
131 fprintf(stderr
, "%c%c%c ",
132 (flags
& TREESAME
) ? ' ' : 'T',
133 (flags
& UNINTERESTING
) ? 'U' : ' ',
134 (flags
& COUNTED
) ? 'C' : ' ');
136 fprintf(stderr
, "%3d", weight(p
));
138 fprintf(stderr
, "---");
139 fprintf(stderr
, " %.*s", 8, sha1_to_hex(commit
->object
.sha1
));
140 for (pp
= commit
->parents
; pp
; pp
= pp
->next
)
141 fprintf(stderr
, " %.*s", 8,
142 sha1_to_hex(pp
->item
->object
.sha1
));
144 sp
= strstr(buf
, "\n\n");
147 for (ep
= sp
; *ep
&& *ep
!= '\n'; ep
++)
149 fprintf(stderr
, " %.*s", (int)(ep
- sp
), sp
);
151 fprintf(stderr
, "\n");
154 #endif /* DEBUG_BISECT */
156 static struct commit_list
*best_bisection(struct commit_list
*list
, int nr
)
158 struct commit_list
*p
, *best
;
159 int best_distance
= -1;
162 for (p
= list
; p
; p
= p
->next
) {
164 unsigned flags
= p
->item
->object
.flags
;
166 if (flags
& TREESAME
)
168 distance
= weight(p
);
169 if (nr
- distance
< distance
)
170 distance
= nr
- distance
;
171 if (distance
> best_distance
) {
173 best_distance
= distance
;
181 struct commit
*commit
;
185 static int compare_commit_dist(const void *a_
, const void *b_
)
187 struct commit_dist
*a
, *b
;
189 a
= (struct commit_dist
*)a_
;
190 b
= (struct commit_dist
*)b_
;
191 if (a
->distance
!= b
->distance
)
192 return b
->distance
- a
->distance
; /* desc sort */
193 return hashcmp(a
->commit
->object
.sha1
, b
->commit
->object
.sha1
);
196 static struct commit_list
*best_bisection_sorted(struct commit_list
*list
, int nr
)
198 struct commit_list
*p
;
199 struct commit_dist
*array
= xcalloc(nr
, sizeof(*array
));
202 for (p
= list
, cnt
= 0; p
; p
= p
->next
) {
204 unsigned flags
= p
->item
->object
.flags
;
206 if (flags
& TREESAME
)
208 distance
= weight(p
);
209 if (nr
- distance
< distance
)
210 distance
= nr
- distance
;
211 array
[cnt
].commit
= p
->item
;
212 array
[cnt
].distance
= distance
;
215 qsort(array
, cnt
, sizeof(*array
), compare_commit_dist
);
216 for (p
= list
, i
= 0; i
< cnt
; i
++) {
217 struct name_decoration
*r
= xmalloc(sizeof(*r
) + 100);
218 struct object
*obj
= &(array
[i
].commit
->object
);
220 sprintf(r
->name
, "dist=%d", array
[i
].distance
);
221 r
->next
= add_decoration(&name_decoration
, obj
, r
);
222 p
->item
= array
[i
].commit
;
232 * zero or positive weight is the number of interesting commits it can
233 * reach, including itself. Especially, weight = 0 means it does not
234 * reach any tree-changing commits (e.g. just above uninteresting one
235 * but traversal is with pathspec).
237 * weight = -1 means it has one parent and its distance is yet to
240 * weight = -2 means it has more than one parent and its distance is
241 * unknown. After running count_distance() first, they will get zero
242 * or positive distance.
244 static struct commit_list
*do_find_bisection(struct commit_list
*list
,
245 int nr
, int *weights
,
249 struct commit_list
*p
;
253 for (n
= 0, p
= list
; p
; p
= p
->next
) {
254 struct commit
*commit
= p
->item
;
255 unsigned flags
= commit
->object
.flags
;
257 p
->item
->util
= &weights
[n
++];
258 switch (count_interesting_parents(commit
)) {
260 if (!(flags
& TREESAME
)) {
263 show_list("bisection 2 count one",
267 * otherwise, it is known not to reach any
268 * tree-changing commit and gets weight 0.
280 show_list("bisection 2 initialize", counted
, nr
, list
);
283 * If you have only one parent in the resulting set
284 * then you can reach one commit more than that parent
285 * can reach. So we do not have to run the expensive
286 * count_distance() for single strand of pearls.
288 * However, if you have more than one parents, you cannot
289 * just add their distance and one for yourself, since
290 * they usually reach the same ancestor and you would
291 * end up counting them twice that way.
293 * So we will first count distance of merges the usual
294 * way, and then fill the blanks using cheaper algorithm.
296 for (p
= list
; p
; p
= p
->next
) {
297 if (p
->item
->object
.flags
& UNINTERESTING
)
301 weight_set(p
, count_distance(p
));
302 clear_distance(list
);
304 /* Does it happen to be at exactly half-way? */
305 if (!find_all
&& halfway(p
, nr
))
310 show_list("bisection 2 count_distance", counted
, nr
, list
);
312 while (counted
< nr
) {
313 for (p
= list
; p
; p
= p
->next
) {
314 struct commit_list
*q
;
315 unsigned flags
= p
->item
->object
.flags
;
319 for (q
= p
->item
->parents
; q
; q
= q
->next
) {
320 if (q
->item
->object
.flags
& UNINTERESTING
)
329 * weight for p is unknown but q is known.
330 * add one for p itself if p is to be counted,
331 * otherwise inherit it from q directly.
333 if (!(flags
& TREESAME
)) {
334 weight_set(p
, weight(q
)+1);
336 show_list("bisection 2 count one",
340 weight_set(p
, weight(q
));
342 /* Does it happen to be at exactly half-way? */
343 if (!find_all
&& halfway(p
, nr
))
348 show_list("bisection 2 counted all", counted
, nr
, list
);
351 return best_bisection(list
, nr
);
353 return best_bisection_sorted(list
, nr
);
356 struct commit_list
*find_bisection(struct commit_list
*list
,
357 int *reaches
, int *all
,
361 struct commit_list
*p
, *best
, *next
, *last
;
364 show_list("bisection 2 entry", 0, 0, list
);
367 * Count the number of total and tree-changing items on the
368 * list, while reversing the list.
370 for (nr
= on_list
= 0, last
= NULL
, p
= list
;
373 unsigned flags
= p
->item
->object
.flags
;
376 if (flags
& UNINTERESTING
)
380 if (!(flags
& TREESAME
))
385 show_list("bisection 2 sorted", 0, nr
, list
);
388 weights
= xcalloc(on_list
, sizeof(*weights
));
390 /* Do the real work of finding bisection commit. */
391 best
= do_find_bisection(list
, nr
, weights
, find_all
);
395 *reaches
= weight(best
);
401 static int register_ref(const char *refname
, const unsigned char *sha1
,
402 int flags
, void *cb_data
)
404 if (!strcmp(refname
, "bad")) {
405 ALLOC_GROW(rev_argv
, rev_argv_nr
+ 1, rev_argv_alloc
);
406 rev_argv
[rev_argv_nr
++] = xstrdup(sha1_to_hex(sha1
));
407 } else if (!prefixcmp(refname
, "good-")) {
408 const char *hex
= sha1_to_hex(sha1
);
409 char *good
= xmalloc(strlen(hex
) + 2);
411 memcpy(good
+ 1, hex
, strlen(hex
) + 1);
412 ALLOC_GROW(rev_argv
, rev_argv_nr
+ 1, rev_argv_alloc
);
413 rev_argv
[rev_argv_nr
++] = good
;
414 } else if (!prefixcmp(refname
, "skip-")) {
415 ALLOC_GROW(skipped_sha1
, skipped_sha1_nr
+ 1,
417 hashcpy(skipped_sha1
[skipped_sha1_nr
++], sha1
);
423 static int read_bisect_refs(void)
425 return for_each_ref_in("refs/bisect/", register_ref
, NULL
);
428 void read_bisect_paths(void)
430 struct strbuf str
= STRBUF_INIT
;
431 const char *filename
= git_path("BISECT_NAMES");
432 FILE *fp
= fopen(filename
, "r");
435 die("Could not open file '%s': %s", filename
, strerror(errno
));
437 while (strbuf_getline(&str
, fp
, '\n') != EOF
) {
442 quoted
= strbuf_detach(&str
, NULL
);
443 res
= sq_dequote_to_argv(quoted
, &rev_argv
,
444 &rev_argv_nr
, &rev_argv_alloc
);
446 die("Badly quoted content in file '%s': %s",
450 strbuf_release(&str
);
454 static int skipcmp(const void *a
, const void *b
)
456 return hashcmp(a
, b
);
459 static void prepare_skipped(void)
461 qsort(skipped_sha1
, skipped_sha1_nr
, sizeof(*skipped_sha1
), skipcmp
);
464 static const unsigned char *skipped_sha1_access(size_t index
, void *table
)
466 unsigned char (*skipped
)[20] = table
;
467 return skipped
[index
];
470 static int lookup_skipped(unsigned char *sha1
)
472 return sha1_pos(sha1
, skipped_sha1
, skipped_sha1_nr
,
473 skipped_sha1_access
);
476 struct commit_list
*filter_skipped(struct commit_list
*list
,
477 struct commit_list
**tried
,
480 struct commit_list
*filtered
= NULL
, **f
= &filtered
;
484 if (!skipped_sha1_nr
)
490 struct commit_list
*next
= list
->next
;
492 if (0 <= lookup_skipped(list
->item
->object
.sha1
)) {
493 /* Move current to tried list */
499 /* Move current to filtered list */
509 static void bisect_rev_setup(struct rev_info
*revs
, const char *prefix
)
511 init_revisions(revs
, prefix
);
513 revs
->commit_format
= CMIT_FMT_UNSPECIFIED
;
515 /* argv[0] will be ignored by setup_revisions */
516 ALLOC_GROW(rev_argv
, rev_argv_nr
+ 1, rev_argv_alloc
);
517 rev_argv
[rev_argv_nr
++] = xstrdup("bisect_rev_setup");
519 if (read_bisect_refs())
520 die("reading bisect refs failed");
522 ALLOC_GROW(rev_argv
, rev_argv_nr
+ 1, rev_argv_alloc
);
523 rev_argv
[rev_argv_nr
++] = xstrdup("--");
527 ALLOC_GROW(rev_argv
, rev_argv_nr
+ 1, rev_argv_alloc
);
528 rev_argv
[rev_argv_nr
++] = NULL
;
530 setup_revisions(rev_argv_nr
, rev_argv
, revs
, NULL
);
535 int bisect_next_vars(const char *prefix
)
537 struct rev_info revs
;
538 struct rev_list_info info
;
539 int reaches
= 0, all
= 0;
541 memset(&info
, 0, sizeof(info
));
543 info
.bisect_show_flags
= BISECT_SHOW_TRIED
| BISECT_SHOW_STRINGED
;
545 bisect_rev_setup(&revs
, prefix
);
547 if (prepare_revision_walk(&revs
))
548 die("revision walk setup failed");
549 if (revs
.tree_objects
)
550 mark_edges_uninteresting(revs
.commits
, &revs
, NULL
);
552 revs
.commits
= find_bisection(revs
.commits
, &reaches
, &all
,
555 return show_bisect_vars(&info
, reaches
, all
);