Class: Toolshed::Git::Branch
Constant Summary
DEFAULT_BRANCH_FROM, DEFAULT_GIT_TOOL
Instance Attribute Summary
#force, #from_remote_branch_name, #from_remote_name, #passed_branch_name, #to_remote_branch_name, #to_remote_name, #validator
Class Method Summary
collapse
Instance Method Summary
collapse
clean_branch_name, git_submodule_command, #remote_update
Constructor Details
#initialize(options = {}) ⇒ Branch
Returns a new instance of Branch.
6
7
8
|
# File 'lib/toolshed/git/branch.rb', line 6
def initialize(options = {})
super(options)
end
|
Class Method Details
.ask_which_branch(branch_names) ⇒ Object
13
14
15
16
17
18
19
20
21
22
23
24
25
|
# File 'lib/toolshed/git/branch.rb', line 13
def ask_which_branch(branch_names)
selected_branch_name = ''
choose do ||
.prompt = "Multiple branches matched input branch name. Which branch are you looking for? "
branch_names.each do |branch_name|
.choice(branch_name.to_sym, branch_name) do |branch_name|
selected_branch_name = branch_name
end
end
end
selected_branch_name.to_s
end
|
.checkout(checkout_branch_name) ⇒ Object
.from ⇒ Object
27
28
29
|
# File 'lib/toolshed/git/branch.rb', line 27
def from
`git rev-parse --abbrev-ref --symbolic-full-name @{u}`.split('/')[-1].strip
end
|
.name ⇒ Object
46
47
48
49
|
# File 'lib/toolshed/git/branch.rb', line 46
def name
branch = Toolshed::Git::Branch.new
branch.name
end
|
.name_from_substring(substring) ⇒ Object
51
52
53
54
55
56
57
58
|
# File 'lib/toolshed/git/branch.rb', line 51
def name_from_substring(substring)
branches = Toolshed::Base.wait_for_command("git branch | grep \"#{substring}\"")
branch_names = branches[:all].map { |branch_name| branch_name.gsub('*', '').strip }
return substring if branch_names.length == 0
return branch_names.first if branch_names.length == 1
Toolshed::Git::Branch.ask_which_branch(branch_names)
end
|
Instance Method Details
#delete(delete_branch_name) ⇒ Object
#delete_local(local_branch_name) ⇒ Object
107
108
109
110
111
112
113
114
115
116
117
|
# File 'lib/toolshed/git/branch.rb', line 107
def delete_local(local_branch_name)
unless local.map { |local_branch| local_branch[:branch_name] }.include?(local_branch_name)
Toolshed.logger.warn "Unable to delete '#{local_branch_name}' from local as it does not exist skipping"
return
end
results = Toolshed::Base.wait_for_command("git branch -D #{local_branch_name}")
results[:all].each do |result|
Toolshed.logger.info result.rstrip
end
end
|
#delete_remote(remote_branch_name) ⇒ Object
119
120
121
122
123
124
125
126
127
128
129
130
|
# File 'lib/toolshed/git/branch.rb', line 119
def delete_remote(remote_branch_name)
unless remote.include?(remote_branch_name)
Toolshed.logger.warn "Unable to delete '#{remote_branch_name}' from remote as it does not exist skipping"
return
end
Toolshed.logger.info "Deleting #{remote_branch_name} from remote"
results = Toolshed::Base.wait_for_command("git push #{Toolshed::Client.instance.push_to_remote_name} --delete #{remote_branch_name}")
results[:all].each do |result|
Toolshed.logger.info result.rstrip
end
end
|
#list ⇒ Object
132
133
134
135
136
|
# File 'lib/toolshed/git/branch.rb', line 132
def list
remote_update
list_local
list_remote
end
|
#list_local ⇒ Object
138
139
140
141
142
143
144
145
146
|
# File 'lib/toolshed/git/branch.rb', line 138
def list_local
Toolshed.logger.info ''
Toolshed.logger.info 'Local Branches'
Toolshed.logger.info ''
current_branch_name = name
local.each do |local_branch|
Toolshed.logger.info "#{local_branch[:branch_name]} #{local_branch[:branch_info]}"
end
end
|
#local ⇒ Object
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
|
# File 'lib/toolshed/git/branch.rb', line 157
def local
@local ||= begin
local_branches = []
results = Toolshed::Base.wait_for_command('git branch -avv')
results[:stdout].each do |stdout|
next if /remotes.*/.match(stdout) || /HEAD.*/.match(stdout)
matches = /([^\s]+)/.match(stdout)
branch_name = matches[0]
branch_name = branch_name.gsub('*', '')
branch_info_match = /\[[a-z].*\]/.match(stdout)
branch_info = ''
branch_info = branch_info_match[0] unless branch_info_match.nil?
local_branches << { branch_name: branch_name.lstrip.rstrip, branch_info: branch_info.lstrip.rstrip }
end
local_branches
end
end
|
#name ⇒ Object
175
176
177
|
# File 'lib/toolshed/git/branch.rb', line 175
def name
(passed_branch_name.nil? || passed_branch_name.empty?) ? `git rev-parse --abbrev-ref HEAD`.strip : Toolshed::Git::Branch.name_from_substring(passed_branch_name) end
|
#push ⇒ Object
179
180
181
182
183
184
185
186
187
188
189
190
191
|
# File 'lib/toolshed/git/branch.rb', line 179
def push
Toolshed.logger.info "Pushing #{name}"
if (name.nil? || name.empty?) && (!passed_branch_name.nil? && !passed_branch_name.empty?)
Toolshed.logger.fatal "Branch #{passed_branch_name} was not found. Unable to push branch."
Toolshed.die
end
result = Toolshed::Base.wait_for_command("git push #{to_remote_name} #{name} #{force} #{Toolshed::Client.instance.git_quiet}")
result[:all].each do |stdout|
Toolshed.logger.info stdout
end
Toolshed.logger.info 'Everything up-to-date' if result[:stdout].empty? && result[:stderr].empty?
Toolshed.logger.info "#{name} has been pushed"
end
|
#remote ⇒ Object
193
194
195
196
197
198
199
200
201
202
203
|
# File 'lib/toolshed/git/branch.rb', line 193
def remote
remote_branches = []
results = Toolshed::Base.wait_for_command('git branch -avv')
results[:stdout].each do |stdout|
next unless /remotes\/#{from_remote_name}.*/.match(stdout)
next if /.*HEAD.*/.match(stdout)
matches = /([^\s]+)/.match(stdout)
remote_branches << matches[0].gsub("remotes/#{from_remote_name}/", '')
end
remote_branches
end
|
#rename(new_branch_name) ⇒ Object
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
|
# File 'lib/toolshed/git/branch.rb', line 205
def rename(new_branch_name)
current_branch_name = passed_branch_name
results = Toolshed::Base.wait_for_command("git branch -m #{passed_branch_name} #{new_branch_name}")
results[:all].each do |out|
matches = /(error.*|fatal.*)/.match(out)
if matches.length > 0
Toolshed.logger.fatal out
Toolshed.die("Unable to proceed supplied branch '#{current_branch_name}' does not exist in local repository.")
else
Toolshed.logger.info out
end
end
self.passed_branch_name = new_branch_name
push
delete_remote(current_branch_name)
Toolshed.logger.info ''
Toolshed.logger.info "Deleted branch #{current_branch_name}"
end
|