Class: Amp::Core::Repositories::Git::Encoding::BinaryDelta

Inherits:
Object
  • Object
show all
Defined in:
lib/amp-git/encoding/binary_delta.rb

Overview

Handles the binary delta format that are found in Git’s packfiles. The format is detailed in the link above.

Defined Under Namespace

Classes: CopyHunk, Hunk, InsertHunk

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(delta) ⇒ BinaryDelta

Parses a git-format binary delta from a string.

Parameters:

  • delta (String)

    the delta to parse



41
42
43
44
45
46
47
48
49
# File 'lib/amp-git/encoding/binary_delta.rb', line 41

def initialize(delta)
  fp = StringIO.new(delta)
  @base_length = read_little_endian_base128(fp)
  @result_length = read_little_endian_base128(fp)
  @hunks = []
  while !fp.eof?
    @hunks << Hunk.parse(fp)
  end
end

Instance Attribute Details

#base_lengthObject

Returns the value of attribute base_length.



35
36
37
# File 'lib/amp-git/encoding/binary_delta.rb', line 35

def base_length
  @base_length
end

#hunksObject

Returns the value of attribute hunks.



35
36
37
# File 'lib/amp-git/encoding/binary_delta.rb', line 35

def hunks
  @hunks
end

#result_lengthObject

Returns the value of attribute result_length.



35
36
37
# File 'lib/amp-git/encoding/binary_delta.rb', line 35

def result_length
  @result_length
end

Instance Method Details

#apply(original) ⇒ Object

Applies the binary delta to an original text. Returns the patched data.

Parameters:

  • original (String)

    the text to patch with the delta

Returns:

  • the patched data



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/amp-git/encoding/binary_delta.rb', line 57

def apply(original)
  if original.size != @base_length
    raise DeltaError.new("Expected input data to be #{@base_length} bytes, but was #{original.size} bytes.")
  end
  output = StringIO.new
  output.string.force_encoding('BINARY') if output.string.respond_to?(:force_encoding)
  @hunks.each do |hunk|
    hunk.apply(output, original)
  end
  if output.string.size != @result_length
    raise DeltaError.new("Expected patched data to be #{@result_length} bytes, but was #{output.string.size} bytes.")
  end
  output.string
end