Skip to content

Commit 54ca659

Browse files
authored
Merge pull request #25 from buildkite-plugins/SUP-5229-optimize-clean-checkout
Optimize clean_checkout workflow
2 parents fc0c860 + 5de0a4c commit 54ca659

3 files changed

Lines changed: 18 additions & 23 deletions

File tree

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,13 @@ Whether to skip ssh-keyscan step. This will skip adding each ssh public key into
2626

2727
#### `clean_checkout` ('true' or 'false')
2828

29-
Whether to perform aggressive repository cleanup before checkout. This option handles scenarios where interrupted or cancelled jobs leave the git repository in a corrupted state with uncommitted changes that would prevent checkout. When enabled, it performs `git reset --hard HEAD` and `git sparse-checkout disable` in addition to the normal cleanup.
29+
Whether to perform aggressive repository cleanup before checkout. This option handles scenarios where interrupted or cancelled jobs leave the git repository in a corrupted state that would prevent checkout. When enabled, it removes git lock files, resets the repository with `git reset --hard HEAD`, and cleans all untracked files with `git clean -ffxdq`.
30+
31+
**What it fixes:**
32+
- Stale git lock files (from interrupted operations)
33+
- Corrupted git index
34+
- Uncommitted changes in tracked files
35+
- Untracked and ignored files
3036

3137
**⚠️ Warning:** This option will destroy ALL local changes and remove ALL untracked files. The `git clean -ffxdq` command with the `-x` flag will also remove ignored files (such as credentials, local configuration, or cache files). Only use this option when you're certain no important local data needs to be preserved.
3238

hooks/checkout

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -78,20 +78,14 @@ fi
7878
# Enable clean checkout option to deal with corrupted repository states
7979
if [[ "${CLEAN_CHECKOUT_OPTION}" = "true" ]]; then
8080
log_info "Clean checkout enabled - resetting repository state"
81-
# Remove index lock files
81+
# Remove lock files (prevents "another git process" errors)
8282
find .git -name "*.lock" -delete 2>/dev/null || true
83-
# Reset index if corrupted
84-
if ! git status >/dev/null 2>&1; then
85-
rm -f .git/index 2>/dev/null || true
86-
fi
87-
# Disable sparse-checkout
88-
git sparse-checkout disable 2>/dev/null || true
89-
# Clean and reset
90-
git clean -ffxdq
91-
92-
if git rev-parse --verify HEAD >/dev/null 2>&1; then
93-
git reset --hard HEAD
94-
fi
83+
# Reset everything: index, working tree, and tracked files
84+
# This handles corrupted index, uncommitted changes, and staged changes
85+
git reset --hard HEAD 2>/dev/null || true
86+
# Clean untracked and ignored files
87+
# Must run AFTER reset to also clean files that were previously tracked
88+
git clean -ffxdq 2>/dev/null || true
9589
else
9690
git clean -ffxdq
9791
fi

tests/checkout.bats

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,8 @@ setup() {
125125
export BUILDKITE_PLUGIN_SPARSE_CHECKOUT_CLEAN_CHECKOUT="true"
126126

127127
stub ssh-keyscan "* : echo 'keyscan'"
128-
stub git "status : echo 'status ok'"
129-
stub git "sparse-checkout disable : echo 'git sparse-checkout disable'"
130-
stub git "clean -ffxdq : echo 'git clean aggressive'"
131-
stub git "rev-parse --verify HEAD : echo 'HEAD'"
132128
stub git "reset --hard HEAD : echo 'git reset hard'"
129+
stub git "clean -ffxdq : echo 'git clean aggressive'"
133130
stub git "fetch --depth 1 origin * : echo 'git fetch'"
134131
stub git "sparse-checkout set * * : echo 'git sparse-checkout'"
135132
stub git "checkout * : echo 'checkout'"
@@ -140,7 +137,7 @@ setup() {
140137
assert_output --partial 'Clean checkout enabled - resetting repository state'
141138
assert_output --partial 'git reset hard'
142139
assert_output --partial 'git clean aggressive'
143-
assert_output --partial 'git sparse-checkout disable'
140+
refute_output --partial 'git sparse-checkout disable'
144141

145142
unstub ssh-keyscan
146143
unstub git
@@ -150,10 +147,8 @@ setup() {
150147
export BUILDKITE_PLUGIN_SPARSE_CHECKOUT_CLEAN_CHECKOUT="true"
151148

152149
stub ssh-keyscan "* : echo 'keyscan'"
153-
stub git "status : echo 'status ok'"
154-
stub git "sparse-checkout disable : echo 'sparse-checkout disable'"
150+
stub git "reset --hard HEAD : exit 1"
155151
stub git "clean -ffxdq : echo 'git clean'"
156-
stub git "rev-parse --verify HEAD : exit 1"
157152
stub git "fetch --depth 1 origin * : echo 'git fetch'"
158153
stub git "sparse-checkout set * * : echo 'git sparse-checkout'"
159154
stub git "checkout * : echo 'checkout'"
@@ -163,7 +158,7 @@ setup() {
163158
assert_success
164159
assert_output --partial 'Clean checkout enabled - resetting repository state'
165160
assert_output --partial 'git clean'
166-
assert_output --partial 'sparse-checkout disable'
161+
refute_output --partial 'sparse-checkout disable'
167162

168163
unstub ssh-keyscan
169164
unstub git

0 commit comments

Comments
 (0)