This repository has been archived by the owner on Jun 8, 2019. It is now read-only.
/
git.rb
74 lines (63 loc) · 2.07 KB
/
git.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
require 'cocaine'
module Kumade
class Git < Base
attr_reader :environment
def initialize(pretending, environment)
super()
@pretending = pretending
@environment = environment
end
def heroku_remote?
`git config --get remote.#{environment}.url`.strip =~ /^git@heroku\..+:(.+)\.git$/
end
def self.environments
url_remotes = `git remote`.strip.split("\n").map{|remote| [remote, `git config --get remote.#{remote}.url`.strip] }.select{|remote| remote.last =~ /^git@heroku\.com:(.+)\.git$/}.map{|remote| remote.first}
end
def push(branch, remote = 'origin', force = false)
command = ["git push"]
command << "-f" if force
command << remote
command << branch
command = command.join(" ")
run_or_error(command, "Failed to push #{branch} -> #{remote}")
success("Pushed #{branch} -> #{remote}")
end
def create(branch)
unless branch_exist?(branch)
run_or_error("git branch #{branch}", "Failed to create #{branch}")
end
end
def delete(branch_to_delete, branch_to_checkout)
run_or_error("git checkout #{branch_to_checkout} && git branch -D #{branch_to_delete}",
"Failed to clean up #{branch_to_delete} branch")
end
def add_and_commit_all_in(dir, branch, commit_message, success_output, error_output)
run_or_error "git checkout -b #{branch} && git add -f #{dir} && git commit -m '#{commit_message}'",
"Cannot deploy: #{error_output}"
success success_output
end
def current_branch
`git symbolic-ref HEAD`.sub("refs/heads/", "").strip
end
def remote_exists?(remote_name)
if @pretending
true
else
`git remote` =~ /^#{remote_name}$/
end
end
def dirty?
!run("git diff --exit-code")
end
def ensure_clean_git
if ! @pretending && dirty?
error("Cannot deploy: repo is not clean.")
else
success("Git repo is clean")
end
end
def branch_exist?(branch)
run("git show-ref #{branch}")
end
end
end