mirror of
https://github.com/sasjs/core.git
synced 2026-01-07 01:20:05 +00:00
chore: adding a git hook to prevent sas files from appearing with capital letters. To install, just run
> @sasjs/core@1.0.0 postinstall > node-git-hooks Installing Git hooks... up to date, audited 2 packages in 1s found 0 vulnerabilities.
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
#!/bin/sh
|
#!/bin/bash
|
||||||
#
|
#
|
||||||
# A hook script to verify that no filenames with capital letters are committed.
|
# A hook script to verify that no filenames with capital letters are committed.
|
||||||
# Called by "git commit" with no arguments. The hook should
|
# Called by "git commit" with no arguments. The hook should
|
||||||
@@ -6,69 +6,11 @@
|
|||||||
# it wants to stop the commit.
|
# it wants to stop the commit.
|
||||||
#
|
#
|
||||||
# Go through all the changed files (except for deleted and unmerged)
|
# Go through all the changed files (except for deleted and unmerged)
|
||||||
#
|
|
||||||
# Check for lines containing "debugger" "console." or "alert("
|
|
||||||
|
|
||||||
# Test only JS files
|
# Save exit code of last executed action
|
||||||
js_pattern="\.js"
|
|
||||||
# Check for changed lines
|
|
||||||
added_lines="^\+"
|
|
||||||
# Check for lines with comments
|
|
||||||
comment="(//|/\*).*"
|
|
||||||
beginning_of_line="^\+[\s]*"
|
|
||||||
# Check for lines containing "debugger" "console." or "alert(", skip comments
|
|
||||||
evil_pattern="(console\.|alert\(|debugger).*"
|
|
||||||
# Set exit code to 0, will be set to 1 on error.
|
|
||||||
exit_code=0
|
exit_code=0
|
||||||
# Grep git diff of files to commit
|
|
||||||
js_files=$(
|
|
||||||
git diff --cached --find-copies --find-renames --name-only --diff-filter=ACMRTXBU |
|
|
||||||
grep -E "$js_pattern"
|
|
||||||
)
|
|
||||||
|
|
||||||
|
# Check if file is one of SAS|DDL|CSV|SH and check for uppercase letters
|
||||||
if [[ -n $js_files ]];
|
|
||||||
then
|
|
||||||
for file in $js_files; do
|
|
||||||
# Get only changed lines AND
|
|
||||||
# Only lines starting with '+' AND
|
|
||||||
# NOT lines with comments AND
|
|
||||||
# Test for console, debug etc.
|
|
||||||
lines_with_no_comment=$(
|
|
||||||
git diff --cached $file |
|
|
||||||
grep -E "$added_lines" |
|
|
||||||
grep -Ev "$comment" |
|
|
||||||
grep -E "$evil_pattern"
|
|
||||||
)
|
|
||||||
# Get only changed lines AND
|
|
||||||
# Only lines starting with '+' AND
|
|
||||||
# NOT lines starting with a comment AND
|
|
||||||
# Test for lines with console, debug etc. before a comment
|
|
||||||
lines_with_comment=$(
|
|
||||||
git diff --cached $file |
|
|
||||||
grep -E "$added_lines" |
|
|
||||||
grep -Ev "$beginning_of_line$comment" |
|
|
||||||
grep -E "${evil_pattern}$comment"
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
if [[ -n $lines_with_no_comment || -n $lines_with_comment ]];
|
|
||||||
then
|
|
||||||
echo
|
|
||||||
echo -e "Found illegal commands in \033[1m$file:\033[0m"
|
|
||||||
echo -e '\E[0;32m'"$lines_with_no_comment"
|
|
||||||
echo -e "$lines_with_comment"'\033[0m'
|
|
||||||
echo
|
|
||||||
# Abort commit
|
|
||||||
exit_code=1
|
|
||||||
fi
|
|
||||||
|
|
||||||
done
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Check if file is an image JPG|GIF|PNG|JPEG and check for uppercase letters
|
|
||||||
|
|
||||||
# Check for image file types
|
|
||||||
mime_pattern="\.(sas|ddl|csv|sh)"
|
mime_pattern="\.(sas|ddl|csv|sh)"
|
||||||
# Check for capital letters only in file names
|
# Check for capital letters only in file names
|
||||||
extra_pattern="(^|/)[^/]*([A-Z]+)[^/]*\.[A-Za-z]{3}$"
|
extra_pattern="(^|/)[^/]*([A-Z]+)[^/]*\.[A-Za-z]{3}$"
|
||||||
@@ -76,8 +18,8 @@ extra_pattern="(^|/)[^/]*([A-Z]+)[^/]*\.[A-Za-z]{3}$"
|
|||||||
files=$( git diff --cached --find-copies --find-renames --name-only --diff-filter=ACMRTXBU |
|
files=$( git diff --cached --find-copies --find-renames --name-only --diff-filter=ACMRTXBU |
|
||||||
grep -Ei "$mime_pattern" |
|
grep -Ei "$mime_pattern" |
|
||||||
grep -E "$extra_pattern" )
|
grep -E "$extra_pattern" )
|
||||||
|
echo "$files"
|
||||||
if [[ -n $files ]];
|
if [ "$files" !="" ];
|
||||||
then
|
then
|
||||||
echo
|
echo
|
||||||
echo "Found files that contain capital letters."
|
echo "Found files that contain capital letters."
|
||||||
@@ -90,7 +32,7 @@ then
|
|||||||
exit_code=1
|
exit_code=1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ $exit_code == 0 ]; then
|
if [ "$exit_code" == "0" ]; then
|
||||||
echo
|
echo
|
||||||
echo -e '\033[1m'"Pre-commit validation Passed"'\033[0m'
|
echo -e '\033[1m'"Pre-commit validation Passed"'\033[0m'
|
||||||
echo
|
echo
|
||||||
|
|||||||
Reference in New Issue
Block a user