5 #include "sha1-lookup.h"
8 static unsigned char (*skipped_sha1
)[20];
9 static int skipped_sha1_nr
;
11 /* bits #0-15 in revision.h */
13 #define COUNTED (1u<<16)
16 * This is a truly stupid algorithm, but it's only
17 * used for bisection, and we just don't care enough.
19 * We care just barely enough to avoid recursing for
22 static int count_distance(struct commit_list
*entry
)
27 struct commit
*commit
= entry
->item
;
28 struct commit_list
*p
;
30 if (commit
->object
.flags
& (UNINTERESTING
| COUNTED
))
32 if (!(commit
->object
.flags
& TREESAME
))
34 commit
->object
.flags
|= COUNTED
;
40 nr
+= count_distance(p
);
49 static void clear_distance(struct commit_list
*list
)
52 struct commit
*commit
= list
->item
;
53 commit
->object
.flags
&= ~COUNTED
;
58 #define DEBUG_BISECT 0
60 static inline int weight(struct commit_list
*elem
)
62 return *((int*)(elem
->item
->util
));
65 static inline void weight_set(struct commit_list
*elem
, int weight
)
67 *((int*)(elem
->item
->util
)) = weight
;
70 static int count_interesting_parents(struct commit
*commit
)
72 struct commit_list
*p
;
75 for (count
= 0, p
= commit
->parents
; p
; p
= p
->next
) {
76 if (p
->item
->object
.flags
& UNINTERESTING
)
83 static inline int halfway(struct commit_list
*p
, int nr
)
86 * Don't short-cut something we are not going to return!
88 if (p
->item
->object
.flags
& TREESAME
)
93 * 2 and 3 are halfway of 5.
94 * 3 is halfway of 6 but 2 and 4 are not.
96 switch (2 * weight(p
) - nr
) {
97 case -1: case 0: case 1:
105 #define show_list(a,b,c,d) do { ; } while (0)
107 static void show_list(const char *debug
, int counted
, int nr
,
108 struct commit_list
*list
)
110 struct commit_list
*p
;
112 fprintf(stderr
, "%s (%d/%d)\n", debug
, counted
, nr
);
114 for (p
= list
; p
; p
= p
->next
) {
115 struct commit_list
*pp
;
116 struct commit
*commit
= p
->item
;
117 unsigned flags
= commit
->object
.flags
;
118 enum object_type type
;
120 char *buf
= read_sha1_file(commit
->object
.sha1
, &type
, &size
);
123 fprintf(stderr
, "%c%c%c ",
124 (flags
& TREESAME
) ? ' ' : 'T',
125 (flags
& UNINTERESTING
) ? 'U' : ' ',
126 (flags
& COUNTED
) ? 'C' : ' ');
128 fprintf(stderr
, "%3d", weight(p
));
130 fprintf(stderr
, "---");
131 fprintf(stderr
, " %.*s", 8, sha1_to_hex(commit
->object
.sha1
));
132 for (pp
= commit
->parents
; pp
; pp
= pp
->next
)
133 fprintf(stderr
, " %.*s", 8,
134 sha1_to_hex(pp
->item
->object
.sha1
));
136 sp
= strstr(buf
, "\n\n");
139 for (ep
= sp
; *ep
&& *ep
!= '\n'; ep
++)
141 fprintf(stderr
, " %.*s", (int)(ep
- sp
), sp
);
143 fprintf(stderr
, "\n");
146 #endif /* DEBUG_BISECT */
148 static struct commit_list
*best_bisection(struct commit_list
*list
, int nr
)
150 struct commit_list
*p
, *best
;
151 int best_distance
= -1;
154 for (p
= list
; p
; p
= p
->next
) {
156 unsigned flags
= p
->item
->object
.flags
;
158 if (flags
& TREESAME
)
160 distance
= weight(p
);
161 if (nr
- distance
< distance
)
162 distance
= nr
- distance
;
163 if (distance
> best_distance
) {
165 best_distance
= distance
;
173 struct commit
*commit
;
177 static int compare_commit_dist(const void *a_
, const void *b_
)
179 struct commit_dist
*a
, *b
;
181 a
= (struct commit_dist
*)a_
;
182 b
= (struct commit_dist
*)b_
;
183 if (a
->distance
!= b
->distance
)
184 return b
->distance
- a
->distance
; /* desc sort */
185 return hashcmp(a
->commit
->object
.sha1
, b
->commit
->object
.sha1
);
188 static struct commit_list
*best_bisection_sorted(struct commit_list
*list
, int nr
)
190 struct commit_list
*p
;
191 struct commit_dist
*array
= xcalloc(nr
, sizeof(*array
));
194 for (p
= list
, cnt
= 0; p
; p
= p
->next
) {
196 unsigned flags
= p
->item
->object
.flags
;
198 if (flags
& TREESAME
)
200 distance
= weight(p
);
201 if (nr
- distance
< distance
)
202 distance
= nr
- distance
;
203 array
[cnt
].commit
= p
->item
;
204 array
[cnt
].distance
= distance
;
207 qsort(array
, cnt
, sizeof(*array
), compare_commit_dist
);
208 for (p
= list
, i
= 0; i
< cnt
; i
++) {
209 struct name_decoration
*r
= xmalloc(sizeof(*r
) + 100);
210 struct object
*obj
= &(array
[i
].commit
->object
);
212 sprintf(r
->name
, "dist=%d", array
[i
].distance
);
213 r
->next
= add_decoration(&name_decoration
, obj
, r
);
214 p
->item
= array
[i
].commit
;
224 * zero or positive weight is the number of interesting commits it can
225 * reach, including itself. Especially, weight = 0 means it does not
226 * reach any tree-changing commits (e.g. just above uninteresting one
227 * but traversal is with pathspec).
229 * weight = -1 means it has one parent and its distance is yet to
232 * weight = -2 means it has more than one parent and its distance is
233 * unknown. After running count_distance() first, they will get zero
234 * or positive distance.
236 static struct commit_list
*do_find_bisection(struct commit_list
*list
,
237 int nr
, int *weights
,
241 struct commit_list
*p
;
245 for (n
= 0, p
= list
; p
; p
= p
->next
) {
246 struct commit
*commit
= p
->item
;
247 unsigned flags
= commit
->object
.flags
;
249 p
->item
->util
= &weights
[n
++];
250 switch (count_interesting_parents(commit
)) {
252 if (!(flags
& TREESAME
)) {
255 show_list("bisection 2 count one",
259 * otherwise, it is known not to reach any
260 * tree-changing commit and gets weight 0.
272 show_list("bisection 2 initialize", counted
, nr
, list
);
275 * If you have only one parent in the resulting set
276 * then you can reach one commit more than that parent
277 * can reach. So we do not have to run the expensive
278 * count_distance() for single strand of pearls.
280 * However, if you have more than one parents, you cannot
281 * just add their distance and one for yourself, since
282 * they usually reach the same ancestor and you would
283 * end up counting them twice that way.
285 * So we will first count distance of merges the usual
286 * way, and then fill the blanks using cheaper algorithm.
288 for (p
= list
; p
; p
= p
->next
) {
289 if (p
->item
->object
.flags
& UNINTERESTING
)
293 weight_set(p
, count_distance(p
));
294 clear_distance(list
);
296 /* Does it happen to be at exactly half-way? */
297 if (!find_all
&& halfway(p
, nr
))
302 show_list("bisection 2 count_distance", counted
, nr
, list
);
304 while (counted
< nr
) {
305 for (p
= list
; p
; p
= p
->next
) {
306 struct commit_list
*q
;
307 unsigned flags
= p
->item
->object
.flags
;
311 for (q
= p
->item
->parents
; q
; q
= q
->next
) {
312 if (q
->item
->object
.flags
& UNINTERESTING
)
321 * weight for p is unknown but q is known.
322 * add one for p itself if p is to be counted,
323 * otherwise inherit it from q directly.
325 if (!(flags
& TREESAME
)) {
326 weight_set(p
, weight(q
)+1);
328 show_list("bisection 2 count one",
332 weight_set(p
, weight(q
));
334 /* Does it happen to be at exactly half-way? */
335 if (!find_all
&& halfway(p
, nr
))
340 show_list("bisection 2 counted all", counted
, nr
, list
);
343 return best_bisection(list
, nr
);
345 return best_bisection_sorted(list
, nr
);
348 struct commit_list
*find_bisection(struct commit_list
*list
,
349 int *reaches
, int *all
,
353 struct commit_list
*p
, *best
, *next
, *last
;
356 show_list("bisection 2 entry", 0, 0, list
);
359 * Count the number of total and tree-changing items on the
360 * list, while reversing the list.
362 for (nr
= on_list
= 0, last
= NULL
, p
= list
;
365 unsigned flags
= p
->item
->object
.flags
;
368 if (flags
& UNINTERESTING
)
372 if (!(flags
& TREESAME
))
377 show_list("bisection 2 sorted", 0, nr
, list
);
380 weights
= xcalloc(on_list
, sizeof(*weights
));
382 /* Do the real work of finding bisection commit. */
383 best
= do_find_bisection(list
, nr
, weights
, find_all
);
387 *reaches
= weight(best
);
393 static int skipcmp(const void *a
, const void *b
)
395 return hashcmp(a
, b
);
398 static void prepare_skipped(void)
400 qsort(skipped_sha1
, skipped_sha1_nr
, sizeof(*skipped_sha1
), skipcmp
);
403 static const unsigned char *skipped_sha1_access(size_t index
, void *table
)
405 unsigned char (*skipped
)[20] = table
;
406 return skipped
[index
];
409 static int lookup_skipped(unsigned char *sha1
)
411 return sha1_pos(sha1
, skipped_sha1
, skipped_sha1_nr
,
412 skipped_sha1_access
);
415 struct commit_list
*filter_skipped(struct commit_list
*list
,
416 struct commit_list
**tried
,
419 struct commit_list
*filtered
= NULL
, **f
= &filtered
;
423 if (!skipped_sha1_nr
)
429 struct commit_list
*next
= list
->next
;
431 if (0 <= lookup_skipped(list
->item
->object
.sha1
)) {
432 /* Move current to tried list */
438 /* Move current to filtered list */