Skip to content

Commit

Permalink
duet-install-hook displays a helpful message when failing
Browse files Browse the repository at this point in the history
  • Loading branch information
Kehrlann committed Sep 18, 2019
1 parent 84a6040 commit 79823a6
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
7 changes: 6 additions & 1 deletion git-duet-install-hook/main.go
Expand Up @@ -111,7 +111,12 @@ func main() {
if hook == preCommitHook && !strings.Contains(contents, preCommitHook) ||
hook == prepareCommitMsgHook && !strings.Contains(contents, prepareCommitMsgHook) ||
hook == postCommitHook && !strings.Contains(contents, postCommitHook) {
fmt.Printf("can't install hook: file %s already exists\n", hookPath)
fmt.Printf(`It seems you already have a "%s" hook.
To enable the git-duet hook, please append:
%s
to your %s file.`, hookFileName, hook, hookPath)
os.Exit(1)
}
os.Exit(0) // hook file with the desired content already exists
Expand Down
36 changes: 36 additions & 0 deletions test/git-duet-install-hooks.bats
Expand Up @@ -25,15 +25,51 @@ load test_helper
[ -x .git/hooks/pre-commit ]
}

@test "does not overwrite existing pre-commit hook file with other contents" {
echo "Some content" > .git/hooks/pre-commit
local EXPECTED_HELP_MESSAGE=$(cat << EOM
It seems you already have a "pre-commit" hook.
To enable the git-duet hook, please append:
exec git duet-pre-commit "\$@"
to your $PWD/.git/hooks/pre-commit file.
EOM
)
run git duet-install-hook -q pre-commit
assert_output "$EXPECTED_HELP_MESSAGE"
assert_failure
}

@test "does not overwrite existing prepare-commit-msg hook file with other contents" {
echo "Some content" > .git/hooks/prepare-commit-msg
local EXPECTED_HELP_MESSAGE=$(cat << EOM
It seems you already have a "prepare-commit-msg" hook.
To enable the git-duet hook, please append:
exec git duet-prepare-commit-msg "\$@"
to your $PWD/.git/hooks/prepare-commit-msg file.
EOM
)
run git duet-install-hook -q prepare-commit-msg
assert_output "$EXPECTED_HELP_MESSAGE"
assert_failure
}

@test "does not overwrite existing post-commit hook file with other contents" {
echo "Some content" > .git/hooks/post-commit
local EXPECTED_HELP_MESSAGE=$(cat << EOM
It seems you already have a "post-commit" hook.
To enable the git-duet hook, please append:
exec git duet-post-commit "\$@"
to your $PWD/.git/hooks/post-commit file.
EOM
)
run git duet-install-hook -q post-commit
assert_output "$EXPECTED_HELP_MESSAGE"
assert_failure
}

Expand Down

0 comments on commit 79823a6

Please sign in to comment.