-
Notifications
You must be signed in to change notification settings - Fork 0
/
remix_pre_commit
executable file
·79 lines (65 loc) · 1.86 KB
/
remix_pre_commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/bin/bash
PASS=true
STAGED_FILES=$(git diff --cached --name-only)
UNSTAGED_FILES=$(git diff --name-only)
if [[ "$STAGED_FILES" = "" ]]; then
echo "no files"
exit 0
fi
# stash everything that hasn't been added. If a file is partially staged, this
# will stash the changes that have not been added to the index to be committed
git stash --keep-index -u
npm run typecheck
if [[ "$?" != 0 ]]; then
git stash pop
exit 1
fi
npm run lint
if [[ "$?" != 0 ]]; then
git stash pop
exit 1
fi
# run prettier on the remaining files
npm run format $STAGED_FILES
# if anything that was unstaged before has been modified now, git stash pop is
# going to get upset later. So we'll undo the formatting prettier did to the
# files and tell the user to fix it
PRETTIFIED_FILES=$(git diff --name-only)
for file in $PRETTIFIED_FILES; do
echo "$file"
echo $UNSTAGED_FILES
echo ""
if [[ $UNSTAGED_FILES == *"$file"* ]]; then
echo "matches"
PASS=false
fi
done
if ! $PASS; then
git restore $PRETTIFIED_FILES
git stash pop
cat << END
${PRETTIFIED_FILES}
The files above were only partially staged for commit, but need to be formatted
by prettier. That will cause problems when restoring the rest of your
changes after the commit is done. Your working directory is back to how it was
before you tried to commit. If you really want to do this partial commit thing
"npm run format ." then "git add -i", then try and commit again, and it should
work.
END
exit 1
fi
# This is kind of dumb, but we have to restore any prettified files to pop
# any unstaged changes back. Then since we know there aren't any unstaged
# changes to those files we can just add them and carry on
if [[ "$PRETTIFIED_FILES" != "" ]]; then
git restore $PRETTIFIED_FILES
git stash pop
npm run format $PRETTIFIED_FILES
git add $PRETTIFIED_FILES
else
git stash pop
fi
if ! $PASS; then
exit 1
fi
exit 0