Class: Amp::Core::Repositories::Git::Encoding::BinaryDelta
- Inherits:
-
Object
- Object
- Amp::Core::Repositories::Git::Encoding::BinaryDelta
- 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
-
#base_length ⇒ Object
Returns the value of attribute base_length.
-
#hunks ⇒ Object
Returns the value of attribute hunks.
-
#result_length ⇒ Object
Returns the value of attribute result_length.
Instance Method Summary collapse
-
#apply(original) ⇒ Object
Applies the binary delta to an original text.
-
#initialize(delta) ⇒ BinaryDelta
constructor
Parses a git-format binary delta from a string.
Constructor Details
#initialize(delta) ⇒ BinaryDelta
Parses a git-format binary delta from a string.
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_length ⇒ Object
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 |
#hunks ⇒ Object
Returns the value of attribute hunks.
35 36 37 |
# File 'lib/amp-git/encoding/binary_delta.rb', line 35 def hunks @hunks end |
#result_length ⇒ Object
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.
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 |