fetchgit: Support sparse checkout

This allow git checkout small parts of a large repo, and avoid fetching
unnecessary blobs from server.
This commit is contained in:
Zhong Jianxin 2021-08-27 16:25:20 +08:00
parent e8d0231396
commit d03a07d5a7
3 changed files with 10 additions and 2 deletions

View file

@ -11,6 +11,7 @@ $SHELL $fetcher --builder --url "$url" --out "$out" --rev "$rev" \
${fetchLFS:+--fetch-lfs} \ ${fetchLFS:+--fetch-lfs} \
${deepClone:+--deepClone} \ ${deepClone:+--deepClone} \
${fetchSubmodules:+--fetch-submodules} \ ${fetchSubmodules:+--fetch-submodules} \
${sparseCheckout:+--sparse-checkout "$sparseCheckout"} \
${branchName:+--branch-name "$branchName"} ${branchName:+--branch-name "$branchName"}
runHook postFetch runHook postFetch

View file

@ -15,6 +15,7 @@ in
{ url, rev ? "HEAD", md5 ? "", sha256 ? "", hash ? "", leaveDotGit ? deepClone { url, rev ? "HEAD", md5 ? "", sha256 ? "", hash ? "", leaveDotGit ? deepClone
, fetchSubmodules ? true, deepClone ? false , fetchSubmodules ? true, deepClone ? false
, branchName ? null , branchName ? null
, sparseCheckout ? ""
, name ? urlToName url rev , name ? urlToName url rev
, # Shell code executed after the file has been fetched , # Shell code executed after the file has been fetched
# successfully. This can do things like check or transform the file. # successfully. This can do things like check or transform the file.
@ -74,7 +75,7 @@ stdenvNoCC.mkDerivation {
else else
lib.fakeSha256; lib.fakeSha256;
inherit url rev leaveDotGit fetchLFS fetchSubmodules deepClone branchName postFetch; inherit url rev leaveDotGit fetchLFS fetchSubmodules deepClone branchName sparseCheckout postFetch;
postHook = if netrcPhase == null then null else '' postHook = if netrcPhase == null then null else ''
${netrcPhase} ${netrcPhase}

View file

@ -48,6 +48,7 @@ Options:
--rev ref Any sha1 or references (such as refs/heads/master) --rev ref Any sha1 or references (such as refs/heads/master)
--hash h Expected hash. --hash h Expected hash.
--branch-name Branch name to check out into --branch-name Branch name to check out into
--sparse-checkout Only fetch and checkout part of the repository.
--deepClone Clone the entire repository. --deepClone Clone the entire repository.
--no-deepClone Make a shallow clone of just the required ref. --no-deepClone Make a shallow clone of just the required ref.
--leave-dotGit Keep the .git directories. --leave-dotGit Keep the .git directories.
@ -75,6 +76,7 @@ for arg; do
--hash) argfun=set_hashType;; --hash) argfun=set_hashType;;
--branch-name) argfun=set_branchName;; --branch-name) argfun=set_branchName;;
--deepClone) deepClone=true;; --deepClone) deepClone=true;;
--sparse-checkout) argfun=set_sparseCheckout;;
--quiet) QUIET=true;; --quiet) QUIET=true;;
--no-deepClone) deepClone=;; --no-deepClone) deepClone=;;
--leave-dotGit) leaveDotGit=true;; --leave-dotGit) leaveDotGit=true;;
@ -96,7 +98,7 @@ for arg; do
case $argfun in case $argfun in
set_*) set_*)
var=${argfun#set_} var=${argfun#set_}
eval $var=$arg eval "$var=$(printf %q "$arg")"
;; ;;
esac esac
argfun="" argfun=""
@ -112,6 +114,10 @@ init_remote(){
local url=$1 local url=$1
clean_git init --initial-branch=master clean_git init --initial-branch=master
clean_git remote add origin "$url" clean_git remote add origin "$url"
if [ -n "$sparseCheckout" ]; then
git config remote.origin.partialclonefilter "blob:none"
echo "$sparseCheckout" | git sparse-checkout set --stdin
fi
( [ -n "$http_proxy" ] && clean_git config http.proxy "$http_proxy" ) || true ( [ -n "$http_proxy" ] && clean_git config http.proxy "$http_proxy" ) || true
} }