Files
devvm/testdata/script/git_protect.txtar
T
w3cjandClaude Opus 5.5 d8a7d4de7f refactor(state): a folder per VM, keyed by its project folder's hash
Every file devvm keeps for a project VM is in one folder, vms/<hash>/,
where <hash> is the 14-hex hash of the project folder's path that the VM's
name ends with: its state, its own rules (policy), its backend's files
(backend/: record.json, key, known_hosts, proxy, ports and the port
forwarder's ports.*), what its runs leave (run/: proxy.*, sandbox.sb,
started, lifeline.pid, secrets.*, links.*) and its logs (logs/:
network.jsonl, proxy.log, links.log). Build VMs keep the same layout in
run/builds/<name>/. The top-level run/ holds only what isn't one VM's or
must be short: the hashed control sockets, links' link-<hash>.sock/.ctl,
the QEMU wrapper and the ports lock.

A folder's VM is found by its path's hash (state.ReadHash), with the name
and backend its state records, so devvm --backend docker then devvm in
the same folder opens the one VM, and a backend's name limit applies to
new VMs only. The state records the VM's name and its ssh alias, chosen
once at creation (sshconfig.ChooseAlias) and never recomputed. Links'
targets match recorded names, aliases and folders (project.Targets).

One teardown (vmFolders.teardown) replaces Lifecycle.forget,
undoCreate's own list, Leftovers.Forget's, Proxy.Forget and space's
leftoverFiles: it stops the proxy and link session, then the pieces
outside the folder (clone remote or .git locks, other VMs' links and the
group, host keys, the backend's leftovers), removes the folder, and
writes the ssh config. Each backend's Delete and Forget remove the VM's
backend/ folder (vmssh.Forwards.Forget). A new VM made where an earlier
one's folder was left clears it first. devvm space counts an orphan's
whole folder as leftover state and the other VMs' logs as network logs.

Path builders move into paths: Dirs.VMDir, Dirs.VM and paths.VM's files
(secrets' BindingsFile/PolicyFile, netpolicy's run, EventLog, ProxyLog,
LinkList, VMPolicyFile, Lima's profileFile, startedFile, forwards.file,
vmssh.Forwards.file, links' LinksLog/linksPID, Docker's vmFile and
lifeline), ToolLockConfig, ToolLockChecked and ToolsCheckLog; the
user's cache folder for Lima's and smolvm's caches is Dirs.UserCache,
from HOME, not os.UserCacheDir. Dirs.VMState, LimaVMs, SmolvmVM and
DockerVM go. vmname.HashOf and IsHash, project.Hash.

The proxy takes the links' sockets' folder (-link-sockets, devvm's run
folder) apart from the links' list, which is in the VM's run folder.

Tests: a VM made and removed (rm, gc --orphans) leaves nothing under
vms/, and with network policy has no file named for it outside its
folder (TestTeardownLeavesNothing); a new VM over an old folder starts
clean; lookup finds a VM made on another backend with a long folder name
and makes no second one (TestLookupOtherBackend, and in docker.txtar);
the alias stays put when a second VM shares the folder name
(TestChooseAlias, ssh.txtar); every socket fits under a 35-character
home with the longest VM name (TestSocketsFit); space counts an
orphan's whole folder (TestLeftoverFolders); paths.VM's files; state by
hash and name; the proxy refuses a socket in the list's own folder.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-03 08:16:39 -07:00

151 lines
4.9 KiB
Plaintext

# devvm git-protect: shows a folder's VM's .git protection, and turns it on,
# strict or off while the VM is stopped; a running VM's change is saved and
# applied when it next stops. Strict also keeps host git status and diff
# out of submodules (diff.ignoreSubmodules=all in .git/config). Locks need
# macOS: elsewhere on and strict are refused. The chflags stub changes no
# flags.
# No VM yet.
project web
! exec devvm git-protect
stderr '^devvm: no_vm_git_protect dir=\S*/web$'
! exec devvm git-protect maybe
stderr '^devvm: arg_not_choice choices=on, strict, off command=devvm git-protect given=maybe$'
# A new VM, running: shown, but on macOS changed only when it next stops
# (it could race the locks), the last choice kept; elsewhere off, and on
# is refused.
exec git init -q
exec devvm --no-network-policy
vm_name WEB web
exec devvm git-protect
[darwin] stdout '\Aon\n\z'
[linux] stdout '\Aoff\n\z'
[linux] exec devvm git-protect off
[linux] stdout '^==> git_unlocked vm=devvm-web-[0-9a-f]{14}$'
[linux] ! exec devvm git-protect strict
[linux] stderr '^devvm: git_protect_on_needs_macos$'
[linux] stop 'no locks off macOS'
stub_reset
exec devvm git-protect strict
stdout '^==> git_protect_next_stop mode=strict vm=devvm-web-[0-9a-f]{14}$'
exec devvm git-protect off
stdout '^==> git_protect_next_stop mode=off vm=devvm-web-[0-9a-f]{14}$'
stub_calls
! stdout chflags
exec devvm git-protect
stdout '\Aon\n\z'
catg $WEB_dir/state
stdout '^git_protect_next=off$'
# devvm stop applies it.
exec devvm stop
stdout '^==> git_unlocked vm=devvm-web-[0-9a-f]{14}$'
stub_calls
stdout '^chflags -R -P nouchg /\S*/web/\.git/config$'
exec devvm git-protect
stdout '\Aoff\n\z'
catg $WEB_dir/state
! stdout '^git_protect_next=.'
# Stopped outside devvm: the next launch applies it before the VM starts.
exec devvm
exec devvm git-protect on
stdout '^==> git_protect_next_stop mode=on vm=devvm-web-[0-9a-f]{14}$'
exec limactl stop $WEB
stub_reset
exec devvm
stdout '^==> git_locked vm=devvm-web-[0-9a-f]{14}$'
stub_calls
stdout '^chflags -R -P uchg /\S*/web/\.git/config\n(.*\n)*limactl .*start'
exec devvm git-protect
stdout '\Aon\n\z'
# A .git the VM replaced while it ran is named when it stops, before the
# mode saved meanwhile is set and takes .git as it is.
exec devvm git-protect strict
stdout '^==> git_protect_next_stop mode=strict vm=devvm-web-[0-9a-f]{14}$'
mv .git .git-old
exec git init -q
exec devvm stop
stderr '^devvm: git_replaced git=\S*/web/\.git$'
stdout '^==> git_locked_strict vm=devvm-web-[0-9a-f]{14}$'
exec devvm
exec devvm git-protect
stdout '\Astrict\n\z'
rm .git-old
# Stopped: strict sets diff.ignoreSubmodules with .git/config unlocked,
# then locks .git again.
exec limactl stop $WEB
stub_reset
exec devvm git-protect strict
stdout '^==> git_locked_strict vm=devvm-web-[0-9a-f]{14}$'
exec git config --file .git/config diff.ignoreSubmodules
stdout '\Aall\n\z'
stub_calls
stdout '^chflags -R -P nouchg /\S*/web/\.git/config\n(.*\n)*chflags -R -P uchg /\S*/web/\.git/config$'
stdout '^chflags -R -P uchg /\S*/web/\.git/hooks$'
! stdout 'limactl (start|edit)'
exec devvm git-protect
stdout '\Astrict\n\z'
exec devvm git-protect --json
stdout '\A{\n "version": 1,\n "mode": "strict"\n}\n\z'
! exec devvm git-protect --json off
stderr '^devvm: json_shows_only command=devvm git-protect$'
catg $WEB_dir/state
stdout '^git_protect=strict$'
stdout '^git_inode=[0-9]+$'
# On takes diff.ignoreSubmodules back out.
exec devvm git-protect on
stdout '^==> git_locked vm=devvm-web-[0-9a-f]{14}$'
! exec git config --file .git/config diff.ignoreSubmodules
# Off unlocks, and takes it out too; a launch then locks nothing.
exec devvm git-protect strict
stub_reset
exec devvm git-protect off
stdout '^==> git_unlocked vm=devvm-web-[0-9a-f]{14}$'
! exec git config --file .git/config diff.ignoreSubmodules
stub_calls
stdout '^chflags -R -P nouchg /\S*/web/\.git/hooks$'
stdout '^chflags -R -P nouchg /\S*/web/\.git/config$'
! stdout ' uchg '
exec devvm git-protect
stdout '\Aoff\n\z'
exec limactl stop $WEB
stub_reset
exec devvm
stub_calls
! stdout chflags
# --no-git-protect on an existing VM names devvm git-protect off.
exec devvm --no-git-protect
stderr '^devvm: git_protect_flag_ignored$'
# Strict is kept on launches, and rm unlocks it.
exec limactl stop $WEB
exec devvm git-protect strict
stub_reset
exec devvm
stub_calls
stdout '^chflags -R -P uchg /\S*/web/\.git/config$'
stub_reset
exec devvm rm --force --no-export
stub_calls
stdout '^chflags -R -P nouchg /\S*/web/\.git/config$'
# Without a .git yet: locked when one appears.
project nogit
exec devvm --no-network-policy
vm_name NOGIT nogit
exec limactl stop $NOGIT
exec devvm git-protect on
stdout '^==> git_lock_later vm=devvm-nogit-[0-9a-f]{14}$'
# A --clone VM mounts no .git: shown as clone, never changed.
exec sh -c 'echo mode=clone >> "$NOGIT_dir/state"'
exec devvm git-protect
stdout '\Aclone\n\z'
! exec devvm git-protect on
stderr '^devvm: git_protect_clone vm=devvm-nogit-[0-9a-f]{14}$'