diff --git a/bin/penchant b/bin/penchant index 96c2135..e9ae180 100755 --- a/bin/penchant +++ b/bin/penchant @@ -45,6 +45,8 @@ class PenchantCLI < Thor method_options :switch_back => false desc "gemfile ENV", "Switch the gemfile environment, or rebuild the current environment if not given" def gemfile(env = get_current_env) + check_git_hooks! + if env if options[:switch_back] puts "[penchant] Switching back, fallback: #{env}..." @@ -93,9 +95,17 @@ class PenchantCLI < Thor out << "deployment" if gemfile.deployment? out.join(' ') end + + def check_git_hooks! + if !Penchant::Hooks.installed? + puts "[penchant] git hooks not installed. Run script/install-git-hooks." + puts + end + end end default_task :gemfile end PenchantCLI.start + diff --git a/features/cli.feature b/features/cli.feature index 2204dc2..3ece4c7 100644 --- a/features/cli.feature +++ b/features/cli.feature @@ -27,4 +27,30 @@ Feature: CLI source :rubygems """ And the output should include "No git" - + + @wip + Scenario: Run in a project where the git hooks are not set up + Given I have the file "tmp/Gemfile.penchant" with the content: + """ + gem 'rake' + """ + Given I have the file "tmp/script/hooks/pre-commit" with the content: + """ + a penchant hook + """ + When I run "bin/penchant gemfile remote" in the "tmp" directory + Then the output should include "git hooks not installed" + + @wip + Scenario: Run in a project where git hooks are set up + Given I have the file "tmp/Gemfile.penchant" with the content: + """ + gem 'rake' + """ + Given I have the file "tmp/script/hooks/pre-commit" with the content: + """ + a penchant hook + """ + Given I have the symlink "tmp/.git/hooks/pre-commit" which points to "tmp/script/hooks/pre-commit" + When I run "bin/penchant gemfile remote" in the "tmp" directory + Then the output should not include "git hooks not installed" diff --git a/features/step_definitions/given/i_have_the_symlink_git_hooks_pre_commit_which_points_to_script_hooks_pre_commit.rb b/features/step_definitions/given/i_have_the_symlink_git_hooks_pre_commit_which_points_to_script_hooks_pre_commit.rb new file mode 100644 index 0000000..ce6daf7 --- /dev/null +++ b/features/step_definitions/given/i_have_the_symlink_git_hooks_pre_commit_which_points_to_script_hooks_pre_commit.rb @@ -0,0 +1,4 @@ +Given /^I have the symlink "(.*?)" which points to "(.*?)"$/ do |source, target| + FileUtils.mkdir_p(File.dirname(source)) + File.symlink(target, source) +end diff --git a/features/step_definitions/then/the_output_should_not_include_git_hooks_not_installed.rb b/features/step_definitions/then/the_output_should_not_include_git_hooks_not_installed.rb new file mode 100644 index 0000000..a08bfa7 --- /dev/null +++ b/features/step_definitions/then/the_output_should_not_include_git_hooks_not_installed.rb @@ -0,0 +1,3 @@ +Then /^the output should not include "(.*?)"$/ do |text| + @output.should_not include(text) +end diff --git a/lib/penchant.rb b/lib/penchant.rb index 54f57e9..89c71c4 100644 --- a/lib/penchant.rb +++ b/lib/penchant.rb @@ -2,4 +2,5 @@ module Penchant autoload :Gemfile, 'penchant/gemfile' autoload :Repo, 'penchant/repo' autoload :DotPenchant, 'penchant/dot_penchant' + autoload :Hooks, 'penchant/hooks' end diff --git a/lib/penchant/hooks.rb b/lib/penchant/hooks.rb new file mode 100644 index 0000000..f6a2607 --- /dev/null +++ b/lib/penchant/hooks.rb @@ -0,0 +1,21 @@ +require 'pathname' + +module Penchant + class Hooks + HOOKS_DIR = 'script/hooks' + GIT_HOOKS_DIR = '.git/hooks' + + def self.installed? + if File.directory?(HOOKS_DIR) + Dir[File.join(HOOKS_DIR, '*')].each do |file| + target = File.join(GIT_HOOKS_DIR, File.basename(file)) + return false if !File.symlink?(target) + return false if !File.expand_path(File.readlink(target)) == File.expand_path(file) + end + + true + end + end + end +end + diff --git a/script/hooks/commit-msg b/script/hooks/commit-msg index 1b1c32a..538118e 100755 --- a/script/hooks/commit-msg +++ b/script/hooks/commit-msg @@ -2,14 +2,13 @@ msg=$(cat $1) -OLD_GIT_DIR=$GIT_DIR +# wtf mac os x lion +if [ ! -z "$MY_RUBY_HOME" ]; then + PATH="$MY_RUBY_HOME/bin:$PATH" +fi if [[ "${msg}" != *"[ci skip]"* ]]; then - if [ "$(penchant gemfile-env)" != "remote" ]; then - penchant gemfile remote - fi - - bundle exec rake + bundle exec rake --trace R=$? if [ $R -ne 0 ]; then exit $R; fi fi diff --git a/script/hooks/post-commit b/script/hooks/post-commit index 490f85d..05a7907 100755 --- a/script/hooks/post-commit +++ b/script/hooks/post-commit @@ -1,4 +1,2 @@ #!/bin/bash -penchant gemfile remote --switch-back - diff --git a/script/hooks/pre-commit b/script/hooks/pre-commit index 8cfef3d..05a7907 100755 --- a/script/hooks/pre-commit +++ b/script/hooks/pre-commit @@ -1,4 +1,2 @@ #!/bin/bash -# this has been moved to commit-msg -