class Gistory::GitRepo

Public Class Methods

new(path:) click to toggle source
# File lib/gistory/git_repo.rb, line 7
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

Public Instance Methods

changes_to_file(filename) click to toggle source
# File lib/gistory/git_repo.rb, line 12
def changes_to_file(filename)
  max_count = Gistory.config.max_fetched_commits
  strategy = git_log_strategy(filename)
  hashes_and_dates = git("log --pretty=format:'%h|%cD' --max-count=#{max_count} #{strategy}")
  to_commits(hashes_and_dates.split("\n"))
end
file_content_at_commit(commit_hash, filename) click to toggle source
# File lib/gistory/git_repo.rb, line 19
def file_content_at_commit(commit_hash, filename)
  git("show #{commit_hash}:#{filename}")
end

Private Instance Methods

git(command) click to toggle source
# File lib/gistory/git_repo.rb, line 45
def git(command)
  out = `git #{command}`
  raise "Git CLI command failed" unless $CHILD_STATUS.success?

  out
end
git_cli_available?() click to toggle source
# File lib/gistory/git_repo.rb, line 34
def git_cli_available?
  system("which git > /dev/null 2>&1")
end
git_log_strategy(filename) click to toggle source
# File lib/gistory/git_repo.rb, line 25
def git_log_strategy(filename)
  if Gistory.config.all_branches?
    "--follow #{filename}"
  else
    # TODO: filter out commits that did not introduce changes to the lock file
    "--first-parent"
  end
end
to_commits(hashes_and_dates) click to toggle source
# File lib/gistory/git_repo.rb, line 38
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