Code

Allow builtin-fetch to work on a detached HEAD
[git.git] / builtin-rev-list.c
index 2dae287129fa383ff6bf1b9309060623d569dcaa..899a31d09ac31aaf1336b5ab3959345ac83a9579 100644 (file)
@@ -189,7 +189,7 @@ static int count_interesting_parents(struct commit *commit)
        return count;
 }
 
-static inline int halfway(struct commit_list *p, int distance, int nr)
+static inline int halfway(struct commit_list *p, int nr)
 {
        /*
         * Don't short-cut something we are not going to return!
@@ -202,8 +202,7 @@ static inline int halfway(struct commit_list *p, int distance, int nr)
         * 2 and 3 are halfway of 5.
         * 3 is halfway of 6 but 2 and 4 are not.
         */
-       distance *= 2;
-       switch (distance - nr) {
+       switch (2 * weight(p) - nr) {
        case -1: case 0: case 1:
                return 1;
        default:
@@ -255,6 +254,30 @@ static void show_list(const char *debug, int counted, int nr,
 }
 #endif /* DEBUG_BISECT */
 
+static struct commit_list *best_bisection(struct commit_list *list, int nr)
+{
+       struct commit_list *p, *best;
+       int best_distance = -1;
+
+       best = list;
+       for (p = list; p; p = p->next) {
+               int distance;
+               unsigned flags = p->item->object.flags;
+
+               if (revs.prune_fn && !(flags & TREECHANGE))
+                       continue;
+               distance = weight(p);
+               if (nr - distance < distance)
+                       distance = nr - distance;
+               if (distance > best_distance) {
+                       best = p;
+                       best_distance = distance;
+               }
+       }
+
+       return best;
+}
+
 /*
  * zero or positive weight is the number of interesting commits it can
  * reach, including itself.  Especially, weight = 0 means it does not
@@ -271,8 +294,8 @@ static void show_list(const char *debug, int counted, int nr,
 static struct commit_list *do_find_bisection(struct commit_list *list,
                                             int nr, int *weights)
 {
-       int n, counted, distance;
-       struct commit_list *p, *best;
+       int n, counted;
+       struct commit_list *p;
 
        counted = 0;
 
@@ -322,15 +345,13 @@ static struct commit_list *do_find_bisection(struct commit_list *list,
        for (p = list; p; p = p->next) {
                if (p->item->object.flags & UNINTERESTING)
                        continue;
-               n = weight(p);
-               if (n != -2)
+               if (weight(p) != -2)
                        continue;
-               distance = count_distance(p);
+               weight_set(p, count_distance(p));
                clear_distance(list);
-               weight_set(p, distance);
 
                /* Does it happen to be at exactly half-way? */
-               if (halfway(p, distance, nr))
+               if (halfway(p, nr))
                        return p;
                counted++;
        }
@@ -368,8 +389,7 @@ static struct commit_list *do_find_bisection(struct commit_list *list,
                                weight_set(p, weight(q));
 
                        /* Does it happen to be at exactly half-way? */
-                       distance = weight(p);
-                       if (halfway(p, distance, nr))
+                       if (halfway(p, nr))
                                return p;
                }
        }
@@ -377,22 +397,7 @@ static struct commit_list *do_find_bisection(struct commit_list *list,
        show_list("bisection 2 counted all", counted, nr, list);
 
        /* Then find the best one */
-       counted = -1;
-       best = list;
-       for (p = list; p; p = p->next) {
-               unsigned flags = p->item->object.flags;
-
-               if (revs.prune_fn && !(flags & TREECHANGE))
-                       continue;
-               distance = weight(p);
-               if (nr - distance < distance)
-                       distance = nr - distance;
-               if (distance > counted) {
-                       best = p;
-                       counted = distance;
-               }
-       }
-       return best;
+       return best_bisection(list, nr);
 }
 
 static struct commit_list *find_bisection(struct commit_list *list,