Skip to content

plumbing: Properly encode index version 4 #1251

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 18, 2024
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
60 changes: 51 additions & 9 deletions plumbing/format/index/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import (
"errors"
"fmt"
"io"
"path"
"sort"
"strings"
"time"

"github.com/go-git/go-git/v5/plumbing/hash"
Expand All @@ -14,7 +16,7 @@ import (

var (
// EncodeVersionSupported is the range of supported index versions
EncodeVersionSupported uint32 = 3
EncodeVersionSupported uint32 = 4

// ErrInvalidTimestamp is returned by Encode if a Index with a Entry with
// negative timestamp values
Expand All @@ -23,15 +25,16 @@ var (

// An Encoder writes an Index to an output stream.
type Encoder struct {
w io.Writer
hash hash.Hash
w io.Writer
hash hash.Hash
lastEntry *Entry
}

// NewEncoder returns a new encoder that writes to w.
func NewEncoder(w io.Writer) *Encoder {
h := hash.New(hash.CryptoType)
mw := io.MultiWriter(w, h)
return &Encoder{mw, h}
return &Encoder{mw, h, nil}
}

// Encode writes the Index to the stream of the encoder.
Expand All @@ -41,7 +44,6 @@ func (e *Encoder) Encode(idx *Index) error {

func (e *Encoder) encode(idx *Index, footer bool) error {

// TODO: support v4
// TODO: support extensions
if idx.Version > EncodeVersionSupported {
return ErrUnsupportedVersion
Expand Down Expand Up @@ -73,7 +75,7 @@ func (e *Encoder) encodeEntries(idx *Index) error {
sort.Sort(byName(idx.Entries))

for _, entry := range idx.Entries {
if err := e.encodeEntry(entry); err != nil {
if err := e.encodeEntry(idx, entry); err != nil {
return err
}
entryLength := entryHeaderLength
Expand All @@ -82,15 +84,15 @@ func (e *Encoder) encodeEntries(idx *Index) error {
}

wrote := entryLength + len(entry.Name)
if err := e.padEntry(wrote); err != nil {
if err := e.padEntry(idx, wrote); err != nil {
return err
}
}

return nil
}

func (e *Encoder) encodeEntry(entry *Entry) error {
func (e *Encoder) encodeEntry(idx *Index, entry *Entry) error {
sec, nsec, err := e.timeToUint32(&entry.CreatedAt)
if err != nil {
return err
Expand Down Expand Up @@ -141,9 +143,45 @@ func (e *Encoder) encodeEntry(entry *Entry) error {
return err
}

switch idx.Version {
case 2, 3:
err = e.encodeEntryName(entry)
case 4:
err = e.encodeEntryNameV4(entry)
default:
err = ErrUnsupportedVersion
}

return err
}

func (e *Encoder) encodeEntryName(entry *Entry) error {
return binary.Write(e.w, []byte(entry.Name))
}

func (e *Encoder) encodeEntryNameV4(entry *Entry) error {
name := entry.Name
l := 0
if e.lastEntry != nil {
dir := path.Dir(e.lastEntry.Name) + "/"
if strings.HasPrefix(entry.Name, dir) {
l = len(e.lastEntry.Name) - len(dir)
name = strings.TrimPrefix(entry.Name, dir)
} else {
l = len(e.lastEntry.Name)
}
}

e.lastEntry = entry

err := binary.WriteVariableWidthInt(e.w, int64(l))
if err != nil {
return err
}

return binary.Write(e.w, []byte(name+string('\x00')))
}

func (e *Encoder) encodeRawExtension(signature string, data []byte) error {
if len(signature) != 4 {
return fmt.Errorf("invalid signature length")
Expand Down Expand Up @@ -179,7 +217,11 @@ func (e *Encoder) timeToUint32(t *time.Time) (uint32, uint32, error) {
return uint32(t.Unix()), uint32(t.Nanosecond()), nil
}

func (e *Encoder) padEntry(wrote int) error {
func (e *Encoder) padEntry(idx *Index, wrote int) error {
if idx.Version == 4 {
return nil
}

padLen := 8 - wrote%8

_, err := e.w.Write(bytes.Repeat([]byte{'\x00'}, padLen))
Expand Down
58 changes: 57 additions & 1 deletion plumbing/format/index/encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,64 @@ func (s *IndexSuite) TestEncode(c *C) {

}

func (s *IndexSuite) TestEncodeV4(c *C) {
idx := &Index{
Version: 4,
Entries: []*Entry{{
CreatedAt: time.Now(),
ModifiedAt: time.Now(),
Dev: 4242,
Inode: 424242,
UID: 84,
GID: 8484,
Size: 42,
Stage: TheirMode,
Hash: plumbing.NewHash("e25b29c8946e0e192fae2edc1dabf7be71e8ecf3"),
Name: "foo",
}, {
CreatedAt: time.Now(),
ModifiedAt: time.Now(),
Name: "bar",
Size: 82,
}, {
CreatedAt: time.Now(),
ModifiedAt: time.Now(),
Name: strings.Repeat(" ", 20),
Size: 82,
}, {
CreatedAt: time.Now(),
ModifiedAt: time.Now(),
Name: "baz/bar",
Size: 82,
}, {
CreatedAt: time.Now(),
ModifiedAt: time.Now(),
Name: "baz/bar/bar",
Size: 82,
}},
}

buf := bytes.NewBuffer(nil)
e := NewEncoder(buf)
err := e.Encode(idx)
c.Assert(err, IsNil)

output := &Index{}
d := NewDecoder(buf)
err = d.Decode(output)
c.Assert(err, IsNil)

c.Assert(cmp.Equal(idx, output), Equals, true)

c.Assert(output.Entries[0].Name, Equals, strings.Repeat(" ", 20))
c.Assert(output.Entries[1].Name, Equals, "bar")
c.Assert(output.Entries[2].Name, Equals, "baz/bar")
c.Assert(output.Entries[3].Name, Equals, "baz/bar/bar")
c.Assert(output.Entries[4].Name, Equals, "foo")
}

func (s *IndexSuite) TestEncodeUnsupportedVersion(c *C) {
idx := &Index{Version: 4}
idx := &Index{Version: 5}

buf := bytes.NewBuffer(nil)
e := NewEncoder(buf)
Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy