Class: Divergence::GitManager

Inherits:
Object
  • Object
show all
Defined in:
lib/divergence/git_manager.rb

Overview

Manages the configured Git repository

Instance Method Summary collapse

Constructor Details

#initialize(git_path) ⇒ GitManager

Returns a new instance of GitManager.



6
7
8
9
10
# File 'lib/divergence/git_manager.rb', line 6

def initialize(git_path)
  @git_path = git_path
  @log = Logger.new('./log/git.log')
  @current_branch = current_branch
end

Instance Method Details

#discover(branch) ⇒ Object

Since underscores are technically not allowed in URLs, but they are allowed in Git branch names, we have to do some magic to possibly convert dashes to underscores so we can load the right branch.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/divergence/git_manager.rb', line 23

def discover(branch)
  return branch if is_branch?(branch)

  resp = Application.config.callback :on_branch_discover, @git_path, branch

  unless resp.nil?
    return resp
  end

  local_search = "^" + branch.gsub(/-/, ".") + "$"
  remote_search = "^remotes/origin/(" + branch.gsub(/-/, ".") + ")$"
  local_r = Regexp.new(local_search, Regexp::IGNORECASE)
  remote_r = Regexp.new(remote_search, Regexp::IGNORECASE)

  git('branch -a').split("\n").each do |b|
    b = b.gsub('*', '').strip
    
    return b if local_r.match(b)
    if remote_r.match(b)
      return remote_r.match(b)[1]
    end
  end 

  raise "Unable to automatically detect branch. Given = #{branch}"
end

#is_current?(branch) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/divergence/git_manager.rb', line 49

def is_current?(branch)
  @current_branch.to_s == branch
end

#switch(branch, force = false) ⇒ Object



12
13
14
15
16
17
# File 'lib/divergence/git_manager.rb', line 12

def switch(branch, force=false)
  return @git_path if is_current?(branch) and !force
  
  pull branch
  return @git_path
end