Skip to content

Commit c5973c9

Browse files
committed
Reduce commands required to fix local git directory
1 parent fc0c860 commit c5973c9

2 files changed

Lines changed: 11 additions & 22 deletions

File tree

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)