Sha256: 7de678c95e183737a320bccd14c8af7cde63f412c8c42f3b596fe16f130b6f8a

Contents?: true

Size: 1.99 KB

Versions: 19

Compression:

Stored size: 1.99 KB

Contents

# frozen_string_literal: true

require 'avm/git/scms/git_subrepo'
require 'avm/scms/base'
require 'eac_ruby_utils/core_ext'

module Avm
  module Git
    module Scms
      class Git < ::Avm::Scms::Base
        require_sub __FILE__
        include ::Comparable

        COMMIT_DIRTY_DEFAULT_MESSAGE = 'Dirty files.'

        def <=>(other)
          git_repo <=> other.git_repo
        end

        # @return [Avm::Git::Scms::Git::Commit,nil]
        def commit(source)
          if source.is_a?(::Avm::Git::Scms::Git::Commit)
            return source if source.git_repo == self

            raise 'Not same Git repository'
          end
          git_repo.commitize(source).if_present do |v|
            ::Avm::Git::Scms::Git::Commit.new(self, v)
          end
        end

        # @return [Avm::Git::Scms::Git::Commit,nil]
        def commit_dirty(message = nil)
          return nil unless git_repo.dirty?

          git_repo.command('add', '.').execute!
          git_repo.command('commit', '-m',
                           message.call_if_proc.if_present(COMMIT_DIRTY_DEFAULT_MESSAGE)).execute!
          commit(git_repo.head)
        end

        # @return [Avm::Git::Scms::Git::Commit,nil]
        def commit_if_change(message = nil)
          tracker = ::Avm::Git::Scms::Git::ChangeTracker.new(self, message)
          tracker.start
          yield
          tracker.stop
        end

        def git_repo
          @git_repo ||= ::EacGit::Local.new(path)
        end

        # @return [Avm::Git::Scms::Git::Commit]
        def reset_and_commit(commit_to_reset, message)
          git_repo.command('reset', '--soft', commit(commit_to_reset).git_commit.id).execute!
          commit_dirty(message)
        end

        # @return [Enumerable<Avm::Git::Scms::GitSubrepo>]
        def subs
          git_repo.subrepos.map do |subrepo|
            ::Avm::Git::Scms::GitSubrepo.new(subrepo.subpath.expand_path(path))
          end
        end

        def valid?
          path.join('.git').exist?
        end
      end
    end
  end
end

Version data entries

19 entries across 19 versions & 2 rubygems

Version Path
eac_tools-0.49.0 sub/avm-git/lib/avm/git/scms/git.rb
eac_tools-0.48.1 sub/avm-git/lib/avm/git/scms/git.rb
eac_tools-0.48.0 sub/avm-git/lib/avm/git/scms/git.rb
eac_tools-0.47.2 sub/avm-git/lib/avm/git/scms/git.rb
eac_tools-0.47.1 sub/avm-git/lib/avm/git/scms/git.rb
avm-git-0.10.1 lib/avm/git/scms/git.rb
eac_tools-0.47.0 sub/avm-git/lib/avm/git/scms/git.rb
eac_tools-0.46.0 sub/avm-git/lib/avm/git/scms/git.rb
eac_tools-0.45.2 sub/avm-git/lib/avm/git/scms/git.rb
eac_tools-0.45.1 sub/avm-git/lib/avm/git/scms/git.rb
eac_tools-0.45.0 sub/avm-git/lib/avm/git/scms/git.rb
eac_tools-0.44.0 sub/avm-git/lib/avm/git/scms/git.rb
eac_tools-0.43.0 sub/avm-git/lib/avm/git/scms/git.rb
eac_tools-0.42.0 sub/avm-git/lib/avm/git/scms/git.rb
avm-git-0.10.0 lib/avm/git/scms/git.rb
eac_tools-0.41.0 sub/avm-git/lib/avm/git/scms/git.rb
eac_tools-0.40.0 sub/avm-git/lib/avm/git/scms/git.rb
avm-git-0.9.0 lib/avm/git/scms/git.rb
eac_tools-0.39.0 sub/avm-git/lib/avm/git/scms/git.rb