Sha256: d26190ac2675d864053f7e418a6261f8f059fa988f81bd1147e9162a2e65176d

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.3 lib/gistory/git_repo.rb
gistory-0.1.2 lib/gistory/git_repo.rb
gistory-0.1.1 lib/gistory/git_repo.rb