I am trying to create bash script that checks my source code to flag absolutely url's as all of our devs should be using relative url's when it comes to internal links.
I was thinking to create a bash script that could then be included as part of our pre-commit hooks, so using git grep also seemed logical. I have everything working, however, I'm having some difficulty with the regex for the git grep.
Here is what I have so far:
`# Set's the regex test & files
readonly FORBIDDEN_RE="\((\|'\|\"\)\(http\|https\):\/\/\(www\.\)\?example\.com"
readonly FORBIDDEN_FILES="'*.md' '*.html' ':!README.md'"
# Adds double quotes around regex since without them git grep doesn't work,
# However, even trying to put the re_test variable in place of FORBIDDEN_RE
# is not working in the actual git grep.
# re_test="\"""$FORBIDDEN_RE""\""
if [ "$(git grep -n --cached -i -e "$FORBIDDEN_RE" -- "$FORBIDDEN_FILES")" != 0 ]; then
git grep -n --cached -i -e "$FORBIDDEN_RE" -- "$FORBIDDEN_FILES"
echo "ERROR: You're trying to make a commit that contains a hardcoded internal URL. Please make it relative."
exit 1
fi`
And here are the tests that I'm doing and if I expect them to be flagged by the regex or not:
http://www.example.com
- not flagged
(http://www.example.com
- flagged (We use markdown, so this would be the link in a markdown file)
"http://www.example.com
- flagged (because this the double quote signifies this is probably in an href value)
'http://www.example.com
- flagged (because this could also signify the URL is in an href value)
http://example.com
- not flagged
(http://example.com
- flagged
"http://example.com
- flagged
'http://example.com
- flagged
(http://example.com?param=abc
- flagged
"http://example.com?param=abc
- flagged
'http://example.com?param=abc
- flagged
(http://subdomain.example.com
- not flagged
What works for me perfectly just using the command line is:
git grep -n --cached -i -e "\((\|'\|\"\)\(http\|https\):\/\/\(www\.\)\?example\.com" -- '*.html' '*.md' ':!README.md'
However, with all the character escaping that I'm having to do and trying to get quotes around the regex when doing it in a bash file, I just can't seem to get this working within the bash file.
I echo'd out what $FORBIDDEN_RE
's value is and it seems to be taking out the \"
in the first capture group that is looking for either "(", single quote, or double quote. Here is the value of $FORBIDDEN_RE
when I echo out:
git grep -n --cached -i -e "\((\|'\|"\)\(http\|https\):\/\/\(www\.\)\?example\.com" -- '*.md' '*.html' ':!README.md'
Any ideas what I'm doing wrong or maybe how to make the regex simpler to work with git grep?