Class: Hiiro::Git::Branch

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, ref: nil, upstream: nil, head: nil, current: false) ⇒ Branch

Returns a new instance of Branch.



17
18
19
20
21
22
23
# File 'lib/hiiro/git/branch.rb', line 17

def initialize(name:, ref: nil, upstream: nil, head: nil, current: false)
  @name = name
  @ref = ref || "refs/heads/#{name}"
  @upstream = upstream
  @head = head
  @current = current
end

Instance Attribute Details

#headObject (readonly)

Returns the value of attribute head.



4
5
6
# File 'lib/hiiro/git/branch.rb', line 4

def head
  @head
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/hiiro/git/branch.rb', line 4

def name
  @name
end

#refObject (readonly)

Returns the value of attribute ref.



4
5
6
# File 'lib/hiiro/git/branch.rb', line 4

def ref
  @ref
end

#upstreamObject (readonly)

Returns the value of attribute upstream.



4
5
6
# File 'lib/hiiro/git/branch.rb', line 4

def upstream
  @upstream
end

Class Method Details

.currentObject



11
12
13
14
15
# File 'lib/hiiro/git/branch.rb', line 11

def self.current
  name = `git branch --show-current 2>/dev/null`.strip
  return nil if name.empty?
  new(name: name, current: true)
end

.from_format_line(line, format: :short) ⇒ Object



6
7
8
9
# File 'lib/hiiro/git/branch.rb', line 6

def self.from_format_line(line, format: :short)
  # Parses output from git branch --format
  new(name: line.strip)
end

Instance Method Details

#checkoutObject



37
38
39
# File 'lib/hiiro/git/branch.rb', line 37

def checkout
  system('git', 'checkout', name)
end

#current?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/hiiro/git/branch.rb', line 25

def current?
  @current
end

#delete(force: false) ⇒ Object



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

def delete(force: false)
  flag = force ? '-D' : '-d'
  system('git', 'branch', flag, name)
end

#exists?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/hiiro/git/branch.rb', line 46

def exists?
  system('git', 'show-ref', '--verify', '--quiet', ref)
end

#local?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/hiiro/git/branch.rb', line 29

def local?
  ref.start_with?('refs/heads/')
end

#remote?Boolean

Returns:

  • (Boolean)


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

def remote?
  ref.start_with?('refs/remotes/')
end

#to_hObject



54
55
56
57
58
59
60
61
62
# File 'lib/hiiro/git/branch.rb', line 54

def to_h
  {
    name: name,
    ref: ref,
    upstream: upstream,
    head: head,
    current: current?,
  }.compact
end

#to_sObject



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

def to_s
  name
end