summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2025-11-10 23:43:17 +0800
committerGitHub <[email protected]>2025-11-10 23:43:17 +0800
commited81f6d290a515dc22f67ec72d3df7d46f489825 (patch)
treef4f5a9f067466015138cd459fbf426c4b2393657
parentf631d3119bbad7760495d2856bdc13c0c671d9f4 (diff)
parent8cd0f8c32375c01d3384b4199551358a92c728d2 (diff)
Merge pull request #7012 from xmake-io/git
fix sparse checkout #7007
-rw-r--r--xmake/modules/devel/git/checkout.lua5
-rw-r--r--xmake/modules/devel/git/clone.lua47
-rw-r--r--xmake/modules/devel/git/support.lua95
-rw-r--r--xmake/modules/private/action/require/impl/actions/download.lua12
4 files changed, 107 insertions, 52 deletions
diff --git a/xmake/modules/devel/git/checkout.lua b/xmake/modules/devel/git/checkout.lua
index 6c7645d52..dfbfeb423 100644
--- a/xmake/modules/devel/git/checkout.lua
+++ b/xmake/modules/devel/git/checkout.lua
@@ -21,6 +21,7 @@
-- imports
import("core.base.option")
import("core.base.semver")
+import("devel.git.support")
import("lib.detect.find_tool")
-- checkout to given branch, tag or commit
@@ -39,7 +40,7 @@ import("lib.detect.find_tool")
--
function main(commit, opt)
opt = opt or {}
- local git = assert(find_tool("git", {version = true}), "git not found!")
+ local git = assert(find_tool("git"), "git not found!")
local argv = {}
if opt.fsmonitor then
table.insert(argv, "-c")
@@ -51,7 +52,7 @@ function main(commit, opt)
-- @see https://github.com/xmake-io/xmake/issues/6071
-- https://github.blog/open-source/git/bring-your-monorepo-down-to-size-with-sparse-checkout/
- if opt.includes and git.version and semver.compare(git.version, "2.25") >= 0 then
+ if opt.includes and support.can_sparse_checkout() then
os.vrunv(git.program, {"sparse-checkout", "init", "--cone"}, {curdir = opt.repodir})
os.vrunv(git.program, table.join({"sparse-checkout", "set"}, opt.includes), {curdir = opt.repodir})
end
diff --git a/xmake/modules/devel/git/clone.lua b/xmake/modules/devel/git/clone.lua
index 2d2f0eb26..ceb5282b6 100644
--- a/xmake/modules/devel/git/clone.lua
+++ b/xmake/modules/devel/git/clone.lua
@@ -23,48 +23,7 @@ import("core.base.option")
import("core.base.semver")
import("lib.detect.find_tool")
import("net.proxy")
-
--- can clone tag?
--- @see https://github.com/xmake-io/xmake/issues/4151
-function can_clone_tag()
- local can = _g.can_clone_tag
- if can == nil then
- local git = assert(find_tool("git", {version = true}), "git not found!")
- if git.version and semver.compare(git.version, "1.7.10") >= 0 then
- can = true
- end
- _g.can_clone_tag = can or false
- end
- return can or false
-end
-
--- can clone with --shallow-submodules?
--- @see https://github.com/xmake-io/xmake/issues/4151
-function can_shallow_submodules()
- local can = _g.can_shallow_submodules
- if can == nil then
- local git = assert(find_tool("git", {version = true}), "git not found!")
- if git.version and semver.compare(git.version, "2.9.0") >= 0 then
- can = true
- end
- _g.can_shallow_submodules = can or false
- end
- return can or false
-end
-
--- can clone with --filter=tree:0?
--- @see https://github.com/xmake-io/xmake/issues/6246
-function can_treeless()
- local can = _g.can_treeless
- if can == nil then
- local git = assert(find_tool("git", {version = true}), "git not found!")
- if git.version and semver.compare(git.version, "2.16.0") >= 0 then
- can = true
- end
- _g.can_treeless = can or false
- end
- return can or false
-end
+import("devel.git.support")
-- clone url
--
@@ -103,7 +62,7 @@ function main(url, opt)
-- treeless?
-- @see https://github.com/xmake-io/xmake/issues/5507
- if opt.treeless and can_treeless() then
+ if opt.treeless and support.can_treeless() then
table.insert(argv, "--filter=tree:0")
end
@@ -121,7 +80,7 @@ function main(url, opt)
if opt.recurse_submodules then
table.insert(argv, "--recurse-submodules")
end
- if opt.shallow_submodules and can_shallow_submodules() then
+ if opt.shallow_submodules and support.can_shallow_submodules() then
table.insert(argv, "--shallow-submodules")
end
diff --git a/xmake/modules/devel/git/support.lua b/xmake/modules/devel/git/support.lua
new file mode 100644
index 000000000..5211d62d2
--- /dev/null
+++ b/xmake/modules/devel/git/support.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 support.lua
+--
+
+-- imports
+import("core.base.option")
+import("core.base.semver")
+import("lib.detect.find_tool")
+
+-- get git version
+function _git_version()
+ local git_version = _g.git_version
+ if git_version == nil then
+ local git = assert(find_tool("git", {version = true}), "git not found!")
+ if git.version then
+ git_version = git.version
+ end
+ _g.git_version = git_version or false
+ end
+ return git_version or nil
+end
+
+-- can clone tag?
+-- @see https://github.com/xmake-io/xmake/issues/4151
+function can_clone_tag()
+ local can = _g.can_clone_tag
+ if can == nil then
+ local git_version = _git_version()
+ if git_version and semver.compare(git_version, "1.7.10") >= 0 then
+ can = true
+ end
+ _g.can_clone_tag = can or false
+ end
+ return can or false
+end
+
+-- can clone with --shallow-submodules?
+-- @see https://github.com/xmake-io/xmake/issues/4151
+function can_shallow_submodules()
+ local can = _g.can_shallow_submodules
+ if can == nil then
+ local git_version = _git_version()
+ if git_version and semver.compare(git_version, "2.9.0") >= 0 then
+ can = true
+ end
+ _g.can_shallow_submodules = can or false
+ end
+ return can or false
+end
+
+-- can clone with --filter=tree:0?
+-- @see https://github.com/xmake-io/xmake/issues/6246
+function can_treeless()
+ local can = _g.can_treeless
+ if can == nil then
+ local git_version = _git_version()
+ if git_version and semver.compare(git_version, "2.16.0") >= 0 then
+ can = true
+ end
+ _g.can_treeless = can or false
+ end
+ return can or false
+end
+
+-- can sparse checkout?
+-- @see https://github.com/xmake-io/xmake/issues/6071
+-- https://github.blog/open-source/git/bring-your-monorepo-down-to-size-with-sparse-checkout/
+function can_sparse_checkout()
+ local can = _g.can_sparse_checkout
+ if can == nil then
+ local git_version = _git_version()
+ if git_version and semver.compare(git_version, "2.25.0") >= 0 then
+ can = true
+ end
+ _g.can_sparse_checkout = can or false
+ end
+ return can or false
+end
+
diff --git a/xmake/modules/private/action/require/impl/actions/download.lua b/xmake/modules/private/action/require/impl/actions/download.lua
index df47673a3..a247acfdc 100644
--- a/xmake/modules/private/action/require/impl/actions/download.lua
+++ b/xmake/modules/private/action/require/impl/actions/download.lua
@@ -50,6 +50,8 @@ end
function _checkout(package, url, sourcedir, opt)
opt = opt or {}
local filename = opt.url_filename or url_filename(url)
+ local sparse_includes = opt.url_includes
+ local clone_submodules = opt.url_submodules ~= false
-- we need to enable longpaths on windows
local longpaths = package:policy("platform.longpaths")
@@ -108,8 +110,7 @@ function _checkout(package, url, sourcedir, opt)
branch = nil
end
- -- only shallow clone this branch
- local clone_submodules = opt.url_submodules ~= false
+ -- only shallow clone this branch
git.clone(url, {depth = 1, recursive = clone_submodules, shallow_submodules = clone_submodules, longpaths = longpaths, branch = branch, outputdir = packagedir})
-- download package from revision or tag?
@@ -130,8 +131,7 @@ function _checkout(package, url, sourcedir, opt)
-- only shallow clone this tag
-- @see https://github.com/xmake-io/xmake/issues/4151
- if tag and git.clone.can_clone_tag() then
- local clone_submodules = opt.url_submodules ~= false
+ if tag and git.support.can_clone_tag() and not sparse_includes then
git.clone(url, {depth = 1, recursive = clone_submodules, shallow_submodules = clone_submodules, longpaths = longpaths, branch = tag, outputdir = packagedir})
else
@@ -140,10 +140,10 @@ function _checkout(package, url, sourcedir, opt)
git.clone(url, {treeless = true, checkout = false, longpaths = longpaths, outputdir = packagedir})
-- attempt to checkout the given version
- git.checkout(revision, {repodir = packagedir, includes = opt.url_includes})
+ git.checkout(revision, {repodir = packagedir, includes = sparse_includes})
-- update all submodules
- if os.isfile(path.join(packagedir, ".gitmodules")) and opt.url_submodules ~= false then
+ if os.isfile(path.join(packagedir, ".gitmodules")) and clone_submodules then
git.submodule.update({init = true, recursive = true, longpaths = longpaths, repodir = packagedir})
end
end