Class: GitProc::GitBranch

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/git-process/git_branch.rb

Overview

A Git Branch

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, current, lib) ⇒ GitBranch

TODO:

instead of passing in current, detect it dynamically (e.g., look at HEAD)

Returns a new instance of GitBranch.

Parameters:

  • name (String)

    the name of the branch; if it starts with “remotes/” that part is stripped and #remote? will return true

  • current (Boolean)

    is this the current branch?

  • lib (GitLib)

    the GitProc::GitLib to use for operations



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/git-process/git_branch.rb', line 32

def initialize(name, current, lib)
  if /^remotes\// =~ name
    @name = name[8..-1]
    @remote = true
  else
    @name = name
    @remote = false
  end
  @current = current
  @lib = lib
end

Instance Attribute Details

#nameString (readonly)

the name of the branch

Returns:

  • (String)

    the current value of name



20
21
22
# File 'lib/git-process/git_branch.rb', line 20

def name
  @name
end

Instance Method Details

#<=>(other) ⇒ int?

Implements Comparable based on the branch name

Parameters:

  • other (String, #name)

    the item to compare to this; if a String then it is compared to self.name, otherwise the names are compared

Returns:

  • (int, nil)

    -1, 0, 1 or nil per Object#<=>



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/git-process/git_branch.rb', line 87

def <=>(other)
  if other.nil?
    return nil
  elsif other.is_a? String
    return self.name <=> other
  elsif other.respond_to? :name
    return self.name <=> other.name
  else
    return nil
  end
end

#checkout_to_new(new_branch, opts = {}) ⇒ Object



138
139
140
# File 'lib/git-process/git_branch.rb', line 138

def checkout_to_new(new_branch, opts = {})
  @lib.checkout(new_branch, :new_branch => @name, :no_track => opts[:no_track])
end

#contains_all_of(branch_name) ⇒ Object



133
134
135
# File 'lib/git-process/git_branch.rb', line 133

def contains_all_of(branch_name)
  @lib.rev_list(@name, branch_name, :oneline => true, :num_revs => 1) == ''
end

#current?Boolean

Returns is this the current branch?.

Returns:

  • (Boolean)

    is this the current branch?



46
47
48
# File 'lib/git-process/git_branch.rb', line 46

def current?
  @current
end

#delete!(force = false) ⇒ String

Delete this branch

Parameters:

  • force (Boolean) (defaults to: false)

    should this force removal even if the branch has not been fully merged?

Returns:

  • (String)

    the output of running the git command



114
115
116
117
118
119
120
# File 'lib/git-process/git_branch.rb', line 114

def delete!(force = false)
  if local?
    @lib.branch(@name, :force => force, :delete => true)
  else
    @lib.push(Process.server_name, nil, nil, :delete => @name)
  end
end

#is_ahead_of(base_branch_name) ⇒ Boolean

Returns does this branch contain every commit in base_branch_name as well as at least one more?.

Parameters:

  • base_branch_name (String)

    the branch to compare to

Returns:

  • (Boolean)

    does this branch contain every commit in base_branch_name as well as at least one more?



102
103
104
105
# File 'lib/git-process/git_branch.rb', line 102

def is_ahead_of(base_branch_name)
  contains_all_of(base_branch_name) and
      (@lib.rev_list(base_branch_name, @name, :oneline => true, :num_revs => 1) != '')
end

#local?Boolean

Returns does this represent a local branch?.

Returns:

  • (Boolean)

    does this represent a local branch?



58
59
60
# File 'lib/git-process/git_branch.rb', line 58

def local?
  !@remote
end

#loggerGitLogger

Returns the logger to use.

Returns:



70
71
72
# File 'lib/git-process/git_branch.rb', line 70

def logger
  @lib.logger
end

#remote?Boolean

Returns does this represent a remote branch?.

Returns:

  • (Boolean)

    does this represent a remote branch?



52
53
54
# File 'lib/git-process/git_branch.rb', line 52

def remote?
  @remote
end

#rename(new_name) ⇒ Object



123
124
125
# File 'lib/git-process/git_branch.rb', line 123

def rename(new_name)
  @lib.branch(@name, :rename => new_name)
end

#shaString

Returns the SHA-1 of the tip of this branch.

Returns:

  • (String)

    the SHA-1 of the tip of this branch



76
77
78
# File 'lib/git-process/git_branch.rb', line 76

def sha
  @lib.sha(name)
end

#to_sString

Returns the name of the branch.

Returns:

  • (String)

    the name of the branch



64
65
66
# File 'lib/git-process/git_branch.rb', line 64

def to_s
  name
end

#upstream(upstream_name) ⇒ Object



128
129
130
# File 'lib/git-process/git_branch.rb', line 128

def upstream(upstream_name)
  @lib.branch(@name, :upstream => upstream_name)
end