From 0b590ebc491ffe74d90bc4b32eb7932bd1fe009a Mon Sep 17 00:00:00 2001 From: Will Farrington Date: Thu, 28 Feb 2013 14:24:10 +1100 Subject: [PATCH] Refactor git handling --- script/boxen | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/script/boxen b/script/boxen index 22565e9..c37b49b 100755 --- a/script/boxen +++ b/script/boxen @@ -27,29 +27,35 @@ Dir.chdir Pathname.new(__FILE__).realpath + "../.." # to boxen support code or dependencies can be grabbed. 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? current_branch = `git symbolic-ref HEAD`.chomp 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 warn "Boxen on a non-master branch '#{short_branch}', won't auto-update!" - elsif !no_new_commits - warn "Boxen has unpushed commits on master, won't auto-update!" + elsif !fast_forwardable + warn "Boxen's master branch is out of sync, won't auto-update!" elsif !clean warn "Boxen has a dirty tree, won't auto-update!" + elsif !upstream_changes + warn "Boxen is up-to-date." end - if clean && master && no_new_commits - quietly = "> /dev/null 2>&1" - fetch = "(git fetch origin #{quietly})" + if clean && master && fast_forwardable && upstream_changes 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." end end -- 1.8.5.3