Skip to content

Commit 2333a55

Browse files
authored
Merge pull request #67 from t13a/feature/fix-scan-files
Feature/fix scan files
2 parents 1b5125d + edeef54 commit 2333a55

2 files changed

Lines changed: 15 additions & 11 deletions

File tree

git-secrets

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,15 @@ load_combined_patterns() {
8181

8282
# Scans files or a repo using patterns.
8383
scan() {
84-
local files="$1" options=""
84+
local files=("${@}") options=""
8585
[ "${SCAN_CACHED}" == 1 ] && options+="--cached"
8686
[ "${SCAN_UNTRACKED}" == 1 ] && options+=" --untracked"
8787
[ "${SCAN_NO_INDEX}" == 1 ] && options+=" --no-index"
8888
# Scan using git-grep if there are no files or if git options are applied.
89-
if [ -z "${files}" ] || [ ! -z "${options}" ]; then
90-
output=$(git_grep $options $files)
89+
if [ ${#files[@]} -eq 0 ] || [ ! -z "${options}" ]; then
90+
output=$(git_grep $options "${files[@]}")
9191
else
92-
output=$(regular_grep $files)
92+
output=$(regular_grep "${files[@]}")
9393
fi
9494
process_output $? "${output}"
9595
}
@@ -110,18 +110,19 @@ scan_history() {
110110
# Note: this function returns 1 on success, 0 on error.
111111
git_grep() {
112112
local options="$1"; shift
113-
local files=$@ combined_patterns=$(load_combined_patterns)
113+
local files=("${@}") combined_patterns=$(load_combined_patterns)
114+
114115
[ -z "${combined_patterns}" ] && return 1
115-
GREP_OPTIONS= LC_ALL=C git grep -nwHEI ${options} "${combined_patterns}" -- $files
116+
GREP_OPTIONS= LC_ALL=C git grep -nwHEI ${options} "${combined_patterns}" -- "${files[@]}"
116117
}
117118

118119
# Performs a regular grep, taking into account patterns and recursion.
119120
# Note: this function returns 1 on success, 0 on error.
120121
regular_grep() {
121-
local files=$@ patterns=$(load_patterns) action='skip'
122+
local files=("${@}") patterns=$(load_patterns) action='skip'
122123
[ -z "${patterns}" ] && return 1
123124
[ ${RECURSIVE} -eq 1 ] && action="recurse"
124-
GREP_OPTIONS= LC_ALL=C grep -d "${action}" -nwHEI "${patterns}" $files
125+
GREP_OPTIONS= LC_ALL=C grep -d "${action}" -nwHEI "${patterns}" "${files[@]}"
125126
}
126127

127128
# Process the given status ($1) and output variables ($2).
@@ -168,11 +169,14 @@ commit_msg_hook() {
168169
# Scans all files that are about to be committed.
169170
pre_commit_hook() {
170171
SCAN_CACHED=1
171-
local file found_match=0 rev="4b825dc642cb6eb9a060e54bf8d69288fbee4904"
172+
local files=() file found_match=0 rev="4b825dc642cb6eb9a060e54bf8d69288fbee4904"
172173
# Diff against HEAD if this is not the first commit in the repo.
173174
git rev-parse --verify HEAD >/dev/null 2>&1 && rev="HEAD"
174175
# Filter out deleted files using --diff-filter
175-
IFS=$'\n' scan_with_fn_or_die "scan" "$(git diff-index --diff-filter 'ACMU' --name-only --cached $rev --)"
176+
while IFS= read -r file; do
177+
[ -n "$file" ] && files+=("$file")
178+
done <<< "$(git diff-index --diff-filter 'ACMU' --name-only --cached $rev --)"
179+
scan_with_fn_or_die "scan" "${files[@]}"
176180
}
177181

178182
# Determines if merging in a commit will introduce tainted history.

test/pre-commit.bats

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ load test_helper
4141
[ $status -eq 0 ]
4242
# Ensure deleted files are filtered out of the grep
4343
rm $TEST_REPO/data.txt
44-
echo 'aaa' $TEST_REPO/data_2.txt
44+
echo 'aaa' > $TEST_REPO/data_2.txt
4545
run git add -A
4646
run git commit -m 'This is also fine'
4747
[ $status -eq 0 ]

0 commit comments

Comments
 (0)