From ac41d9a2db7511695df464f8d8bbaadbf8ec6791 Mon Sep 17 00:00:00 2001 From: Runxi Yu Date: Thu, 11 Jun 2026 07:08:34 +0000 Subject: internal/format/packfile: Add EntryType and associates --- internal/format/packfile/entry_type.go | 67 ++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 internal/format/packfile/entry_type.go (limited to 'internal/format') 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 +} -- cgit v1.3.1-10-gc9f91