aboutsummaryrefslogtreecommitdiffstats
path: root/plumbing/format/index
diff options
context:
space:
mode:
authorAlexander Block <ablock84@gmail.com>2024-04-06 09:26:42 +0200
committerAlexander Block <ablock84@gmail.com>2024-04-06 09:29:13 +0200
commit4cf29e8ac6ad5a7861a6f8eb70cff57fda6ebefe (patch)
tree6e5646636c8f5adf1c6eea8f662d9a31cdd82010 /plumbing/format/index
parent8a3757ce0b63e842f1e466955f0a4cc2eed429eb (diff)
downloadgo-git-4cf29e8ac6ad5a7861a6f8eb70cff57fda6ebefe.tar.gz
plumbing: Implement encodeRawExtension that can be used by tests
Diffstat (limited to 'plumbing/format/index')
-rw-r--r--plumbing/format/index/encoder.go34
1 files changed, 33 insertions, 1 deletions
diff --git a/plumbing/format/index/encoder.go b/plumbing/format/index/encoder.go
index fa2d814..c292c2c 100644
--- a/plumbing/format/index/encoder.go
+++ b/plumbing/format/index/encoder.go
@@ -3,6 +3,7 @@ package index
import (
"bytes"
"errors"
+ "fmt"
"io"
"sort"
"time"
@@ -35,6 +36,11 @@ func NewEncoder(w io.Writer) *Encoder {
// Encode writes the Index to the stream of the encoder.
func (e *Encoder) Encode(idx *Index) error {
+ return e.encode(idx, true)
+}
+
+func (e *Encoder) encode(idx *Index, footer bool) error {
+
// TODO: support v4
// TODO: support extensions
if idx.Version > EncodeVersionSupported {
@@ -49,7 +55,10 @@ func (e *Encoder) Encode(idx *Index) error {
return err
}
- return e.encodeFooter()
+ if footer {
+ return e.encodeFooter()
+ }
+ return nil
}
func (e *Encoder) encodeHeader(idx *Index) error {
@@ -135,6 +144,29 @@ func (e *Encoder) encodeEntry(entry *Entry) error {
return binary.Write(e.w, []byte(entry.Name))
}
+func (e *Encoder) encodeRawExtension(signature string, data []byte) error {
+ if len(signature) != 4 {
+ return fmt.Errorf("invalid signature length")
+ }
+
+ _, err := e.w.Write([]byte(signature))
+ if err != nil {
+ return err
+ }
+
+ err = binary.WriteUint32(e.w, uint32(len(data)))
+ if err != nil {
+ return err
+ }
+
+ _, err = e.w.Write(data)
+ if err != nil {
+ return err
+ }
+
+ return nil
+}
+
func (e *Encoder) timeToUint32(t *time.Time) (uint32, uint32, error) {
if t.IsZero() {
return 0, 0, nil