Class: RightGit::Git::Branch

Inherits:
Object
  • Object
show all
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 =
'[#A-Za-z0-9._\/-]+'
BRANCH_INFO =
/^(\* |  )?(#{BRANCH_NAME})( -> #{BRANCH_NAME})?$/
BRANCH_FULLNAME =
/(remotes\/)?(#{BRANCH_NAME})/
DEFAULT_DISPLAY_WIDTH =
40
ELLIPSIS =
'...'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repo, line) ⇒ Branch

Returns a new instance of Branch.

Parameters:

  • repo (Repository)

    hosting branch

  • line (String)

    of git output describing branch



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/right_git/git/branch.rb', line 46

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, 'Unreachable due to already matching name pattern'
    end
  else
    raise BranchError, "Unrecognized branch info: #{line.inspect}"
  end
end

Instance Attribute Details

#fullnameObject (readonly)

Returns the value of attribute fullname.



42
43
44
# File 'lib/right_git/git/branch.rb', line 42

def fullname
  @fullname
end

#repoObject (readonly)

Returns the value of attribute repo.



42
43
44
# File 'lib/right_git/git/branch.rb', line 42

def repo
  @repo
end

Instance Method Details

#<=>(other) ⇒ Integer

Returns comparison value.

Parameters:

Returns:

  • (Integer)

    comparison value



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

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



76
77
78
79
80
81
82
# File 'lib/right_git/git/branch.rb', line 76

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



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

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

#deleteTrueClass

Deletes this (local or remote) branch.

Returns:

  • (TrueClass)

    always true



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

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



116
117
118
119
120
121
122
# File 'lib/right_git/git/branch.rb', line 116

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

#nameString

Returns name of branch sans origin (if any).

Returns:

  • (String)

    name of branch sans origin (if any)



100
101
102
103
104
105
106
107
108
109
# File 'lib/right_git/git/branch.rb', line 100

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



95
96
97
# File 'lib/right_git/git/branch.rb', line 95

def remote?
  @remote
end

#to_sString Also known as: inspect

Returns stringized.

Returns:

  • (String)

    stringized



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

def to_s
  "#{self.class.name}: #{@fullname.inspect}"
end