Skip to content

Commit

Permalink
support sparse-checkout for git
Browse files Browse the repository at this point in the history
  • Loading branch information
waruqi committed Jan 13, 2025
1 parent 876fc4c commit 774980f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
10 changes: 9 additions & 1 deletion xmake/core/package/package.lua
Original file line number Diff line number Diff line change
Expand Up @@ -351,11 +351,19 @@ function _instance:url_version(url)
return self:extraconf("urls", url, "version")
end

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

-- get the includes paths of url
-- @note it does not support the path pattern, and it only supports for git url now.
-- @see https://github.com/xmake-io/xmake/issues/6071
function _instance:url_includes(url)
return self:extraconf("urls", url, "includes")
end

-- get the http headers of url, @note need raw url
function _instance:url_http_headers(url)
return self:extraconf("urls", url, "http_headers")
Expand Down
10 changes: 9 additions & 1 deletion xmake/modules/devel/git/checkout.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

-- imports
import("core.base.option")
import("core.base.semver")
import("lib.detect.find_tool")

-- checkout to given branch, tag or commit
Expand All @@ -38,7 +39,7 @@ import("lib.detect.find_tool")
--
function main(commit, opt)
opt = opt or {}
local git = assert(find_tool("git"), "git not found!")
local git = assert(find_tool("git", {version = true}), "git not found!")
local argv = {}
if opt.fsmonitor then
table.insert(argv, "-c")
Expand All @@ -48,6 +49,13 @@ function main(commit, opt)
table.insert(argv, "core.fsmonitor=false")
end

-- @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
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

table.insert(argv, "checkout")
table.insert(argv, commit)
os.vrunv(git.program, argv, {curdir = opt.repodir})
Expand Down
10 changes: 5 additions & 5 deletions xmake/modules/private/action/require/impl/actions/download.lua
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ 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})
git.checkout(revision, {repodir = packagedir, includes = opt.url_includes})

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

-- download codes from script
function _download_from_script(package, script, opt)

-- do download
script(package, opt)

-- trace
tty.erase_line_to_start().cr()
cprint("${yellow} => ${clear}download %s .. ${color.success}${text.success}", opt.url)
end
Expand Down Expand Up @@ -314,6 +310,7 @@ function main(package, opt)
for idx, url in ipairs(urls) do
local url_alias = package:url_alias(url)
local url_excludes = package:url_excludes(url)
local url_includes = package:url_includes(url)
local url_http_headers = package:url_http_headers(url)
local url_submodules = package:extraconf("urls", url, "submodules")

Expand Down Expand Up @@ -349,16 +346,19 @@ function main(package, opt)
url = url,
url_alias = url_alias,
url_excludes = url_excludes,
url_includes = url_includes,
url_submodules = url_submodules})
elseif git.checkurl(url) then
_checkout(package, url, sourcedir, {
url_alias = url_alias,
url_includes = url_includes,
url_submodules = url_submodules})
else
_download(package, url, sourcedir, {
download_only = opt.download_only,
url_alias = url_alias,
url_excludes = url_excludes,
url_includes = url_includes,
url_http_headers = url_http_headers})
end
return true
Expand Down

0 comments on commit 774980f

Please sign in to comment.