Class: Gitmine::Branch

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

Direct Known Subclasses

LocalBranch, RemoteBranch

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(issue_id) ⇒ Branch

Returns a new instance of Branch.



68
69
70
# File 'lib/gitmine/branch.rb', line 68

def initialize(issue_id)
  @issue_id = issue_id
end

Instance Attribute Details

#issue_idObject

class methods



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

def issue_id
  @issue_id
end

Class Method Details

.find(issue_id) ⇒ Object



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

def find(issue_id)
  new(issue_id)
end

.find_local(issue_id) ⇒ Object

TODO: specs Return local branch name for issue_id



10
11
12
# File 'lib/gitmine/branch.rb', line 10

def find_local(issue_id)
  local_branches.select { |branch| branch[/^#{issue_id}-/] }.first
end

.find_remote(issue_id) ⇒ Object

TODO: specs Return remote branch name for issue_id



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/gitmine/branch.rb', line 16

def find_remote(issue_id)
  remote_branch = remote_branches.select { |branch| branch[/^#{issue_id}-/] }.first
  unless remote_branch
    # Fetch and retry
    Git.fetch
    clear_memoized_remote_branches!
    remote_branch = remote_branches.select { |branch| branch[/^#{issue_id}-/] }.first
  end

  remote_branch
end

.local_branchesObject

Return an array of local branches starting with digits Example

['123-my-branch', '1234-your-branch']

TODO specs



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/gitmine/branch.rb', line 33

def local_branches
  branches = []

  Git.local_branches.each_line do |line|
    if match = line[/\d+.*$/]
      branches << match
    end
  end

  branches
end

.remote_branchesObject

Return an array of remote branches TODO specs



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/gitmine/branch.rb', line 47

def remote_branches
  return @@remote_branches if defined?(@@remote_branches) && @@remote_branches
  branches = []
  Git.remote_branches.each_line do |line|
    if match = line.match(/origin\/(\d+.*)/)
      branches << match[1]
    end
  end

  @@remote_branches = branches
end

Instance Method Details

#localObject



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

def local
  LocalBranch.new(issue_id)
end

#remoteObject



76
77
78
# File 'lib/gitmine/branch.rb', line 76

def remote
  RemoteBranch.new(issue_id)
end