Sha256: cc650019792cf89e668bc5c75c967a95337d62a52df61d6e12a53073cc11040a

Contents?: true

Size: 1.79 KB

Versions: 141

Compression:

Stored size: 1.79 KB

Contents

# frozen_string_literal: true

require 'eac_ruby_utils/core_ext'

module EacGit
  class Local
    class Commit
      require_sub __FILE__, include_modules: true
      enable_simple_cache
      include ::Comparable

      FIELDS = {
        author_name: '%an', author_email: '%ae', author_date: '%ai',
        subject: '%s',
        author_all: '%an <%ae>, %ai',
        commit_hash: '%H', abbreviated_commit_hash: '%h',
        commiter_name: '%cn', commiter_email: '%ce', commiter_date: '%ci',
        commiter_all: '%cn <%ce>, %ci',
        raw_body: '%B'
      }.freeze

      common_constructor :repo, :id

      def <=>(other)
        [repo, id] <=> [other.repo, other.id]
      end

      def format(format)
        repo.command('--no-pager', 'log', '-1', "--pretty=format:#{format}", id).execute!.strip
      end

      FIELDS.each do |field, format|
        define_method(field) { format(format) }
      end

      def changed_files_uncached
        diff_tree_execute.each_line.map do |line|
          ::EacGit::Local::Commit::ChangedFile.new(self, line)
        end
      end

      def changed_files_size_uncached
        changed_files.inject(0) { |a, e| a + e.dst_size }
      end

      # @return [EacGit::Local::Commit, nil]
      def parent
        ps = parents
        raise "#{self} has more than one parent" if ps.count > 2

        ps.empty? ? nil : ps.first
      end

      # @return [Array<EacGit::Local::Commit>]
      def parents
        format('%P').each_line.map { |line| repo.commitize(line) }
      end

      def root_child?
        parents.empty?
      end

      private

      def diff_tree_execute
        args = []
        args << '--root' if root_child?
        args << id
        repo.command(*::EacGit::Local::Commit::DiffTreeLine::GIT_COMMAND_ARGS, *args).execute!
      end
    end
  end
end

Version data entries

141 entries across 141 versions & 3 rubygems

Version Path
eac_tools-0.40.0 sub/eac_git/lib/eac_git/local/commit.rb
eac_tools-0.39.0 sub/eac_git/lib/eac_git/local/commit.rb
eac_tools-0.38.0 sub/eac_git/lib/eac_git/local/commit.rb
eac_tools-0.37.2 sub/eac_git/lib/eac_git/local/commit.rb
eac_git-0.13.0 lib/eac_git/local/commit.rb
eac_tools-0.37.1 sub/eac_git/lib/eac_git/local/commit.rb
eac_tools-0.37.0 sub/eac_git/lib/eac_git/local/commit.rb
eac_tools-0.36.1 sub/eac_git/lib/eac_git/local/commit.rb
eac_tools-0.36.0 sub/eac_git/lib/eac_git/local/commit.rb
eac_tools-0.35.0 sub/eac_git/lib/eac_git/local/commit.rb
eac_tools-0.34.0 sub/eac_git/lib/eac_git/local/commit.rb
eac_git-0.12.3 lib/eac_git/local/commit.rb
eac_tools-0.33.0 sub/eac_git/lib/eac_git/local/commit.rb
eac_tools-0.32.0 sub/eac_git/lib/eac_git/local/commit.rb
eac_tools-0.31.1 sub/eac_git/lib/eac_git/local/commit.rb
eac_git-0.12.2 lib/eac_git/local/commit.rb
avm-tools-0.129.0 sub/eac_git/lib/eac_git/local/commit.rb
eac_tools-0.16.0 sub/avm-tools/sub/eac_git/lib/eac_git/local/commit.rb
avm-tools-0.128.0 sub/eac_git/lib/eac_git/local/commit.rb
eac_tools-0.15.0 sub/avm-tools/sub/eac_git/lib/eac_git/local/commit.rb