summaryrefslogtreecommitdiff
path: root/xmake/modules/cli/bisect.lua
diff options
context:
space:
mode:
authorruki <[email protected]>2024-08-13 22:43:12 +0800
committerruki <[email protected]>2024-08-13 22:43:12 +0800
commit2af38a7f5bea256822a9a5b266d07d58a69e10ed (patch)
tree329db1f514fbc90e67d2cc7ae4739f4ab30aba65 /xmake/modules/cli/bisect.lua
parentddb86e44c1e4849fee6e2a4d72dc8ee47350aaa0 (diff)
add cli.bisect
Diffstat (limited to 'xmake/modules/cli/bisect.lua')
-rw-r--r--xmake/modules/cli/bisect.lua103
1 files changed, 103 insertions, 0 deletions
diff --git a/xmake/modules/cli/bisect.lua b/xmake/modules/cli/bisect.lua
new file mode 100644
index 000000000..3b8452f33
--- /dev/null
+++ b/xmake/modules/cli/bisect.lua
@@ -0,0 +1,103 @@
+--!A cross-platform build utility based on Lua
+--
+-- Licensed under the Apache License, Version 2.0 (the "License");
+-- you may not use this file except in compliance with the License.
+-- You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+-- Copyright (C) 2015-present, TBOOX Open Source Group.
+--
+-- @author ruki
+-- @file bisect.lua
+--
+
+-- imports
+import("core.base.option")
+import("lib.detect.find_tool")
+
+-- the options
+local options = {
+ {'g', "good", "kv", nil, "Set the good commit."},
+ {'b', "bad", "kv", nil, "Set the bad commit."},
+ {'c', "commands", "kv" , nil, "Run the multiple commands instead of the default build command.",
+ "e.g.",
+ " $ xmake l cli.bisect -c 'xmake -rv' -b bad -g good",
+ " $ xmake l cli.bisect -c 'xmake -vD; xmake run hello' -b bad -g good"},
+ {'s', "script" , "kv" , nil, "Run the given lua script file.",
+ "e.g.",
+ " $ xmake l cli.bisect -s /tmp/test.lua -b bad -g good"},
+ {'-', "arbitrary", "vs", nil, "Run an arbitrary command.",
+ "e.g.",
+ " $ xmake l cli.bisect -b ddb86e4 -g 90846dd -- xmake -rv"}
+}
+
+-- run command
+function _run_command(opt)
+ opt = opt or {}
+ local ok = try
+ {
+ function ()
+ local commands = opt.commands
+ local scriptfile = opt.script
+ local arbitrary = opt.arbitrary
+ if commands then
+ for _, command in ipairs(commands:split(";")) do
+ os.exec(command)
+ end
+ elseif arbitrary then
+ local program = arbitrary[1]
+ local argv = #arbitrary > 1 and table.slice(arbitrary, 2) or {}
+ os.execv(program, argv)
+ elseif scriptfile and os.isfile(scriptfile) and path.extension(scriptfile) == ".lua" then
+ local script = import(path.basename(scriptfile),
+ {rootdir = path.directory(scriptfile), anonymous = true})
+ script(events)
+ end
+ return true
+ end,
+ catch
+ {
+ function (errors)
+ cprint(tostring(errors))
+ end
+ }
+ }
+ return ok
+end
+
+
+-- use `git bisect` to analyze problem
+function main(...)
+ local argv = table.pack(...)
+ local args = option.parse(argv, options, "Analyze problem using `git bisect`.",
+ "",
+ "Usage: xmake l cli.bisect [options]")
+
+ local git = assert(find_tool("git"), "git not found!")
+ local good = assert(args.good, "please set `--good commit`")
+ local bad = assert(args.bad, "please set `--bad commit`")
+ os.execv(git.program, {"bisect", "start"})
+ os.execv(git.program, {"bisect", "good", good})
+ os.execv(git.program, {"bisect", "bad", bad})
+ while true do
+ local output
+ if _run_command(args) then
+ output = os.iorunv(git.program, {"bisect", "good"})
+ else
+ output = os.iorunv(git.program, {"bisect", "bad"})
+ end
+ if output then
+ print(output)
+ if output:find("is the first bad commit", 1, true) then
+ break
+ end
+ end
+ end
+end