Class: GitCommands::Branch

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

Constant Summary collapse

MASTER =
"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.



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

def initialize(name)
  @name = name
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



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

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(&:master?)
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
45
# File 'lib/git_commands/branch.rb', line 41

def self.valid_path?(path)
  path = Pathname.new(path)
  warn "'#{path}' must be an absolute path!".red unless path.absolute?
  path.absolute? && path.file?
end

Instance Method Details

#==(other) ⇒ Object



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

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

#exists?(remote = true) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#master?Boolean

Returns:

  • (Boolean)


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

def master?
  @name == MASTER
end

#to_sObject



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

def to_s
  @name
end

#valid?Boolean

Returns:

  • (Boolean)


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

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