From 2af38a7f5bea256822a9a5b266d07d58a69e10ed Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 13 Aug 2024 22:43:12 +0800 Subject: add cli.bisect --- xmake/modules/cli/bisect.lua | 103 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 xmake/modules/cli/bisect.lua 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 -- cgit v1.3.1 From ff6027db26b9ea5534c78934e92bdb53f7e0284d Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 13 Aug 2024 22:43:41 +0800 Subject: update help --- xmake/modules/cli/bisect.lua | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/xmake/modules/cli/bisect.lua b/xmake/modules/cli/bisect.lua index 3b8452f33..f96936d4f 100644 --- a/xmake/modules/cli/bisect.lua +++ b/xmake/modules/cli/bisect.lua @@ -28,14 +28,14 @@ local options = { {'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"}, + " $ xmake l cli.bisect -c 'xmake -rv' -g good -b bad", + " $ xmake l cli.bisect -c 'xmake -vD; xmake run hello' -g good -b bad"}, {'s', "script" , "kv" , nil, "Run the given lua script file.", "e.g.", - " $ xmake l cli.bisect -s /tmp/test.lua -b bad -g good"}, + " $ xmake l cli.bisect -s /tmp/test.lua -g good -b bad"}, {'-', "arbitrary", "vs", nil, "Run an arbitrary command.", "e.g.", - " $ xmake l cli.bisect -b ddb86e4 -g 90846dd -- xmake -rv"} + " $ xmake l cli.bisect -g 90846dd -b ddb86e4 -- xmake -rv"} } -- run command -- cgit v1.3.1 From 72da9018e251c0aea4b5d9ab04fc25e3148dcd93 Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 13 Aug 2024 22:44:22 +0800 Subject: add gitdir --- xmake/modules/cli/bisect.lua | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/xmake/modules/cli/bisect.lua b/xmake/modules/cli/bisect.lua index f96936d4f..2ce090960 100644 --- a/xmake/modules/cli/bisect.lua +++ b/xmake/modules/cli/bisect.lua @@ -26,6 +26,7 @@ import("lib.detect.find_tool") local options = { {'g', "good", "kv", nil, "Set the good commit."}, {'b', "bad", "kv", nil, "Set the bad commit."}, + {nil, "gitdir", "kv", nil, "Set the git root directory."}, {'c', "commands", "kv" , nil, "Run the multiple commands instead of the default build command.", "e.g.", " $ xmake l cli.bisect -c 'xmake -rv' -g good -b bad", @@ -83,15 +84,16 @@ function main(...) 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}) + local gitdir = args.gitdir or os.curdir() + os.execv(git.program, {"bisect", "start"}, {curdir = gitdir}) + os.execv(git.program, {"bisect", "good", good}, {curdir = gitdir}) + os.execv(git.program, {"bisect", "bad", bad}, {curdir = gitdir}) while true do local output if _run_command(args) then - output = os.iorunv(git.program, {"bisect", "good"}) + output = os.iorunv(git.program, {"bisect", "good"}, {curdir = gitdir}) else - output = os.iorunv(git.program, {"bisect", "bad"}) + output = os.iorunv(git.program, {"bisect", "bad"}, {curdir = gitdir}) end if output then print(output) -- cgit v1.3.1 From 7b00e3f8fe78cf32b2ecef81bd18c14d5f84e790 Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 13 Aug 2024 22:46:12 +0800 Subject: fix commands --- xmake/modules/cli/bisect.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xmake/modules/cli/bisect.lua b/xmake/modules/cli/bisect.lua index 2ce090960..5e69a8915 100644 --- a/xmake/modules/cli/bisect.lua +++ b/xmake/modules/cli/bisect.lua @@ -50,7 +50,7 @@ function _run_command(opt) local arbitrary = opt.arbitrary if commands then for _, command in ipairs(commands:split(";")) do - os.exec(command) + os.exec(command:trim()) end elseif arbitrary then local program = arbitrary[1] -- cgit v1.3.1