Commit 9c3b1691563b10fc4fdf788202da0c1e943830ee
1 parent
549f113930
It's cleaner to have another case: "not on any branch".
Showing 1 changed file with 6 additions and 7 deletions Inline Diff
script/boxen
#!/usr/bin/ruby | 1 | 1 | #!/usr/bin/ruby | |
# Run Boxen. | 2 | 2 | # Run Boxen. | |
3 | 3 | |||
require "pathname" | 4 | 4 | require "pathname" | |
5 | 5 | |||
if ENV["USER"] == "root" | 6 | 6 | if ENV["USER"] == "root" | |
abort "Run this as a normal user, I'll sudo if I need to." | 7 | 7 | abort "Run this as a normal user, I'll sudo if I need to." | |
end | 8 | 8 | end | |
9 | 9 | |||
# Make sure only one boxen process runs at a time. | 10 | 10 | # Make sure only one boxen process runs at a time. | |
11 | 11 | |||
myself = File.new __FILE__ | 12 | 12 | myself = File.new __FILE__ | |
13 | 13 | |||
unless myself.flock File::LOCK_EX | File::LOCK_NB | 14 | 14 | unless myself.flock File::LOCK_EX | File::LOCK_NB | |
abort "You're already running a boxen process! Know a patience." | 15 | 15 | abort "You're already running a boxen process! Know a patience." | |
end | 16 | 16 | end | |
17 | 17 | |||
# Yeah yeah, I like to be explicit. | 18 | 18 | # Yeah yeah, I like to be explicit. | |
19 | 19 | |||
at_exit { myself.flock File::LOCK_UN } | 20 | 20 | at_exit { myself.flock File::LOCK_UN } | |
21 | 21 | |||
# Put us where we belong, in the root dir of our boxen repo. | 22 | 22 | # Put us where we belong, in the root dir of our boxen repo. | |
23 | 23 | |||
Dir.chdir Pathname.new(__FILE__).realpath + "../.." | 24 | 24 | Dir.chdir Pathname.new(__FILE__).realpath + "../.." | |
25 | 25 | |||
# Auto-update code. This is done as early as possible so that changes | 26 | 26 | # Auto-update code. This is done as early as possible so that changes | |
# to boxen support code or dependencies can be grabbed. | 27 | 27 | # to boxen support code or dependencies can be grabbed. | |
28 | 28 | |||
unless ENV["BOXEN_NO_PULL"] || ARGV.include?("--no-pull") | 29 | 29 | unless ENV["BOXEN_NO_PULL"] || ARGV.include?("--no-pull") | |
quietly = "> /dev/null 2>&1" | 30 | 30 | quietly = "> /dev/null 2>&1" | |
31 | 31 | |||
if system("which git > /dev/null") && File.directory?(".git") \ | 32 | 32 | if system("which git > /dev/null") && File.directory?(".git") \ | |
&& fetch = system("git fetch -q origin") | 33 | 33 | && fetch = system("git fetch -q origin") | |
34 | 34 | |||
clean = `git status --porcelain`.empty? | 35 | 35 | clean = `git status --porcelain`.empty? | |
current_branch = `git symbolic-ref HEAD`.chomp | 36 | 36 | current_branch = `git symbolic-ref HEAD`.chomp | |
master = current_branch == "refs/heads/master" | 37 | 37 | master = current_branch == "refs/heads/master" | |
38 | 38 | |||
upstream_changes = `git rev-list --count master..origin/master`.chomp != '0' | 39 | 39 | upstream_changes = `git rev-list --count master..origin/master`.chomp != '0' | |
fast_forwardable = `git rev-list --count origin/master..master`.chomp == '0' | 40 | 40 | fast_forwardable = `git rev-list --count origin/master..master`.chomp == '0' | |
41 | 41 | |||
if !master | 42 | 42 | if current_branch.empty? | |
short_branch = if current_branch.empty? | 43 | 43 | ref = `git log -1 --pretty=format:%h` | |
`git log -1 --pretty=format:%h` | 44 | 44 | warn "Boxen not currently on any branch (ref: #{ref}), won't auto-update!" | |
else | 45 | 45 | elsif !master | |
current_branch.split('/')[2..-1].join('/') | 46 | 46 | local_branch = current_branch.split('/')[2..-1].join('/') | |
end | 47 | 47 | warn "Boxen on a non-master branch '#{local_branch}', won't auto-update!" | |
warn "Boxen on a non-master branch '#{short_branch}', won't auto-update!" | 48 | |||
elsif !fast_forwardable | 49 | 48 | elsif !fast_forwardable | |
warn "Boxen's master branch is out of sync, won't auto-update!" | 50 | 49 | warn "Boxen's master branch is out of sync, won't auto-update!" | |
elsif !clean | 51 | 50 | elsif !clean | |
warn "Boxen has a dirty tree, won't auto-update!" | 52 | 51 | warn "Boxen has a dirty tree, won't auto-update!" | |
elsif !upstream_changes | 53 | 52 | elsif !upstream_changes | |
warn "Boxen is up-to-date." | 54 | 53 | warn "Boxen is up-to-date." | |
end | 55 | 54 | end | |
56 | 55 | |||
if clean && master && fast_forwardable && upstream_changes | 57 | 56 | if clean && master && fast_forwardable && upstream_changes | |
reset = "(git reset --hard origin/master #{quietly})" | 58 | 57 | reset = "(git reset --hard origin/master #{quietly})" | |
reclean = "(git clean -qdf)" | 59 | 58 | reclean = "(git clean -qdf)" | |
60 | 59 | |||
unless system "#{reset} && #{reclean}" | 61 | 60 | unless system "#{reset} && #{reclean}" | |
warn "Auto-update of Boxen FAILED, continuing." | 62 | 61 | warn "Auto-update of Boxen FAILED, continuing." | |
end | 63 | 62 | end |