Sha256: 2b2a7e55da2d2c00155fcbc5df39b433c99e96b31902d147e88c9e4e84555236

Contents?: true

Size: 1.1 KB

Versions: 3

Compression:

Stored size: 1.1 KB

Contents

# frozen_string_literal: true

require 'English'

module Gistory
  class GitRepo
    def initialize(path:)
      raise(Gistory::Error, 'This is not a valid git repository') unless Dir.exist?(File.join(path, '.git'))
      raise(Gistory::Error, 'git is not available, please install it') unless git_cli_available?
    end

    def changes_to_file(filename)
      max_count = Gistory.config.max_lockfile_changes
      hashes_and_dates = git("log --pretty=format:'%h|%cD' --max-count=#{max_count} --follow #{filename}")
      to_commits(hashes_and_dates.split("\n"))
    end

    def file_content_at_commit(commit_hash, filename)
      git("show #{commit_hash}:#{filename}")
    end

    private

    def git_cli_available?
      system('which git > /dev/null 2>&1')
    end

    def to_commits(hashes_and_dates)
      hashes_and_dates.map do |hash_and_date|
        commit_hash, date = hash_and_date.split('|')
        Commit.new(short_hash: commit_hash, date: date)
      end
    end

    def git(command)
      out = `git #{command}`
      raise 'Git CLI command failed' unless $CHILD_STATUS.success?
      out
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
gistory-0.1.8 lib/gistory/git_repo.rb
gistory-0.1.7 lib/gistory/git_repo.rb
gistory-0.1.6 lib/gistory/git_repo.rb