Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 29 additions & 19 deletions .hooks/post-commit
Original file line number Diff line number Diff line change
@@ -1,37 +1,47 @@
#!/bin/bash
SHORT_SHA=$(git rev-parse HEAD)
LINT_LOG="$TMPDIR"/lint."$SHORT_SHA".log
#!/usr/bin/env bash

# strip color
# Enable strict mode:
# -E: inherit ERR traps in functions/subshells
# -e: exit on any command failure
# -u: error on unset variables
# -o pipefail: fail pipelines if any command fails
set -Eeuo pipefail

SHORT_SHA=$(git rev-parse --short HEAD)
LINT_LOG="${TMPDIR:-/tmp}/lint.${SHORT_SHA}.log"

# Strip color
strip() {
sed -r "s/\x1B\[([0-9]{1,3}(;[0-9]{1,2};?)?)?[mGK]//g"
}

# display a notification
# Display a notification
notify() {
# if osascript is not supported, do nothing
if [ -f /usr/bin/osascript ]; then
# read back in the lint errors
ERRORS=$(cat "$LINT_LOG" | sed 1,4d)
# read back in the lint errors
local ERRORS
ERRORS=$(sed 1,4d "${LINT_LOG}")

# Trigger apple- or OSA-script on supported platforms
/usr/bin/osascript -e "display notification \"$ERRORS\" with title \"$*\""
# macOS via osascript, Linux via notify-send; do nothing if neither exists
if [[ -x /usr/bin/osascript ]]; then
/usr/bin/osascript -e "display notification \"${ERRORS}\" with title \"$*\""
elif command -v notify-send >/dev/null; then
notify-send "$*" "${ERRORS}"
fi

# clean up
rm "$LINT_LOG"
}

# ensure failed lint exit code passes through sed
set -o pipefail
# Do not proceed if node_modules folder is missing
if [[ ! -d node_modules ]]; then
echo "post-commit: node_modules not found, skipping lint" >&2
exit 0
fi

# Do NOT run this when rebasing or we can't get the branch
# Do not run this when rebasing or we can't get the branch
branch=$(git branch --show-current)
if [ -z "$branch" ]; then
if [[ -z "${branch}" ]]; then
exit 0
fi

# Lint in the background, not blocking the terminal and piping all output to a file.
# If the lint fails, trigger a notification (on supported platforms) with at least the first error shown.
# We pipe output so that the terminal (tmux, vim, emacs etc.) isn't borked by stray output.
yarn lint:src | strip &>"$LINT_LOG" || notify "Lint Error" &
(yarn lint:src 2>&1 | strip >"${LINT_LOG}" 2>&1 || notify "Lint Error"; rm -f -- "${LINT_LOG}") &
Loading