Class: RightGit::Git::Branch

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

Overview

A branch in a Git repository. Has some proxy methods that make it act a bit like a string, whose value is the name of the branch. This allows branches to be sorted, matched against Regexp, and certain other string-y operations.

Defined Under Namespace

Classes: BranchError

Constant Summary collapse

BRANCH_NAME =

Regexp fragment that matches a valid Git branch name consisting of alphanumerics plus the punctuation characters “#”, “.” “_”, “/” and “-”.

'[#A-Za-z0-9._\/+-]+'
BRANCH_INFO =

Regexp that matches a line of Git output containing information about a branch.

/^(\* |  )?(#{BRANCH_NAME})( -> #{BRANCH_NAME})?$/
BRANCH_FULLNAME =

Regexp that matches a valid Git branch name, possibly prepended by “remotes/”

/(remotes\/)?(#{BRANCH_NAME})/
DEFAULT_DISPLAY_WIDTH =
40
ELLIPSIS =
'...'

Instance Attribute Summary collapse

Attributes included from BelongsToRepository

#repo

Instance Method Summary collapse

Methods included from BelongsToRepository

#logger

Constructor Details

#initialize(repo, line) ⇒ Branch

Returns a new instance of Branch.

Parameters:

  • repo (Repository)

    hosting branch

  • line (String)

    of git output describing branch



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/right_git/git/branch.rb', line 54

def initialize(repo, line)
  match = BRANCH_INFO.match(line)
  if match && (fullname = match[2])
    match = BRANCH_FULLNAME.match(fullname)
    if match
      @fullname = match[2]
      @remote = !!(match[1] || fullname.index('/'))
      @repo = repo
    else
      raise BranchError, "Internal error; matched info but not fullname of #{line.inspect}"
    end
  else
    raise BranchError, "Malformed branch name #{line.inspect}"
  end
end

Instance Attribute Details

#fullnameObject (readonly)

Returns the value of attribute fullname.



50
51
52
# File 'lib/right_git/git/branch.rb', line 50

def fullname
  @fullname
end

Instance Method Details

#<=>(other) ⇒ Integer

Returns comparison value.

Parameters:

Returns:

  • (Integer)

    comparison value



98
99
100
101
102
103
104
# File 'lib/right_git/git/branch.rb', line 98

def <=>(other)
  if other.kind_of?(self.class)
    @fullname <=> other.fullname
  else
    raise ::ArgumentError, 'Wrong type'
  end
end

#==(other) ⇒ TrueClass|FalseClass

Returns true if equivalent.

Parameters:

Returns:

  • (TrueClass|FalseClass)

    true if equivalent



88
89
90
91
92
93
94
# File 'lib/right_git/git/branch.rb', line 88

def ==(other)
  if other.kind_of?(self.class)
    @fullname == other.fullname
  else
    false
  end
end

#=~(other) ⇒ Integer

Returns match offset.

Parameters:

  • regexp (Regexp)

Returns:

  • (Integer)

    match offset



82
83
84
# File 'lib/right_git/git/branch.rb', line 82

def =~(other)
  @fullname =~ other
end

#deleteTrueClass

Deletes this (local or remote) branch.

Returns:

  • (TrueClass)

    always true



139
140
141
142
143
144
145
146
# File 'lib/right_git/git/branch.rb', line 139

def delete
  if self.remote?
    repo.vet_output("push origin :#{self.name}")
  else
    repo.vet_output("branch -D #{@fullname}")
  end
  true
end

#display(width = DEFAULT_DISPLAY_WIDTH) ⇒ String

For display in a column of given width.

Parameters:

  • width (Integer) (defaults to: DEFAULT_DISPLAY_WIDTH)

    for columns

Returns:

  • (String)

    display string



128
129
130
131
132
133
134
# File 'lib/right_git/git/branch.rb', line 128

def display(width = DEFAULT_DISPLAY_WIDTH)
  if @fullname.length >= width
    (@fullname[0..(width - ELLIPSIS.length - 1)] + ELLIPSIS).ljust(width)
  else
    @fullname.ljust(width)
  end
end

#inspectObject

Provide a programmer-friendly representation of this branch.



76
77
78
# File 'lib/right_git/git/branch.rb', line 76

def inspect
  '#<%s:%s>' % [self.class.name, fullname.inspect]
end

#nameString

Returns name of branch sans name of remote (if any).

Returns:

  • (String)

    name of branch sans name of remote (if any)



112
113
114
115
116
117
118
119
120
121
# File 'lib/right_git/git/branch.rb', line 112

def name
  if remote?
    #remove the initial remote-name in the branch (origin/master --> master)
    bits = @fullname.split('/')
    bits.shift
    bits.join('/')
  else
    @fullname
  end
end

#remote?TrueClass|FalseClass

Returns true if branch is remote.

Returns:

  • (TrueClass|FalseClass)

    true if branch is remote



107
108
109
# File 'lib/right_git/git/branch.rb', line 107

def remote?
  @remote
end

#to_sObject

Provide a String representation of this branch (specifically, its fullname).



71
72
73
# File 'lib/right_git/git/branch.rb', line 71

def to_s
  fullname.to_s
end