Skip to content
git-stuntsPublic

About

Path locks stored as git refs. Pure bash. JSON Lines out, a public schema, and one winner under contention.

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

title git-locks: cooperative path reservations
date 2026-09-15
author James Ross
description Advisory path locks for cooperative workers on one machine. Bash and Git, with JSON Lines output.
tags
git
locking
bash
concurrency
jsonl
draft false
status published
project git-stunts/locks
version 0.9.0
     ▄▄▄▄▄▄▄    ▄▄▄▄▄▄    ▄▄▄▄▄▄▄▄▄▄         ▄▄▄▄▄▄            ▄▄▄▄▄▄           ▄▄▄▄▄▄ ▄▄▄▄▄▄   ▄▄▄▄▄▄      ▄▄▄▄▄▄▄▄▄▄
  ▄██▀▀  ▀▀▀     ▓██▓  ▄░▄▀  ▓██▄  ▀▄░▄       ▒ ░▓          ▄██▀▀  ▀▀░▓▄     ▄██▀▀  ▀▀  ▓██▓     ▓██▓    ▄░▓░▀   ▓░█▓ 
 ░██▌            ▒█░▒        ▒██▓             ░█░▓         ░██▌      ▐░░▒   ░██▌        ▒█░▒    ▐▒█░▒   ▒░░▒          
▒██░  ▄▄▄▄▄▄▄▄▄  ▒ ░░        ▒██▓             ░ ░▓        ▒██░        ▒█░▒ ▒██░         ▒ ░░▄▄▄▀██ ▀     ▀▀░░▄▄▄▄     
▓█░░      ▓██▓   ▒██░        ▒██▒             ▒█░▓        ▓█░░        ▓██▒ ▓█░░         ▒██░   ▀▄███▄          ▀░██▄  
▓█░▒      ▒█░▓   ▒█░░        ▒██░             ▓██▓        ▓█░▒        ▓██░ ▓█░▒         ▒█░░     ▒█░░   ▒█░░     ▒█░░ 
 ▓▒▒▌     ███▓   ▓█░▒        ▓██▒             ▓██░      ▒  ▓▒▒▌      ▐░██   ▓▒▒▌        ▓█░▒     ▓█░▒   ▓█░▒    ▐██░▒ 
  ▀░▒▄▄  ▄███▓   ▓█░▓        ▓██▓             ▒██░▄▄▄▄▄░░   ▀░▒▄▄  ▄█░█▀     ▀░▒▄▄  ▄█  ▓█░▓     ▓█░▓   ▓█░▓   ▄███░▀ 
     ▀▀▀▀ ▀▀▀▀  ▀▀▀▀▀▀      ▀▀▀▀▀▀           ▀▀▀▀▀▀▀▀▀▀▀▀      ▀▀▀▀▀▀           ▀▀▀▀▀▀ ▀▀▀▀▀▀   ▀▀▀▀▀▀ ▀▀▀▀▀▀▀▀▀▀▀    

git-locks

Reserve paths before parallel workers change them. A refusal identifies the holder, job, and reason for the existing reservation.

Bash and Git provide the store. No daemon is required. Locks are advisory: every worker must acquire a lock and obey its expiry.

Install

Requirements: Bash 4.4+ (older Bash is refused at startup; see Bash compatibility), Git 2.39+ (every store read needs symbolic-ref --no-recurse; see Git compatibility for the tested releases and the supported files ref backend), and, for the terminal foreground handoff in git locks with, ps with pgid and tpgid support (Linux and macOS). Where the shell cannot execute ps (status 126 or 127), for example in a sandbox that forbids setuid binaries, with still runs and supervises its command, without handing it the terminal's foreground (see Wrapper lifetime). On macOS, put a newer Bash on PATH; /bin/bash 3.2 is insufficient.

From a release or reviewed checkout:

make install                   # copies the script to ~/.local/bin
export PATH="$HOME/.local/bin:$PATH"
git locks version

Use make install PREFIX=/your/prefix for another location. Installation copies a snapshot. Repeat the command to upgrade.

Quick start

Run this example from your project's root:

git locks with --job "report-$$" --holder "${USER:-worker}" \
  --note 'update the report' --ttl 60 --wait 10 report.md -- \
  sh -c 'printf "%s\n" "Ready" > report.md'

git locks list
git locks doctor

with acquires the paths, starts the command, and attempts release after it exits. A trailing / reserves a directory prefix. --wait 10 waits up to ten seconds for the locks. Without --wait, a conflict returns immediately.

git locks list --no-init reads the same selected store as acquisition without creating it. An absent store produces a store-missing event on stderr and exits

  1. An existing empty store exits 0 with no lease lines. Invalid or inaccessible stores exit 2.

with can acquire several semaphores and the path holds in one operation. Use --sem NAME for one slot, or --sem 'NAME*COUNT' for a count from one through 4,294,967,295. Supply each name once. Capacity refusal publishes no reservations and leaves the command unstarted.

