Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't assume HEAD is a symref in prepare-commit-msg hook. #184

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions git-secrets
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,7 @@ prepare_commit_msg_hook() {
merge,)
local git_head=$(env | grep GITHEAD) # e.g. GITHEAD_<sha>=release/1.43
local sha="${git_head##*=}" # Get just the SHA
local branch=$(git symbolic-ref HEAD) # e.g. refs/heads/master
local dest="${branch#refs/heads/}" # cut out "refs/heads"
git log "${dest}".."${sha}" -p | scan_with_fn_or_die "scan" -
git log HEAD.."${sha}" -p | scan_with_fn_or_die "scan" -
;;
esac
}
Expand Down
31 changes: 31 additions & 0 deletions test/prepare-commit-msg.bats
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,34 @@ load test_helper
run git merge --no-ff feature
[ $status -eq 0 ]
}

@test "Works with detached HEAD" {
setup_good_repo
repo_run git-secrets --install
cd $TEST_REPO
git commit -m 'OK'
git checkout -b branchA
echo "A" > a.txt
git add -A
git commit -m "Commit to branchA"
git checkout -f $(git rev-parse master)
run git merge --no-ff branchA
[ -z "$( echo $output | grep fatal )" ]
}

@test "Rejects bad merge with detached HEAD" {
setup_good_repo
repo_run git-secrets --install $TEST_REPO
cd $TEST_REPO
git commit -m 'OK'
git checkout -b feature
echo '@todo' > data.txt
git add -A
git commit -m 'Bad commit' --no-verify
echo 'Fixing!' > data.txt
git add -A
git commit -m 'Fixing commit'
git checkout -f $(git rev-parse master)
run git merge --no-ff feature
[ $status -eq 1 ]
}