Skip to content

Commit ed548d8

Browse files
authored
Merge pull request #18 from buildkite-plugins/sup-4558-add-env-var
Add BUILDKITE_GIT_FETCH_FLAGS (SUP-4558)
2 parents 6d6d076 + 27692eb commit ed548d8

2 files changed

Lines changed: 33 additions & 3 deletions

File tree

hooks/checkout

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,23 @@ if [[ ! -d .git ]]; then
4242
fi
4343

4444
git clean -ffxdq
45-
# fetch branch if commit is HEAD
45+
46+
FETCH_FLAGS=()
47+
if [[ -n "${BUILDKITE_GIT_FETCH_FLAGS:-}" ]]; then
48+
# Use read -a to split the flags safely and shellcheck-compliantly
49+
read -r -a FETCH_FLAGS <<< "${BUILDKITE_GIT_FETCH_FLAGS}"
50+
fi
51+
52+
FETCH_FLAGS+=(--depth 1 origin)
53+
4654
if [[ ${BUILDKITE_COMMIT} = "HEAD" ]]; then
47-
git fetch --depth 1 origin "${BUILDKITE_BRANCH}"
55+
FETCH_FLAGS+=( "${BUILDKITE_BRANCH}" )
4856
else
49-
git fetch --depth 1 origin "${BUILDKITE_COMMIT}"
57+
FETCH_FLAGS+=( "${BUILDKITE_COMMIT}" )
5058
fi
5159

60+
git fetch "${FETCH_FLAGS[@]}"
61+
5262
git sparse-checkout set ${NO_CONE_PARAM:+--no-cone} "${CHECKOUT_PATHS[@]}"
5363
if [[ ${BUILDKITE_COMMIT} = "HEAD" ]]; then
5464
git checkout FETCH_HEAD

tests/checkout.bats

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,23 @@ setup() {
8080

8181
unstub git
8282
}
83+
84+
@test "Respects BUILDKITE_GIT_FETCH_FLAGS in git fetch" {
85+
export BUILDKITE_GIT_FETCH_FLAGS="--prune --verbose"
86+
export BUILDKITE_COMMIT="HEAD"
87+
export BUILDKITE_BRANCH="main"
88+
89+
stub ssh-keyscan "* : echo 'keyscan'"
90+
stub git "clean * : echo 'git clean'"
91+
stub git "fetch --prune --verbose --depth 1 origin * : echo 'git fetch with flags'"
92+
stub git "sparse-checkout set * * : echo 'git sparse-checkout'"
93+
stub git "checkout * : echo 'checkout'"
94+
95+
run "$PWD"/hooks/checkout
96+
97+
assert_success
98+
assert_output --partial 'git fetch with flags'
99+
100+
unstub ssh-keyscan
101+
unstub git
102+
}

0 commit comments

Comments
 (0)