Module: GitCli::Branch

Included in:
Gvcs::Workspace
Defined in:
lib/git_cli/branch.rb

Instance Method Summary collapse

Instance Method Details

#all_branchesObject

current_branch



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/git_cli/branch.rb', line 61

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

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

  [true, br]
   
end

#create_branch(branch) ⇒ Object

switch_branch



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/git_cli/branch.rb', line 162

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?
      [true, res.strip]
    else
      [false, res]
    end
  end
  
end

#current_branchObject



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

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

      [true, res.strip!]
    else
      [false, res]
    end
  end
  
end

#delete_branch(branch) ⇒ Object

merge_branch



243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/git_cli/branch.rb', line 243

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?
      [true, res.strip]
    else
      [false, res]
    end

  end
  
end

#download_all_remote_branches_nameObject Also known as: sync_all_remote_branches_name

create_branch



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/git_cli/branch.rb', line 191

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?
      [true, res.strip]
    else
      [false, res]
    end
  end
  
end

#local_branchesObject

all_branches



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/git_cli/branch.rb', line 77

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
      [true, b]
    else
      [false, res]
    end
  end
   
end

#merge_branch(branch) ⇒ Object



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/git_cli/branch.rb', line 217

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?
      [true, res.strip]
    else
      [false, res]
    end
  end
 
end

#remote_branchesObject

local_branches



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/git_cli/branch.rb', line 106

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]
    else
      [false, res]
    end
  end
  
end

#switch_branch(branch) ⇒ Object

remote_branches



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/git_cli/branch.rb', line 136

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?
      [true, res.strip]
    else
      [false, res]
    end
  end

end