Documentation
¶
Overview ¶
Package swhid provides functionality for computing and parsing Software Heritage Identifiers (SWHIDs).
SWHIDs are intrinsic identifiers for digital objects (source code files, directories, commits, etc.) based on cryptographic hashes. This package implements the SWHID specification v1.
Basic usage:
// Compute SWHID for file content
id := swhid.FromContent([]byte("hello world"))
fmt.Println(id) // swh:1:cnt:...
// Parse an existing SWHID
id, err := swhid.Parse("swh:1:cnt:94a9ed024d3859793618152ea559a168bbcbb5e2")
Index ¶
- Constants
- Variables
- type Identifier
- func FromContent(data []byte) *Identifier
- func FromDirectory(entries []objects.DirectoryEntry) *Identifier
- func FromDirectoryPath(path string) (*Identifier, error)
- func FromDirectoryPathWithOptions(path string, gitRepo *git.Repository, permissions map[string]os.FileMode) (*Identifier, error)
- func FromRelease(repoPath, tagName string) (*Identifier, error)
- func FromReleaseMetadata(meta objects.ReleaseMetadata) *Identifier
- func FromRevision(repoPath, ref string) (*Identifier, error)
- func FromRevisionMetadata(meta objects.RevisionMetadata) *Identifier
- func FromSnapshot(repoPath string) (*Identifier, error)
- func FromSnapshotBranches(branches []objects.Branch) *Identifier
- func NewIdentifier(objectType ObjectType, objectHash string, qualifiers map[string]string) (*Identifier, error)
- func Parse(swhidString string) (*Identifier, error)
- type ObjectType
Constants ¶
const ( Scheme = "swh" SchemeVersion = 1 ObjectIDLen = 40 )
Variables ¶
var ( ErrEmptySWHID = errors.New("SWHID string cannot be nil or empty") ErrInvalidFormat = errors.New("invalid SWHID format") ErrInvalidScheme = errors.New("invalid scheme") ErrInvalidVersion = errors.New("invalid version") ErrInvalidObjectType = errors.New("invalid object type") ErrInvalidObjectHash = errors.New("invalid object hash") )
Error types
Functions ¶
This section is empty.
Types ¶
type Identifier ¶
type Identifier struct {
Scheme string
Version int
ObjectType ObjectType
ObjectHash string
Qualifiers map[string]string
}
Identifier represents a parsed SWHID.
func FromContent ¶
func FromContent(data []byte) *Identifier
FromContent computes the SWHID for file content.
func FromDirectory ¶
func FromDirectory(entries []objects.DirectoryEntry) *Identifier
FromDirectory computes the SWHID for a directory with the given entries.
func FromDirectoryPath ¶
func FromDirectoryPath(path string) (*Identifier, error)
FromDirectoryPath computes the SWHID for a directory on the filesystem. It recursively hashes all files and subdirectories. If the directory is within a Git repository, it uses the Git index for file permissions.
func FromDirectoryPathWithOptions ¶
func FromDirectoryPathWithOptions(path string, gitRepo *git.Repository, permissions map[string]os.FileMode) (*Identifier, error)
FromDirectoryPathWithOptions computes the SWHID with custom options. gitRepo can be provided to use Git index for permissions. permissions can be provided as a map of path -> mode for explicit permissions.
func FromRelease ¶
func FromRelease(repoPath, tagName string) (*Identifier, error)
FromRelease computes the SWHID for a Git release (annotated tag).
func FromReleaseMetadata ¶
func FromReleaseMetadata(meta objects.ReleaseMetadata) *Identifier
FromReleaseMetadata computes the SWHID for a release with the given metadata.
func FromRevision ¶
func FromRevision(repoPath, ref string) (*Identifier, error)
FromRevision computes the SWHID for a Git revision (commit).
func FromRevisionMetadata ¶
func FromRevisionMetadata(meta objects.RevisionMetadata) *Identifier
FromRevisionMetadata computes the SWHID for a revision with the given metadata.
func FromSnapshot ¶
func FromSnapshot(repoPath string) (*Identifier, error)
FromSnapshot computes the SWHID for a Git repository snapshot.
func FromSnapshotBranches ¶
func FromSnapshotBranches(branches []objects.Branch) *Identifier
FromSnapshotBranches computes the SWHID for a snapshot with the given branches.
func NewIdentifier ¶
func NewIdentifier(objectType ObjectType, objectHash string, qualifiers map[string]string) (*Identifier, error)
NewIdentifier creates a new Identifier with validation.
func Parse ¶
func Parse(swhidString string) (*Identifier, error)
Parse parses a SWHID string into an Identifier.
func (*Identifier) CoreSWHID ¶
func (id *Identifier) CoreSWHID() string
CoreSWHID returns the core SWHID without qualifiers.
func (*Identifier) Equal ¶
func (id *Identifier) Equal(other *Identifier) bool
Equal returns true if two identifiers are equal.
func (*Identifier) String ¶
func (id *Identifier) String() string
String returns the canonical SWHID string representation.
func (*Identifier) WithQualifiers ¶
func (id *Identifier) WithQualifiers(qualifiers map[string]string) *Identifier
WithQualifiers returns a new Identifier with the given qualifiers.
type ObjectType ¶
type ObjectType string
ObjectType represents the type of object identified by a SWHID.
const ( ObjectTypeContent ObjectType = "cnt" ObjectTypeDirectory ObjectType = "dir" ObjectTypeRevision ObjectType = "rev" ObjectTypeRelease ObjectType = "rel" ObjectTypeSnapshot ObjectType = "snp" )