Class: EacGit::Local

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/eac_git/local.rb,
lib/eac_git/local/log.rb,
lib/eac_git/local/branch.rb,
lib/eac_git/local/commit.rb,
lib/eac_git/local/remote.rb,
lib/eac_git/local/remotes.rb,
lib/eac_git/local/subrepo.rb,
lib/eac_git/local/dirty_files.rb,
lib/eac_git/local/remote/push.rb,
lib/eac_git/local/changed_file.rb,
lib/eac_git/local/commit/archive.rb,
lib/eac_git/local/subrepo/config.rb,
lib/eac_git/local/commit/changed_file.rb,
lib/eac_git/local/commit/diff_tree_line.rb

Overview

A Git repository in local filesystem.

Direct Known Subclasses

Rspec::StubbedGitLocalRepo::Repository

Defined Under Namespace

Modules: DirtyFiles, Log, Remotes Classes: Branch, ChangedFile, Commit, Remote, Subrepo

Constant Summary collapse

HEAD_REFERENCE =
'HEAD'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.find(path) ⇒ Object



12
13
14
15
16
17
18
19
# File 'lib/eac_git/local.rb', line 12

def find(path)
  path = path.to_pathname.expand_path
  if path.join('.git').exist?
    new(path)
  elsif path.to_path != '/'
    find(path.parent)
  end
end

Instance Method Details

#<=>(other) ⇒ Object



26
27
28
# File 'lib/eac_git/local.rb', line 26

def <=>(other)
  root_path <=> other.root_path
end

#branch(name) ⇒ EacGit::Local::Branch

Retrieves a local branch.

Parameters:

  • name (String)

    Ex.: for “refs/heads/master”, name should be “master”.

Returns:



34
35
36
# File 'lib/eac_git/local.rb', line 34

def branch(name)
  ::EacGit::Local::Branch.new(self, name)
end

#command(*args) ⇒ Object



86
87
88
# File 'lib/eac_git/local.rb', line 86

def command(*args)
  ::EacGit::Executables.git.command('-C', root_path.to_path, *args)
end

#commit(ref, required = false) ⇒ Object

rubocop:disable Style/OptionalBooleanParameter



38
39
40
# File 'lib/eac_git/local.rb', line 38

def commit(ref, required = false) # rubocop:disable Style/OptionalBooleanParameter
  rev_parse(ref, required).if_present { |v| ::EacGit::Local::Commit.new(self, v) }
end

#commitize(source) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/eac_git/local.rb', line 42

def commitize(source)
  if source.is_a?(::EacGit::Local::Commit)
    return source if source.repo == self

    source = source.id
  end

  source.to_s.strip.if_present(nil) { |v| ::EacGit::Local::Commit.new(self, v) }
end

#current_branchEacGit::Local::Branch?

Retrieves the current local branch.

Returns:



55
56
57
58
59
60
# File 'lib/eac_git/local.rb', line 55

def current_branch
  command('symbolic-ref', '--quiet', HEAD_REFERENCE)
    .execute!(exit_outputs: { 256 => '' })
    .gsub(%r{\Arefs/heads/}, '').strip
    .if_present { |v| branch(v) }
end

#descendant?(descendant, ancestor) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
65
66
67
68
# File 'lib/eac_git/local.rb', line 62

def descendant?(descendant, ancestor)
  base = merge_base(descendant, ancestor)
  return false if base.blank?

  revparse = command('rev-parse', '--verify', ancestor).execute!.strip
  base == revparse
end

#head(required = true) ⇒ EacGit::Local::Commit

Returns EacGit::Local::Commit.

Returns:



71
72
73
# File 'lib/eac_git/local.rb', line 71

def head(required = true) # rubocop:disable Style/OptionalBooleanParameter
  commit(HEAD_REFERENCE, required)
end

#merge_base(*commits) ⇒ Object



75
76
77
78
79
80
81
82
83
84
# File 'lib/eac_git/local.rb', line 75

def merge_base(*commits)
  refs = commits.dup
  while refs.count > 1 # rubocop:disable Style/CollectionQuerying
    refs[1] = merge_base_pair(refs[0], refs[1])
    return nil if refs[1].blank?

    refs.shift
  end
  refs.first
end

#raise_error(message) ⇒ Object



90
91
92
# File 'lib/eac_git/local.rb', line 90

def raise_error(message)
  raise "#{root_path}: #{message}"
end

#rev_parse(ref, required = false) ⇒ Object

rubocop:disable Style/OptionalBooleanParameter



94
95
96
97
98
99
100
101
# File 'lib/eac_git/local.rb', line 94

def rev_parse(ref, required = false) # rubocop:disable Style/OptionalBooleanParameter
  r = command('rev-parse', ref).execute!(exit_outputs: { 128 => nil, 32_768 => nil })
  r.strip! if r.is_a?(String)
  return r if r.present?
  return nil unless required

  raise_error "Reference \"#{ref}\" not found"
end

#subrepo(subpath) ⇒ Object



103
104
105
# File 'lib/eac_git/local.rb', line 103

def subrepo(subpath)
  ::EacGit::Local::Subrepo.new(self, subpath)
end

#subreposArray<EacGit::Local::Subrepo>

Returns:



108
109
110
111
# File 'lib/eac_git/local.rb', line 108

def subrepos
  command('subrepo', '-q', 'status').execute!.split("\n").map(&:strip).select(&:present?)
    .map { |subpath| subrepo(subpath) }
end

#to_sObject



113
114
115
# File 'lib/eac_git/local.rb', line 113

def to_s
  "#{self.class}[#{root_path}]"
end