diff options
| author | 2026-06-11 07:08:34 +0000 | |
|---|---|---|
| committer | 2026-06-11 07:09:10 +0000 | |
| commit | ac41d9a2db7511695df464f8d8bbaadbf8ec6791 (patch) | |
| tree | 11f93af470f9508cc963f96fc542a1c6d791220c /internal/format | |
| parent | object/typ: Remove Type prefix from object/typ.Type literals (diff) | |
internal/format/packfile: Add EntryType and associates
Diffstat (limited to 'internal/format')
| -rw-r--r-- | internal/format/packfile/entry_type.go | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/internal/format/packfile/entry_type.go b/internal/format/packfile/entry_type.go new file mode 100644 index 00000000..784f9b69 --- /dev/null +++ b/internal/format/packfile/entry_type.go @@ -0,0 +1,67 @@ +package packfile + +import ( + "errors" + + "lindenii.org/go/furgit/object/typ" +) + +var ( + // ErrInternalEntryType reports that + // a supplied packfile [EntryType] + // cannot be converted into an ordinary [typ.Type] + // since it is a packfile implementation detail. + ErrInternalEntryType = errors.New("internal/format/packfile: packfile-internal entry type cannot be converted to an object type") + + // ErrUnrepresentableObjectType reports that + // a supplied ordinary [typ.Type] + // is not currently representable + // as a packfile [EntryType]. + ErrUnrepresentableObjectType = errors.New("internal/format/packfile: object type not representable in packfiles") +) + +// EntryType represents the type of an entry in a git packfile. +type EntryType uint8 + +const ( + EntryTypeInvalid EntryType = 0 + EntryTypeCommit EntryType = 1 + EntryTypeTree EntryType = 2 + EntryTypeBlob EntryType = 3 + EntryTypeTag EntryType = 4 + EntryTypeFuture EntryType = 5 + EntryTypeOfsDelta EntryType = 6 + EntryTypeRefDelta EntryType = 7 +) + +// ObjectTypeToEntryType converts an ordinary [typ.Type] into a packfile [EntryType]. +func ObjectTypeToEntryType(ty typ.Type) (EntryType, error) { + switch ty { + case typ.Commit: + return EntryTypeCommit, nil + case typ.Tree: + return EntryTypeTree, nil + case typ.Blob: + return EntryTypeBlob, nil + case typ.Tag: + return EntryTypeTag, nil + } + + return EntryTypeInvalid, ErrUnrepresentableObjectType +} + +// ObjectTypeToEntryType converts a a packfile [EntryType] into an ordinary [typ.Type]. +func (entryType EntryType) EntryTypeToObjectType() (typ.Type, error) { + switch entryType { + case EntryTypeCommit: + return typ.Commit, nil + case EntryTypeTree: + return typ.Tree, nil + case EntryTypeBlob: + return typ.Blob, nil + case EntryTypeTag: + return typ.Tag, nil + } + + return typ.Unknown, ErrInternalEntryType +} |
