| 1 |
package packfile |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| •5 |
"compress/zlib" |
| 6 |
"io" |
| 7 |
|
| 8 |
"github.com/go-git/go-git/v5/plumbing" |
| 9 |
"github.com/go-git/go-git/v5/plumbing/format/idxfile" |
| 10 |
"github.com/go-git/go-git/v5/plumbing/hash" |
| 11 |
"github.com/go-git/go-git/v5/storage/memory" |
| 12 |
|
| 13 |
"github.com/go-git/go-billy/v5/memfs" |
| 14 |
fixtures "github.com/go-git/go-git-fixtures/v4" |
| 15 |
. "gopkg.in/check.v1" |
| 16 |
) |
| 17 |
|
| 18 |
type EncoderSuite struct { |
| 19 |
fixtures.Suite |
| 20 |
buf *bytes.Buffer |
| 21 |
store *memory.Storage |
| 22 |
enc *Encoder |
| 23 |
} |
| 24 |
|
| 25 |
var _ = Suite(&EncoderSuite{}) |
| 26 |
|
| 27 |
func (s *EncoderSuite) SetUpTest(c *C) { |
| 28 |
s.buf = bytes.NewBuffer(nil) |
| 29 |
s.store = memory.NewStorage() |
| 30 |
s.enc = NewEncoder(s.buf, s.store, false) |
| 31 |
} |
| 32 |
|
| 33 |
func (s *EncoderSuite) TestCorrectPackHeader(c *C) { |
| 34 |
h, err := s.enc.Encode([]plumbing.Hash{}, 10) |
| 35 |
c.Assert(err, IsNil) |
| 36 |
|
| 37 |
hb := [hash.Size]byte(h) |
| 38 |
|
| 39 |
// PACK + VERSION + OBJECTS + HASH |
| 40 |
expectedResult := []byte{'P', 'A', 'C', 'K', 0, 0, 0, 2, 0, 0, 0, 0} |
| 41 |
expectedResult = append(expectedResult, hb[:]...) |
| 42 |
|
| 43 |
result := s.buf.Bytes() |
| 44 |
|
| 45 |
c.Assert(result, DeepEquals, expectedResult) |
| 46 |
} |
| 47 |
|
| 48 |
func (s *EncoderSuite) TestCorrectPackWithOneEmptyObject(c *C) { |
| 49 |
o := &plumbing.MemoryObject{} |
| 50 |
o.SetType(plumbing.CommitObject) |
| 51 |
o.SetSize(0) |
| 52 |
_, err := s.store.SetEncodedObject(o) |
| 53 |
c.Assert(err, IsNil) |
| 54 |
|
| 55 |
h, err := s.enc.Encode([]plumbing.Hash{o.Hash()}, 10) |
| 56 |
c.Assert(err, IsNil) |
| 57 |
|
| 58 |
// PACK + VERSION(2) + OBJECT NUMBER(1) |
| 59 |
expectedResult := []byte{'P', 'A', 'C', 'K', 0, 0, 0, 2, 0, 0, 0, 1} |
| 60 |
// OBJECT HEADER(TYPE + SIZE)= 0001 0000 |
| 61 |
expectedResult = append(expectedResult, []byte{16}...) |
| 62 |
|
| •63 |
// Zlib stream of the empty object contents. The exact bytes depend on the |
| 64 |
// compress/flate implementation, so compress it here instead of hardcoding. |
| 65 |
var zbuf bytes.Buffer |
| 66 |
zw := zlib.NewWriter(&zbuf) |
| 67 |
c.Assert(zw.Close(), IsNil) |
| 68 |
expectedResult = append(expectedResult, zbuf.Bytes()...) |
| 69 |
|
| 70 |
// + HASH |
| 71 |
hb := [hash.Size]byte(h) |
| 72 |
expectedResult = append(expectedResult, hb[:]...) |
| 73 |
|
| 74 |
result := s.buf.Bytes() |
| 75 |
|
| 76 |
c.Assert(result, DeepEquals, expectedResult) |
| 77 |
} |
| 78 |
|
| 79 |
func (s *EncoderSuite) TestMaxObjectSize(c *C) { |
| 80 |
o := s.store.NewEncodedObject() |
| 81 |
o.SetSize(9223372036854775807) |
| 82 |
o.SetType(plumbing.CommitObject) |
| 83 |
_, err := s.store.SetEncodedObject(o) |
| 84 |
c.Assert(err, IsNil) |
| 85 |
hash, err := s.enc.Encode([]plumbing.Hash{o.Hash()}, 10) |
| 86 |
c.Assert(err, IsNil) |
| 87 |
c.Assert(hash.IsZero(), Not(Equals), true) |
| 88 |
} |
| 89 |
|
| 90 |
func (s *EncoderSuite) TestHashNotFound(c *C) { |
| 91 |
h, err := s.enc.Encode([]plumbing.Hash{plumbing.NewHash("BAD")}, 10) |
| 92 |
c.Assert(h, Equals, plumbing.ZeroHash) |
| 93 |
c.Assert(err, NotNil) |
| 94 |
c.Assert(err, Equals, plumbing.ErrObjectNotFound) |
| 95 |
} |
| 96 |
|
| 97 |
func (s *EncoderSuite) TestDecodeEncodeWithDeltaDecodeREF(c *C) { |
| 98 |
s.enc = NewEncoder(s.buf, s.store, true) |
| 99 |
s.simpleDeltaTest(c) |
| 100 |
} |
| 101 |
|
| 102 |
func (s *EncoderSuite) TestDecodeEncodeWithDeltaDecodeOFS(c *C) { |
| 103 |
s.enc = NewEncoder(s.buf, s.store, false) |
| 104 |
s.simpleDeltaTest(c) |
| 105 |
} |
| 106 |
|
| 107 |
func (s *EncoderSuite) TestDecodeEncodeWithDeltasDecodeREF(c *C) { |
| 108 |
s.enc = NewEncoder(s.buf, s.store, true) |
| 109 |
s.deltaOverDeltaTest(c) |
| 110 |
} |
| 111 |
|
| 112 |
func (s *EncoderSuite) TestDecodeEncodeWithDeltasDecodeOFS(c *C) { |
| 113 |
s.enc = NewEncoder(s.buf, s.store, false) |
| 114 |
s.deltaOverDeltaTest(c) |
| 115 |
} |
| 116 |
|
| 117 |
func (s *EncoderSuite) TestDecodeEncodeWithCycleREF(c *C) { |
| 118 |
s.enc = NewEncoder(s.buf, s.store, true) |
| 119 |
s.deltaOverDeltaCyclicTest(c) |
| 120 |
} |
| 121 |
|
| 122 |
func (s *EncoderSuite) TestDecodeEncodeWithCycleOFS(c *C) { |
| 123 |
s.enc = NewEncoder(s.buf, s.store, false) |
| 124 |
s.deltaOverDeltaCyclicTest(c) |
| 125 |
} |
| 126 |
|
| 127 |
func (s *EncoderSuite) simpleDeltaTest(c *C) { |
| 128 |
srcObject := newObject(plumbing.BlobObject, []byte("0")) |
| 129 |
targetObject := newObject(plumbing.BlobObject, []byte("01")) |
| 130 |
|
| 131 |
deltaObject, err := GetDelta(srcObject, targetObject) |
| 132 |
c.Assert(err, IsNil) |
| 133 |
|
| 134 |
srcToPack := newObjectToPack(srcObject) |
| 135 |
encHash, err := s.enc.encode([]*ObjectToPack{ |
| 136 |
srcToPack, |
| 137 |
newDeltaObjectToPack(srcToPack, targetObject, deltaObject), |
| 138 |
}) |
| 139 |
c.Assert(err, IsNil) |
| 140 |
|
| 141 |
p, cleanup := packfileFromReader(c, s.buf) |
| 142 |
defer cleanup() |
| 143 |
decHash, err := p.ID() |
| 144 |
c.Assert(err, IsNil) |
| 145 |
|
| 146 |
c.Assert(encHash, Equals, decHash) |
| 147 |
|
| 148 |
decSrc, err := p.Get(srcObject.Hash()) |
| 149 |
c.Assert(err, IsNil) |
| 150 |
objectsEqual(c, decSrc, srcObject) |
| 151 |
|
| 152 |
decTarget, err := p.Get(targetObject.Hash()) |
| 153 |
c.Assert(err, IsNil) |
| 154 |
objectsEqual(c, decTarget, targetObject) |
| 155 |
} |
| 156 |
|
| 157 |
func (s *EncoderSuite) deltaOverDeltaTest(c *C) { |
| 158 |
srcObject := newObject(plumbing.BlobObject, []byte("0")) |
| 159 |
targetObject := newObject(plumbing.BlobObject, []byte("01")) |
| 160 |
otherTargetObject := newObject(plumbing.BlobObject, []byte("011111")) |
| 161 |
|
| 162 |
deltaObject, err := GetDelta(srcObject, targetObject) |
| 163 |
c.Assert(err, IsNil) |
| 164 |
c.Assert(deltaObject.Hash(), Not(Equals), plumbing.ZeroHash) |
| 165 |
|
| 166 |
otherDeltaObject, err := GetDelta(targetObject, otherTargetObject) |
| 167 |
c.Assert(err, IsNil) |
| 168 |
c.Assert(otherDeltaObject.Hash(), Not(Equals), plumbing.ZeroHash) |
| 169 |
|
| 170 |
srcToPack := newObjectToPack(srcObject) |
| 171 |
targetToPack := newObjectToPack(targetObject) |
| 172 |
encHash, err := s.enc.encode([]*ObjectToPack{ |
| 173 |
targetToPack, |
| 174 |
srcToPack, |
| 175 |
newDeltaObjectToPack(srcToPack, targetObject, deltaObject), |
| 176 |
newDeltaObjectToPack(targetToPack, otherTargetObject, otherDeltaObject), |
| 177 |
}) |
| 178 |
c.Assert(err, IsNil) |
| 179 |
|
| 180 |
p, cleanup := packfileFromReader(c, s.buf) |
| 181 |
defer cleanup() |
| 182 |
decHash, err := p.ID() |
| 183 |
c.Assert(err, IsNil) |
| 184 |
|
| 185 |
c.Assert(encHash, Equals, decHash) |
| 186 |
|
| 187 |
decSrc, err := p.Get(srcObject.Hash()) |
| 188 |
c.Assert(err, IsNil) |
| 189 |
objectsEqual(c, decSrc, srcObject) |
| 190 |
|
| 191 |
decTarget, err := p.Get(targetObject.Hash()) |
| 192 |
c.Assert(err, IsNil) |
| 193 |
objectsEqual(c, decTarget, targetObject) |
| 194 |
|
| 195 |
decOtherTarget, err := p.Get(otherTargetObject.Hash()) |
| 196 |
c.Assert(err, IsNil) |
| 197 |
objectsEqual(c, decOtherTarget, otherTargetObject) |
| 198 |
} |
| 199 |
|
| 200 |
func (s *EncoderSuite) deltaOverDeltaCyclicTest(c *C) { |
| 201 |
o1 := newObject(plumbing.BlobObject, []byte("0")) |
| 202 |
o2 := newObject(plumbing.BlobObject, []byte("01")) |
| 203 |
o3 := newObject(plumbing.BlobObject, []byte("011111")) |
| 204 |
o4 := newObject(plumbing.BlobObject, []byte("01111100000")) |
| 205 |
|
| 206 |
_, err := s.store.SetEncodedObject(o1) |
| 207 |
c.Assert(err, IsNil) |
| 208 |
_, err = s.store.SetEncodedObject(o2) |
| 209 |
c.Assert(err, IsNil) |
| 210 |
_, err = s.store.SetEncodedObject(o3) |
| 211 |
c.Assert(err, IsNil) |
| 212 |
_, err = s.store.SetEncodedObject(o4) |
| 213 |
c.Assert(err, IsNil) |
| 214 |
|
| 215 |
d2, err := GetDelta(o1, o2) |
| 216 |
c.Assert(err, IsNil) |
| 217 |
|
| 218 |
d3, err := GetDelta(o4, o3) |
| 219 |
c.Assert(err, IsNil) |
| 220 |
|
| 221 |
d4, err := GetDelta(o3, o4) |
| 222 |
c.Assert(err, IsNil) |
| 223 |
|
| 224 |
po1 := newObjectToPack(o1) |
| 225 |
pd2 := newDeltaObjectToPack(po1, o2, d2) |
| 226 |
pd3 := newObjectToPack(o3) |
| 227 |
pd4 := newObjectToPack(o4) |
| 228 |
|
| 229 |
pd3.SetDelta(pd4, d3) |
| 230 |
pd4.SetDelta(pd3, d4) |
| 231 |
|
| 232 |
// SetOriginal is used by delta selector when generating ObjectToPack. |
| 233 |
// It also fills type, hash and size values to be used when Original |
| 234 |
// is nil. |
| 235 |
po1.SetOriginal(po1.Original) |
| 236 |
pd2.SetOriginal(pd2.Original) |
| 237 |
pd2.CleanOriginal() |
| 238 |
|
| 239 |
pd3.SetOriginal(pd3.Original) |
| 240 |
pd3.CleanOriginal() |
| 241 |
|
| 242 |
pd4.SetOriginal(pd4.Original) |
| 243 |
|
| 244 |
encHash, err := s.enc.encode([]*ObjectToPack{ |
| 245 |
po1, |
| 246 |
pd2, |
| 247 |
pd3, |
| 248 |
pd4, |
| 249 |
}) |
| 250 |
c.Assert(err, IsNil) |
| 251 |
|
| 252 |
p, cleanup := packfileFromReader(c, s.buf) |
| 253 |
defer cleanup() |
| 254 |
decHash, err := p.ID() |
| 255 |
c.Assert(err, IsNil) |
| 256 |
|
| 257 |
c.Assert(encHash, Equals, decHash) |
| 258 |
|
| 259 |
decSrc, err := p.Get(o1.Hash()) |
| 260 |
c.Assert(err, IsNil) |
| 261 |
objectsEqual(c, decSrc, o1) |
| 262 |
|
| 263 |
decTarget, err := p.Get(o2.Hash()) |
| 264 |
c.Assert(err, IsNil) |
| 265 |
objectsEqual(c, decTarget, o2) |
| 266 |
|
| 267 |
decOtherTarget, err := p.Get(o3.Hash()) |
| 268 |
c.Assert(err, IsNil) |
| 269 |
objectsEqual(c, decOtherTarget, o3) |
| 270 |
|
| 271 |
decAnotherTarget, err := p.Get(o4.Hash()) |
| 272 |
c.Assert(err, IsNil) |
| 273 |
objectsEqual(c, decAnotherTarget, o4) |
| 274 |
} |
| 275 |
|
| 276 |
func objectsEqual(c *C, o1, o2 plumbing.EncodedObject) { |
| 277 |
c.Assert(o1.Type(), Equals, o2.Type()) |
| 278 |
c.Assert(o1.Hash(), Equals, o2.Hash()) |
| 279 |
c.Assert(o1.Size(), Equals, o2.Size()) |
| 280 |
|
| 281 |
r1, err := o1.Reader() |
| 282 |
c.Assert(err, IsNil) |
| 283 |
|
| 284 |
b1, err := io.ReadAll(r1) |
| 285 |
c.Assert(err, IsNil) |
| 286 |
|
| 287 |
r2, err := o2.Reader() |
| 288 |
c.Assert(err, IsNil) |
| 289 |
|
| 290 |
b2, err := io.ReadAll(r2) |
| 291 |
c.Assert(err, IsNil) |
| 292 |
|
| 293 |
c.Assert(bytes.Compare(b1, b2), Equals, 0) |
| 294 |
|
| 295 |
err = r2.Close() |
| 296 |
c.Assert(err, IsNil) |
| 297 |
|
| 298 |
err = r1.Close() |
| 299 |
c.Assert(err, IsNil) |
| 300 |
} |
| 301 |
|
| 302 |
func packfileFromReader(c *C, buf *bytes.Buffer) (*Packfile, func()) { |
| 303 |
fs := memfs.New() |
| 304 |
file, err := fs.Create("packfile") |
| 305 |
c.Assert(err, IsNil) |
| 306 |
|
| 307 |
_, err = file.Write(buf.Bytes()) |
| 308 |
c.Assert(err, IsNil) |
| 309 |
|
| 310 |
_, err = file.Seek(0, io.SeekStart) |
| 311 |
c.Assert(err, IsNil) |
| 312 |
|
| 313 |
scanner := NewScanner(file) |
| 314 |
|
| 315 |
w := new(idxfile.Writer) |
| 316 |
p, err := NewParser(scanner, w) |
| 317 |
c.Assert(err, IsNil) |
| 318 |
|
| 319 |
_, err = p.Parse() |
| 320 |
c.Assert(err, IsNil) |
| 321 |
|
| 322 |
index, err := w.Index() |
| 323 |
c.Assert(err, IsNil) |
| 324 |
|
| 325 |
return NewPackfile(index, fs, file, 0), func() { |
| 326 |
c.Assert(file.Close(), IsNil) |
| 327 |
} |
| 328 |
} |