On this page
Package lock provides ref-lock primitives for concurrent ref updates using O_CREAT|O_EXCL for atomic lock file creation and exponential backoff polling.
#internal/lock
#internal/lock
go:build !windows
#RefLock
type RefLock structRefLock represents an acquired lock on a git ref.
#Acquire
func Acquire(locksBaseDir, safegitDir, ref, op string, timeout time.Duration) (*RefLock, error)Acquire attempts to acquire a lock on the given ref. locksBaseDir is the safegit directory whose "locks/" subtree holds lock files; for worktrees this should be the shared (common) safegit dir so that all worktrees serialize on the same lock. safegitDir is the worktree-local safegit dir used for oplog writes (stale-lock recovery events). It uses O_CREAT|O_EXCL for atomic creation. If the lock is held by a dead process, it is automatically replaced. Uses exponential backoff polling bounded by timeout.
#IsStale
func IsStale(path string) (bool, error)IsStale checks whether the process that holds the lock file is dead. Returns (true, nil) if the lock is stale and can be reclaimed. A corrupt or zero-length lock file (no parseable PID) is treated as stale.
Hardening checks beyond simple PID liveness: - If the lock contains a host= field that differs from the local hostname, refuse to reclaim (the PID belongs to a different machine's namespace). - On Linux, if the process started after the lock was created, the PID was reused by a new process and the lock is stale.
#ForceRelease
func ForceRelease(locksBaseDir, ref string) errorForceRelease unconditionally removes the lock file for a ref. locksBaseDir is the safegit directory whose "locks/" subtree holds lock files; for worktrees this should be the shared (common) safegit dir.
#ParsePID
func ParsePID(lockPath string) (int, error)ParsePID reads the lock file and extracts the pid= value.
#RefLock.Release
func (l *RefLock) Release() errorRelease removes the lock file.