Class: GitSafe::Git
Instance Attribute Summary collapse
Instance Method Summary
collapse
-
#add_and_commit(commit_msg) ⇒ Object
-
#add_remote(uri, name: 'origin') ⇒ Object
-
#branch_a ⇒ Object
-
#checkout(branch: nil, create: false, sha: nil) ⇒ Object
-
#clone(remote_uri, depth: nil) ⇒ Object
-
#clone_or_fetch_and_merge(remote_uri, branch: 'master', remote_name: 'origin', depth: nil, force_fresh_clone: false, git_config: {}) ⇒ Object
-
#config_get(name) ⇒ Object
-
#config_set(name_values = {}) ⇒ Object
-
#delete_work_tree ⇒ Object
-
#execute_git_cmd(git_cmd) ⇒ Object
-
#fetch ⇒ Object
-
#git_config ⇒ Object
-
#git_config_path ⇒ Object
-
#git_locale ⇒ Object
-
#has_git_config? ⇒ Boolean
(also: #work_tree_is_git_repo?)
-
#has_remote? ⇒ Boolean
-
#init ⇒ Object
-
#initialize(work_tree, options) ⇒ Git
constructor
-
#last_commit_sha ⇒ Object
-
#last_commit_timestamp ⇒ Object
-
#merge(to_merge_name) ⇒ Object
-
#pull(branch: 'master') ⇒ Object
-
#push(remote: 'origin', branch: 'master', force: false) ⇒ Object
-
#remotes ⇒ Object
-
#status ⇒ Object
-
#work_tree_flag ⇒ Object
#safe_unlink_private_key_tmp_file, #ssh_cmd, #ssh_private_key_file_path, #ssh_tempfile
Constructor Details
#initialize(work_tree, options) ⇒ Git
Returns a new instance of Git.
9
10
11
12
13
14
15
|
# File 'lib/git-safe/git.rb', line 9
def initialize(work_tree, options)
@work_tree = work_tree
@options = options
@ssh_private_key = options[:ssh_private_key]
@logger = options[:logger]
FileUtils.mkdir_p(work_tree)
end
|
Instance Attribute Details
#logger ⇒ Object
Returns the value of attribute logger.
7
8
9
|
# File 'lib/git-safe/git.rb', line 7
def logger
@logger
end
|
#options ⇒ Object
Returns the value of attribute options.
7
8
9
|
# File 'lib/git-safe/git.rb', line 7
def options
@options
end
|
#ssh_private_key ⇒ Object
Returns the value of attribute ssh_private_key.
7
8
9
|
# File 'lib/git-safe/git.rb', line 7
def ssh_private_key
@ssh_private_key
end
|
#work_tree ⇒ Object
Returns the value of attribute work_tree.
7
8
9
|
# File 'lib/git-safe/git.rb', line 7
def work_tree
@work_tree
end
|
Instance Method Details
#add_and_commit(commit_msg) ⇒ Object
35
36
37
38
|
# File 'lib/git-safe/git.rb', line 35
def add_and_commit(commit_msg)
execute_git_cmd("git #{git_locale} add .")
execute_git_cmd("git #{git_locale} commit -m '#{commit_msg}'")
end
|
#add_remote(uri, name: 'origin') ⇒ Object
103
104
105
|
# File 'lib/git-safe/git.rb', line 103
def add_remote(uri, name: 'origin')
execute_git_cmd("git #{git_locale} remote add #{name} #{uri}")
end
|
#branch_a ⇒ Object
48
49
50
|
# File 'lib/git-safe/git.rb', line 48
def branch_a
execute_git_cmd("git #{git_locale} branch -a")
end
|
#checkout(branch: nil, create: false, sha: nil) ⇒ Object
52
53
54
55
56
57
58
59
60
61
|
# File 'lib/git-safe/git.rb', line 52
def checkout(branch: nil, create: false, sha: nil)
co = "git #{git_locale} checkout"
create_flag = create ? ' -b' : ''
if branch
co = "#{co}#{create_flag} #{branch}"
elsif sha
co = "#{co} #{sha}"
end
execute_git_cmd(co)
end
|
#clone(remote_uri, depth: nil) ⇒ Object
84
85
86
87
88
89
90
91
92
93
|
# File 'lib/git-safe/git.rb', line 84
def clone(remote_uri, depth: nil)
if options[:clone_command]
execute_git_cmd(options[:clone_command].gsub(options[:clone_command_repo_dir_replace_text], work_tree))
else
depth_cmd = depth ? " --depth=#{depth}" : ''
execute_git_cmd("#{ssh_cmd}git clone #{remote_uri}#{depth_cmd} #{work_tree}")
end
ensure
safe_unlink_private_key_tmp_file
end
|
#clone_or_fetch_and_merge(remote_uri, branch: 'master', remote_name: 'origin', depth: nil, force_fresh_clone: false, git_config: {}) ⇒ Object
122
123
124
125
126
127
128
129
130
131
132
133
|
# File 'lib/git-safe/git.rb', line 122
def clone_or_fetch_and_merge(remote_uri, branch: 'master', remote_name: 'origin', depth: nil, force_fresh_clone: false, git_config: {})
delete_work_tree if force_fresh_clone
unless has_remote?
clone(remote_uri, depth: depth)
end
config_set(git_config)
fetch
checkout(branch: branch)
merge("#{remote_name}/#{branch}")
end
|
#config_get(name) ⇒ Object
31
32
33
|
# File 'lib/git-safe/git.rb', line 31
def config_get(name)
execute_git_cmd("git #{git_locale} config #{name}")
end
|
#config_set(name_values = {}) ⇒ Object
25
26
27
28
29
|
# File 'lib/git-safe/git.rb', line 25
def config_set(name_values = {})
name_values.keys.each do |key|
execute_git_cmd("git #{git_locale} config #{key} #{name_values[key]}") if name_values[key]
end
end
|
#delete_work_tree ⇒ Object
147
148
149
|
# File 'lib/git-safe/git.rb', line 147
def delete_work_tree
FileUtils.rm_rf(work_tree) if Dir.exists?(work_tree)
end
|
#execute_git_cmd(git_cmd) ⇒ Object
153
154
155
156
157
158
|
# File 'lib/git-safe/git.rb', line 153
def execute_git_cmd(git_cmd)
stdout_str, stderr_str, status = Open3.capture3(git_cmd)
raise CommandError.new("error executing '#{git_cmd}', status: #{status.exitstatus}, std_error: #{stderr_str}") unless status.exitstatus == 0
[stdout_str, stderr_str].reject { |out| out.nil? || out.strip == '' }.join(',')
end
|
#fetch ⇒ Object
72
73
74
75
76
|
# File 'lib/git-safe/git.rb', line 72
def fetch
execute_git_cmd("#{ssh_cmd}git #{git_locale} fetch")
ensure
safe_unlink_private_key_tmp_file
end
|
#git_config ⇒ Object
135
136
137
|
# File 'lib/git-safe/git.rb', line 135
def git_config
File.read(git_config_path) if has_git_config?
end
|
#git_config_path ⇒ Object
143
144
145
|
# File 'lib/git-safe/git.rb', line 143
def git_config_path
"#{work_tree}/.git/config"
end
|
#git_locale ⇒ Object
95
96
97
|
# File 'lib/git-safe/git.rb', line 95
def git_locale
"#{work_tree_flag} --git-dir=#{work_tree}/.git"
end
|
#has_git_config? ⇒ Boolean
Also known as:
work_tree_is_git_repo?
139
140
141
|
# File 'lib/git-safe/git.rb', line 139
def has_git_config?
File.exists?(git_config_path)
end
|
#has_remote? ⇒ Boolean
107
108
109
|
# File 'lib/git-safe/git.rb', line 107
def has_remote?
git_config&.match(/\[remote/)
end
|
#init ⇒ Object
17
18
19
|
# File 'lib/git-safe/git.rb', line 17
def init
execute_git_cmd("git #{git_locale} init")
end
|
#last_commit_sha ⇒ Object
40
41
42
|
# File 'lib/git-safe/git.rb', line 40
def last_commit_sha
execute_git_cmd("git #{git_locale} rev-parse HEAD")
end
|
#last_commit_timestamp ⇒ Object
44
45
46
|
# File 'lib/git-safe/git.rb', line 44
def last_commit_timestamp
execute_git_cmd("git #{git_locale} log -1 --format=%cd ")
end
|
#merge(to_merge_name) ⇒ Object
63
64
65
|
# File 'lib/git-safe/git.rb', line 63
def merge(to_merge_name)
execute_git_cmd("git #{git_locale} merge #{to_merge_name}")
end
|
#pull(branch: 'master') ⇒ Object
78
79
80
81
82
|
# File 'lib/git-safe/git.rb', line 78
def pull(branch: 'master')
execute_git_cmd("#{ssh_cmd}git #{git_locale} pull origin #{branch}")
ensure
safe_unlink_private_key_tmp_file
end
|
#push(remote: 'origin', branch: 'master', force: false) ⇒ Object
67
68
69
70
|
# File 'lib/git-safe/git.rb', line 67
def push(remote: 'origin', branch: 'master', force: false)
force_flag = force ? '-f ' : ''
execute_git_cmd("git #{git_locale} push #{force_flag}#{remote} #{branch}")
end
|
#remotes ⇒ Object
111
112
113
114
115
116
117
118
119
120
|
# File 'lib/git-safe/git.rb', line 111
def remotes
return [] unless has_git_config? && remote_strs = execute_git_cmd("git #{git_locale} remote -v")
remote_strs.split("\n").collect do |remote_str|
name, uri, type = remote_str.split(' ')
{ name: name,
uri: uri,
type: type.gsub('(', '').gsub(')', '') }
end
end
|
#status ⇒ Object
21
22
23
|
# File 'lib/git-safe/git.rb', line 21
def status
execute_git_cmd("git #{git_locale} status")
end
|
#work_tree_flag ⇒ Object
99
100
101
|
# File 'lib/git-safe/git.rb', line 99
def work_tree_flag
"--work-tree=#{work_tree}"
end
|