From 9fa6d45605dc05b1fba0d1287b5a711a627924d9 Mon Sep 17 00:00:00 2001 From: Michael Wittig Date: Tue, 3 May 2016 18:01:43 +0200 Subject: [PATCH] fixed problem with spaces in names during commit --- git-secrets | 6 +++--- test/pre-commit.bats | 9 +++++++++ test/test_helper.bash | 8 ++++++++ 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/git-secrets b/git-secrets index 4fe28cd..09644eb 100755 --- a/git-secrets +++ b/git-secrets @@ -106,7 +106,7 @@ git_grep() { local options="$1"; shift local files=$@ combined_patterns=$(load_combined_patterns) [ -z "${combined_patterns}" ] && return 1 - GREP_OPTIONS= LC_ALL=C git grep -nwHEI ${options} "${combined_patterns}" $@ + GREP_OPTIONS= LC_ALL=C git grep -nwHEI ${options} "${combined_patterns}" $files } # Performs a regular grep, taking into account patterns and recursion. @@ -115,7 +115,7 @@ regular_grep() { local files=$@ patterns=$(load_patterns) action='skip' [ -z "${patterns}" ] && return 1 [ "${RECURSIVE}" -eq 1 ] && action="recurse" - GREP_OPTIONS= LC_ALL=C grep -d "${action}" -nwHE "${patterns}" $@ + GREP_OPTIONS= LC_ALL=C grep -d "${action}" -nwHE "${patterns}" $files } # Process the given status ($1) and output variables ($2). @@ -166,7 +166,7 @@ pre_commit_hook() { # Diff against HEAD if this is not the first commit in the repo. git rev-parse --verify HEAD >/dev/null 2>&1 && rev="HEAD" # Filter out deleted files using --diff-filter - scan_with_fn_or_die "scan" "$(git diff-index --diff-filter 'ACMU' --name-only --cached $rev --)" + IFS=$'\n' scan_with_fn_or_die "scan" "$(git diff-index --diff-filter 'ACMU' --name-only --cached $rev --)" } # Determines if merging in a commit will introduce tainted history. diff --git a/test/pre-commit.bats b/test/pre-commit.bats index 779a26a..c3cbacb 100644 --- a/test/pre-commit.bats +++ b/test/pre-commit.bats @@ -12,6 +12,15 @@ load test_helper [ "${lines[2]}" == "failure2.txt:1:me" ] } +@test "Rejects commits with prohibited patterns in changeset with filename that contain spaces" { + setup_bad_repo_with_spaces + repo_run git-secrets --install $TEST_REPO + cd $TEST_REPO + run git commit -m 'Contents are bad not the message' + [ $status -eq 1 ] + [ "${lines[0]}" == "da ta.txt:1:@todo more stuff" ] +} + @test "Scans staged files" { cd $TEST_REPO repo_run git-secrets --install $TEST_REPO diff --git a/test/test_helper.bash b/test/test_helper.bash index 1d7c394..b43567a 100644 --- a/test/test_helper.bash +++ b/test/test_helper.bash @@ -57,6 +57,14 @@ setup_bad_repo() { cd - } +# Creates a repo that should fail +setup_bad_repo_with_spaces() { + cd $TEST_REPO + echo '@todo more stuff' > "$TEST_REPO/da ta.txt" + git add -A + cd - +} + # Creates a repo that should fail setup_bad_repo_history() { cd $TEST_REPO