Skip to content

Commit

Permalink
build: fix commit hooks
Browse files Browse the repository at this point in the history
Provides a more reliable mechanism for recovering commit messages which
don't pass linting.
  • Loading branch information
tlaundal committed Feb 5, 2023
1 parent d4f7c01 commit 31f8db4
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 21 deletions.
25 changes: 6 additions & 19 deletions .husky/commit-msg
Original file line number Diff line number Diff line change
@@ -1,23 +1,10 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

# If we have a STDIN, use it, otherwise get one
if tty >/dev/null 2>&1; then
TTY="$(tty)"
else
TTY=/dev/tty
fi

while ! npx --no -- commitlint --edit "${1}"; do
# exit if we are not in in an interactive terminal
! [ -t 1 ] && exit 1
if ! npx --no -- commitlint --edit "${1}"; then
# Copy the non-comment lines
grep -v '^#' "${1}" > "${1}.aborted"

read -p "Open commit in editor? (Y/n) " yn < "$TTY"
if [ "$yn" = "" ]; then
yn='Y'
fi
case $yn in
[Yy] ) "$(git var GIT_EDITOR)" "${1}";;
* ) exit 1;;
esac
done
echo "If using the CLI, your message will be recovered if you commit again without a message."
exit 1
fi
16 changes: 16 additions & 0 deletions .husky/prepare-commit-msg
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

ABRT_FILE="${1}.aborted"
if [ -f "$ABRT_FILE" ]; then
# Only recover the message if message source is empty, ie. no message was
# provided.
if [ -z "${2}" ]; then
TEMP_FILE="${TMPDIR:-/tmp}/recover-commit-msg.$$"
printf '# Recovered commit message\n' | cat - "$ABRT_FILE" "${1}" > "$TEMP_FILE"
cp "$TEMP_FILE" "${1}"
rm "$TEMP_FILE"
fi

rm "$ABRT_FILE"
fi
2 changes: 1 addition & 1 deletion .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"recommendations": ["samverschueren.linter-xo"]
"recommendations": ["samverschueren.linter-xo", "joshbolduc.commitlint"]
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"test": "ava",
"watch": "ava --watch",
"docs": "typedoc --plugin typedoc-plugin-markdown --readme none --githubPages false ./src/index.ts",
"prepublish": "npm run build && npm run test"
"prepublish": "npm run build && npm run test",
"prepare": "husky install"
},
"keywords": [
"sveltekit",
Expand Down

0 comments on commit 31f8db4

Please sign in to comment.