Class: FormatStaged::Entry

Inherits:
Object
  • Object
show all
Defined in:
lib/format-staged/entry.rb

Overview

Entry in the git index.

Data as produced by ‘git diff-index`

Constant Summary collapse

PATTERN =
/^
:(?<src_mode>\d+)\s
(?<dst_mode>\d+)\s
(?<src_hash>[a-f0-9]+)\s
(?<dst_hash>[a-f0-9]+)\s
(?<status>[A-Z])(?<score>\d+)?\t
(?<src_path>[^\t]+)
(?:\t(?<dst_path>[^\t]+))?
$/x.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(line, root:) ⇒ Entry

Returns a new instance of Entry.



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/format-staged/entry.rb', line 21

def initialize(line, root:)
  matches = line.match(PATTERN) or raise "Cannot parse output #{line}"
  @src_mode = matches[:src_mode]
  @dst_mode = matches[:dst_mode]
  @src_hash = matches[:src_hash]
  @dst_hash = matches[:dst_hash]
  @status = matches[:status]
  @score = matches[:score]&.to_i
  @src_path = matches[:src_path]
  @dst_path = matches[:dst_path]
  @path = File.expand_path(@src_path, root)
  @root = root
end

Instance Attribute Details

#dst_hashObject (readonly)

Returns the value of attribute dst_hash.



19
20
21
# File 'lib/format-staged/entry.rb', line 19

def dst_hash
  @dst_hash
end

#dst_modeObject (readonly)

Returns the value of attribute dst_mode.



19
20
21
# File 'lib/format-staged/entry.rb', line 19

def dst_mode
  @dst_mode
end

#dst_pathObject (readonly)

Returns the value of attribute dst_path.



19
20
21
# File 'lib/format-staged/entry.rb', line 19

def dst_path
  @dst_path
end

#pathObject (readonly)

Returns the value of attribute path.



19
20
21
# File 'lib/format-staged/entry.rb', line 19

def path
  @path
end

#rootObject (readonly)

Returns the value of attribute root.



19
20
21
# File 'lib/format-staged/entry.rb', line 19

def root
  @root
end

#scoreObject (readonly)

Returns the value of attribute score.



19
20
21
# File 'lib/format-staged/entry.rb', line 19

def score
  @score
end

#src_hashObject (readonly)

Returns the value of attribute src_hash.



19
20
21
# File 'lib/format-staged/entry.rb', line 19

def src_hash
  @src_hash
end

#src_modeObject (readonly)

Returns the value of attribute src_mode.



19
20
21
# File 'lib/format-staged/entry.rb', line 19

def src_mode
  @src_mode
end

#src_pathObject (readonly)

Returns the value of attribute src_path.



19
20
21
# File 'lib/format-staged/entry.rb', line 19

def src_path
  @src_path
end

#statusObject (readonly)

Returns the value of attribute status.



19
20
21
# File 'lib/format-staged/entry.rb', line 19

def status
  @status
end

Instance Method Details

#matches?(patterns) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
42
43
44
45
# File 'lib/format-staged/entry.rb', line 39

def matches?(patterns)
  result = false
  patterns.each do |pattern|
    result = true if File.fnmatch? pattern, path, File::FNM_EXTGLOB
  end
  result
end

#symlink?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/format-staged/entry.rb', line 35

def symlink?
  @dst_mode == '120000'
end