util

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 31, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

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

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. Files must have non-empty ContentHash (deleted files are excluded). The hash is computed as SHA256 of sorted entries in format: "{mode} {path}\0{content_hash}"

func ConfigPath

func ConfigPath(repoRoot string) string

ConfigPath returns the path to the config file.

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.

func HashFile

func HashFile(path string) (string, error)

HashFile computes SHA256 hash of a file and returns 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 added in v1.1.0

func RelativeTime(t time.Time) string

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

func RelativeTimeShort added in v1.1.0

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 added in v1.1.0

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 added in v1.1.0

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 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 (*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 string
}

TreeEntry represents a file in the tree for hashing.

Jump to

Keyboard shortcuts

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