forked from joostdenijs/azure-content
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pre-commit
75 lines (57 loc) · 2.25 KB
/
pre-commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/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