Skip to content
This repository was archived by the owner on Sep 11, 2020. It is now read-only.

packfile: A copy operation cannot be bigger than 64kb #411

Merged
merged 1 commit into from
Jun 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions plumbing/format/packfile/delta_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package packfile

import (
"fmt"
"math/rand"

. "gopkg.in/check.v1"
)
Expand Down Expand Up @@ -61,9 +62,25 @@ func (s *DeltaSuite) SetUpSuite(c *C) {
{"4", 400}, {"5", 23}},
target: []piece{{"1", 30}, {"2", 20}, {"7", 40}, {"4", 400},
{"5", 10}},
}, {
description: "A copy operation bigger tan 64kb",
base: []piece{{bigRandStr, 1}, {"1", 200}},
target: []piece{{bigRandStr, 1}},
}}
}

var bigRandStr = randStringBytes(100 * 1024)

const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

func randStringBytes(n int) string {
b := make([]byte, n)
for i := range b {
b[i] = letterBytes[rand.Intn(len(letterBytes))]
}
return string(b)
}

func (s *DeltaSuite) TestAddDelta(c *C) {
for _, t := range s.testCases {
baseBuf := genBytes(t.base)
Expand Down
19 changes: 18 additions & 1 deletion plumbing/format/packfile/diff_delta.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ import (
const (
// Standard chunk size used to generate fingerprints
s = 16

// https://github.com/git/git/blob/f7466e94375b3be27f229c78873f0acf8301c0a5/diff-delta.c#L428
// Max size of a copy operation (64KB)
maxCopySize = 64 * 1024
)

// GetDelta returns an EncodedObject of type OFSDeltaObject. Base and Target object,
Expand Down Expand Up @@ -70,7 +74,20 @@ func DiffDelta(src []byte, tgt []byte) []byte {
ibuf.WriteByte(tgt[i])
} else {
encodeInsertOperation(ibuf, buf)
buf.Write(encodeCopyOperation(offset, l))

rl := l
aOffset := offset
for {
if rl < maxCopySize {
buf.Write(encodeCopyOperation(aOffset, rl))
break
}

buf.Write(encodeCopyOperation(aOffset, maxCopySize))
rl -= maxCopySize
aOffset += maxCopySize
}

i += l - 1
}
}
Expand Down