class Git::Branch
Represents a Git
branch
Constants
- BRANCH_NAME_REGEXP
Attributes
Public Class Methods
Source
# File lib/git/branch.rb, line 10 def initialize(base, name) @full = name @base = base @gcommit = nil @stashes = nil @remote, @name = parse_name(name) end
Public Instance Methods
Source
# File lib/git/branch.rb, line 32 def archive(file, opts = {}) @base.lib.archive(@full, file, opts) end
Source
# File lib/git/branch.rb, line 27 def checkout check_if_create @base.checkout(@full) end
Source
# File lib/git/branch.rb, line 64 def contains?(commit) !@base.lib.branch_contains(commit, name).empty? end
Source
# File lib/git/branch.rb, line 60 def current # rubocop:disable Naming/PredicateMethod @base.lib.branch_current == @name end
Source
# File lib/git/branch.rb, line 18 def gcommit @gcommit ||= @base.gcommit(@full) @gcommit end
Source
# File lib/git/branch.rb, line 41 def in_branch(message = 'in branch work') old_current = @base.lib.branch_current checkout if yield @base.commit_all(message) else @base.reset_hard end @base.checkout(old_current) end
g.branch(‘new_branch’).in_branch do
# create new file # do other stuff return true # auto commits and switches back
end
Source
# File lib/git/branch.rb, line 68 def merge(branch = nil, message = nil) if branch in_branch do @base.merge(branch, message) false end # merge a branch into this one else # merge this branch into the current one @base.merge(@name) end end
Source
# File lib/git/branch.rb, line 23 def stashes @stashes ||= Git::Stashes.new(@base) end
Source
# File lib/git/branch.rb, line 81 def update_ref(commit) if @remote @base.lib.update_ref("refs/remotes/#{@remote.name}/#{@name}", commit) else @base.lib.update_ref("refs/heads/#{@name}", commit) end end
Private Instance Methods
Source
# File lib/git/branch.rb, line 138 def check_if_create @base.lib.branch_new(@name) rescue StandardError nil end
Source
# File lib/git/branch.rb, line 130 def parse_name(name) # Expect this will always match match = name.match(BRANCH_NAME_REGEXP) remote = match[:remote_name] ? Git::Remote.new(@base, match[:remote_name]) : nil branch_name = match[:branch_name] [remote, branch_name] end
Given a full branch name return an Array containing the remote and branch names.
Removes ‘remotes’ from the beggining of the name (if present). Takes the second part (splittign by ‘/’) as the remote name. Takes the rest as the repo name (can also hold one or more ‘/’).
Example:
# local branches parse_name('master') #=> [nil, 'master'] parse_name('origin/master') #=> [nil, 'origin/master'] parse_name('origin/master/v2') #=> [nil, 'origin/master'] # remote branches parse_name('remotes/origin/master') #=> ['origin', 'master'] parse_name('remotes/origin/master/v2') #=> ['origin', 'master/v2'] parse_name('refs/remotes/origin/master') #=> ['origin', 'master'] parse_name('refs/remotes/origin/master/v2') #=> ['origin', 'master/v2']
param [String] name branch full name. return [<Git::Remote,NilClass,String>] an Array containing the remote and branch names.