Sha256: baecac66807644433e21d8e0eeec279d397d20cac5c5c4ae39dcab1421691089

Contents?: true

Size: 2 KB

Versions: 6

Compression:

Stored size: 2 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 current_hash
          @git.head.target_id
        end

        def current_branch
          @git.head.name.split('/').last
        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

6 entries across 6 versions & 1 rubygems

Version Path
fulmar-1.5.0 lib/fulmar/infrastructure/service/git_service.rb
fulmar-1.4.2 lib/fulmar/infrastructure/service/git_service.rb
fulmar-1.4.1 lib/fulmar/infrastructure/service/git_service.rb
fulmar-1.4.0 lib/fulmar/infrastructure/service/git_service.rb
fulmar-1.3.0 lib/fulmar/infrastructure/service/git_service.rb
fulmar-1.2.1 lib/fulmar/infrastructure/service/git_service.rb