Skip to content

Instantly share code, notes, and snippets.

@xpe
Created May 25, 2026 19:47
Show Gist options
  • Select an option

  • Save xpe/b1773b396ca9fef431fc88ff5b41b7e4 to your computer and use it in GitHub Desktop.

Select an option

Save xpe/b1773b396ca9fef431fc88ff5b41b7e4 to your computer and use it in GitHub Desktop.
Zsh script for Jujutsu (JJ) Garbage Collection
# Recommended for use in `.zshrc`
if (( $+commands[jj] )); then
# Autocompletions for Jujutsu
source <(COMPLETE=zsh jj)
# Tell `gh` how to access the git-backend for a JJ repo.
# (Works whether colocated or not.)
github_jj() {
GIT_DIR=$(command jj --ignore-working-copy git root) command gh "$@"
}
# Garbage collect a JJ repo
gc_jj() {
# Verify we're in a JJ repo.
local jj_root
jj_root=$(jj --ignore-working-copy root 2>/dev/null) || {
print -u2 "gc_jj: not in a JJ repo"
return 1
}
# Verify NOT colocated.
if [[ -d "$jj_root/.git" ]]; then
print -u2 "gc_jj: colocated JJ+Git repo at $jj_root — aborting"
return 1
fi
# Verify exact JJ version.
local version
version=$(jj --ignore-working-copy --version 2>/dev/null) || {
print -u2 "gc_jj: could not run jj --version"
return 1
}
if [[ "$version" != "jj 0.41.0" ]]; then
print -u2 "gc_jj: expected 'jj 0.41.0', got '$version'"
return 1
fi
# Size before.
local du_before
du_before=$(du -sk "$jj_root/.jj") || {
print -u2 "gc_jj: du failed"
return 1
}
local size_before=${du_before%%$'\t'*}
# Trim op log to just the current operation.
jj op abandon ..@- || {
print -u2 "gc_jj: jj op abandon failed"
return 1
}
# Collect all unreachable objects immediately.
jj util gc --expire now || {
print -u2 "gc_jj: jj util gc failed"
return 1
}
# Size after.
local du_after
du_after=$(du -sk "$jj_root/.jj") || {
print -u2 "jj_gc: du failed"
return 1
}
local size_after=${du_after%%$'\t'*}
print ".jj: ${size_before}K → ${size_after}K"
}
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment