Sha256: 6e3bc6416d291acffe3c23b8ad92ca19cbd935adfa2cacbe388bf3a713ef3673

Contents?: true

Size: 1.63 KB

Versions: 12

Compression:

Stored size: 1.63 KB

Contents

# frozen_string_literal: true

require 'eac_git/executables'
require 'eac_ruby_utils/core_ext'

module EacGit
  # A Git repository in local filesystem.
  class Local
    require_sub __FILE__, include_modules: true

    common_constructor :root_path do
      self.root_path = root_path.to_pathname
    end

    def commit(ref, required = false)
      rev_parse(ref, required).if_present { |v| ::EacGit::Local::Commit.new(self, v) }
    end

    def descendant?(descendant, ancestor)
      base = merge_base(descendant, ancestor)
      return false if base.blank?

      revparse = command('rev-parse', '--verify', ancestor).execute!.strip
      base == revparse
    end

    def merge_base(*commits)
      refs = commits.dup
      while refs.count > 1
        refs[1] = merge_base_pair(refs[0], refs[1])
        return nil if refs[1].blank?

        refs.shift
      end
      refs.first
    end

    def command(*args)
      ::EacGit::Executables.git.command('-C', root_path.to_path, *args)
    end

    def raise_error(message)
      raise "#{root_path}: #{message}"
    end

    def rev_parse(ref, required = false)
      r = command('rev-parse', ref).execute!(exit_outputs: { 128 => nil, 32_768 => nil })
      r.strip! if r.is_a?(String)
      return r if r.present?
      return nil unless required

      raise_error "Reference \"#{ref}\" not found"
    end

    def subrepo(subpath)
      ::EacGit::Local::Subrepo.new(self, subpath)
    end

    def to_s
      "#{self.class}[#{root_path}]"
    end

    private

    def merge_base_pair(commit1, commit2)
      command('merge-base', commit1, commit2).execute!(exit_outputs: { 256 => nil }).strip
    end
  end
end

Version data entries

12 entries across 12 versions & 2 rubygems

Version Path
avm-tools-0.106.0 vendor/eac_git/lib/eac_git/local.rb
avm-tools-0.105.0 vendor/eac_git/lib/eac_git/local.rb
eac_git-0.7.3 lib/eac_git/local.rb
eac_git-0.7.2 lib/eac_git/local.rb
eac_git-0.7.1 lib/eac_git/local.rb
eac_git-0.7.0 lib/eac_git/local.rb
avm-tools-0.104.0 vendor/eac_git/lib/eac_git/local.rb
avm-tools-0.103.1 vendor/eac_git/lib/eac_git/local.rb
avm-tools-0.103.0 vendor/eac_git/lib/eac_git/local.rb
avm-tools-0.102.2 vendor/eac_git/lib/eac_git/local.rb
eac_git-0.6.0 lib/eac_git/local.rb
avm-tools-0.102.1 vendor/eac_git/lib/eac_git/local.rb