Class: Rfix::Repository

Inherits:
Dry::Struct
  • Object
show all
Includes:
Dry::Core::Constants, Dry::Core::Memoizable
Defined in:
lib/rfix/repository.rb

Constant Summary collapse

INCLUDED =
[File::Untracked, File::Tracked].to_set.freeze
INIT =
Hash.new { |h, k| h[k] = EMPTY_ARRAY.dup }.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.method_added(name) ⇒ Object



26
27
28
# File 'lib/rfix/repository.rb', line 26

def self.method_added(name)
  super.tap { memoize(name) }
end

Instance Method Details

#cacheObject



58
59
60
61
62
63
64
# File 'lib/rfix/repository.rb', line 58

def cache
  Concurrent::Map.new do |storage, path|
    storage.fetch(Types::Path::Absolute.call(path).to_s, File::Null)
  end.tap do |storage|
    files.each { |file| storage.compute_if_absent(file.key) { file } }
  end
end

#collectorObject



111
112
113
# File 'lib/rfix/repository.rb', line 111

def collector
  Collector.call(repository: self, reference: reference.name)
end

#deletedObject



50
51
52
# File 'lib/rfix/repository.rb', line 50

def deleted
  files.select(&:deleted?)
end

#filesObject



66
67
68
69
70
# File 'lib/rfix/repository.rb', line 66

def files
  Diff.new(repository: self, current_path: current_path).deltas.map do |delta|
    File::Tracked.call(**delta.new_file, repository: self, status: delta.status)
  end
end

#ignoredObject



42
43
44
# File 'lib/rfix/repository.rb', line 42

def ignored
  files.select(&:ignored?)
end

#include_file?(path, line = Undefined) ⇒ Boolean Also known as: contains?, include?

Returns:

  • (Boolean)


95
96
97
98
99
# File 'lib/rfix/repository.rb', line 95

def include_file?(path, line = Undefined)
  return cache[path].exists? if line == Undefined

  cache[path].include?(line)
end

#originObject

TODO: Refactor



105
106
107
108
109
# File 'lib/rfix/repository.rb', line 105

def origin
  repository.lookup(repository.rev_parse(reference.name).oid)
rescue Rugged::Error, Rugged::InvalidError, Rugged::ReferenceError
  raise Error, "Reference #{reference.name.inspect} not found"
end

#pathObject



54
55
56
# File 'lib/rfix/repository.rb', line 54

def path
  Pathname(workdir)
end

#pathsObject



91
92
93
# File 'lib/rfix/repository.rb', line 91

def paths
  permitted.map(&:key).to_a
end

#permittedObject



72
73
74
# File 'lib/rfix/repository.rb', line 72

def permitted
  files
end

#skippedObject



34
35
36
# File 'lib/rfix/repository.rb', line 34

def skipped
  ignored + deleted
end

#to_sObject Also known as: inspect



76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/rfix/repository.rb', line 76

def to_s
  files.each_with_object(INIT.dup) do |file, object|
    object[file.class] << file
  end.map do |type, files|
    "%<type>s[%<count>i]:%<files>s" % {
      files: files.map(&:to_s).join(", "),
      type: type.name.demodulize,
      count: files.count
    }
  end.then do |types|
    "Repository<%<types>s>" % { types: types.join(", ") }
  end
end

#trackedObject



38
39
40
# File 'lib/rfix/repository.rb', line 38

def tracked
  files.select(&:tracked?)
end

#untrackedObject



46
47
48
# File 'lib/rfix/repository.rb', line 46

def untracked
  files.select(&:untracked?)
end