summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2025-12-03 00:34:43 +0800
committerruki <[email protected]>2025-12-03 00:34:43 +0800
commitf41c8d038f8896019b0c8cb64da0dd523c8a1c82 (patch)
tree888ed00c22185c0db3d5b768b5e3ea9e7d9a0c2e
parentedb55f6a7799181a2dfbd4eb8b6cf1f8be2f4f32 (diff)
update repo url
-rw-r--r--xmake/modules/devel/git/remote.lua95
-rw-r--r--xmake/plugins/repo/main.lua7
2 files changed, 102 insertions, 0 deletions
diff --git a/xmake/modules/devel/git/remote.lua b/xmake/modules/devel/git/remote.lua
new file mode 100644
index 000000000..57dc0ea21
--- /dev/null
+++ b/xmake/modules/devel/git/remote.lua
@@ -0,0 +1,95 @@
+--!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, Xmake Open Source Community.
+--
+-- @author ruki
+-- @file remote.lua
+--
+
+-- imports
+import("core.base.option")
+import("lib.detect.find_tool")
+
+-- get remote url
+--
+-- @param opt the argument options
+-- - remote: the remote name, default is "origin"
+-- - repodir: the repository directory
+--
+-- @code
+--
+-- import("devel.git")
+--
+-- local url = git.remote.get_url()
+-- local url = git.remote.get_url({remote = "origin", repodir = "/tmp/xmake"})
+--
+-- @endcode
+--
+function get_url(opt)
+ opt = opt or {}
+
+ -- find git
+ local git = assert(find_tool("git"), "git not found!")
+
+ -- init arguments
+ local argv = {"remote", "get-url", opt.remote or "origin"}
+
+ -- trace
+ if option.get("verbose") then
+ print("%s %s", git.program, os.args(argv))
+ end
+
+ -- get remote url
+ local url = try { function() return os.iorunv(git.program, argv, {curdir = opt.repodir}) end }
+ if url then
+ url = url:trim()
+ end
+ return url
+end
+
+-- set remote url
+--
+-- @param url the remote url
+-- @param opt the argument options
+-- - remote: the remote name, default is "origin"
+-- - repodir: the repository directory
+--
+-- @code
+--
+-- import("devel.git")
+--
+-- git.remote.set_url("https://github.com/xmake-io/xmake-repo.git")
+-- git.remote.set_url("https://github.com/xmake-io/xmake-repo.git", {remote = "origin", repodir = "/tmp/xmake"})
+--
+-- @endcode
+--
+function set_url(url, opt)
+ opt = opt or {}
+
+ -- find git
+ local git = assert(find_tool("git"), "git not found!")
+
+ -- init arguments
+ local argv = {"remote", "set-url", opt.remote or "origin", url}
+
+ -- trace
+ if option.get("verbose") then
+ print("%s %s", git.program, os.args(argv))
+ end
+
+ -- set remote url
+ os.vrunv(git.program, argv, {curdir = opt.repodir})
+end
+
diff --git a/xmake/plugins/repo/main.lua b/xmake/plugins/repo/main.lua
index f11e350c5..efc0c4207 100644
--- a/xmake/plugins/repo/main.lua
+++ b/xmake/plugins/repo/main.lua
@@ -127,6 +127,13 @@ function _update()
if os.isdir(repodir) then
-- only update the local repository with the remote url
if not os.isdir(repo:url()) then
+ -- check and update remote URL if it differs from repo:url()
+ local current_url = git.remote.get_url({repodir = repodir})
+ local expected_url = repo:url()
+ if current_url ~= expected_url then
+ vprint("updating remote URL for repository(%s): %s -> %s", repo:name(), current_url or "<none>", expected_url)
+ git.remote.set_url(expected_url, {repodir = repodir})
+ end
vprint("pulling repository(%s): %s to %s ..", repo:name(), repo:url(), repodir)
git.reset({verbose = option.get("verbose"), repodir = repodir, hard = true})
git.pull({verbose = option.get("verbose"), branch = repo:branch(), repodir = repodir, force = true})