From dd717cd8219f0774ecb717f44d6ba3f182f29c99 Mon Sep 17 00:00:00 2001 From: ruki Date: Sun, 15 Aug 2021 23:38:15 +0800 Subject: improve xmake-requires.lock --- xmake/modules/devel/git/lastcommit.lua | 77 ++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 xmake/modules/devel/git/lastcommit.lua (limited to 'xmake/modules/devel/git/lastcommit.lua') diff --git a/xmake/modules/devel/git/lastcommit.lua b/xmake/modules/devel/git/lastcommit.lua new file mode 100644 index 000000000..876812f86 --- /dev/null +++ b/xmake/modules/devel/git/lastcommit.lua @@ -0,0 +1,77 @@ +--!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 lastcommit.lua +-- + +-- imports +import("core.base.option") +import("lib.detect.find_tool") +import("net.proxy") + +-- get last commit in git repository +-- +-- @param opt the options, e.g. {repodir = ..} +-- +-- @return the last commit +-- +-- @code +-- +-- import("devel.git") +-- +-- local lastcommit = git.lastcommit({repodir = ..}) +-- +-- @endcode +-- +function main(opt) + + -- find git + local git = assert(find_tool("git"), "git not found!") + + -- init arguments + local argv = {"rev-parse", "HEAD"} + + -- trace + if option.get("verbose") then + print("%s %s", git.program, os.args(argv)) + end + + -- use proxy? + local envs + local proxy_conf = proxy.config(url) + if proxy_conf then + envs = {ALL_PROXY = proxy_conf} + end + + -- enter repository directory + local oldir = nil + if opt.repodir then + oldir = os.cd(opt.repodir) + end + + -- get last commit + local lastcommit = os.iorunv(git.program, argv, {envs = envs}) + if lastcommit then + lastcommit = lastcommit:trim() + end + + -- leave repository directory + if oldir then + os.cd(oldir) + end + return lastcommit +end -- cgit v1.3.1 From ab2c5e2a3b29bb63b23a7fa59c3f6d56cc56c47c Mon Sep 17 00:00:00 2001 From: ruki Date: Sun, 29 Aug 2021 21:56:52 +0800 Subject: improve git ops --- xmake/modules/devel/git/apply.lua | 13 +------------ xmake/modules/devel/git/checkout.lua | 13 +------------ xmake/modules/devel/git/clean.lua | 13 +------------ xmake/modules/devel/git/lastcommit.lua | 13 +------------ xmake/modules/devel/git/pull.lua | 15 ++------------- xmake/modules/devel/git/reset.lua | 13 +------------ xmake/modules/devel/git/submodule/update.lua | 21 +++++---------------- 7 files changed, 12 insertions(+), 89 deletions(-) (limited to 'xmake/modules/devel/git/lastcommit.lua') diff --git a/xmake/modules/devel/git/apply.lua b/xmake/modules/devel/git/apply.lua index 97923da02..ccfd5bf80 100644 --- a/xmake/modules/devel/git/apply.lua +++ b/xmake/modules/devel/git/apply.lua @@ -44,17 +44,6 @@ function main(patchfile, opt) opt = opt or {} local argv = {"apply", "--reject", "--ignore-whitespace", patchfile} - -- enter repository directory - local oldir = nil - if opt.repodir then - oldir = os.cd(opt.repodir) - end - -- apply it - os.vrunv(git.program, argv) - - -- leave repository directory - if oldir then - os.cd(oldir) - end + os.vrunv(git.program, argv, {curdir = opt.repodir}) end diff --git a/xmake/modules/devel/git/checkout.lua b/xmake/modules/devel/git/checkout.lua index 7a32aa3b1..c78d8f439 100644 --- a/xmake/modules/devel/git/checkout.lua +++ b/xmake/modules/devel/git/checkout.lua @@ -47,17 +47,6 @@ function main(commit, opt) -- init argv local argv = {"checkout", commit} - -- enter repository directory - local oldir = nil - if opt.repodir then - oldir = os.cd(opt.repodir) - end - -- checkout it - os.vrunv(git.program, argv) - - -- leave repository directory - if oldir then - os.cd(oldir) - end + os.vrunv(git.program, argv, {curdir = opt.repodir}) end diff --git a/xmake/modules/devel/git/clean.lua b/xmake/modules/devel/git/clean.lua index ae21c3d88..52fa81a0b 100644 --- a/xmake/modules/devel/git/clean.lua +++ b/xmake/modules/devel/git/clean.lua @@ -61,17 +61,6 @@ function main(opt) table.insert(argv, "-x") end - -- enter repository directory - local oldir = nil - if opt.repodir then - oldir = os.cd(opt.repodir) - end - -- clean it - os.vrunv(git.program, argv) - - -- leave repository directory - if oldir then - os.cd(oldir) - end + os.vrunv(git.program, argv, {curdir = opt.repodir}) end diff --git a/xmake/modules/devel/git/lastcommit.lua b/xmake/modules/devel/git/lastcommit.lua index 876812f86..1ad6de1f4 100644 --- a/xmake/modules/devel/git/lastcommit.lua +++ b/xmake/modules/devel/git/lastcommit.lua @@ -57,21 +57,10 @@ function main(opt) envs = {ALL_PROXY = proxy_conf} end - -- enter repository directory - local oldir = nil - if opt.repodir then - oldir = os.cd(opt.repodir) - end - -- get last commit - local lastcommit = os.iorunv(git.program, argv, {envs = envs}) + local lastcommit = os.iorunv(git.program, argv, {envs = envs, curdir = opt.repodir}) if lastcommit then lastcommit = lastcommit:trim() end - - -- leave repository directory - if oldir then - os.cd(oldir) - end return lastcommit end diff --git a/xmake/modules/devel/git/pull.lua b/xmake/modules/devel/git/pull.lua index 9970f8dbc..412e6e757 100644 --- a/xmake/modules/devel/git/pull.lua +++ b/xmake/modules/devel/git/pull.lua @@ -58,18 +58,12 @@ function main(opt) table.insert(argv, "--tags") end - -- enter repository directory - local oldir = nil - if opt.repodir then - oldir = os.cd(opt.repodir) - end - -- use proxy? local envs local proxy_conf = proxy.config() if proxy_conf then -- get proxy configuration from the current remote url - local remoteinfo = try { function() return os.iorunv(git.program, {"remote", "-v"}) end } + local remoteinfo = try { function() return os.iorunv(git.program, {"remote", "-v"}, {curdir = opt.repodir}) end } if remoteinfo then for _, line in ipairs(remoteinfo:split('\n', {plain = true})) do local splitinfo = line:split("%s+") @@ -86,10 +80,5 @@ function main(opt) end -- pull it - os.vrunv(git.program, argv, {envs = envs}) - - -- leave repository directory - if oldir then - os.cd(oldir) - end + os.vrunv(git.program, argv, {envs = envs, curdir = opt.repodir}) end diff --git a/xmake/modules/devel/git/reset.lua b/xmake/modules/devel/git/reset.lua index 28b984008..4ed798d25 100644 --- a/xmake/modules/devel/git/reset.lua +++ b/xmake/modules/devel/git/reset.lua @@ -71,17 +71,6 @@ function main(opt) table.insert(argv, opt.commit) end - -- enter repository directory - local oldir = nil - if opt.repodir then - oldir = os.cd(opt.repodir) - end - -- reset it - os.vrunv(git.program, argv) - - -- leave repository directory - if oldir then - os.cd(oldir) - end + os.vrunv(git.program, argv, {curdir = opt.repodir}) end diff --git a/xmake/modules/devel/git/submodule/update.lua b/xmake/modules/devel/git/submodule/update.lua index 5d9323a02..881663706 100644 --- a/xmake/modules/devel/git/submodule/update.lua +++ b/xmake/modules/devel/git/submodule/update.lua @@ -58,37 +58,26 @@ function main(opt) table.join2(argv, opt.paths) end - -- enter repository directory - local oldir = nil - if opt.repodir then - oldir = os.cd(opt.repodir) - end - -- enable long paths local longpaths_old local longpaths_changed = false if opt.longpaths then - local longpaths_old = try {function () return os.iorunv(git.program, {"config", "--get", "--global", "core.longpaths"}) end} + local longpaths_old = try {function () return os.iorunv(git.program, {"config", "--get", "--global", "core.longpaths"}, {curdir = opt.repodir}) end} if not longpaths_old or not longpaths_old:find("true") then - os.vrunv(git.program, {"config", "--global", "core.longpaths", "true"}) + os.vrunv(git.program, {"config", "--global", "core.longpaths", "true"}, {curdir = opt.repodir}) longpaths_changed = true end end -- submodule it - os.vrunv(git.program, argv) + os.vrunv(git.program, argv, {curdir = opt.repodir}) -- restore old long paths configuration if longpaths_changed then if longpaths_old and longpaths_old:find("false") then - os.vrunv(git.program, {"config", "--global", "core.longpaths", "false"}) + os.vrunv(git.program, {"config", "--global", "core.longpaths", "false"}, {curdir = opt.repodir}) else - os.vrunv(git.program, {"config", "--global", "--unset", "core.longpaths"}) + os.vrunv(git.program, {"config", "--global", "--unset", "core.longpaths", {curdir = opt.repodir}}) end end - - -- leave repository directory - if oldir then - os.cd(oldir) - end end -- cgit v1.3.1