forked from benhoskings/babushka
/
git_helpers.rb
40 lines (36 loc) · 1.25 KB
/
git_helpers.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
module Babushka
module GitHelpers
def git uri, opts = {}, &block
repo = GitRepo.new(opts[:to] || (BuildPrefix / File.basename(uri.to_s).chomp('.git')))
if git_update(uri, repo)
repo.root.touch # so we can tell when it was last updated
block.nil? || cd(repo.path, &block)
end
end
private
def git_update uri, repo
if !repo.exists?
update_and_log uri, repo, "Cloning #{uri} into #{repo.path}" do
repo.clone! uri
end
else
update_and_log uri, repo, "Updating #{repo.path} from #{uri}" do
repo.repo_shell('git fetch origin')
end
end
end
def update_and_log uri, repo, message, &block
log_block message do
if !block.call
# failed
elsif !repo.behind?
log " at #{repo.current_head.colorize('yellow')},", :newline => false
true
else
log " #{repo.current_head.colorize('yellow')}..#{repo.repo_shell("git rev-parse --short origin/#{repo.current_branch}").colorize('yellow')} (#{repo.repo_shell("git log -1 --pretty=format:%s origin/#{repo.current_branch}").chomp('.')}),", :newline => false
repo.reset_hard! "origin/#{repo.current_branch}"
end
end
end
end
end