class Git::Diff::DiffFile
Information about a single changed file within a {Git::Diff}
@example Access diff file information
diff.each do |file| puts file.path puts file.binary? ? 'binary' : file.patch end
@api public
Constants
- NIL_BLOB_REGEXP
-
Regexp matching a nil blob SHA (all-zero hash of 4 to 40 hex digits)
Attributes
The destination (post-change) blob SHA
@return [String] the destination blob SHA
The file mode
@return [String] the octal file mode (e.g. ‘“100644”`)
The raw diff patch text for this file
@return [String, nil] the patch text
The file path relative to the repository root
@return [String, nil] the file path
The source (pre-change) blob SHA
@return [String] the source blob SHA
The type of change
@return [String] the change type (e.g. ‘“modified”`, `“new”`, `“deleted”`)
Public Class Methods
Source
# File lib/git/diff.rb, line 292 def initialize(base, hash) @base = base @patch = hash[:patch] @path = hash[:path] @mode = hash[:mode] @src = hash[:src] @dst = hash[:dst] @type = hash[:type] @binary = hash[:binary] end
Creates a new DiffFile from parsed diff data
@example
file = Git::Diff::DiffFile.new(base, patch: "diff --git ...", path: 'lib/git.rb', mode: '100644', src: 'abc123', dst: 'def456', type: 'modified', binary: false)
@param base [Git::Repository] the git repository
@param hash [Hash] the parsed diff attributes
@return [void]
Public Instance Methods
Source
# File lib/git/diff.rb, line 310 def binary? !!@binary end
Returns true if this file is a binary file
@example
diff['path/to/image.png'].binary? # => true
@return [Boolean] ‘true` if the file is binary, `false` otherwise
Source
# File lib/git/diff.rb, line 328 def blob(type = :dst) sha = type == :src ? @src : @dst @base.object(sha) unless NIL_BLOB_REGEXP.match(sha) end
Returns the blob object for this file
@example Retrieve the destination blob
file.blob # => #<Git::Object::Blob ...>
@example Retrieve the source blob
file.blob(:src) # => #<Git::Object::Blob ...>
@param type [Symbol] ‘:src` to retrieve the source blob, or `:dst`
(default) for the destination blob
@return [Git::Object::Blob, nil] the blob object, or ‘nil` if the blob
SHA is the null SHA