Refactor git handling

This commit is contained in:
Will Farrington 2013-02-28 14:24:10 +11:00
parent 0ab88d8b30
commit 0b590ebc49

View file

@ -27,29 +27,35 @@ Dir.chdir Pathname.new(__FILE__).realpath + "../.."
# to boxen support code or dependencies can be grabbed. # to boxen support code or dependencies can be grabbed.
unless ENV["BOXEN_NO_PULL"] || ARGV.include?("--no-pull") unless ENV["BOXEN_NO_PULL"] || ARGV.include?("--no-pull")
if system("which git > /dev/null") && File.directory?(".git") quietly = "> /dev/null 2>&1"
if system("which git > /dev/null") && File.directory?(".git") \
&& fetch = system("git fetch -q origin")
clean = `git status --porcelain`.empty? clean = `git status --porcelain`.empty?
current_branch = `git symbolic-ref HEAD`.chomp current_branch = `git symbolic-ref HEAD`.chomp
master = current_branch == "refs/heads/master" master = current_branch == "refs/heads/master"
no_new_commits = system('git diff --exit-code --quiet origin/master master')
short_branch = current_branch.split('/').last upstream_changes = `git rev-list --count master..origin/master`.chomp != '0'
fast_forwardable = `git rev-list --count origin/master..master`.chomp == '0'
short_branch = current_branch.split('/')[2..-1].join('/')
if !master if !master
warn "Boxen on a non-master branch '#{short_branch}', won't auto-update!" warn "Boxen on a non-master branch '#{short_branch}', won't auto-update!"
elsif !no_new_commits elsif !fast_forwardable
warn "Boxen has unpushed commits on master, won't auto-update!" warn "Boxen's master branch is out of sync, won't auto-update!"
elsif !clean elsif !clean
warn "Boxen has a dirty tree, won't auto-update!" warn "Boxen has a dirty tree, won't auto-update!"
elsif !upstream_changes
warn "Boxen is up-to-date."
end end
if clean && master && no_new_commits if clean && master && fast_forwardable && upstream_changes
quietly = "> /dev/null 2>&1"
fetch = "(git fetch origin #{quietly})"
reset = "(git reset --hard origin/master #{quietly})" reset = "(git reset --hard origin/master #{quietly})"
reclean = "(git clean -df #{quietly})" reclean = "(git clean -qdf)"
unless system "#{fetch} && #{reset} && #{reclean}" unless system "#{reset} && #{reclean}"
warn "Auto-update of Boxen FAILED, continuing." warn "Auto-update of Boxen FAILED, continuing."
end end
end end