Skip to content

Instantly share code, notes, and snippets.

@wisq
Created October 27, 2016 01:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wisq/f117bef9eb6df6ea64bb13b9eee12903 to your computer and use it in GitHub Desktop.
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
#!/bin/sh
exec bundle exec rake pre_commit
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