Class: Hiiro::Git::Branches

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/hiiro/git/branches.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(branches) ⇒ Branches

Returns a new instance of Branches.



36
37
38
# File 'lib/hiiro/git/branches.rb', line 36

def initialize(branches)
  @branches = branches
end

Instance Attribute Details

#branchesObject (readonly)

Returns the value of attribute branches.



34
35
36
# File 'lib/hiiro/git/branches.rb', line 34

def branches
  @branches
end

Class Method Details

.all(sort_by: nil, ignore_case: false) ⇒ Object



30
31
32
# File 'lib/hiiro/git/branches.rb', line 30

def self.all(sort_by: nil, ignore_case: false)
  fetch(sort_by: sort_by, ignore_case: ignore_case, remote: :all)
end

.fetch(sort_by: nil, ignore_case: false, remote: false) ⇒ Object



6
7
8
9
10
11
12
13
14
15
# File 'lib/hiiro/git/branches.rb', line 6

def self.fetch(sort_by: nil, ignore_case: false, remote: false)
  args = ['git', 'branch', '--format=%(refname:short)']
  args << '-i' if ignore_case
  args << '-r' if remote
  args << "-a" if remote == :all
  args << "--sort=#{sort_by}" if sort_by

  output = `#{args.shelljoin} 2>/dev/null`
  from_names(output.split("\n").map(&:strip))
end

.from_names(names) ⇒ Object



17
18
19
20
# File 'lib/hiiro/git/branches.rb', line 17

def self.from_names(names)
  branches = names.map { |name| Branch.new(name: name) }
  new(branches)
end

.local(sort_by: nil, ignore_case: false) ⇒ Object



22
23
24
# File 'lib/hiiro/git/branches.rb', line 22

def self.local(sort_by: nil, ignore_case: false)
  fetch(sort_by: sort_by, ignore_case: ignore_case, remote: false)
end

.remote(sort_by: nil, ignore_case: false) ⇒ Object



26
27
28
# File 'lib/hiiro/git/branches.rb', line 26

def self.remote(sort_by: nil, ignore_case: false)
  fetch(sort_by: sort_by, ignore_case: ignore_case, remote: true)
end

Instance Method Details

#containing(substring) ⇒ Object



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

def containing(substring)
  self.class.new(branches.select { |b| b.name.include?(substring) })
end

#currentObject



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

def current
  branches.find(&:current?)
end

#each(&block) ⇒ Object



40
41
42
# File 'lib/hiiro/git/branches.rb', line 40

def each(&block)
  branches.each(&block)
end

#empty?Boolean

Returns:

  • (Boolean)


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

def empty?
  branches.empty?
end

#find_by_name(name) ⇒ Object



44
45
46
# File 'lib/hiiro/git/branches.rb', line 44

def find_by_name(name)
  branches.find { |b| b.name == name }
end

#matching(prefix) ⇒ Object



48
49
50
# File 'lib/hiiro/git/branches.rb', line 48

def matching(prefix)
  self.class.new(branches.select { |b| b.name.start_with?(prefix) })
end

#namesObject



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

def names
  branches.map(&:name)
end

#sizeObject Also known as: count, length



68
69
70
# File 'lib/hiiro/git/branches.rb', line 68

def size
  branches.size
end

#to_aObject



74
75
76
# File 'lib/hiiro/git/branches.rb', line 74

def to_a
  branches.dup
end