Module: GitCli::Branch

Included in:
Push, Workspace
Defined in:
lib/git_cli/branch.rb

Defined Under Namespace

Classes: BranchError

Instance Method Summary collapse

Instance Method Details

#all_branchesObject

current_branch



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/git_cli/branch.rb', line 65

def all_branches
  br = []
  st, lb = local_branches
  if st
    br.concat(lb)
  end

  st, rb = remote_branches
  if st
    br.concat(rb)
  end

  br
  #[true, br]
   
end

#create_branch(branch) ⇒ Object

switch_branch



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/git_cli/branch.rb', line 173

def create_branch(branch)

  raise_if_empty(branch, "Branch name cannot be empty", GitCliException)

  check_vcs

  branch = branch.gsub(" ","_")

  cmd = []
  cmd << "cd"
  cmd << @wsPath
  cmd << "&&"
  cmd << @vcs.exe_path
  cmd << "branch"
  cmd << branch

  cmdln = cmd.join(" ")
  log_debug "Create branch : #{cmdln}"
  res = os_exec(cmdln) do |st, res|
    
    if st.success?
      res.strip
      #[true, res.strip]
    else
      raise BranchError, res.strip
      #[false, res]
    end
  end
  
end

#current_branchObject



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
# File 'lib/git_cli/branch.rb', line 24

def current_branch

  check_vcs

  cmd = []
  cmd << "cd"
  cmd << @wsPath
  cmd << "&&"
  cmd << @vcs.exe_path
  # only works from git version 2.22 onwards
  # Shall return just the name of the branch
  cmd << "branch --show-current"
  # another way but shall return like refs/heads/master
  #cmd << "symbolic-ref HEAD"

  cmdln = cmd.join(" ")
  log_debug "Current branch : #{cmdln}"
  res = os_exec(cmdln) do |st, res|
    
    if st.success?

      # this is for command 'git branch'
      # But for newly created workspace, git branch will give empty string
      #res.each_line do |l|
      #  tok = l.split " "
      #  if tok[0] == "*"
      #    @currBranch = tok[1].strip
      #    break
      #  end
      #end

      res.strip
      #[true, res.strip!]
    else
      raise BranchError, res.strip
      #[false, res]
    end
  end
  
end

#delete_branch(branch) ⇒ Object

merge_branch



260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/git_cli/branch.rb', line 260

def delete_branch(branch)

  raise_if_empty(branch, "Branch name cannot be empty", GitCliException)

  check_vcs

  cmd = []
  cmd << "cd"
  cmd << @wsPath
  cmd << "&&"
  cmd << @vcs.exe_path
  cmd << "branch"
  cmd << "-d"
  cmd << branch

  cmdln = cmd.join(" ")
  log_debug "Delete branch '#{branch}' : #{cmdln}"
  res = os_exec(cmdln) do |st, res|
    
    if st.success?
      res.strip
      #[true, res.strip]
    else
      raise BranchError, res.strip
      #[false, res]
    end

  end
  
end

#download_all_remote_branches_nameObject Also known as: sync_all_remote_branches_name

create_branch



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/git_cli/branch.rb', line 204

def download_all_remote_branches_name

  check_vcs
  check_repos

  cmd = []
  cmd << "cd"
  cmd << @wsPath
  cmd << "&&"
  cmd << @vcs.exe_path
  cmd << "fetch -all"

  cmdln = cmd.join(" ")
  log_debug "Download remote branches name : #{cmdln}"
  res = os_exec(cmdln) do |st, res|
    
    if st.success?
      res.strip
      #[true, res.strip]
    else
      raise BranchError, res.strip
      #[false, res]
    end
  end
  
end

#local_branchesObject

all_branches



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/git_cli/branch.rb', line 82

def local_branches

  check_vcs

  cmd = []
  cmd << "cd"
  cmd << @wsPath
  cmd << "&&"
  cmd << @vcs.exe_path
  cmd << "branch"

  cmdln = cmd.join(" ")
  log_debug "Local branch : #{cmdln}"
  res = os_exec(cmdln) do |st, res|
    
    if st.success?
      b = []
      res.strip!
      res.each_line do |l|
        b << l.strip
      end
      b
      #[true, b]
    else
      raise BranchError, res.strip
      #[false, res]
    end
  end
   
end

#merge_branch(branch) ⇒ Object



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/git_cli/branch.rb', line 232

def merge_branch(branch)
  raise_if_empty(branch, "Branch name cannot be empty", GitCliException)

  check_vcs

  cmd = []
  cmd << "cd"
  cmd << @wsPath
  cmd << "&&"
  cmd << @vcs.exe_path
  cmd << "merge"
  cmd << branch

  cmdln = cmd.join(" ")
  log_debug "Merge current branch with branch '#{branch}' : #{cmdln}"
  res = os_exec(cmdln) do |st, res|
    
    if st.success?
      res.strip
      #[true, res.strip]
    else
      raise BranchError, res.strip
      #[false, res]
    end
  end
 
end

#remote_branchesObject

local_branches



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/git_cli/branch.rb', line 113

def remote_branches

  check_vcs

  cmd = []
  cmd << "cd"
  cmd << @wsPath
  cmd << "&&"
  cmd << @vcs.exe_path
  cmd << "branch -r"

  cmdln = cmd.join(" ")
  log_debug "Remote branch : #{cmdln}"
  res = os_exec(cmdln) do |st, res|
    
    if st.success?
      b = []
      res.strip!
      res.each_line do |l|
        b << l.strip
      end
      
      #[true, b]
      b
    else
      raise BranchError, res.strip
      #[false, res]
    end
  end
  
end

#switch_branch(branch) ⇒ Object

remote_branches



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/git_cli/branch.rb', line 145

def switch_branch(branch)
  
  raise_if_empty(branch, "Branch name cannot be empty", GitCliException)

  check_vcs

  cmd = []
  cmd << "cd"
  cmd << @wsPath
  cmd << "&&"
  cmd << @vcs.exe_path
  cmd << "checkout"
  cmd << branch

  cmdln = cmd.join(" ")
  log_debug "Switch branch : #{cmdln}"
  res = os_exec(cmdln) do |st, res|
    if st.success?
      res.strip
      #[true, res.strip]
    else
      raise BranchError, res.strip
      #[false, res]
    end
  end

end