Sha256: 25a765b35cdec952158d4babdf8fc23f4da07ca2a8845adced108c4ea45277af

Contents?: true

Size: 1.85 KB

Versions: 1

Compression:

Stored size: 1.85 KB

Contents

require 'rugged'

module Fulmar
  module Infrastructure
    module Service
      # Provides access to to the local git repository
      class GitService
        attr_accessor :git

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

        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

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

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

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

        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

        # The current preview branch is the alphabetically last preview branch
        def derive_branch_name
          @config[:git][:branch] == 'preview' ? preview_branches.last : @config[:git][:branch]
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
fulmar-1.1.0 lib/fulmar/infrastructure/service/git_service.rb