Commit 0b590ebc491ffe74d90bc4b32eb7932bd1fe009a

Authored by Will Farrington
1 parent 0ab88d8b30

Refactor git handling

Showing 1 changed file with 16 additions and 10 deletions Side-by-side Diff

... ... @@ -27,29 +27,35 @@
27 27 # to boxen support code or dependencies can be grabbed.
28 28  
29 29 unless ENV["BOXEN_NO_PULL"] || ARGV.include?("--no-pull")
30   - if system("which git > /dev/null") && File.directory?(".git")
  30 + quietly = "> /dev/null 2>&1"
  31 +
  32 + if system("which git > /dev/null") && File.directory?(".git") \
  33 + && fetch = system("git fetch -q origin")
  34 +
31 35 clean = `git status --porcelain`.empty?
32 36 current_branch = `git symbolic-ref HEAD`.chomp
33 37 master = current_branch == "refs/heads/master"
34   - no_new_commits = system('git diff --exit-code --quiet origin/master master')
35 38  
36   - short_branch = current_branch.split('/').last
  39 + upstream_changes = `git rev-list --count master..origin/master`.chomp != '0'
  40 + fast_forwardable = `git rev-list --count origin/master..master`.chomp == '0'
37 41  
  42 + short_branch = current_branch.split('/')[2..-1].join('/')
  43 +
38 44 if !master
39 45 warn "Boxen on a non-master branch '#{short_branch}', won't auto-update!"
40   - elsif !no_new_commits
41   - warn "Boxen has unpushed commits on master, won't auto-update!"
  46 + elsif !fast_forwardable
  47 + warn "Boxen's master branch is out of sync, won't auto-update!"
42 48 elsif !clean
43 49 warn "Boxen has a dirty tree, won't auto-update!"
  50 + elsif !upstream_changes
  51 + warn "Boxen is up-to-date."
44 52 end
45 53  
46   - if clean && master && no_new_commits
47   - quietly = "> /dev/null 2>&1"
48   - fetch = "(git fetch origin #{quietly})"
  54 + if clean && master && fast_forwardable && upstream_changes
49 55 reset = "(git reset --hard origin/master #{quietly})"
50   - reclean = "(git clean -df #{quietly})"
  56 + reclean = "(git clean -qdf)"
51 57  
52   - unless system "#{fetch} && #{reset} && #{reclean}"
  58 + unless system "#{reset} && #{reclean}"
53 59 warn "Auto-update of Boxen FAILED, continuing."
54 60 end
55 61 end