util

package
v2.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 3, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const (
	PgitDir    = ".pgit"
	ConfigFile = "config.toml"
	IndexFile  = "index"
	HeadFile   = "HEAD"
)
View Source
const ContentHashSize = 16

ContentHashSize is the size of BLAKE3 content hashes (16 bytes)

Variables

View Source
var (
	ErrNotARepository      = errors.New("not a pgit repository (or any parent up to mount point)")
	ErrAlreadyInitialized  = errors.New("pgit repository already exists")
	ErrNoContainerRuntime  = errors.New("no container runtime found (docker or podman required)")
	ErrContainerNotRunning = errors.New("local container is not running")
	ErrDatabaseNotFound    = errors.New("database not found")
	ErrNoCommits           = errors.New("no commits yet")
	ErrNothingToCommit     = errors.New("nothing to commit (working tree clean)")
	ErrNothingStaged       = errors.New("nothing staged for commit")
	ErrUncommittedChanges  = errors.New("uncommitted changes would be overwritten")
	ErrMergeConflict       = errors.New("merge conflict detected")
	ErrRemoteNotFound      = errors.New("remote not found")
	ErrRemoteExists        = errors.New("remote already exists")
	ErrNotConnected        = errors.New("not connected to database")
	ErrInvalidCommitID     = errors.New("invalid commit ID")
	ErrCommitNotFound      = errors.New("commit not found")
	ErrFileNotFound        = errors.New("file not found")
	ErrPathNotInRepo       = errors.New("path is outside repository")
)

Common errors used throughout pgit

Functions

func AbsolutePath

func AbsolutePath(repoRoot, relPath string) string

AbsolutePath converts a relative path to an absolute path.

func ComputeTreeHash

func ComputeTreeHash(entries []TreeEntry) string

ComputeTreeHash computes the tree hash from a list of files using BLAKE3. Files must have non-empty ContentHash (deleted files are excluded). The hash is computed as BLAKE3 of sorted entries in format: "{mode} {path}\0{content_hash_bytes}"

func ComputeTreeHashLegacy

func ComputeTreeHashLegacy(entries []TreeEntryLegacy) string

ComputeTreeHashLegacy computes tree hash using the old SHA256 format. Deprecated: Only use for reading existing commits.

func ConfigPath

func ConfigPath(repoRoot string) string

ConfigPath returns the path to the config file.

func ContentHashEqual

func ContentHashEqual(a, b []byte) bool

ContentHashEqual compares two content hashes for equality. Handles nil hashes (nil == nil is true, nil != non-nil is true).

func ContentHashFromHex

func ContentHashFromHex(s string) ([]byte, error)

ContentHashFromHex parses a hex string to content hash. Returns nil for empty string.

func ContentHashToHex

func ContentHashToHex(hash []byte) string

ContentHashToHex converts a content hash to hex string for display. Returns empty string for nil hash.

func FileMode

func FileMode(path string) (int, error)

FileMode returns the Unix file mode as an integer.

func FindRepoRoot

func FindRepoRoot() (string, error)

FindRepoRoot walks up from the current directory to find .pgit directory. Returns the repository root path or error if not found.

func FindRepoRootFrom

func FindRepoRootFrom(start string) (string, error)

FindRepoRootFrom walks up from the given directory to find .pgit directory.

func HashBytes

func HashBytes(data []byte) string

HashBytes computes SHA256 hash of bytes and returns hex string. Deprecated: Use HashBytesBlake3 for new code.

func HashBytesBlake3

func HashBytesBlake3(data []byte) []byte

HashBytesBlake3 computes BLAKE3 hash of bytes and returns 16-byte slice. This is the primary hash function for content hashing in the new schema.

func HashBytesBlake3Hex

func HashBytesBlake3Hex(data []byte) string

HashBytesBlake3Hex computes BLAKE3 hash and returns 32-char hex string. Useful for debugging and display purposes.

func HashFile

func HashFile(path string) (string, error)

HashFile computes SHA256 hash of a file and returns hex string. Deprecated: Use HashFileBlake3 for new code.

func HashFileBlake3

func HashFileBlake3(path string) ([]byte, error)

HashFileBlake3 computes BLAKE3 hash of a file and returns 16-byte slice.

func HashFileBlake3Hex

func HashFileBlake3Hex(path string) (string, error)

HashFileBlake3Hex computes BLAKE3 hash of a file and returns 32-char hex string.

func HashPath

func HashPath(path string) string

HashPath generates a short hash of a path (for database naming).

func HeadPath

func HeadPath(repoRoot string) string

HeadPath returns the path to the HEAD file.

func IndexPath

func IndexPath(repoRoot string) string

IndexPath returns the path to the index file.

func IsBinaryFile

func IsBinaryFile(path string) (bool, error)

IsBinaryFile checks if a file is binary by looking for NUL bytes in the first 8KB.

func IsInsideRepo

func IsInsideRepo(repoRoot, path string) bool

IsInsideRepo checks if a path is inside the repository (not in .pgit).

func IsSymlink(path string) (bool, error)

