Class: GitCompound::Repository::RepositoryLocal

Inherits:
GitRepository show all
Defined in:
lib/git_compound/repository/repository_local.rb

Overview

Local git repository implementation

Instance Method Summary collapse

Methods inherited from GitRepository

#branch?, #branches, #files_contents, #refs, #tags, #versions

Constructor Details

#initialize(source) ⇒ RepositoryLocal

Returns a new instance of RepositoryLocal.



8
9
10
11
12
13
# 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

Instance Method Details

#checkout(ref) ⇒ Object



21
22
23
# File 'lib/git_compound/repository/repository_local.rb', line 21

def checkout(ref)
  GitCommand.new(:checkout, ref, @source).execute
end

#clone(destination, options = nil) ⇒ Object



15
16
17
18
19
# 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

#fetchObject



25
26
27
28
# 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) ⇒ Object

Raises:



40
41
42
43
# 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) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
37
38
# 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_shaObject



79
80
81
# 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') ⇒ Object



30
31
32
# File 'lib/git_compound/repository/repository_local.rb', line 30

def merge(mergeable = 'FETCH_HEAD')
  GitCommand.new(:merge, mergeable, @source).execute
end

#origin_remoteObject



45
46
47
48
# 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?Boolean

Returns:

  • (Boolean)


63
64
65
66
67
68
69
70
71
72
# 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?Boolean

Returns:

  • (Boolean)


74
75
76
77
# 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) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
53
54
55
56
57
58
59
60
61
# 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