Class: Git::Branch

Inherits:
Object
  • Object
show all
Defined in:
lib/git/branch.rb

Overview

Represents a Git branch

Constant Summary collapse

BRANCH_NAME_REGEXP =
%r{
  ^
    # Optional 'refs/remotes/' at the beggining to specify a remote tracking branch
    # with a <remote_name>. <remote_name> is nil if not present.
    (?:
      (?:(?:refs/)?remotes/)(?<remote_name>[^/]+)/
    )?
    (?<branch_name>.*)
  $
}x

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base, name) ⇒ Branch

Returns a new instance of Branch.



10
11
12
13
14
15
16
# 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

Instance Attribute Details

#full

Returns the value of attribute full.



8
9
10
# File 'lib/git/branch.rb', line 8

def full
  @full
end

#name

Returns the value of attribute name.



8
9
10
# File 'lib/git/branch.rb', line 8

def name
  @name
end

#remote

Returns the value of attribute remote.



8
9
10
# File 'lib/git/branch.rb', line 8

def remote
  @remote
end

Instance Method Details

#archive(file, opts = {})



32
33
34
# File 'lib/git/branch.rb', line 32

def archive(file, opts = {})
  @base.lib.archive(@full, file, opts)
end

#checkout



27
28
29
30
# File 'lib/git/branch.rb', line 27

def checkout
  check_if_create
  @base.checkout(@full)
end

#contains?(commit) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/git/branch.rb', line 64

def contains?(commit)
  !@base.lib.branch_contains(commit, name).empty?
end

#create



52
53
54
# File 'lib/git/branch.rb', line 52

def create
  check_if_create
end

#current

rubocop:disable Naming/PredicateMethod



60
61
62
# File 'lib/git/branch.rb', line 60

def current # rubocop:disable Naming/PredicateMethod
  @base.lib.branch_current == @name
end

#delete



56
57
58
# File 'lib/git/branch.rb', line 56

def delete
  @base.lib.branch_delete(@name)
end

#gcommit



18
19
20
21
# File 'lib/git/branch.rb', line 18

def gcommit
  @gcommit ||= @base.gcommit(@full)
  @gcommit
end

#in_branch(message = 'in branch work')

g.branch('new_branch').in_branch do # create new file # do other stuff return true # auto commits and switches back end



41
42
43
44
45
46
47
48
49
50
# 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

#merge(branch = nil, message = nil)



68
69
70
71
72
73
74
75
76
77
78
79
# 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

#stashes



23
24
25
# File 'lib/git/branch.rb', line 23

def stashes
  @stashes ||= Git::Stashes.new(@base)
end

#to_a



89
90
91
# File 'lib/git/branch.rb', line 89

def to_a
  [@full]
end

#to_s



93
94
95
# File 'lib/git/branch.rb', line 93

def to_s
  @full
end

#update_ref(commit)



81
82
83
84
85
86
87
# 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