7 /* bits #0-15 in revision.h */
9 #define COUNTED (1u<<16)
12 * This is a truly stupid algorithm, but it's only
13 * used for bisection, and we just don't care enough.
15 * We care just barely enough to avoid recursing for
18 static int count_distance(struct commit_list
*entry
)
23 struct commit
*commit
= entry
->item
;
24 struct commit_list
*p
;
26 if (commit
->object
.flags
& (UNINTERESTING
| COUNTED
))
28 if (!(commit
->object
.flags
& TREESAME
))
30 commit
->object
.flags
|= COUNTED
;
36 nr
+= count_distance(p
);
45 static void clear_distance(struct commit_list
*list
)
48 struct commit
*commit
= list
->item
;
49 commit
->object
.flags
&= ~COUNTED
;
54 #define DEBUG_BISECT 0
56 static inline int weight(struct commit_list
*elem
)
58 return *((int*)(elem
->item
->util
));
61 static inline void weight_set(struct commit_list
*elem
, int weight
)
63 *((int*)(elem
->item
->util
)) = weight
;
66 static int count_interesting_parents(struct commit
*commit
)
68 struct commit_list
*p
;
71 for (count
= 0, p
= commit
->parents
; p
; p
= p
->next
) {
72 if (p
->item
->object
.flags
& UNINTERESTING
)
79 static inline int halfway(struct commit_list
*p
, int nr
)
82 * Don't short-cut something we are not going to return!
84 if (p
->item
->object
.flags
& TREESAME
)
89 * 2 and 3 are halfway of 5.
90 * 3 is halfway of 6 but 2 and 4 are not.
92 switch (2 * weight(p
) - nr
) {
93 case -1: case 0: case 1:
101 #define show_list(a,b,c,d) do { ; } while (0)
103 static void show_list(const char *debug
, int counted
, int nr
,
104 struct commit_list
*list
)
106 struct commit_list
*p
;
108 fprintf(stderr
, "%s (%d/%d)\n", debug
, counted
, nr
);
110 for (p
= list
; p
; p
= p
->next
) {
111 struct commit_list
*pp
;
112 struct commit
*commit
= p
->item
;
113 unsigned flags
= commit
->object
.flags
;
114 enum object_type type
;
116 char *buf
= read_sha1_file(commit
->object
.sha1
, &type
, &size
);
119 fprintf(stderr
, "%c%c%c ",
120 (flags
& TREESAME
) ? ' ' : 'T',
121 (flags
& UNINTERESTING
) ? 'U' : ' ',
122 (flags
& COUNTED
) ? 'C' : ' ');
124 fprintf(stderr
, "%3d", weight(p
));
126 fprintf(stderr
, "---");
127 fprintf(stderr
, " %.*s", 8, sha1_to_hex(commit
->object
.sha1
));
128 for (pp
= commit
->parents
; pp
; pp
= pp
->next
)
129 fprintf(stderr
, " %.*s", 8,
130 sha1_to_hex(pp
->item
->object
.sha1
));
132 sp
= strstr(buf
, "\n\n");
135 for (ep
= sp
; *ep
&& *ep
!= '\n'; ep
++)
137 fprintf(stderr
, " %.*s", (int)(ep
- sp
), sp
);
139 fprintf(stderr
, "\n");
142 #endif /* DEBUG_BISECT */
144 static struct commit_list
*best_bisection(struct commit_list
*list
, int nr
)
146 struct commit_list
*p
, *best
;
147 int best_distance
= -1;
150 for (p
= list
; p
; p
= p
->next
) {
152 unsigned flags
= p
->item
->object
.flags
;
154 if (flags
& TREESAME
)
156 distance
= weight(p
);
157 if (nr
- distance
< distance
)
158 distance
= nr
- distance
;
159 if (distance
> best_distance
) {
161 best_distance
= distance
;
169 struct commit
*commit
;
173 static int compare_commit_dist(const void *a_
, const void *b_
)
175 struct commit_dist
*a
, *b
;
177 a
= (struct commit_dist
*)a_
;
178 b
= (struct commit_dist
*)b_
;
179 if (a
->distance
!= b
->distance
)
180 return b
->distance
- a
->distance
; /* desc sort */
181 return hashcmp(a
->commit
->object
.sha1
, b
->commit
->object
.sha1
);
184 static struct commit_list
*best_bisection_sorted(struct commit_list
*list
, int nr
)
186 struct commit_list
*p
;
187 struct commit_dist
*array
= xcalloc(nr
, sizeof(*array
));
190 for (p
= list
, cnt
= 0; p
; p
= p
->next
) {
192 unsigned flags
= p
->item
->object
.flags
;
194 if (flags
& TREESAME
)
196 distance
= weight(p
);
197 if (nr
- distance
< distance
)
198 distance
= nr
- distance
;
199 array
[cnt
].commit
= p
->item
;
200 array
[cnt
].distance
= distance
;
203 qsort(array
, cnt
, sizeof(*array
), compare_commit_dist
);
204 for (p
= list
, i
= 0; i
< cnt
; i
++) {
205 struct name_decoration
*r
= xmalloc(sizeof(*r
) + 100);
206 struct object
*obj
= &(array
[i
].commit
->object
);
208 sprintf(r
->name
, "dist=%d", array
[i
].distance
);
209 r
->next
= add_decoration(&name_decoration
, obj
, r
);
210 p
->item
= array
[i
].commit
;
220 * zero or positive weight is the number of interesting commits it can
221 * reach, including itself. Especially, weight = 0 means it does not
222 * reach any tree-changing commits (e.g. just above uninteresting one
223 * but traversal is with pathspec).
225 * weight = -1 means it has one parent and its distance is yet to
228 * weight = -2 means it has more than one parent and its distance is
229 * unknown. After running count_distance() first, they will get zero
230 * or positive distance.
232 static struct commit_list
*do_find_bisection(struct commit_list
*list
,
233 int nr
, int *weights
,
237 struct commit_list
*p
;
241 for (n
= 0, p
= list
; p
; p
= p
->next
) {
242 struct commit
*commit
= p
->item
;
243 unsigned flags
= commit
->object
.flags
;
245 p
->item
->util
= &weights
[n
++];
246 switch (count_interesting_parents(commit
)) {
248 if (!(flags
& TREESAME
)) {
251 show_list("bisection 2 count one",
255 * otherwise, it is known not to reach any
256 * tree-changing commit and gets weight 0.
268 show_list("bisection 2 initialize", counted
, nr
, list
);
271 * If you have only one parent in the resulting set
272 * then you can reach one commit more than that parent
273 * can reach. So we do not have to run the expensive
274 * count_distance() for single strand of pearls.
276 * However, if you have more than one parents, you cannot
277 * just add their distance and one for yourself, since
278 * they usually reach the same ancestor and you would
279 * end up counting them twice that way.
281 * So we will first count distance of merges the usual
282 * way, and then fill the blanks using cheaper algorithm.
284 for (p
= list
; p
; p
= p
->next
) {
285 if (p
->item
->object
.flags
& UNINTERESTING
)
289 weight_set(p
, count_distance(p
));
290 clear_distance(list
);
292 /* Does it happen to be at exactly half-way? */
293 if (!find_all
&& halfway(p
, nr
))
298 show_list("bisection 2 count_distance", counted
, nr
, list
);
300 while (counted
< nr
) {
301 for (p
= list
; p
; p
= p
->next
) {
302 struct commit_list
*q
;
303 unsigned flags
= p
->item
->object
.flags
;
307 for (q
= p
->item
->parents
; q
; q
= q
->next
) {
308 if (q
->item
->object
.flags
& UNINTERESTING
)
317 * weight for p is unknown but q is known.
318 * add one for p itself if p is to be counted,
319 * otherwise inherit it from q directly.
321 if (!(flags
& TREESAME
)) {
322 weight_set(p
, weight(q
)+1);
324 show_list("bisection 2 count one",
328 weight_set(p
, weight(q
));
330 /* Does it happen to be at exactly half-way? */
331 if (!find_all
&& halfway(p
, nr
))
336 show_list("bisection 2 counted all", counted
, nr
, list
);
339 return best_bisection(list
, nr
);
341 return best_bisection_sorted(list
, nr
);
344 struct commit_list
*find_bisection(struct commit_list
*list
,
345 int *reaches
, int *all
,
349 struct commit_list
*p
, *best
, *next
, *last
;
352 show_list("bisection 2 entry", 0, 0, list
);
355 * Count the number of total and tree-changing items on the
356 * list, while reversing the list.
358 for (nr
= on_list
= 0, last
= NULL
, p
= list
;
361 unsigned flags
= p
->item
->object
.flags
;
364 if (flags
& UNINTERESTING
)
368 if (!(flags
& TREESAME
))
373 show_list("bisection 2 sorted", 0, nr
, list
);
376 weights
= xcalloc(on_list
, sizeof(*weights
));
378 /* Do the real work of finding bisection commit. */
379 best
= do_find_bisection(list
, nr
, weights
, find_all
);
383 *reaches
= weight(best
);