Skip to content

Commit 72aaf67

Browse files
committed
add a stash/pop to the tidy pre-push git-hook
added error handling for git stash and apply fix tidy err abort if stash push fails fix tidy errors... again
1 parent 54a8a1d commit 72aaf67

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

src/etc/pre-push.sh

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,38 @@ ROOT_DIR="$(git rev-parse --show-toplevel)"
2626
echo "Running pre-push script $ROOT_DIR/x test tidy"
2727

2828
cd "$ROOT_DIR"
29+
30+
# Check if the local working dir contains uncommitted changes (including untracked files).
31+
if [ -n "$(git status --porcelain)" ]; then
32+
echo "Stashing local uncommitted changes before running Tidy."
33+
# Stash uncommitted changes so that tidy only checks what you are going to push.
34+
git stash push -u -q
35+
if [ $? -ne 0 ]; then
36+
echo "Error: Failed to stash changes."
37+
echo "You may use \`git push --no-verify\` to skip this pre-push check."
38+
exit 1
39+
fi
40+
STASHED=true
41+
else
42+
STASHED=false
43+
fi
44+
2945
./x test tidy --set build.locked-deps=true
30-
if [ $? -ne 0 ]; then
46+
TIDY_RESULT=$?
47+
48+
# Only pop the stash if something was previously stashed during this check.
49+
if [ "$STASHED" = true ]; then
50+
echo "Restoring stashed changes."
51+
# Split the stash pop into `apply` and `drop` so a user can fix things if this fails.
52+
if ! git stash apply -q; then
53+
echo "Warning: Failed to apply stashed changes due to conflicts."
54+
echo "Please resolve the conflicts manually and then run 'git stash drop' when finished."
55+
else
56+
git stash drop -q
57+
fi
58+
fi
59+
60+
if [ $TIDY_RESULT -ne 0 ]; then
3161
echo "You may use \`git push --no-verify\` to skip this check."
3262
exit 1
3363
fi

0 commit comments

Comments
 (0)