Skip to content

Commit

Permalink
Add a basic linter for input files
Browse files Browse the repository at this point in the history
  • Loading branch information
martinthomson committed Jul 4, 2017
1 parent d52cba0 commit 5b25608
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
11 changes: 11 additions & 0 deletions main.mk
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,17 @@ $(TEST_REPORT): $(drafts_html) $(drafts_txt)
done; \
echo '</testsuite>' >>$@

.PHONY: lint
lint::
@err=0; for f in $(join $(drafts),$(draft_types)); do \
if [ ! -z "$$(tail -c 1 "$$f")" ]; then \
echo "$$f has no newline on the last line"; err=1; \
fi; \
if grep -n ' $$' "$$f"; then \
echo "$$f contains trailing whitespace"; err=1; \
fi; \
done; [ "$$err" -eq 0 ]

## Cleanup
COMMA := ,
.PHONY: clean
Expand Down
25 changes: 14 additions & 11 deletions pre-commit.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,32 @@

exec 1>&2

# No tests to update gh-pages
BRANCH=$(git symbolic-ref --short HEAD 2>/dev/null)
if [ "$BRANCH" = "gh-pages" -o "$BRANCH" = "gh-issues" -o -e .git/MERGE_HEAD ]; then
exit
fi

files=($(git status --porcelain draft-* | sed '/^[MARCU]/{s/.*draft-/draft-/;p;};d'))
hash gmake 2> /dev/null && MAKE=gmake || MAKE=make

function abort() {
echo "Commit refused: documents don't build successfully."
echo "To commit anyway, run \"git commit --no-verify\""
exit 1
}
trap abort ERR

files=($(git status --porcelain draft-* | sed '/^[MARCU]/{s/.*draft-/draft-/;p;};d' | sort))
tmpfiles=()
txtfiles=()
trap 'rm -f "${tmpfiles[@]}" "${txtfiles[@]}"' ERR EXIT
trap 'rm -f "${tmpfiles[@]}" "${txtfiles[@]}"' EXIT
for f in "${files[@]}"; do
tmp="${f%.*}"-tmp$$."${f##*.}"
tmpfiles+=("$tmp")
txtfiles+=("${tmp%.*}.txt")
# This makes a copy of the staged file.
git show :"$f" > "$tmp"
done
hash gmake 2> /dev/null && MAKE=gmake || MAKE=make
[ "${#txtfiles[@]}" -eq 0 ] || "$MAKE" "${txtfiles[@]}"
RESULT=$?
[ "${#txtfiles[@]}" -eq 0 ] && exit 0

if [ $RESULT -ne 0 ]; then
echo "Commit refused -- documents don't build successfully."
echo "To commit anyway, run \"git commit --no-verify\""
exit 1
fi
"$MAKE" "${txtfiles[@]}"
"$MAKE" lint "drafts=${txtfiles[@]%.*}"

0 comments on commit 5b25608

Please sign in to comment.