Skip to content

Commit 7013ea1

Browse files
authored
Merge pull request #427 from caseybraithwaite/master
Add ability to perform a sparse checkout
2 parents 8861092 + 7fb4edc commit 7013ea1

3 files changed

Lines changed: 42 additions & 1 deletion

File tree

assets/in

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ configure_credentials "$payload"
3333

3434
uri=$(jq -r '.source.uri // ""' <<< "$payload")
3535
branch=$(jq -r '.source.branch // ""' <<< "$payload")
36+
sparse_paths="$(jq -r '(.source.sparse_paths // ["."])[]' <<< "$payload")" # those "'s are important
3637
git_config_payload=$(jq -r '.source.git_config // []' <<< "$payload")
3738
ref=$(jq -r '.version.ref // "HEAD"' <<< "$payload")
3839
override_branch=$(jq -r '.version.branch // ""' <<< "$payload")
@@ -89,15 +90,25 @@ elif [ -n "$tag_filter" ] || [ -n "$tag_regex" ] || [ "$fetch_tags" == "true" ]
8990
tagflag="--tags"
9091
fi
9192

93+
nocheckoutflag=""
94+
if [ "$sparse_paths" != "." ] && [ "$sparse_paths" != "" ]; then
95+
nocheckoutflag=" --no-checkout"
96+
fi
97+
9298
if [ "$disable_git_lfs" == "true" ]; then
9399
# skip the fetching of LFS objects for all following git commands
94100
export GIT_LFS_SKIP_SMUDGE=1
95101
fi
96102

97-
git clone --single-branch $depthflag $uri $branchflag $destination $tagflag
103+
git clone --single-branch $depthflag $uri $branchflag $destination $tagflag $nocheckoutflag
98104

99105
cd $destination
100106

107+
if [ "$sparse_paths" != "." ] && [ "$sparse_paths" != "" ]; then
108+
git config core.sparseCheckout true
109+
echo "$sparse_paths" >> ./.git/info/sparse-checkout
110+
fi
111+
101112
git fetch origin refs/notes/*:refs/notes/* $tagflag
102113

103114
if [ "$depth" -gt 0 ]; then

test/get.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,23 @@ it_can_get_from_url_at_override_branch() {
9191
test "$(git -C $dest rev-parse HEAD)" = $ref
9292
}
9393

94+
it_can_get_from_url_with_sparse_paths() {
95+
local repo=$(init_repo)
96+
local ref1=$(make_commit_to_file $repo file-a)
97+
local ref2=$(make_commit_to_file $repo file-b)
98+
local dest=$TMPDIR/destination
99+
local sparse_paths="file-a"
100+
101+
get_uri_with_sparse $repo $dest $sparse_paths | jq -e "
102+
.version == {ref: $(echo $ref2 | jq -R .)}
103+
"
104+
105+
test -e $dest/file-a
106+
test ! -e $dest/file-b
107+
108+
test "$(git -C $dest rev-parse HEAD)" = $ref2
109+
}
110+
94111
it_omits_empty_branch_in_metadata() {
95112
local repo=$(init_repo)
96113
local ref1=$(make_commit_to_branch $repo branch-a)
@@ -916,6 +933,7 @@ run it_can_get_from_url_at_ref
916933
run it_can_get_from_url_at_branch
917934
run it_can_get_from_url_only_single_branch
918935
run it_can_get_from_url_at_override_branch
936+
run it_can_get_from_url_with_sparse_paths
919937
run it_omits_empty_branch_in_metadata
920938
run it_returns_branch_in_metadata
921939
run it_omits_empty_tags_in_metadata

test/helpers.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,6 +1056,18 @@ get_uri_with_custom_timestamp() {
10561056
}" | ${resource_dir}/in "$2" | tee /dev/stderr
10571057
}
10581058

1059+
get_uri_with_sparse() {
1060+
jq -n "{
1061+
source: {
1062+
uri: $(echo $1 | jq -R .),
1063+
sparse_paths: $(echo "$@" | jq -R '. | split(" ")')
1064+
},
1065+
params: {
1066+
short_ref_format: \"test-%s\"
1067+
}
1068+
}" | ${resource_dir}/in "$2" | tee /dev/stderr
1069+
}
1070+
10591071
put_uri() {
10601072
jq -n "{
10611073
source: {

0 commit comments

Comments
 (0)