|
1 | 1 | #!/bin/sh |
2 | 2 |
|
3 | | -# TODO: make this work natively on Windows |
| 3 | +# TODO: replace these uglies with JS (mjs) scripts |
4 | 4 |
|
5 | 5 | beginswith() { case $2 in "$1"*) true;; *) false;; esac; } |
| 6 | +prepend() { while read -r line; do echo "${1}${line}"; done; } |
6 | 7 |
|
7 | 8 | if beginswith 'rebase' "$GIT_REFLOG_ACTION"; then |
8 | | - echo 'husky-hook::pre-push: pushing commits in the middle of a rebase is not allowed!' |
| 9 | + echo 'husky-hook::pre-push: pushing commits in the middle of a rebase/merge/cherry-pick is not allowed!' |
9 | 10 | exit 1 |
10 | 11 | fi |
11 | 12 |
|
12 | 13 | echo 'husky-hook::pre-push: checking for commits that should not be pushed...' |
| 14 | + |
13 | 15 | log=$(git log HEAD --oneline --not --remotes) |
14 | 16 |
|
15 | | -# ? Make sure no commits to be pushed contain "mergeme" in their headers |
16 | | -if echo "$log" | grep -q -i -e 'mergeme' -e '\[WIP\]' -e '\[NOPUSH\]'; then |
| 17 | +set +e |
| 18 | + |
| 19 | +incompleteCommitsGrep=$(echo "$log" | grep -q -i -e 'mergeme' -e '\[WIP\]' -e '\[NOPUSH\]'; echo $?) |
| 20 | + |
| 21 | +# ? In the below lines, 'x' is passed as the "zero parameter" so "$@" spits out |
| 22 | +# ? the right stuff. Stuff like this won't be an issue when we switch this to JS |
| 23 | + |
| 24 | +interestingXxxFiles=$(git ls-tree -r --full-tree --name-only HEAD | grep -vE '(^|/)(\.husky|assets/templates/husky)/' | grep -vE '\.snap$') |
| 25 | + |
| 26 | +incompleteXxxFilesGrep=$(echo "$interestingXxxFiles" | xargs -d '\n' -P 0 sh -c 'if grep -q -F -e "XXX: " "$@"; then exit 255; else true; fi' 'x' 1>/dev/null 2>&1; echo $?) |
| 27 | + |
| 28 | +interestingPackageJsonFiles=$(echo "$interestingXxxFiles" | grep -E '^(packages/[^/]+/)?package\.json$') |
| 29 | + |
| 30 | +incompletePackageJsonFilesGrep=$(echo "$interestingPackageJsonFiles" | xargs -d '\n' -P 0 sh -c 'if grep -q -i -P -e "--assets-preset [^\x22]*?(\x27|((basic|cli|lib|react|nextjs) [^\x22]*){3})" "$@"; then exit 255; else true; fi' 'x' 1>/dev/null 2>&1; echo $?) |
| 31 | + |
| 32 | +interestingMdFiles=$(echo "$interestingXxxFiles" | grep '\.md$' | grep -vE '(^|/)(src|test|docs)/' | grep -vE '/CHANGELOG.md$') |
| 33 | + |
| 34 | +incompleteMdFilesGrep=$(echo "$interestingMdFiles" | xargs -d '\n' -P 0 sh -c 'if grep -q -i -F -e "-✄-" "$@"; then exit 255; else true; fi' 'x' 1>/dev/null 2>&1; echo $?) |
| 35 | + |
| 36 | +interestingTestFiles=$(echo "$interestingXxxFiles" | grep '\.test\.ts$') |
| 37 | + |
| 38 | +incompleteTestFilesGrep=$(echo "$interestingTestFiles" | xargs -d '\n' -P 0 sh -c 'if grep -q -i -E -e "(it|test|describe)\.only\(" "$@"; then exit 255; else true; fi' 'x' 1>/dev/null 2>&1; echo $?) |
| 39 | + |
| 40 | +set -e |
| 41 | + |
| 42 | +# ? Make sure no commits to be pushed contain "mergeme" etc in their headers, no |
| 43 | +# ? relevant markdown files contain "-✄-" in their contents, no files anywhere |
| 44 | +# ? anywhere contain "XXX: " in their contents, no test files contain |
| 45 | +# ? erroneously focused tests, and no package.json files have incomplete scripts |
| 46 | +if [ "$incompleteCommitsGrep" = 0 ] || [ "$incompleteMdFilesGrep" != 0 ] || [ "$incompleteXxxFilesGrep" != 0 ] || [ "$incompleteTestFilesGrep" != 0 ] || [ "$incompletePackageJsonFilesGrep" != 0 ]; then |
17 | 47 | echo 'husky-hook::pre-push: BAD COMMIT(S) DETECTED!' |
18 | 48 | echo 'husky-hook::pre-push: merge, delete, reword, or otherwise rectify the following commits before trying again:' |
19 | | - echo |
20 | | - echo "$log" | grep -i -e 'mergeme' -e '\[WIP\]' -e '\[NOPUSH\]' |
| 49 | + |
| 50 | + if [ "$incompleteCommitsGrep" = 0 ]; then |
| 51 | + echo |
| 52 | + echo 'husky-hook::pre-push: incomplete commits (e.g. WIP, mergeme, NOPUSH):' |
| 53 | + echo |
| 54 | + echo "$log" | grep -i -e 'mergeme' -e '\[WIP\]' -e '\[NOPUSH\]' | prepend ' ⋗ ' |
| 55 | + fi |
| 56 | + |
| 57 | + if [ "$incompleteMdFilesGrep" != 0 ]; then |
| 58 | + echo |
| 59 | + echo 'husky-hook::pre-push: markdown files at HEAD with unmerged replacer regions:' |
| 60 | + echo |
| 61 | + echo "$interestingMdFiles" | xargs -d '\n' -P 0 grep -i -l -H -F -e '-✄-' | prepend ' ⋗ ' |
| 62 | + fi |
| 63 | + |
| 64 | + if [ "$incompleteXxxFilesGrep" != 0 ]; then |
| 65 | + echo |
| 66 | + echo 'husky-hook::pre-push: files at HEAD with "XXX" error comments:' |
| 67 | + echo |
| 68 | + echo "$interestingXxxFiles" | xargs -d '\n' -P 0 grep -l -H -F -e 'XXX: ' | prepend ' ⋗ ' |
| 69 | + fi |
| 70 | + |
| 71 | + if [ "$incompleteTestFilesGrep" != 0 ]; then |
| 72 | + echo |
| 73 | + echo 'husky-hook::pre-push: files at HEAD with erroneously focused tests:' |
| 74 | + echo |
| 75 | + echo "$interestingTestFiles" | xargs -d '\n' -P 0 grep -i -l -H -E -e '(it|test|describe)\.only\(' | prepend ' ⋗ ' |
| 76 | + fi |
| 77 | + |
| 78 | + if [ "$incompletePackageJsonFilesGrep" != 0 ]; then |
| 79 | + echo |
| 80 | + echo 'husky-hook::pre-push: package.json files at HEAD with incomplete scripts:' |
| 81 | + echo |
| 82 | + echo "$interestingPackageJsonFiles" | xargs -d '\n' -P 0 grep -i -l -H -P -e "--assets-preset [^\x22]*?(\x27|((basic|cli|lib|react|nextjs) [^\x22]*){3})" | prepend ' ⋗ ' |
| 83 | + fi |
| 84 | + |
21 | 85 | echo |
22 | 86 | exit 2; |
23 | 87 | else |
|
0 commit comments