Created
September 11, 2013 04:20
-
-
Save JonathonMA/6519312 to your computer and use it in GitHub Desktop.
Git post-checkout hook to enable sensible foreman restart upon branch change with bonus bundle check
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
#!/usr/bin/env ruby | |
# post-checkout - git post checkout hook script | |
# | |
# This script will update the contents of .git/CURRENT_BRANCH with the name of | |
# the current branch after a checkout. | |
# | |
# Use it with rerun to restart foreman on branch changes. | |
# Bonus feature: sanity checks your Gemfile using $ bundle check | |
# | |
# Start foreman like this to watch CURRENT_BRANCH: | |
# $ rerun -d .git -p 'CURRENT_BRANCH' foreman start | |
# | |
# You'll probably need these gems: | |
# $ gem install rerun rb-fsevent | |
BRANCH_FILENAME = "CURRENT_BRANCH" | |
def should_update_branchfile? previous, new | |
return true if new == "HEAD" # random commit somewhere, better restart | |
return previous != new | |
end | |
def current_branch | |
@current_branch ||= `git rev-parse --abbrev-ref HEAD`.strip | |
end | |
def branchfile | |
@branchfile ||= | |
begin | |
src_dir = Dir.pwd | |
git_dir = ENV.fetch 'GIT_DIR' | |
File.join src_dir, git_dir, BRANCH_FILENAME | |
end | |
end | |
def previous_branch | |
@previous_branch ||= | |
begin | |
IO.read(branchfile).strip | |
rescue | |
"" | |
end | |
end | |
def update_branchfile new | |
File.open(branchfile, "w") do |f| | |
f.puts new | |
end | |
end | |
if should_update_branchfile? previous_branch, current_branch | |
puts "Branch change detected, trying to restart foreman and sanity checking deps" | |
update_branchfile current_branch | |
system 'bundle check' | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment