Skip to content

Commit 50adb49

Browse files
authored
chore(monorepo): stash in pre-commit to avoid all changes to partially commited files to end up in commit (#7559)
* chore(monorepo): stash in pre-commit to avoid all changes to partially commited files to end up in commit * fix(pre-commit): don't stash staged changes mixed with unstaged to avoid merge conflicts on pop * chore(pre-commit): quiet
1 parent bad01d0 commit 50adb49

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

.husky/pre-commit

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,19 @@
44
# listings staged files only
55
fileList=$(git diff --diff-filter=AM --cached --name-only)
66

7+
# making sure that pre-commit hook preserves the partial commits where only some
8+
# hunks are staged for commit
9+
# 1. stash only staged files first (there's no option to stash only uncommited
10+
# files, so we have to reverse the logic)
11+
git stash --staged -m "pre-commit hook: staged" -q
12+
# 2. stash everything that's left
13+
git stash -m "pre-commit hook: unstaged" -q
14+
# 3. restore staged state
15+
git stash pop stash@{1} -q
16+
17+
# when script exits, pop the stash with unstaged files
18+
trap "git stash pop -q" EXIT
19+
720
if [ -n "$fileList" ]; then
821
echo "Prettifying staged files..."
922
npx prettier-compass --write --ignore-unknown $fileList

0 commit comments

Comments
 (0)