IsSymlink checks if a path is a symbolic link.

func NewULID

func NewULID() string

NewULID generates a new ULID string. ULIDs are time-sortable unique identifiers.

func NewULIDWithTime

func NewULIDWithTime(t time.Time) string

NewULIDWithTime generates a ULID for a specific time. Useful for importing commits with preserved timestamps.

func ParseULID

func ParseULID(s string) (time.Time, error)

ParseULID parses a ULID string and returns its timestamp.

func PgitPath

func PgitPath(repoRoot string) string

PgitPath returns the path to the .pgit directory.

func ReadSymlink(path string) (string, error)

ReadSymlink returns the target of a symbolic link.

func RelativePath

func RelativePath(repoRoot, absPath string) (string, error)

RelativePath converts an absolute path to a path relative to the repo root.

func RelativeTime

func RelativeTime(t time.Time) string

RelativeTime formats a time as relative (e.g., "2 hours ago")

func RelativeTimeShort

func RelativeTimeShort(t time.Time) string

RelativeTimeShort formats a time as a short relative string (e.g., "2h ago")

func ShortID

func ShortID(id string) string

ShortID returns the last 7 characters of an ID in lowercase. For ULIDs, the last part has more entropy than the first (timestamp) part. Lowercase matches git's convention for short hashes.

func ToValidUTF8

func ToValidUTF8(s string) string

ToValidUTF8 ensures a string is valid UTF-8. If the string contains invalid UTF-8 sequences, it attempts to decode as Latin-1 (ISO-8859-1), which is a common encoding in older git repos. This preserves characters like ä, ö, ü, é, etc. instead of replacing them.

func ToValidUTF8Bytes

func ToValidUTF8Bytes(b []byte) []byte

ToValidUTF8Bytes ensures bytes represent valid UTF-8.

func ValidateULID

func ValidateULID(s string) bool

ValidateULID checks if a string is a valid ULID.

Types

type PgitError

type PgitError struct {
	Title       string   // Short error title
	Message     string   // Detailed message
	Context     string   // What was being attempted
	Causes      []string // Possible causes
	Suggestions []string // Actionable suggestions with commands
	Err         error    // Wrapped error
}

PgitError is a structured error with context and suggestions

func CommitNotFoundError

func CommitNotFoundError(ref string) *PgitError

CommitNotFoundError returns a structured error for missing commit

func ContainerNotRunningError

func ContainerNotRunningError() *PgitError

ContainerNotRunningError returns a structured error for stopped container

func DatabaseConnectionError

func DatabaseConnectionError(url string, err error) *PgitError

DatabaseConnectionError returns a structured error for DB connection issues

func MissingArgumentError added in v2.1.0

func MissingArgumentError(argName, example string) *PgitError

MissingArgumentError returns an error for missing required argument

func NewError

func NewError(title string) *PgitError

NewError creates a new PgitError

func NoContainerError

func NoContainerError() *PgitError

NoContainerError returns a structured error for missing container runtime

func NotARepoError

func NotARepoError() *PgitError

NotARepoError returns a structured error for "not a repository"

func RemoteNotFoundError

func RemoteNotFoundError(name string) *PgitError

RemoteNotFoundError returns a structured error for missing remote

func TooManyArgumentsError added in v2.1.0

func TooManyArgumentsError(expected int, got int) *PgitError

TooManyArgumentsError returns an error for too many arguments

func (*PgitError) Error

func (e *PgitError) Error() string

func (*PgitError) Format

func (e *PgitError) Format() string

Format returns a nicely formatted error message

func (*PgitError) Unwrap

func (e *PgitError) Unwrap() error

func (*PgitError) WithCause

func (e *PgitError) WithCause(cause string) *PgitError

WithCause adds a possible cause

func (*PgitError) WithCauses

func (e *PgitError) WithCauses(causes ...string) *PgitError

WithCauses adds multiple possible causes

func (*PgitError) WithContext

func (e *PgitError) WithContext(ctx string) *PgitError

WithContext adds context about what was being attempted

func (*PgitError) WithMessage

func (e *PgitError) WithMessage(msg string) *PgitError

WithMessage adds a detailed message

func (*PgitError) WithSuggestion

func (e *PgitError) WithSuggestion(sug string) *PgitError

WithSuggestion adds an actionable suggestion

func (*PgitError) WithSuggestions

func (e *PgitError) WithSuggestions(sugs ...string) *PgitError

WithSuggestions adds multiple suggestions

func (*PgitError) Wrap

func (e *PgitError) Wrap(err error) *PgitError

Wrap wraps an underlying error

type TreeEntry

type TreeEntry struct {
	Mode        int
	Path        string
	ContentHash []byte // 16 bytes BLAKE3 hash
}

TreeEntry represents a file in the tree for hashing.

type TreeEntryLegacy

type TreeEntryLegacy struct {
	Mode        int
	Path        string
	ContentHash string // SHA256 hex string
}

TreeEntryLegacy represents a file in the tree for hashing (legacy format). Used for backwards compatibility with existing commit tree hashes.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL