Class: GitSafe::Git
Instance Attribute Summary collapse
Instance Method Summary
collapse
-
#add(add_paths) ⇒ Object
-
#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, reset_to_remote: false, git_config: {}) ⇒ Object
-
#commit(commit_msg) ⇒ 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
-
#log ⇒ Object
-
#merge(to_merge_name) ⇒ Object
-
#pull(branch: 'master') ⇒ Object
-
#push(remote: 'origin', branch: 'master', force: false) ⇒ Object
-
#remotes ⇒ Object
-
#reset_local_to_remote(remote_name) ⇒ Object
-
#rm(remove_paths) ⇒ 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(add_paths) ⇒ Object
43
44
45
|
# File 'lib/git-safe/git.rb', line 43
def add(add_paths)
execute_git_cmd("git #{git_locale} add #{add_paths}")
end
|
#add_and_commit(commit_msg) ⇒ Object
51
52
53
54
|
# File 'lib/git-safe/git.rb', line 51
def add_and_commit(commit_msg)
add('.')
commit(commit_msg)
end
|
#add_remote(uri, name: 'origin') ⇒ Object
123
124
125
|
# File 'lib/git-safe/git.rb', line 123
def add_remote(uri, name: 'origin')
execute_git_cmd("git #{git_locale} remote add #{name} #{uri}")
end
|
#branch_a ⇒ Object
64
65
66
|
# File 'lib/git-safe/git.rb', line 64
def branch_a
execute_git_cmd("git #{git_locale} branch -a")
end
|
#checkout(branch: nil, create: false, sha: nil) ⇒ Object
68
69
70
71
72
73
74
75
76
77
|
# File 'lib/git-safe/git.rb', line 68
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
104
105
106
107
108
109
110
111
112
113
|
# File 'lib/git-safe/git.rb', line 104
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, reset_to_remote: false, git_config: {}) ⇒ Object
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
|
# File 'lib/git-safe/git.rb', line 142
def clone_or_fetch_and_merge(remote_uri, branch: 'master', remote_name: 'origin', depth: nil, force_fresh_clone: false, reset_to_remote: 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)
if reset_to_remote
reset_local_to_remote("#{remote_name}/#{branch}")
else
merge("#{remote_name}/#{branch}")
end
end
|
#commit(commit_msg) ⇒ Object
47
48
49
|
# File 'lib/git-safe/git.rb', line 47
def commit(commit_msg)
execute_git_cmd("git #{git_locale} commit -m '#{commit_msg}'")
end
|
#config_get(name) ⇒ Object
35
36
37
|
# File 'lib/git-safe/git.rb', line 35
def config_get(name)
execute_git_cmd("git #{git_locale} config #{name}")
end
|
#config_set(name_values = {}) ⇒ Object
29
30
31
32
33
|
# File 'lib/git-safe/git.rb', line 29
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
171
172
173
|
# File 'lib/git-safe/git.rb', line 171
def delete_work_tree
FileUtils.rm_rf(work_tree) if Dir.exists?(work_tree)
end
|
#execute_git_cmd(git_cmd) ⇒ Object
177
178
179
180
181
182
|
# File 'lib/git-safe/git.rb', line 177
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
92
93
94
95
96
|
# File 'lib/git-safe/git.rb', line 92
def fetch
execute_git_cmd("#{ssh_cmd}git #{git_locale} fetch")
ensure
safe_unlink_private_key_tmp_file
end
|
#git_config ⇒ Object
159
160
161
|
# File 'lib/git-safe/git.rb', line 159
def git_config
File.read(git_config_path) if has_git_config?
end
|
#git_config_path ⇒ Object
167
168
169
|
# File 'lib/git-safe/git.rb', line 167
def git_config_path
"#{work_tree}/.git/config"
end
|
#git_locale ⇒ Object
115
116
117
|
# File 'lib/git-safe/git.rb', line 115
def git_locale
"#{work_tree_flag} --git-dir=#{work_tree}/.git"
end
|
#has_git_config? ⇒ Boolean
Also known as:
work_tree_is_git_repo?
163
164
165
|
# File 'lib/git-safe/git.rb', line 163
def has_git_config?
File.exists?(git_config_path)
end
|
#has_remote? ⇒ Boolean
127
128
129
|
# File 'lib/git-safe/git.rb', line 127
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
56
57
58
|
# File 'lib/git-safe/git.rb', line 56
def last_commit_sha
execute_git_cmd("git #{git_locale} rev-parse HEAD")
end
|
#last_commit_timestamp ⇒ Object
60
61
62
|
# File 'lib/git-safe/git.rb', line 60
def last_commit_timestamp
execute_git_cmd("git #{git_locale} log -1 --format=%cd ")
end
|
#log ⇒ Object
25
26
27
|
# File 'lib/git-safe/git.rb', line 25
def log
execute_git_cmd("git #{git_locale} log")
end
|
#merge(to_merge_name) ⇒ Object
79
80
81
|
# File 'lib/git-safe/git.rb', line 79
def merge(to_merge_name)
execute_git_cmd("git #{git_locale} merge #{to_merge_name}")
end
|
#pull(branch: 'master') ⇒ Object
98
99
100
101
102
|
# File 'lib/git-safe/git.rb', line 98
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
87
88
89
90
|
# File 'lib/git-safe/git.rb', line 87
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
131
132
133
134
135
136
137
138
139
140
|
# File 'lib/git-safe/git.rb', line 131
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
|
#reset_local_to_remote(remote_name) ⇒ Object
83
84
85
|
# File 'lib/git-safe/git.rb', line 83
def reset_local_to_remote(remote_name)
execute_git_cmd("git #{git_locale} reset --hard #{remote_name}")
end
|
#rm(remove_paths) ⇒ Object
39
40
41
|
# File 'lib/git-safe/git.rb', line 39
def rm(remove_paths)
execute_git_cmd("git #{git_locale} rm #{remove_paths}")
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
119
120
121
|
# File 'lib/git-safe/git.rb', line 119
def work_tree_flag
"--work-tree=#{work_tree}"
end
|