class GitCompound::Repository::RepositoryLocal

Local git repository implementation

Public Class Methods

new(source) click to toggle source
# File lib/git_compound/repository/repository_local.rb, line 8
def initialize(source)
  super
  @source = Pathname.new(@source).expand_path.to_s
  raise RepositoryUnreachableError, "Invalid Git repository in #{@source}" unless
    File.directory?("#{@source}/.git")
end

Public Instance Methods

checkout(ref) click to toggle source
# File lib/git_compound/repository/repository_local.rb, line 21
def checkout(ref)
  GitCommand.new(:checkout, ref, @source).execute
end
clone(destination, options = nil) click to toggle source
# File lib/git_compound/repository/repository_local.rb, line 15
def clone(destination, options = nil)
  # Prefer ^file:/// instead of ^/ as latter does not work with --depth
  source = @source.sub(%r{^\/}, 'file:///')
  super(destination, options, source)
end
fetch() click to toggle source
# File lib/git_compound/repository/repository_local.rb, line 25
def fetch
  GitCommand.new(:fetch, '', @source).execute
  GitCommand.new(:fetch, '--tags', @source).execute
end
file_contents(file, ref) click to toggle source
# File lib/git_compound/repository/repository_local.rb, line 40
def file_contents(file, ref)
  raise FileNotFoundError unless file_exists?(file, ref)
  GitCommand.new(:show, "#{ref}:#{file}", @source).execute
end
file_exists?(file, ref) click to toggle source
# File lib/git_compound/repository/repository_local.rb, line 34
def file_exists?(file, ref)
  cmd = GitCommand.new(:show, "#{ref}:#{file}", @source)
  cmd.execute!
  cmd.valid?
end
head_sha() click to toggle source
# File lib/git_compound/repository/repository_local.rb, line 79
def head_sha
  GitCommand.new('rev-parse', 'HEAD', @source).execute
end
merge(mergeable = 'FETCH_HEAD') click to toggle source
# File lib/git_compound/repository/repository_local.rb, line 30
def merge(mergeable = 'FETCH_HEAD')
  GitCommand.new(:merge, mergeable, @source).execute
end
origin_remote() click to toggle source
# File lib/git_compound/repository/repository_local.rb, line 45
def origin_remote
  origin = GitCommand.new(:remote, '-v', @source).execute.match(/origin\t(.*?)\s/)
  origin.captures.first if origin
end
uncommited_changes?() click to toggle source
# File lib/git_compound/repository/repository_local.rb, line 63
def uncommited_changes?
  GitCommand.new('update-index', '-q --refresh', @source).execute
  unstaged = GitCommand.new('diff-files', '--quiet', @source)
  uncommited = GitCommand.new('diff-index', '--cached --quiet HEAD', @source)

  [unstaged, uncommited].any? do |cmd|
    cmd.execute!
    !cmd.valid?
  end
end
unpushed_commits?() click to toggle source
# File lib/git_compound/repository/repository_local.rb, line 74
def unpushed_commits?
  unpushed = GitCommand.new('rev-list', '@{u}..', @source)
  unpushed.execute.length > 0
end
untracked_files?(exclude = nil) click to toggle source
# File lib/git_compound/repository/repository_local.rb, line 50
def untracked_files?(exclude = nil)
  untracked =
    GitCommand.new('ls-files', '--exclude-standard --others', @source).execute
  return (untracked.length > 0) unless exclude

  untracked = untracked.split("\n")
  untracked.delete_if do |file|
    exclude.include?(file) || exclude.include?(file.split(File::SEPARATOR).first)
  end

  untracked.any?
end