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/commit.rb,
lib/eac_git/local/remote.rb,
lib/eac_git/local/subrepo.rb,
lib/eac_git/local/dirty_files.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.

Defined Under Namespace

Modules: DirtyFiles, Log Classes: 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

#command(*args) ⇒ Object



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

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

#commit(ref, required = false) ⇒ Object



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

def commit(ref, required = false)
  rev_parse(ref, required).if_present { |v| ::EacGit::Local::Commit.new(self, v) }
end

#commitize(source) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/eac_git/local.rb', line 37

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

#descendant?(descendant, ancestor) ⇒ Boolean

Returns:

  • (Boolean)


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

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:



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

def head(required = true)
  commit(HEAD_REFERENCE, required)
end

#merge_base(*commits) ⇒ Object



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

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



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

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

#rev_parse(ref, required = false) ⇒ Object



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

def rev_parse(ref, required = false)
  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



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

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

#subreposArray<EacGit::Local::Subrepo>

Returns:



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

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

#to_sObject



98
99
100
# File 'lib/eac_git/local.rb', line 98

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