Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

76 lines (57 sloc) 2.305 kb
#!/bin/bash
# Pre-commit hook
# Last update 7/13/2012 waltpo
Version="Version 0.1.0"
# Run VerifyMarkdown as part of a pre-commit hook, to ensure the Markdown is
# capable of producing valid XHTML.
# This hook will return 0 (meaning the commit can proceed) unless
# one or more of the Markdown (.md or .markdown) files fails VerifyMarkdown.
# If one or more of the Markdown files fails VerifyMarkdown, this hook
# will return 1, which will halt the commit.
# Function to call VerifyMarkdown for an individual file.
# This function requires the VerifyMarkdown folder to be in the PATH.
function validateMD
{
printf "Running VerifyMarkdown for ${@}\n"
VerifyMarkdown "${@}"
}
# Silently exit if VerifyMarkdown is not in the PATH,
# resulting in no check occuring on the Markdown files.
which VerifyMarkdown > /dev/null
if [ $? != 0 ] ; then
exit 0
fi
# Validate the .md or .markdown files.
printf "\e[1;36mPre-commit Validation ($Version)\n\e[0m"
printf "\e[1;36mChecking Markdown files.\n\e[0m"
# Track number of files checked and number of files that fail validation.
numChecked=0
numFailed=0
# Determine the top-level folder (used to fully qualify the file name).
toplevel=$(git rev-parse --show-toplevel)
for file in `git diff-index --cached --name-only --diff-filter=ACMR HEAD | grep -E '\.(md|markdown)' ` ;
do
fullyQualifiedFile="$toplevel/$file"
# Call the validation routine.
validateMD $fullyQualifiedFile
# Check the validateMD return value to see if the file succeeded.
if [ $? != 0 ] ; then
# Failed validation.
((++numFailed))
printf "\e[1;31mERROR: ${file} failed VerifyMarkdown.\n\e[0m"
fi
((++numChecked))
printf "\n" # Blank line for readability of output.
done
printf "\e[1;36mFinished checking Markdown files.\nChecked $numChecked Markdown file(s), $numFailed with errors.\n\e[0m"
if [ $numFailed != 0 ] ; then
printf "\n\e[1;31m"
printf "******************************************************\n"
printf "ERROR(s) ENCOUNTERED:\n"
printf "Your commit has been halted due to validation failure.\n"
printf "Fix the error(s) above and try the commit again.\n"
printf "******************************************************\n\e[0m"
exit 1
else
exit 0
fi
Jump to Line
Something went wrong with that request. Please try again.