Skip to content

Commit 8c85efd

Browse files
committed
multifile grep: perform greps in series
Passing a very long argument list to git-grep can cause it to fail; indeed, it's possible for the list of paths passed by git-secrets to either grep or git-grep to exceed the maximum number of arguments allowed in a user's environment (`getconf ARG_MAX`). Signed-off-by: Emily Shaffer <[email protected]>
1 parent 5e28df3 commit 8c85efd

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed

Diff for: git-secrets

+34-2
Original file line numberDiff line numberDiff line change
@@ -111,18 +111,50 @@ scan_history() {
111111
git_grep() {
112112
local options="$1"; shift
113113
local files=("${@}") combined_patterns=$(load_combined_patterns)
114+
local status=0
114115

115116
[ -z "${combined_patterns}" ] && return 1
116-
GREP_OPTIONS= LC_ALL=C git grep -nwHEI ${options} "${combined_patterns}" -- "${files[@]}"
117+
if [ ${#files[@]} -eq 0 ]; then
118+
GREP_OPTIONS= LC_ALL=C git grep -nwHEI ${options} "${combined_patterns}" --
119+
status=$?
120+
else
121+
for file in "${files[@]}"
122+
do
123+
GREP_OPTIONS= LC_ALL=C git grep -nwHEI ${options} "${combined_patterns}" -- "${file}"
124+
(( status += $? ))
125+
done
126+
fi
127+
128+
if [ $status -gt 0 ]; then
129+
return 1
130+
else
131+
return 0
132+
fi
117133
}
118134

119135
# Performs a regular grep, taking into account patterns and recursion.
120136
# Note: this function returns 1 on success, 0 on error.
121137
regular_grep() {
122138
local files=("${@}") patterns=$(load_patterns) action='skip'
139+
local status=0
123140
[ -z "${patterns}" ] && return 1
124141
[ ${RECURSIVE} -eq 1 ] && action="recurse"
125-
GREP_OPTIONS= LC_ALL=C grep -d "${action}" -nwHEI "${patterns}" "${files[@]}"
142+
if [ ${#files[@]} -eq 0]; then
143+
GREP_OPTIONS= LC_ALL=C grep -d "${action}" -nwHEI "${patterns}"
144+
status=$?
145+
else
146+
for file in "${files[@]}"
147+
do
148+
GREP_OPTIONS= LC_ALL=C grep -d "${action}" -nwHEI "${patterns}" "${file}"
149+
(( status += $? ))
150+
done
151+
fi
152+
153+
if [ $status -gt 0 ]; then
154+
return 1
155+
else
156+
return 0
157+
fi
126158
}
127159

128160
# Process the given status ($1) and output variables ($2).

0 commit comments

Comments
 (0)