Class: GitHooks::Repository::DiffIndexEntry

Inherits:
OpenStruct
  • Object
show all
Defined in:
lib/githooks/repository/diff_index_entry.rb

Defined Under Namespace

Classes: FileState

Constant Summary collapse

DIFF_STRUCTURE_REGEXP =
%r{
  ^:
  (?<original_mode>\d+)\s
  (?<new_mode>\d+)\s
  (?<original_sha>[a-f\d]+)\.*\s
  (?<new_sha>[a-f\d]+)\.*\s
  (?<change_type>.)
  (?:(?<score>\d+)?)\s
  (?<file_path>\S+)\s?
  (?<rename_path>\S+)?
}xi

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repo, entry) ⇒ DiffIndexEntry

Returns a new instance of DiffIndexEntry.



27
28
29
30
31
32
33
# File 'lib/githooks/repository/diff_index_entry.rb', line 27

def initialize(repo, entry)
  @repo = repo
  unless entry =~ DIFF_STRUCTURE_REGEXP
    fail ArgumentError, "Unable to parse incoming diff entry data: #{entry}"
  end
  super parse_data(entry)
end

Class Method Details

.from_file_path(repo, path, tracked = false) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/githooks/repository/diff_index_entry.rb', line 19

def self.from_file_path(repo, path, tracked = false)
  relative_path = Pathname.new(path)
  full_path = repo.path + relative_path
  entry_line = format(":%06o %06o %040x %040x %s\t%s",
                      0, full_path.stat.mode, 0, 0, (tracked ? '^' : '?'), relative_path.to_s)
  new(repo, entry_line)
end

Instance Method Details

#parse_data(entry) ⇒ Object

rubocop:disable MultilineOperationIndentation



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/githooks/repository/diff_index_entry.rb', line 36

def parse_data(entry) # rubocop:disable MethodLength, AbcSize
  data = Hash[
    DIFF_STRUCTURE_REGEXP.names.collect(&:to_sym).zip(
      entry.match(DIFF_STRUCTURE_REGEXP).captures
    )
  ]

  {
    from:  FileState.new(
      data[:original_mode].to_i(8),
      data[:original_sha],
      data[:file_path].nil? ? nil : Pathname.new(data[:file_path])
    ),
    to:    FileState.new(
      data[:new_mode].to_i(8),
      data[:new_sha],
      data[:rename_path].nil? ? nil : Pathname.new(data[:rename_path])
    ),
    type:  Repository::CHANGE_TYPES[data[:change_type]],
    score: data[:score].to_i
  }
end

#to_repo_fileObject

rubocop:enable MultilineOperationIndentation



60
61
62
# File 'lib/githooks/repository/diff_index_entry.rb', line 60

def to_repo_file
  Repository::File.new(@repo, self)
end