diff options
| author | ruki <[email protected]> | 2025-12-03 00:10:23 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2025-12-03 00:10:23 +0800 |
| commit | 6e84afe485a93d52c1971cf8d6287679057e76dc (patch) | |
| tree | bba5b9dde7a3439e31fad6c38b919a4a6e0298d9 | |
| parent | 3aca93627b3cada55db8d0215d6ff87ca418ceab (diff) | |
| parent | 410c9ad1bef7982b47852491a3a9a7d870c090b5 (diff) | |
Merge pull request #7093 from xmake-io/mirror
Improve mirror repo url
| -rw-r--r-- | xmake/core/sandbox/modules/import/core/package/repository.lua | 45 | ||||
| -rw-r--r-- | xmake/modules/devel/git/pull.lua | 16 | ||||
| -rw-r--r-- | xmake/modules/devel/git/push.lua | 24 | ||||
| -rw-r--r-- | xmake/modules/devel/git/remote.lua | 90 | ||||
| -rw-r--r-- | xmake/modules/devel/git/submodule/update.lua | 16 | ||||
| -rw-r--r-- | xmake/plugins/repo/main.lua | 7 | ||||
| -rw-r--r-- | xmake/plugins/show/lists/envs.lua | 6 |
7 files changed, 167 insertions, 37 deletions
diff --git a/xmake/core/sandbox/modules/import/core/package/repository.lua b/xmake/core/sandbox/modules/import/core/package/repository.lua index 73b56a231..91c05982e 100644 --- a/xmake/core/sandbox/modules/import/core/package/repository.lua +++ b/xmake/core/sandbox/modules/import/core/package/repository.lua @@ -26,6 +26,7 @@ local global = require("base/global") local project = require("project/project") local localcache = require("cache/localcache") local repository = require("package/repository") +local os = require("base/os") local raise = require("sandbox/modules/raise") local import = require("sandbox/modules/import") @@ -113,29 +114,31 @@ function sandbox_core_package_repository.repositories(is_global) network = global.get("network") end - -- add artifacts urls - local artifacts_urls = os.getenv("XMAKE_BINARY_REPO") - if artifacts_urls then - artifacts_urls = {artifacts_urls} - else - artifacts_urls = localcache.cache("repository"):get("artifacts_urls") - if not artifacts_urls then - artifacts_urls = {"https://github.com/xmake-mirror/build-artifacts.git", - "https://gitlab.com/xmake-mirror/build-artifacts.git", - "https://gitee.com/xmake-mirror/build-artifacts.git"} - if network ~= "private" then - import("net.fasturl") - fasturl.add(artifacts_urls) - artifacts_urls = fasturl.sort(artifacts_urls) - localcache.cache("repository"):set("artifacts_urls", artifacts_urls) - localcache.cache("repository"):save() + -- add artifacts urls (only on Windows) + if os.is_host("windows") then + local artifacts_urls = os.getenv("XMAKE_BINARY_REPO") + if artifacts_urls then + artifacts_urls = {artifacts_urls} + else + artifacts_urls = localcache.cache("repository"):get("artifacts_urls") + if not artifacts_urls then + artifacts_urls = {"https://github.com/xmake-mirror/build-artifacts.git", + "https://gitlab.com/xmake-mirror/build-artifacts.git", + "https://gitee.com/xmake-mirror/build-artifacts.git"} + if network ~= "private" then + import("net.fasturl") + fasturl.add(artifacts_urls) + artifacts_urls = fasturl.sort(artifacts_urls) + localcache.cache("repository"):set("artifacts_urls", artifacts_urls) + localcache.cache("repository"):save() + end end end - end - if #artifacts_urls > 0 then - local repo = repository.load("build-artifacts", artifacts_urls[1], "main", true) - if repo then - table.insert(repositories, repo) + if #artifacts_urls > 0 then + local repo = repository.load("build-artifacts", artifacts_urls[1], "main", true) + if repo then + table.insert(repositories, repo) + end end end diff --git a/xmake/modules/devel/git/pull.lua b/xmake/modules/devel/git/pull.lua index eebf806b9..ea432461b 100644 --- a/xmake/modules/devel/git/pull.lua +++ b/xmake/modules/devel/git/pull.lua @@ -20,6 +20,7 @@ -- imports import("core.base.option") +import("devel.git.remote") import("lib.detect.find_tool") import("net.proxy") @@ -77,18 +78,9 @@ function main(opt) 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"}, {curdir = opt.repodir}) end } - if remoteinfo then - for _, line in ipairs(remoteinfo:split('\n', {plain = true})) do - local splitinfo = line:split("%s+") - if #splitinfo > 1 and splitinfo[1] == (opt.remote or "origin") then - local url = splitinfo[2] - if url then - proxy_conf = proxy.config(url) - end - break - end - end + local url = remote.get_url({remote = opt.remote or "origin", repodir = opt.repodir}) + if url then + proxy_conf = proxy.config(url) end envs = {ALL_PROXY = proxy_conf} end diff --git a/xmake/modules/devel/git/push.lua b/xmake/modules/devel/git/push.lua index 6609ca4a7..75272045f 100644 --- a/xmake/modules/devel/git/push.lua +++ b/xmake/modules/devel/git/push.lua @@ -20,7 +20,9 @@ -- imports import("core.base.option") +import("devel.git.remote") import("lib.detect.find_tool") +import("net.proxy") import("branch", {alias = "git_branch"}) -- push to given remote url and branch @@ -58,9 +60,27 @@ function main(url, opt) branch = branch .. ":" .. opt.remote_branch end table.insert(argv, branch) + + -- use proxy? + local envs + local proxy_conf = proxy.config() + if proxy_conf then + -- get proxy configuration from the remote url + -- if url is a remote name, get its URL; otherwise use url directly + local remote_url = url + if not url:find("://") and not url:find("@") then + -- looks like a remote name, get its URL + remote_url = remote.get_url({remote = url, repodir = opt.repodir}) + end + if remote_url then + proxy_conf = proxy.config(remote_url) + end + envs = {ALL_PROXY = proxy_conf} + end + if opt.verbose then - os.execv(git.program, argv, {curdir = opt.repodir}) + os.execv(git.program, argv, {envs = envs, curdir = opt.repodir}) else - os.vrunv(git.program, argv, {curdir = opt.repodir}) + os.vrunv(git.program, argv, {envs = envs, curdir = opt.repodir}) end end diff --git a/xmake/modules/devel/git/remote.lua b/xmake/modules/devel/git/remote.lua new file mode 100644 index 000000000..92f700bf0 --- /dev/null +++ b/xmake/modules/devel/git/remote.lua @@ -0,0 +1,90 @@ +--!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} + + -- set remote url + 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 5660d5506..da2c02cc0 100644 --- a/xmake/modules/devel/git/submodule/update.lua +++ b/xmake/modules/devel/git/submodule/update.lua @@ -20,7 +20,9 @@ -- imports import("core.base.option") +import("devel.git.remote") import("lib.detect.find_tool") +import("net.proxy") -- update submodule -- @@ -70,6 +72,18 @@ function main(opt) table.join2(argv, opt.paths) end + -- use proxy? + local envs + local proxy_conf = proxy.config() + if proxy_conf then + -- get proxy configuration from the current remote url + local url = remote.get_url({remote = opt.remote or "origin", repodir = opt.repodir}) + if url then + proxy_conf = proxy.config(url) + end + envs = {ALL_PROXY = proxy_conf} + end + -- update it - os.vrunv(git.program, argv, {curdir = opt.repodir}) + os.vrunv(git.program, argv, {envs = envs, 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}) diff --git a/xmake/plugins/show/lists/envs.lua b/xmake/plugins/show/lists/envs.lua index b4f9474a2..bb2bce8e3 100644 --- a/xmake/plugins/show/lists/envs.lua +++ b/xmake/plugins/show/lists/envs.lua @@ -38,7 +38,11 @@ function main() XMAKE_TMPDIR = {"Set the temporary directory.", os.tmpdir()}, XMAKE_PROFILE = {"Start profiler, e.g. perf:call, perf:tag, trace, stuck.", os.getenv("XMAKE_PROFILE")}, XMAKE_PKG_CACHEDIR = {"Set the cache directory of packages.", os.getenv("XMAKE_PKG_CACHEDIR")}, - XMAKE_PKG_INSTALLDIR = {"Set the install directory of packages.", os.getenv("XMAKE_PKG_INSTALLDIR")}} + XMAKE_PKG_INSTALLDIR = {"Set the install directory of packages.", os.getenv("XMAKE_PKG_INSTALLDIR")}, + XMAKE_MAIN_REPO = {"Set the official package master repository url.", os.getenv("XMAKE_MAIN_REPO")}, + XMAKE_BINARY_REPO = {"Set the official package pre-compiled repository url.", os.getenv("XMAKE_BINARY_REPO")}, + XMAKE_THEME = {"Set theme.", os.getenv("XMAKE_THEME") or global.get("theme")}, + XMAKE_STATS = {"Enable or disable user statistics.", os.getenv("XMAKE_STATS")}} local width = 24 for name, env in pairs(envs) do cprint("${color.dump.string}%s${clear}%s%s", name, (" "):rep(width - #name), env[1]) |
