Class: Fulmar::Infrastructure::Service::GitService

Inherits:
Object
  • Object
show all
Defined in:
lib/fulmar/infrastructure/service/git_service.rb

Overview

Provides access to to the local git repository

Constant Summary collapse

DEFAULT_CONFIG =
{
  local_path: '.',
  git: {
    branch: nil
  }
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ GitService

Returns a new instance of GitService.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/fulmar/infrastructure/service/git_service.rb', line 17

def initialize(config)
  @config = config
  @config.merge(DEFAULT_CONFIG)

  unless @config[:git][:branch]
    @config[:git][:branch] = case config.environment
                             when :preview
                               'preview'
                             when :live
                               'release'
                             else
                               'master'
                             end
  end

  @git = Rugged::Repository.new(@config[:local_path]) # :log => Logger.new(STDOUT)
end

Instance Attribute Details

#gitObject

Returns the value of attribute git.



8
9
10
# File 'lib/fulmar/infrastructure/service/git_service.rb', line 8

def git
  @git
end

Instance Method Details

#branchesObject



35
36
37
# File 'lib/fulmar/infrastructure/service/git_service.rb', line 35

def branches
  @git.branches.collect(:name)
end

#checkout(branch_name = derive_branch_name) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/fulmar/infrastructure/service/git_service.rb', line 47

def checkout(branch_name = derive_branch_name)
  if branches.include?(branch_name)
    @git.checkout(branches.first)
  else
    branches = @git.branches.select { |b| b.name.match(/\/#{branch_name}$/) }
    fail "Cannot find a valid branch, last search was for #{branch_name}" unless branches.any?
    @git.checkout(branches.first)
  end
end

#derive_branch_nameObject

The current preview branch is the alphabetically last preview branch



58
59
60
# File 'lib/fulmar/infrastructure/service/git_service.rb', line 58

def derive_branch_name
  @config[:git][:branch] == 'preview' ? preview_branches.last : @config[:git][:branch]
end

#feature_branchesObject



39
40
41
# File 'lib/fulmar/infrastructure/service/git_service.rb', line 39

def feature_branches
  branches.select { |name| name.match(/^feature_/) }.sort
end

#preview_branchesObject



43
44
45
# File 'lib/fulmar/infrastructure/service/git_service.rb', line 43

def preview_branches
  branches.select { |name| name.match(/^preview_/) }.sort
end