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)


292
293
294
295
296
297
298
299
300
301
# 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

Instance Attribute Details

#dstString

The destination (post-change) blob SHA



265
266
267
# File 'lib/git/diff.rb', line 265

def dst
  @dst
end

#modeString

The file mode



253
254
255
# File 'lib/git/diff.rb', line 253

def mode
  @mode
end

#patchString?

The raw diff patch text for this file



241
242
243
# File 'lib/git/diff.rb', line 241

def patch
  @patch
end

#pathString?

The file path relative to the repository root



247
248
249
# File 'lib/git/diff.rb', line 247

def path
  @path
end

#srcString

The source (pre-change) blob SHA



259
260
261
# File 'lib/git/diff.rb', line 259

def src
  @src
end

#typeString

The type of change



271
272
273
# File 'lib/git/diff.rb', line 271

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


310
311
312
# File 'lib/git/diff.rb', line 310

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 ...>


328
329
330
331
# 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