Weighted slots use the git-locks-slot/2 record format. Upgrade every client that accesses a shared store before using weighted slots, and wait for every git locks with started by a 0.8.0 client to exit, or stop it. A client that reads only the old one-slot format, such as 0.8.0, rejects a weighted record instead of counting it as one slot. While the record remains, that client refuses the whole store, not only the weighted semaphore: doctor exits 1 with a sem-record finding, and every other command that reads the store exits 2 with store-read. A 0.8.0 with that was already running when the record appeared, and exits while it remains, exits 125 even when its command succeeded, and its reservations stay held until they expire or are released with git locks release --job JOB and git locks sem release NAME --job JOB. A 0.8.0 client also keeps only the last --sem, so run invocations with repeated --sem only through 0.9.0 or later. Decision record Clients 0.1.x and 0.2.x do not read the current store format at all and grant conflicting claims; never run them against it.

TTL is the reservation lifetime, in seconds. with does not renew automatically or stop the command at expiry. Keep work that holds semaphore slots within its TTL. Path reservations can use acquisition-guarded renewal. check only reports state; it does not grant a lock.

Workers must use the same store and path names. Linked worktrees share the default store, separate from project refs. For different repositories to share a resource, select the same explicit store.

When to use

  • Cooperative agents, scripts, and build workers on one machine.
  • Several paths that must be acquired together, or directory prefixes.
  • Visible ownership and refusal reasons, with JSON Lines output for tools.
  • A shared concurrency limit through a semaphore.

When not to use

Need Use instead
One simple local command lock util-linux flock, where available.
Coordination across machines A service such as etcd. The protected resource must reject writes from expired owners when required.
Protection against workers that ignore locks OS permissions or a sandbox. git-locks cannot prevent their writes.
Enforced CPU, memory, or disk limits OS/container limits and filesystem quotas. For example, Docker CPU and memory limits. Semaphores only count admissions.

All writers contend on one Git ref. Store scans grow with stored state. Measure your workload before use at high volume. Tests cover specified cases; they do not prove correctness for every possible execution.

Limits

No ordinary command runs Git garbage collection, so superseded state accumulates as unreachable objects until you maintain it. To maintain a dedicated store, stop every client, including with wrappers, and run git locks gc --offline: it refuses self mode, a store with refs outside refs/locks/, lock files, an unreadable or unhealthy store and too little free space before any write, then reclaims everything the current state does not reach. Run it when git locks store --objects reports more unreachable objects or KiB than you accept; nothing needs migrating first, and a store you never maintain behaves as before (maintenance). Run any other pruning maintenance, which is git gc with any prune horizon including the two-week default, git prune, git repack -a -d, or any git maintenance run, only after every client, including with wrappers, has stopped. A pruning gc beside running clients is unsupported: Git documents that a concurrent gc can delete an object another process is about to reference, which here could leave a published state naming a missing object, so the store refuses to open (exit 2, store-read) until it is restored from a backup or retired offline (offline recovery). Beside running clients, use only a non-deleting repack, such as git --git-dir=<store> repack -A -d, which packs live objects but frees little, because it keeps every unreachable object. Your own Git configuration applies to commands you run against the store, so check that its aliases and gc.*, repack.* and maintenance.* keys do not make them prune. In self mode the store is your project's own Git directory: ordinary commands such as git commit start automatic maintenance, and scheduled maintenance can run too, which with Git's default settings can delete unreachable objects older than the project's gc.pruneExpire; that breaks this rule while clients run. Use a dedicated store unless the project's automatic and scheduled maintenance cannot prune and nobody runs pruning commands in the project while clients run. Clients older than 0.3.0 do not refuse a current store; they grant conflicting claims, so never run them against it. Publication is atomic if a process crashes, though a writer killed mid-publication, or a gc killed while packing refs, can leave refs/locks/state.lock, which blocks writes until you confirm that process has stopped and remove it: with every client stopped, git locks recover --offline --backup <dir> backs up the store and removes it, along with the other damage that grants nothing. The store sets no fsync options (setting core.fsync in the store narrows the window but does not close it), so a power loss or operating-system crash can roll back recent reservations or leave a store that refuses to open; a job that resumes after the reboot can still believe it holds a reservation the rolled-back store no longer contains. The storage lifecycle decision states the full contract.

How it works

flowchart LR
    W["Workers"] -->|"read and conditionally update"| R["refs/locks/state"]
    R --> T["Immutable Git tree"]
    T --> B["Lock records"]
Loading

One ref selects the complete lock state. See the Git object layout and two-worker example for the publication rules.

Documentation

License

Apache 2.0. See NOTICE.

About

Path locks stored as git refs. Pure bash. JSON Lines out, a public schema, and one winner under contention.

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages