Class: Git::Diff::DiffFile

Inherits:
Object
  • Object
show all
Defined in:
lib/git/diff.rb

Overview

Information about a single changed file within a Git::Diff

Examples:

Access diff file information

diff.each do |file|
  puts file.path
  puts file.binary? ? 'binary' : file.patch
end

Constant Summary collapse

NIL_BLOB_REGEXP =

Regexp matching a nil blob SHA (all-zero hash of 4 to 40 hex digits)

/\A0{4,40}\z/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base, hash)

Creates a new DiffFile from parsed diff data

Examples:

file = Git::Diff::DiffFile.new(base,
  patch: "diff --git ...", path: 'lib/git.rb',
  mode: '100644', src: 'abc123', dst: 'def456',
  type: 'modified', binary: false)

Parameters:



296
297
298
299
300
301
302
303
304
305
# File 'lib/git/diff.rb', line 296

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

Instance Attribute Details

#dstString

The destination (post-change) blob SHA

Returns:

  • (String)

    the destination blob SHA



269
270
271
# File 'lib/git/diff.rb', line 269

def dst
  @dst
end

#modeString

The file mode

Returns:

  • (String)

    the octal file mode (e.g. "100644")



257
258
259
# File 'lib/git/diff.rb', line 257

def mode
  @mode
end

#patchString?

The raw diff patch text for this file

Returns:

  • (String, nil)

    the patch text



245
246
247
# File 'lib/git/diff.rb', line 245

def patch
  @patch
end

#pathString?

The file path relative to the repository root

Returns:

  • (String, nil)

    the file path



251
252
253
# File 'lib/git/diff.rb', line 251

def path
  @path
end

#srcString

The source (pre-change) blob SHA

Returns:

  • (String)

    the source blob SHA



263
264
265
# File 'lib/git/diff.rb', line 263

def src
  @src
end

#typeString

The type of change

Returns:

  • (String)

    the change type (e.g. "modified", "new", "deleted")



275
276
277
# File 'lib/git/diff.rb', line 275

def type
  @type
end

Instance Method Details

#binary?Boolean

Returns true if this file is a binary file

Examples:

diff['path/to/image.png'].binary? # => true

Returns:

  • (Boolean)

    true if the file is binary, false otherwise



314
315
316
# File 'lib/git/diff.rb', line 314

def binary?
  !!@binary
end

#blob(type = :dst) ⇒ Git::Object::Blob?

Returns the blob object for this file

Examples:

Retrieve the destination blob

file.blob       # => #<Git::Object::Blob ...>

Retrieve the source blob

file.blob(:src) # => #<Git::Object::Blob ...>

Parameters:

  • type (Symbol) (defaults to: :dst)

    :src to retrieve the source blob, or :dst (default) for the destination blob

Returns:

  • (Git::Object::Blob, nil)

    the blob object, or nil if the blob SHA is the null SHA



332
333
334
335
336
337
338
# File 'lib/git/diff.rb', line 332

def blob(type = :dst)
  if type == :src && !NIL_BLOB_REGEXP.match(@src)
    @base.object(@src)
  elsif !NIL_BLOB_REGEXP.match(@dst)
    @base.object(@dst)
  end
end