diff options
| author | 2026-05-30 05:13:09 +0000 | |
|---|---|---|
| committer | 2026-05-30 05:13:54 +0000 | |
| commit | 86a2a381cd99fcceef24587d95cddbe94c149482 (patch) | |
| tree | 177837c9c72be3376c1f6e91670fa66c61512b4f | |
| parent | ci: Simplify with a local script (diff) | |
| signature | No signature | |
object/id: Parse should belong to object format next
| -rw-r--r-- | internal/testgit/object.go | 2 | ||||
| -rw-r--r-- | object/commit/parse.go | 4 | ||||
| -rw-r--r-- | object/id/object_format_ops.go | 49 | ||||
| -rw-r--r-- | object/id/object_id_parse.go | 49 | ||||
| -rw-r--r-- | object/id/object_id_test.go | 34 |
5 files changed, 68 insertions, 70 deletions
diff --git a/internal/testgit/object.go b/internal/testgit/object.go index 5d2a38ec..a46ef02b 100644 --- a/internal/testgit/object.go +++ b/internal/testgit/object.go @@ -19,7 +19,7 @@ func (repo *Repo) HashObject(tb testing.TB, ty typ.Type, body io.Reader) id.Obje tb.Fatalf("hash-object: %v", hex) } - id, err := id.FromHex(repo.objectFormat, string(hex)) + id, err := repo.objectFormat.FromString(string(hex)) if err != nil { tb.Fatalf("parse git hash-object output %q: %v", hex, err) } diff --git a/object/commit/parse.go b/object/commit/parse.go index 1212423f..1864055e 100644 --- a/object/commit/parse.go +++ b/object/commit/parse.go @@ -37,14 +37,14 @@ func Parse(body []byte, objectFormat id.ObjectFormat) (*Commit, error) { switch string(key) { case "tree": - id, err := id.FromHex(objectFormat, string(value)) + id, err := objectFormat.FromString(string(value)) if err != nil { return nil, fmt.Errorf("object: commit: tree: %w", err) } c.Tree = id case "parent": - id, err := id.FromHex(objectFormat, string(value)) + id, err := objectFormat.FromString(string(value)) if err != nil { return nil, fmt.Errorf("object: commit: parent: %w", err) } diff --git a/object/id/object_format_ops.go b/object/id/object_format_ops.go index 76077235..51bcae82 100644 --- a/object/id/object_format_ops.go +++ b/object/id/object_format_ops.go @@ -1,6 +1,10 @@ package id -import "hash" +import ( + "encoding/hex" + "fmt" + "hash" +) // HexLen returns the encoded hexadecimal length. func (objectFormat ObjectFormat) HexLen() int { @@ -22,6 +26,49 @@ func (objectFormat ObjectFormat) New() (hash.Hash, error) { return newFn(), nil } +// FromBytes builds an object ID from raw bytes for this object format. +func (objectFormat ObjectFormat) FromBytes(b []byte) (ObjectID, error) { + var id ObjectID + if objectFormat.Size() == 0 { + return id, ErrInvalidObjectFormat + } + + if len(b) != objectFormat.Size() { + return id, fmt.Errorf("%w: got %d bytes, expected %d", ErrInvalidObjectID, len(b), objectFormat.Size()) + } + + copy(id.data[:], b) + id.objectFormat = objectFormat + + return id, nil +} + +// FromString parses an object ID from its canonical hex representation. +func (objectFormat ObjectFormat) FromString(s string) (ObjectID, error) { + var id ObjectID + if objectFormat.Size() == 0 { + return id, ErrInvalidObjectFormat + } + + if len(s)&1 != 0 { + return id, fmt.Errorf("%w: odd hex length %d", ErrInvalidObjectID, len(s)) + } + + if len(s) != objectFormat.HexLen() { + return id, fmt.Errorf("%w: got %d chars, expected %d", ErrInvalidObjectID, len(s), objectFormat.HexLen()) + } + + decoded, err := hex.DecodeString(s) + if err != nil { + return id, fmt.Errorf("%w: decode: %w", ErrInvalidObjectID, err) + } + + copy(id.data[:], decoded) + id.objectFormat = objectFormat + + return id, nil +} + // String returns the canonical object format name. func (objectFormat ObjectFormat) String() string { return objectFormat.details().name diff --git a/object/id/object_id_parse.go b/object/id/object_id_parse.go deleted file mode 100644 index 59b6669c..00000000 --- a/object/id/object_id_parse.go +++ /dev/null @@ -1,49 +0,0 @@ -package id - -import ( - "encoding/hex" - "fmt" -) - -// FromBytes builds an object ID from raw bytes for the specified object format. -func FromBytes(objectFormat ObjectFormat, b []byte) (ObjectID, error) { - var id ObjectID - if objectFormat.Size() == 0 { - return id, ErrInvalidObjectFormat - } - - if len(b) != objectFormat.Size() { - return id, fmt.Errorf("%w: got %d bytes, expected %d", ErrInvalidObjectID, len(b), objectFormat.Size()) - } - - copy(id.data[:], b) - id.objectFormat = objectFormat - - return id, nil -} - -// FromHex parses an object ID from hex for the specified object format. -func FromHex(objectFormat ObjectFormat, s string) (ObjectID, error) { - var id ObjectID - if objectFormat.Size() == 0 { - return id, ErrInvalidObjectFormat - } - - if len(s)%2 != 0 { - return id, fmt.Errorf("%w: odd hex length %d", ErrInvalidObjectID, len(s)) - } - - if len(s) != objectFormat.HexLen() { - return id, fmt.Errorf("%w: got %d chars, expected %d", ErrInvalidObjectID, len(s), objectFormat.HexLen()) - } - - decoded, err := hex.DecodeString(s) - if err != nil { - return id, fmt.Errorf("%w: decode: %w", ErrInvalidObjectID, err) - } - - copy(id.data[:], decoded) - id.objectFormat = objectFormat - - return id, nil -} diff --git a/object/id/object_id_test.go b/object/id/object_id_test.go index 5d6130d2..40957692 100644 --- a/object/id/object_id_test.go +++ b/object/id/object_id_test.go @@ -11,13 +11,13 @@ import ( func TestFromBytesErrors(t *testing.T) { t.Parallel() - _, err := id.FromBytes(id.ObjectFormatUnknown, []byte{1, 2}) + _, err := id.ObjectFormatUnknown.FromBytes([]byte{1, 2}) if err == nil { t.Fatalf("expected FromBytes unknown object format error") } for _, objectFormat := range id.SupportedObjectFormats() { - _, err = id.FromBytes(objectFormat, []byte{1, 2}) + _, err = objectFormat.FromBytes([]byte{1, 2}) if err == nil { t.Fatalf("expected FromBytes wrong size error") } @@ -28,9 +28,9 @@ func TestBytesReturnsCopy(t *testing.T) { t.Parallel() for _, objectFormat := range id.SupportedObjectFormats() { - id, err := id.FromHex(objectFormat, strings.Repeat("01", objectFormat.Size())) + id, err := objectFormat.FromString(strings.Repeat("01", objectFormat.Size())) if err != nil { - t.Fatalf("ParseHex failed: %v", err) + t.Fatalf("FromString failed: %v", err) } b1 := id.Bytes() @@ -47,15 +47,15 @@ func TestBytesReturnsCopy(t *testing.T) { } } -func TestFromHexErrors(t *testing.T) { +func TestFromStringErrors(t *testing.T) { t.Parallel() t.Run("unknown object format", func(t *testing.T) { t.Parallel() - _, err := id.FromHex(id.ObjectFormatUnknown, "00") + _, err := id.ObjectFormatUnknown.FromString("00") if err == nil { - t.Fatalf("expected FromHex error") + t.Fatalf("expected FromString error") } }) // TODO: This may need to be revisited when hash-function-transition is implemented. @@ -64,25 +64,25 @@ func TestFromHexErrors(t *testing.T) { t.Run(objectFormat.String(), func(t *testing.T) { t.Parallel() - _, err := id.FromHex(objectFormat, strings.Repeat("0", objectFormat.HexLen()-1)) + _, err := objectFormat.FromString(strings.Repeat("0", objectFormat.HexLen()-1)) if err == nil { - t.Fatalf("expected FromHex odd-len error") + t.Fatalf("expected FromString odd-len error") } - _, err = id.FromHex(objectFormat, strings.Repeat("0", objectFormat.HexLen()-2)) + _, err = objectFormat.FromString(strings.Repeat("0", objectFormat.HexLen()-2)) if err == nil { - t.Fatalf("expected FromHex wrong-len error") + t.Fatalf("expected FromString wrong-len error") } - _, err = id.FromHex(objectFormat, "z"+strings.Repeat("0", objectFormat.HexLen()-1)) + _, err = objectFormat.FromString("z" + strings.Repeat("0", objectFormat.HexLen()-1)) if err == nil { - t.Fatalf("expected FromHex invalid-hex error") + t.Fatalf("expected FromString invalid-hex error") } }) } } -func TestFromHexRoundtrip(t *testing.T) { +func TestFromStringRoundtrip(t *testing.T) { t.Parallel() for _, objectFormat := range id.SupportedObjectFormats() { @@ -91,9 +91,9 @@ func TestFromHexRoundtrip(t *testing.T) { hex := strings.Repeat("01", objectFormat.Size()) - i, err := id.FromHex(objectFormat, hex) + i, err := objectFormat.FromString(hex) if err != nil { - t.Fatalf("FromHex failed: %v", err) + t.Fatalf("FromString failed: %v", err) } if got := i.String(); got != hex { @@ -109,7 +109,7 @@ func TestFromHexRoundtrip(t *testing.T) { t.Fatalf("Bytes len = %d, want %d", len(raw), objectFormat.Size()) } - id2, err := id.FromBytes(objectFormat, raw) + id2, err := objectFormat.FromBytes(raw) if err != nil { t.Fatalf("FromBytes failed: %v", err) } |
