Class: GitCommands::Branch

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

Constant Summary collapse

DEFAULT =
"master"
ORIGIN =
"origin"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Branch

Returns a new instance of Branch.



48
49
50
# File 'lib/git_commands/branch.rb', line 48

def initialize(name)
  @name = name
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

Class Method Details

.by_file(path) ⇒ Object



13
14
15
16
17
18
# File 'lib/git_commands/branch.rb', line 13

def self.by_file(path)
  return [] unless valid_path?(path)
  File.foreach(path).map do |name|
    new(name.strip)
  end.select(&:valid?)
end

.by_names(names_list) ⇒ Object



27
28
29
30
31
# File 'lib/git_commands/branch.rb', line 27

def self.by_names(names_list)
  String(names_list).split(",").map do |name|
    new(name.strip)
  end.select(&:valid?)
end

.by_pattern(pattern) ⇒ Object



20
21
22
23
24
25
# File 'lib/git_commands/branch.rb', line 20

def self.by_pattern(pattern)
  return [] unless pattern.index("*")
  `git branch -r --list #{ORIGIN}/#{pattern}`.split("\n").map do |name|
    new(strip_origin(name))
  end.reject(&:default?)
end

.factory(src) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/git_commands/branch.rb', line 33

def self.factory(src)
  return [] unless src
  branches = by_file(src)
  branches = by_pattern(src) if branches.empty?
  branches = by_names(src) if branches.empty?
  branches
end

.strip_origin(name) ⇒ Object



9
10
11
# File 'lib/git_commands/branch.rb', line 9

def self.strip_origin(name)
  name.strip.split("#{ORIGIN}/").last
end

.valid_path?(path) ⇒ Boolean

Returns:

  • (Boolean)


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

def self.valid_path?(path)
  path = Pathname.new(path)
  path.absolute? && path.file?
end

Instance Method Details

#==(other) ⇒ Object



62
63
64
# File 'lib/git_commands/branch.rb', line 62

def ==(other)
  self.name == other.name
end

#default?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/git_commands/branch.rb', line 66

def default?
  @name == DEFAULT
end

#exists?(remote = true) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
73
# File 'lib/git_commands/branch.rb', line 70

def exists?(remote = true)
  origin = "#{ORIGIN}/" if remote
  `git rev-parse --verify #{origin}#{@name} 2> /dev/null`.match(/^[0-9a-z]+/)
end

#to_sObject



52
53
54
# File 'lib/git_commands/branch.rb', line 52

def to_s
  @name
end

#valid?Boolean

Returns:

  • (Boolean)


56
57
58
59
60
# File 'lib/git_commands/branch.rb', line 56

def valid?
  return false if default?
  return false unless exists?
  true
end