This repository has been archived by the owner on Jun 8, 2019. It is now read-only.
/
git.rb
84 lines (71 loc) · 2.76 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
75
76
77
78
79
80
81
82
83
84
require 'cocaine'
module Kumade
class Git
def heroku_remote?
remote_url = `git config --get remote.#{Kumade.configuration.environment}.url`.strip
!! remote_url.strip.match(/^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)
return unless remote_exists?(remote)
command = ["git push"]
command << "-f" if force
command << remote
command << branch
command = command.join(" ")
command_line = CommandLine.new(command)
command_line.run_or_error("Failed to push #{branch} -> #{remote}")
Kumade.configuration.outputter.success("Pushed #{branch} -> #{remote}")
end
def create(branch)
unless has_branch?(branch)
CommandLine.new("git branch #{branch} >/dev/null").run_or_error("Failed to create #{branch}")
end
end
def delete(branch_to_delete, branch_to_checkout)
if has_branch?(branch_to_delete)
command_line = CommandLine.new("git checkout #{branch_to_checkout} 2>/dev/null && git branch -D #{branch_to_delete}")
command_line.run_or_error("Failed to clean up #{branch_to_delete} branch")
end
end
def add_and_commit_all_assets_in(dir)
command = ["git checkout -b #{Kumade::Heroku::DEPLOY_BRANCH} 2>/dev/null",
"git add -f #{dir}",
"git commit -m 'Compiled assets.'"].join(' && ')
command_line = CommandLine.new(command)
command_line.run_or_error("Cannot deploy: couldn't commit assets")
Kumade.configuration.outputter.success("Added and committed all assets")
end
def current_branch
`git symbolic-ref HEAD`.sub("refs/heads/", "").strip
end
def remote_exists?(remote_name)
if Kumade.configuration.pretending?
true
else
`git remote` =~ /^#{remote_name}$/
end
end
def dirty?
! CommandLine.new("git diff --exit-code").run
end
def ensure_clean_git
if ! Kumade.configuration.pretending? && dirty?
Kumade.configuration.outputter.error("Cannot deploy: repo is not clean.")
else
Kumade.configuration.outputter.success("Git repo is clean")
end
end
def has_untracked_files_in?(directory)
relative_dir = directory.sub(Dir.pwd + '/', '')
untracked_output = CommandLine.new("git status --porcelain --untracked-files").run
untracked_output.split("\n").grep(/^\?{2} #{relative_dir}/).size > 0
end
private
def has_branch?(branch)
CommandLine.new("git show-ref #{branch}").run
end
end
end