Files
devvm/testdata/script/git_protect.txtar
T
w3cjandClaude Opus 5.5 9f6f62d28a refactor(state): vm.json, typed state in package sections, changed under the VM's lock
A VM's state is vms/<hash>/vm.json: core fields (version, id, name,
alias, image, chain, from, dir, folder, backend, backend_at, created)
and one section per package, each a json.RawMessage its owner decodes
into its own type (state.Section, state.SetSection), what was asked for
at its top level and what devvm made under "made":
net (netpolicy), git (gitprotect), ports (the new internal/ports),
agent (sshagent), tools (tools), secrets (secrets), and size, clone,
devcontainer, desktop and links (vm, internal/vm/state.go).

- state.VM.Update takes vms/<hash>/lock (platform.LockFile), reads the
  file again, applies a change and writes it once; a failed change, or
  write, writes nothing. VM.Set and its calls are gone; read-modify-write
  of lists (links, publish, secrets, tools, size, ports) happens inside
  Update. VM.Claim also holds run/claims.lock, after the VM's own;
  VM.Create writes a new VM's whole record once, its alias chosen
  (sshconfig.ChooseAlias) under the claims lock. Reads take no lock.
- created replaces "net unset" as unfinished (VM.Finished).
- internal/ports: the ports section (forward, host.ssh/proxy/desktop),
  ports.Used (was netpolicy.UsedPorts), and one Allocator for ssh, proxy
  and desktop ports under the claims lock (desktop allocated without
  one before; run/ports.lock and netpolicy's own ports key are gone).
- gitprotect: mode recorded before the backend makes the VM (NewMode;
  clone for --clone, which ls and git-protect share as gitprotect.Mode);
  made.git_id (platform.FolderID), one git_dirs list for modules and
  embedded git dirs, delegations, protected; Unlock takes the union of
  recorded and current git dirs.
- links' entries, git dirs, tools and devcontainer commands are JSON
  lists, not strings in values; the key=value parser is gone.
- vm.checkState reads every section of the folder's VM as it's found.
- registry.For drops its unused Dirs.
- Tests: TestVMRoundTrip, TestVMOtherName, TestVMID, TestVMUpdate,
  TestVMUpdateTogether (two writers on one VM and on crossed VMs),
  TestVMClaim, TestVMFailures, TestVMCorrupt, TestChooseAliasTogether,
  TestUsed, TestSSH, TestChooseTogether, TestUnlockRecorded,
  TestCheckState, TestChangesTogether, TestChangesAtOnce; testscript's
  vm_state, json_set with dotted and := keys; e2e's vm_state/vm_set.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-03 14:47:06 -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'
vm_state $WEB_dir/vm.json
stdout '^git.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'
vm_state $WEB_dir/vm.json
! stdout '^git.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$'
vm_state $WEB_dir/vm.json
stdout '^git.mode=strict$'
stdout '^git.made.git_id=[0-9]+:[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.
json_set $NOGIT_dir/vm.json clone.on:=true git.mode=clone
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}$'