diff options
| author | 2026-04-02 07:59:50 +0000 | |
|---|---|---|
| committer | 2026-04-02 07:59:50 +0000 | |
| commit | f33cae96a9d7418f8512398c9411130eaebe4e26 (patch) | |
| tree | b2541e7cf4cf28b996ac50c416cf821251ca6662 /object | |
| parent | ci: Stricter linting (diff) | |
| signature | No signature | |
object/typ: Implement object types
Diffstat (limited to 'object')
| -rw-r--r-- | object/typ/doc.go | 5 | ||||
| -rw-r--r-- | object/typ/type.go | 38 | ||||
| -rw-r--r-- | object/typ/type_details.go | 30 | ||||
| -rw-r--r-- | object/typ/type_ops.go | 19 | ||||
| -rw-r--r-- | object/typ/type_parse.go | 8 |
5 files changed, 100 insertions, 0 deletions
diff --git a/object/typ/doc.go b/object/typ/doc.go new file mode 100644 index 00000000..155d60eb --- /dev/null +++ b/object/typ/doc.go @@ -0,0 +1,5 @@ +// Package typ provides Git object type enums and names. +// +// I would prefer naming this package "type" +// if it wasn't a keyword. +package typ diff --git a/object/typ/type.go b/object/typ/type.go new file mode 100644 index 00000000..69a07de3 --- /dev/null +++ b/object/typ/type.go @@ -0,0 +1,38 @@ +package typ + +// Type represents a Git object type. +// +// The values currently mirror what's found in the Git packfile format. +// +// TODO: Revisit this. +type Type uint8 + +const ( + // TypeInvalid represents an invalid Git object type. + TypeInvalid Type = 0 + + // TypeCommit represents a Git commit. + TypeCommit Type = 1 + + // TypeTree represents a Git tree. + TypeTree Type = 2 + + // TypeBlob represents a Git blob. + TypeBlob Type = 3 + + // TypeTag represents a Git tag. + TypeTag Type = 4 + + // TypeFuture is reserved for the future, just like in the packfile format. + TypeFuture Type = 5 + + // TypeOfsDelta is reserved for internal use in packfile handlers. + // + // TODO: Revisit this. + TypeOfsDelta Type = 6 + + // TypeRefDelta is reserved for internal use in packfile handlers. + // + // TODO: Revisit this. + TypeRefDelta Type = 7 +) diff --git a/object/typ/type_details.go b/object/typ/type_details.go new file mode 100644 index 00000000..48dcf829 --- /dev/null +++ b/object/typ/type_details.go @@ -0,0 +1,30 @@ +package typ + +type typeDetails struct { + name string + isBaseObject bool +} + +func (ty Type) details() typeDetails { + return typeTable[ty] +} + +//nolint:gochecknoglobals +var typeTable = [...]typeDetails{ + TypeInvalid: {name: "", isBaseObject: false}, + TypeCommit: {name: "commit", isBaseObject: true}, + TypeTree: {name: "tree", isBaseObject: true}, + TypeBlob: {name: "blob", isBaseObject: true}, + TypeTag: {name: "tag", isBaseObject: true}, + TypeFuture: {name: "", isBaseObject: false}, + TypeOfsDelta: {name: "", isBaseObject: false}, + TypeRefDelta: {name: "", isBaseObject: false}, +} + +//nolint:gochecknoglobals +var typeByName = map[string]Type{ + typeTable[TypeCommit].name: TypeCommit, + typeTable[TypeTree].name: TypeTree, + typeTable[TypeBlob].name: TypeBlob, + typeTable[TypeTag].name: TypeTag, +} diff --git a/object/typ/type_ops.go b/object/typ/type_ops.go new file mode 100644 index 00000000..82e68147 --- /dev/null +++ b/object/typ/type_ops.go @@ -0,0 +1,19 @@ +package typ + +// IsBaseObject reports whether ty is +// one of the four ordinary Git object types. +// +// TODO: This should probably be removed. +func (ty Type) IsBaseObject() bool { + return ty.details().isBaseObject +} + +// Name returns the canonical Git object type name. +func (ty Type) Name() (string, bool) { + details := ty.details() + if details.name == "" { + return "", false + } + + return details.name, true +} diff --git a/object/typ/type_parse.go b/object/typ/type_parse.go new file mode 100644 index 00000000..a39a1949 --- /dev/null +++ b/object/typ/type_parse.go @@ -0,0 +1,8 @@ +package typ + +// Parse parses a canonical Git object type name. +func Parse(name string) (Type, bool) { + ty, ok := typeByName[name] + + return ty, ok +} |
