Skip to content

Commit ac4f795

Browse files
authored
Merge pull request #6072 from xmake-io/git
support sparse-checkout for git
2 parents 19f7c63 + 774980f commit ac4f795

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed

xmake/core/package/package.lua

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,11 +351,19 @@ function _instance:url_version(url)
351351
return self:extraconf("urls", url, "version")
352352
end
353353

354-
-- get the excludes list of url for the archive extractor, @note need raw url
354+
-- get the excludes paths of url
355+
-- @note it supports the path pattern, but it only supports for archiver.
355356
function _instance:url_excludes(url)
356357
return self:extraconf("urls", url, "excludes")
357358
end
358359

360+
-- get the includes paths of url
361+
-- @note it does not support the path pattern, and it only supports for git url now.
362+
-- @see https://github.com/xmake-io/xmake/issues/6071
363+
function _instance:url_includes(url)
364+
return self:extraconf("urls", url, "includes")
365+
end
366+
359367
-- get the http headers of url, @note need raw url
360368
function _instance:url_http_headers(url)
361369
return self:extraconf("urls", url, "http_headers")

xmake/modules/devel/git/checkout.lua

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
-- imports
2222
import("core.base.option")
23+
import("core.base.semver")
2324
import("lib.detect.find_tool")
2425

2526
-- checkout to given branch, tag or commit
@@ -38,7 +39,7 @@ import("lib.detect.find_tool")
3839
--
3940
function main(commit, opt)
4041
opt = opt or {}
41-
local git = assert(find_tool("git"), "git not found!")
42+
local git = assert(find_tool("git", {version = true}), "git not found!")
4243
local argv = {}
4344
if opt.fsmonitor then
4445
table.insert(argv, "-c")
@@ -48,6 +49,13 @@ function main(commit, opt)
4849
table.insert(argv, "core.fsmonitor=false")
4950
end
5051

52+
-- @see https://github.com/xmake-io/xmake/issues/6071
53+
-- https://github.blog/open-source/git/bring-your-monorepo-down-to-size-with-sparse-checkout/
54+
if opt.includes and git.version and semver.compare(git.version, "2.25") >= 0 then
55+
os.vrunv(git.program, {"sparse-checkout", "init", "--cone"}, {curdir = opt.repodir})
56+
os.vrunv(git.program, table.join({"sparse-checkout", "set"}, opt.includes), {curdir = opt.repodir})
57+
end
58+
5159
table.insert(argv, "checkout")
5260
table.insert(argv, commit)
5361
os.vrunv(git.program, argv, {curdir = opt.repodir})

xmake/modules/private/action/require/impl/actions/download.lua

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ function _checkout(package, url, sourcedir, opt)
125125
git.clone(url, {treeless = true, checkout = false, longpaths = longpaths, outputdir = packagedir})
126126

127127
-- attempt to checkout the given version
128-
git.checkout(revision, {repodir = packagedir})
128+
git.checkout(revision, {repodir = packagedir, includes = opt.url_includes})
129129

130130
-- update all submodules
131131
if os.isfile(path.join(packagedir, ".gitmodules")) and opt.url_submodules ~= false then
@@ -260,11 +260,7 @@ end
260260

261261
-- download codes from script
262262
function _download_from_script(package, script, opt)
263-
264-
-- do download
265263
script(package, opt)
266-
267-
-- trace
268264
tty.erase_line_to_start().cr()
269265
cprint("${yellow} => ${clear}download %s .. ${color.success}${text.success}", opt.url)
270266
end
@@ -314,6 +310,7 @@ function main(package, opt)
314310
for idx, url in ipairs(urls) do
315311
local url_alias = package:url_alias(url)
316312
local url_excludes = package:url_excludes(url)
313+
local url_includes = package:url_includes(url)
317314
local url_http_headers = package:url_http_headers(url)
318315
local url_submodules = package:extraconf("urls", url, "submodules")
319316

@@ -349,16 +346,19 @@ function main(package, opt)
349346
url = url,
350347
url_alias = url_alias,
351348
url_excludes = url_excludes,
349+
url_includes = url_includes,
352350
url_submodules = url_submodules})
353351
elseif git.checkurl(url) then
354352
_checkout(package, url, sourcedir, {
355353
url_alias = url_alias,
354+
url_includes = url_includes,
356355
url_submodules = url_submodules})
357356
else
358357
_download(package, url, sourcedir, {
359358
download_only = opt.download_only,
360359
url_alias = url_alias,
361360
url_excludes = url_excludes,
361+
url_includes = url_includes,
362362
url_http_headers = url_http_headers})
363363
end
364364
return true

0 commit comments

Comments
 (0)