Skip to content

Instantly share code, notes, and snippets.

@JonathonMA
Created September 11, 2013 04:20
Show Gist options
  • Save JonathonMA/6519312 to your computer and use it in GitHub Desktop.
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
#!/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