From 2d7567b653cabaa0d259996a47227cefa9b54563 Mon Sep 17 00:00:00 2001
From: Philipp Gesang <phg42.2a@gmail.com>
Date: Sun, 23 Mar 2014 17:57:42 +0100
Subject: [db,tool] implement termination of bisect session

---
 src/luaotfload-database.lua | 17 +++++++++++++++++
 src/luaotfload-tool.lua     | 27 ++++++++++++++++++++++++---
 2 files changed, 41 insertions(+), 3 deletions(-)

diff --git a/src/luaotfload-database.lua b/src/luaotfload-database.lua
index de1594a..a369d8e 100644
--- a/src/luaotfload-database.lua
+++ b/src/luaotfload-database.lua
@@ -2945,6 +2945,22 @@ local collect_font_filenames = function ()
     return filenames
 end
 
+--[[doc--
+
+    nth_font_file -- Return the filename of the nth font.
+
+--doc]]--
+
+--- int -> string
+local nth_font_filename = function (n)
+    report ("info", 4, "db", "Picking the %d font file.", n)
+    if not p_blacklist then
+        read_blacklist ()
+    end
+    local filenames = collect_font_filenames ()
+    return filenames[n] and filenames[n][1] or "<error>"
+end
+
 --[[doc
 
     count_font_files -- Return the number of files found by
@@ -3490,6 +3506,7 @@ names.sanitize_fontname           = sanitize_fontname
 names.getfilename                 = resolve_fullpath
 names.set_location_precedence     = set_location_precedence
 names.count_font_files            = count_font_files
+names.nth_font_filename           = nth_font_filename
 
 --- font cache
 names.purge_cache    = purge_cache
diff --git a/src/luaotfload-tool.lua b/src/luaotfload-tool.lua
index 9529a4d..2301636 100755
--- a/src/luaotfload-tool.lua
+++ b/src/luaotfload-tool.lua
@@ -889,6 +889,21 @@ local bisect_stop = function ()
     return true, false
 end
 
+--[[doc--
+
+    bisect_terminate -- Wrap up a bisect session by printing the
+    offending font and removing the state file.
+
+--doc]]--
+
+local bisect_terminate = function (nsteps, culprit)
+    report ("info", 1, "bisect",
+            "Bisection completed after %d steps.", nsteps)
+    report ("info", 0, "bisect",
+            "Bad file: %s.", names.nth_font_filename (culprit))
+    return bisect_stop ()
+end
+
 --[[doc--
 
     bisect_set -- Prepare the next bisection step by setting high, low,
@@ -911,13 +926,19 @@ local bisect_set = function (outcome)
             nsteps, lo, hi, pivot)
 
     if outcome == "bad" then
-        hi    = pivot
+        hi = pivot
+        if lo >= hi then --- complete
+            return bisect_terminate (nsteps, lo)
+        end
         pivot = mathfloor ((lo + hi) / 2)
         report ("info", 0, "bisect",
                 "Continuing with the lower segment: lo=%d, hi=%d, pivot=%d.",
                 lo, hi, pivot)
     elseif outcome == "good" then
-        lo    = pivot
+        lo = pivot + 1
+        if lo >= hi then --- complete
+            return bisect_terminate (nsteps, lo)
+        end
         pivot = mathfloor ((lo + hi) / 2)
         report ("info", 0, "bisect",
                 "Continuing with the upper segment: lo=%d, hi=%d, pivot=%d.",
@@ -987,7 +1008,7 @@ end
 local bisect_modes = {
     start   = bisect_start,
     good    = function () return bisect_set "good" end,
-    bad     = function () return bisect_set "bad" end,
+    bad     = function () return bisect_set "bad"  end,
     stop    = bisect_stop,
     status  = bisect_status,
     run     = bisect_run,
-- 
cgit v1.2.3