Skip to content

Add git worktree support, make git repo data read-only by default - #10

Merged
didvc merged 2 commits into
didvc:masterfrom
Frenzie:worktree
Sep 18, 2026
Merged

didvc merged 2 commits into
didvc:masterfrom
Frenzie:worktree

Conversation

@Frenzie

@Frenzie Frenzie commented Sep 14, 2026

Copy link
Copy Markdown
Contributor

What & why

git worktree depends on being able to read the original .git dir, not just pwd .git.

I don't want it to be able to mess up all my git stuff. For context, I wrote my own cobbled together script that looks like like this but I figured some kind of shared effort would be a better use of time. ;-)

Details
BWRAP_ARGS=(
    --setenv OPENROUTER_API_KEY "$OPENROUTER_API_KEY"
    --ro-bind /usr /usr
    --ro-bind /lib /lib
    --ro-bind /lib64 /lib64
    --ro-bind /bin /bin
    --ro-bind /etc/alternatives /etc/alternatives
    --ro-bind /etc/resolv.conf /etc/resolv.conf
    --ro-bind /etc/hosts /etc/hosts
    --ro-bind /etc/ssl /etc/ssl
    --dev-bind /dev/null /dev/null
    --ro-bind "$HOME/bin/bwrap-ante" "$HOME/bin/brwap-ante"
    --bind "$HOME/.ante/bin/ante" "$HOME/.ante/bin/ante"
    --ro-bind "$HOME/.gitconfig" "$HOME/.gitconfig"
    --ro-bind "$HOME/.nvm" "$HOME/.nvm"
    --bind "$PROJECT_DIR" "$PROJECT_DIR"
    --bind "$HOME/.ante" "$HOME/.ante"
    --bind "$HOME/.cargo" "$HOME/.cargo"
    --bind "$HOME/.rustup" "$HOME/.rustup"
    --tmpfs /tmp
    --proc /proc
    --dev /dev
    --share-net
    --unshare-pid
    --die-with-parent
    --chdir "$PROJECT_DIR"
    --ro-bind /dev/null "$PROJECT_DIR/.env"
    --ro-bind /dev/null "$PROJECT_DIR/.env.local"
    --ro-bind /dev/null "$PROJECT_DIR/.env.production"
)

# Check if the current directory is inside a Git repository
# Enforce worktree if so
if git rev-parse --is-inside-work-tree >/dev/null 2>&1; then
	GIT_COMMON_DIR=$(git rev-parse --path-format=absolute --git-common-dir)
	BWRAP_ARGS+=(--ro-bind "$GIT_COMMON_DIR" "$GIT_COMMON_DIR")
else
	echo Not in a git repo.
	exit 1
fi

# Execute bwrap with the constructed arguments
bwrap "${BWRAP_ARGS[@]}" ante "$@"

Checklist

  • shellcheck ai-bwrap is clean
  • --help text and README updated together (if the CLI surface changed)
  • Verified with --dry-run (paste relevant output below if helpful)
Details
bwrap --unshare-all --die-with-parent --proc /proc --dev /dev --tmpfs /tmp --tmpfs /run --ro-bind /usr /usr --symlink usr/lib /lib --symlink usr/lib64 /lib64 --symlink usr/bin /bin --symlink usr/sbin /sbin --ro-bind /etc /etc --dir /home/frans --setenv HOME /home/frans --setenv PATH /home/frans/.ante/bin:/home/frans/.nvm/versions/node/v22.9.0/bin:/home/frans/.bun/bin:/home/frans/.deno/bin:/home/frans/.cargo/bin:/home/frans/.local/bin:/home/frans/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/usr/local/go/bin:/home/frans/go/bin:\~/.local/bin --setenv TMPDIR /tmp --chdir /home/frans/src/ai-bwrap --ro-bind-try /home/frans/.nvm /home/frans/.nvm --ro-bind-try /home/frans/.pyenv /home/frans/.pyenv --ro-bind-try /home/frans/.local/bin /home/frans/.local/bin --ro-bind-try /home/frans/.config/gh /home/frans/.config/gh --ro-bind-try /home/frans/.gitconfig /home/frans/.gitconfig --bind-try /home/frans/.npm /home/frans/.npm --ro-bind-try /home/frans/.npmrc /home/frans/.npmrc --bind-try /home/frans/.cargo /home/frans/.cargo --bind /home/frans/src/ai-bwrap /home/frans/src/ai-bwrap --ro-bind /home/frans/src/ai-bwrap/.git /home/frans/src/ai-bwrap/.git --share-net --bind /home/frans/.config/opencode /home/frans/.config/opencode --bind /home/frans/.local/share/opencode /home/frans/.local/share/opencode --bind /home/frans/.cache/opencode /home/frans/.cache/opencode --bind /home/frans/.local/state/opencode /home/frans/.local/state/opencode --ro-bind-try /home/frans/.opencode /home/frans/.opencode -- /home/frans/bin/opencode

Notes

It means it can't commit but I consider that a feature. The default could be flipped if desired.

Frenzie and others added 2 commits September 14, 2026 18:45
`rev-parse --path-format=absolute --git-common-dir` needs git 2.31+.
Older git does not reject the unknown flag: it echoes it back and exits
0, so GIT_COMMON_DIR became "--path-format=absolute\n.git" and bwrap
refused to start in any git repo. This is the default path -- no flag
involved -- so ai-bwrap was unusable on e.g. Ubuntu 20.04 (git 2.25.1).

Use plain --git-common-dir, which works on every version and is what
resolves worktrees and subdirectories anyway, then absolutize by hand.

Verified on git 2.25.1: read-only .git by default, --git-rw restores
writes, tracked files stay editable, and subdirectories, linked
worktrees and non-git dirs all behave as intended.
@didvc

didvc commented Sep 18, 2026

Copy link
Copy Markdown
Owner

Thanks — merging this.

One thing I fixed on the branch before merge rather than sending back, since it only shows up on older git than you'd have: rev-parse --path-format=absolute --git-common-dir needs git 2.31+. Older git doesn't reject the unknown flag — it echoes it back and exits 0, so GIT_COMMON_DIR became "--path-format=absolute\n.git" and bwrap wouldn't start in any git repo. Since read-only .git is the default, that hit every invocation, not just --git-rw ones.

Swapped it for plain --git-common-dir (all versions, and it's what resolves worktrees/subdirs anyway) plus a manual absolutize. Verified on git 2.25.1: read-only .git by default, --git-rw restores writes, tracked files stay editable, and subdirectories, linked worktrees and non-git dirs all work.

@didvc
didvc merged commit 420856f into didvc:master Sep 18, 2026
@Frenzie
Frenzie deleted the worktree branch September 18, 2026 09:05
@Frenzie

Frenzie commented Sep 18, 2026

Copy link
Copy Markdown
Contributor Author

Git older than 2.31? But that's over five years ago. :-) I wouldn't have added that extra complexity since the Ubuntu 20.04 mentioned in the commit message has been EOL since 31 May 2025.

For some quick context, Debian Bullseye (oldold-stable, no more security updates) 2.30, Bookworm (old-stable) 2.39, Ubuntu Jammy (22.04) 2.34.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants