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

Commit 53a64ed

Browse files
committed
plumbing: packfile/scanner, add option to skip calculating checksum; use the option at place where the checksum is ignored anyway
Signed-off-by: Filip Navara <[email protected]>
1 parent aa6f288 commit 53a64ed

File tree

3 files changed

+38
-8
lines changed

3 files changed

+38
-8
lines changed

plumbing/format/packfile/packfile.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func NewPackfileWithCache(
5050
file billy.File,
5151
cache cache.Object,
5252
) *Packfile {
53-
s := NewScanner(file)
53+
s := NewScannerWithOptions(file, ScannerOptions{NoChecksum: true})
5454
return &Packfile{
5555
index,
5656
fs,

plumbing/format/packfile/scanner.go

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,30 @@ type Scanner struct {
5353
IsSeekable bool
5454
}
5555

56-
// NewScanner returns a new Scanner based on a reader, if the given reader
57-
// implements io.ReadSeeker the Scanner will be also Seekable
58-
func NewScanner(r io.Reader) *Scanner {
56+
// ScannerOptions specifies options for use by NewScannerWithOptions
57+
type ScannerOptions struct {
58+
// Avoid calculating the CRC checksums
59+
NoChecksum bool
60+
}
61+
62+
// NewScannerWithOptions returns a new Scanner based on a reader, if
63+
// the given reader implements io.ReadSeeker the Scanner will be also
64+
// Seekable. Additional options can be specified to disable checksum
65+
// calculation.
66+
func NewScannerWithOptions(r io.Reader, ops ScannerOptions) *Scanner {
5967
seeker, ok := r.(io.ReadSeeker)
6068
if !ok {
6169
seeker = &trackableReader{Reader: r}
6270
}
6371

72+
if ops.NoChecksum {
73+
return &Scanner{
74+
r: newByteReadSeeker(seeker),
75+
crc: nil,
76+
IsSeekable: ok,
77+
}
78+
}
79+
6480
crc := crc32.NewIEEE()
6581
return &Scanner{
6682
r: newTeeReader(newByteReadSeeker(seeker), crc),
@@ -69,6 +85,12 @@ func NewScanner(r io.Reader) *Scanner {
6985
}
7086
}
7187

88+
// NewScanner returns a new Scanner based on a reader, if the given reader
89+
// implements io.ReadSeeker the Scanner will be also Seekable
90+
func NewScanner(r io.Reader) *Scanner {
91+
return NewScannerWithOptions(r, ScannerOptions{})
92+
}
93+
7294
// Header reads the whole packfile header (signature, version and object count).
7395
// It returns the version and the object count and performs checks on the
7496
// validity of the signature and the version fields.
@@ -184,7 +206,9 @@ func (s *Scanner) NextObjectHeader() (*ObjectHeader, error) {
184206
func (s *Scanner) nextObjectHeader() (*ObjectHeader, error) {
185207
defer s.Flush()
186208

187-
s.crc.Reset()
209+
if s.crc != nil {
210+
s.crc.Reset()
211+
}
188212

189213
h := &ObjectHeader{}
190214
s.pendingObject = h
@@ -304,12 +328,18 @@ func (s *Scanner) readLength(first byte) (int64, error) {
304328
// NextObject writes the content of the next object into the reader, returns
305329
// the number of bytes written, the CRC32 of the content and an error, if any
306330
func (s *Scanner) NextObject(w io.Writer) (written int64, crc32 uint32, err error) {
307-
defer s.crc.Reset()
331+
if s.crc != nil {
332+
s.crc.Reset()
333+
}
308334

309335
s.pendingObject = nil
310336
written, err = s.copyObject(w)
311337
s.Flush()
312-
crc32 = s.crc.Sum32()
338+
if s.crc != nil {
339+
crc32 = s.crc.Sum32()
340+
} else {
341+
crc32 = 0
342+
}
313343
return
314344
}
315345

storage/filesystem/object.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ func (s *ObjectStorage) decodeDeltaObjectAt(
415415
return nil, err
416416
}
417417

418-
p := packfile.NewScanner(f)
418+
p := packfile.NewScannerWithOptions(f, packfile.ScannerOptions{NoChecksum: true})
419419
header, err := p.SeekObjectHeader(offset)
420420
if err != nil {
421421
return nil, err

0 commit comments

Comments
 (0)