207
return best;
208
}
209
210
+static inline int commit_interesting(struct commit_list *elem)
211
+{
212
+ unsigned flags = elem->item->object.flags;
213
+ if (flags & UNINTERESTING)
214
+ return 0;
215
+ return (!revs.prune_fn || (flags & TREECHANGE));
216
+}
217
+
218
+static inline int weight(struct commit_list *elem)
219
+{
220
+ return *((int*)(elem->item->util));
221
+}
222
+
223
+static inline void weight_set(struct commit_list *elem, int weight)
224
+{
225
+ *((int*)(elem->item->util)) = weight;
226
+}
227
+
228
+static int count_interesting_parents(struct commit_list *elem)
229
+{
230
+ int cnt = 0;
231
+ if (!elem->item->parents)
232
+ return cnt;
233
+ for (elem = elem->item->parents; elem; elem = elem->next) {
234
+ if (commit_interesting(elem))
235
+ cnt++;
236
+ }
237
+ return cnt;
238
+}
239
+
240
+static struct commit_list *find_bisection_2(struct commit_list *list,
241
+ int *reaches, int *all)
242
+{
243
+ int n, nr, counted, distance;
244
+ struct commit_list *p, *best;
245
+ int *weights;
246
+
247
+ for (nr = 0, p = list; p; p = p->next) {
248
+ if (commit_interesting(p))
249
+ nr++;
250
+ }
251
+ *all = nr;
252
+ weights = xcalloc(nr, sizeof(int*));
253
+ counted = 0;
254
+
255
+ for (n = 0, p = list; p; p = p->next) {
256
+ if (!commit_interesting(p))
257
+ continue;
258
+ if (commit_interesting(p)) {
259
+ /*
260
+ * positive weight is the number of interesting
261
+ * commits it can reach, including itself.
262
+ * weight = 0 means it has one parent and
263
+ * its distance is unknown.
264
+ * weight < 0 means it has more than one
265
+ * parent and its distance is unknown.
266
+ */
267
+ p->item->util = &weights[n++];
268
+ switch (count_interesting_parents(p)) {
269
+ case 0:
270
+ weight_set(p, 1);
271
+ counted++;
272
+ break;
273
+ case 1:
274
+ weight_set(p, 0);
275
+ break;
276
+ default:
277
+ weight_set(p, -1);
278
+ break;
279
+ }
280
+ }
281
+ }
282
+
283
+ /*
284
+ * If you have only one parent in the resulting set
285
+ * then you can reach one commit more than that parent
286
+ * can reach. So we do not have to run the expensive
287
+ * count_distance() for single strand of pearls.
288
+ *
289
+ * However, if you have more than one parents, you cannot
290
+ * just add their distance and one for yourself, since
291
+ * they usually reach the same ancestor and you would
292
+ * end up counting them twice that way.
293
+ *
294
+ * So we will first count distance of merges the usual
295
+ * way, and then fill the blanks using cheaper algorithm.
296
+ */
297
+ for (p = list; p; p = p->next) {
298
+ if (!commit_interesting(p))
299
+ continue;
300
+ n = weight(p);
301
+ if (0 <= n)
302
+ continue;
303
+ distance = count_distance(p);
304
+ clear_distance(p);
305
+ weight_set(p, distance);
306
+
307
+ /* Does it happen to be at exactly half-way? */
308
+ distance *= 2;
309
+ if (nr == distance || (nr+1) == distance) {
310
+ p->next = NULL;
311
+ *reaches = weight(p);
312
+ free(weights);
313
+ return p;
314
+ }
315
+ counted++;
316
+ }
317
+
318
+ while (counted < nr) {
319
+ for (p = list; p; p = p->next) {
320
+ struct commit_list *q;
321
+
322
+ if (!commit_interesting(p) || 0 < weight(p))
323
+ continue;
324
+ for (q = p->item->parents; q; q = q->next)
325
+ if (commit_interesting(q) && 0 < weight(q))
326
+ break;
327
+ if (!q)
328
+ continue;
329
+ weight_set(p, weight(q)+1);
330
+ counted++;
331
+
332
+ /* Does it happen to be at exactly half-way? */
333
+ distance = weight(p) * 2;
334
+ if (nr == distance || (nr+1) == distance) {
335
+ p->next = NULL;
336
+ *reaches = weight(p);
337
+ free(weights);
338
+ return p;
339
+ }
340
+ }
341
+ }
342
+
343
+ /* Then find the best one */
344
+ counted = 0;
345
+ best = list;
346
+ for (p = list; p; p = p->next) {
347
+ if (!commit_interesting(p))
348
+ continue;
349
+ distance = weight(p);
350
+ if (nr - distance < distance)
351
+ distance = nr - distance;
352
+ if (distance > counted) {
353
+ best = p;
354
+ counted = distance;
355
+ *reaches = weight(p);
356
+ }
357
+ }
358
+ if (best)
359
+ best->next = NULL;
360
+ free(weights);
361
+ return best;
362
+}
363
+
364
static void read_revisions_from_stdin(struct rev_info *revs)
365
{
366
char line[1000];
452
if (bisect_list) {
453
int reaches = reaches, all = all;
454
301
- revs.commits = find_bisection(revs.commits,
302
- &reaches, &all);
455
+ if (!revs.prune_fn)
456
+ revs.commits = find_bisection_2(revs.commits,
457
+ &reaches, &all);
458
+ else
459
+ revs.commits = find_bisection(revs.commits,
460
+ &reaches, &all);
461
if (bisect_show_vars) {
462
int cnt;
463
if (!revs.commits)