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/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, Commit, Remote, Subrepo

Constant Summary collapse

HEAD_REFERENCE =
'HEAD'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.find(path) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/eac_git/local.rb', line 15

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



29
30
31
# File 'lib/eac_git/local.rb', line 29

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:



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

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

#command(*args) ⇒ Object



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

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

#commit(ref, required = false) ⇒ Object

rubocop:disable Style/OptionalBooleanParameter



41
42
43
# File 'lib/eac_git/local.rb', line 41

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



45
46
47
48
49
50
51
52
53
# File 'lib/eac_git/local.rb', line 45

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:



58
59
60
61
62
63
# File 'lib/eac_git/local.rb', line 58

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)


65
66
67
68
69
70
71
# File 'lib/eac_git/local.rb', line 65

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:



74
75
76
# File 'lib/eac_git/local.rb', line 74

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

#merge_base(*commits) ⇒ Object



78
79
80
81
82
83
84
85
86
87
# File 'lib/eac_git/local.rb', line 78

def merge_base(*commits)
  refs = commits.dup
  while refs.count > 1
    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



93
94
95
# File 'lib/eac_git/local.rb', line 93

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

#rev_parse(ref, required = false) ⇒ Object

rubocop:disable Style/OptionalBooleanParameter



97
98
99
100
101
102
103
104
# File 'lib/eac_git/local.rb', line 97

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



106
107
108
# File 'lib/eac_git/local.rb', line 106

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

#subreposArray<EacGit::Local::Subrepo>

Returns:



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

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

#to_sObject



116
117
118
# File 'lib/eac_git/local.rb', line 116

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