Skip to content

Commit

Permalink
multifile grep: perform greps in series
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
nasamuffin committed Mar 16, 2020
1 parent 5e28df3 commit 8c85efd
Showing 1 changed file with 34 additions and 2 deletions.
36 changes: 34 additions & 2 deletions git-secrets
Original file line number Diff line number Diff line change
Expand Up @@ -111,18 +111,50 @@ scan_history() {
git_grep() {
local options="$1"; shift
local files=("${@}") combined_patterns=$(load_combined_patterns)
local status=0

[ -z "${combined_patterns}" ] && return 1
GREP_OPTIONS= LC_ALL=C git grep -nwHEI ${options} "${combined_patterns}" -- "${files[@]}"
if [ ${#files[@]} -eq 0 ]; then
GREP_OPTIONS= LC_ALL=C git grep -nwHEI ${options} "${combined_patterns}" --
status=$?
else
for file in "${files[@]}"
do
GREP_OPTIONS= LC_ALL=C git grep -nwHEI ${options} "${combined_patterns}" -- "${file}"
(( status += $? ))
done
fi

if [ $status -gt 0 ]; then
return 1
else
return 0
fi
}

# Performs a regular grep, taking into account patterns and recursion.
# Note: this function returns 1 on success, 0 on error.
regular_grep() {
local files=("${@}") patterns=$(load_patterns) action='skip'
local status=0
[ -z "${patterns}" ] && return 1
[ ${RECURSIVE} -eq 1 ] && action="recurse"
GREP_OPTIONS= LC_ALL=C grep -d "${action}" -nwHEI "${patterns}" "${files[@]}"
if [ ${#files[@]} -eq 0]; then
GREP_OPTIONS= LC_ALL=C grep -d "${action}" -nwHEI "${patterns}"
status=$?
else
for file in "${files[@]}"
do
GREP_OPTIONS= LC_ALL=C grep -d "${action}" -nwHEI "${patterns}" "${file}"
(( status += $? ))
done
fi

if [ $status -gt 0 ]; then
return 1
else
return 0
fi
}

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

0 comments on commit 8c85efd

Please sign in to comment.