Created
October 27, 2016 01:03
-
-
Save wisq/f117bef9eb6df6ea64bb13b9eee12903 to your computer and use it in GitHub Desktop.
Basic Ruby+git test-before-commit workflow, testing only the changes being committed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
exec bundle exec rake pre_commit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
task(default: [:link_hooks, :test]) | |
task :link_hooks do | |
sh 'ln', '-nsf', '../../hooks/pre-commit', '.git/hooks/pre-commit' | |
end | |
task :pre_commit do | |
require 'open3' | |
Dir.mktmpdir do |tmpdir| | |
tree = "#{tmpdir}/tree" | |
sh 'git', 'clone', Dir.getwd, tree | |
diff, status = Open3.capture2(*%w(git diff --cached)) | |
raise "git diff failed: #{status}" unless status.success? | |
Dir.chdir(tree) do | |
unless diff.empty? | |
output, status = Open3.capture2(*%w(git apply), stdin_data: diff) | |
raise "git apply failed: #{status}" unless status.success? | |
end | |
sh *%w(bundle exec rake test) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment