mirror of
https://github.com/devvmhq/devvm.git
synced 2026-12-01 06:23:17 -10:00
devvm git-protect strict (0.1.0, 0.1.1-rc.1) set diff.ignoreSubmodules = all in the locked .git/config, which can't keep git on this machine out of a submodule the VM adds: a .gitmodules ignore line the VM writes overrides it, and git add -A/-u, describe --dirty, diff-index, diff-files, fetch and pull recursion and --ignore-submodules on the command line enter submodules anyway (#37). --clone is the only way to keep VM code away from host git. - devvm git-protect takes on|off; strict is a usage error (git_protect_strict_refused) naming devvm clone on. Use, help, completion (Modes) and --json reflect it. - A VM left strict reads as on (Mode, Next) and is pinned and kept as on. The first check that finds it stopped (Check, then converter.unstrict: launches, devvm stop, devvm clone on and its undo) removes the value all from the .git/config of each repo its state recorded before that check, under the host's git lock, and only where .git's identity is the recorded git_id (a replaced .git, or a VM with no record after devvm adopt, is left alone, its all named with the manual remedy: git_strict_unknown). Each config is read through safefile first, unlocked for the edit and locked again even after an interrupt (context.WithoutCancel), and checked to still be a regular file in that .git afterwards (git_lock_raced). On a watching host, a config whose sum isn't the check's is left, and the new sums go into the watch's. An interrupt records nothing. Then on is recorded (mode and next) with one warning (git_strict_converted), none when next is off. Set on/off and Unlock (devvm rm) remove it the same way, silently (strictLeft). - strict recorded nothing about a value it overwrote, so nothing can be restored: only all is removed (--unset-all '^all$'); any other value is the user's and stays. This fixes off, which unset any local value. - A config another VM keeps locked keeps it, named with the manual remedy (git_strict_kept, via msg.M.Value), and its repo goes in the state's strict_left, retried at each later stopped check until empty. A config git can't change is named (git_strict_left). - Messages: git_locked and git_embedded_new say what on can't stop; git_locked_strict and git_config_failed go. - Docs: .git protection's Strict section becomes VMs that were strict, with the remedies; on and Not covered say a submodule or repo the VM adds runs through host git until the next check names it, and only --clone keeps VM code away; attack vectors, How it works, the design notes, the commands table, ls, the cli skill and the CLI reference follow. - Tests: strict_test.go (conversion, values kept, off/on/unlock, nested, kept and retried, replaced and unrecorded .git, interrupted, next off, changed since the check, Linux pins and sums, Watch, next, a failed edit), TestUnstrictFIFO, msg TestValue, git_protect.txtar, git_pins.txtar, fixture_v010_strict.txtar (0.1.0's app VM made strict on macOS), e2e project checks on macOS and Linux. Co-Authored-By: Claude Opus 5.5 <[email protected]>
59 lines
1.9 KiB
Go
59 lines
1.9 KiB
Go
package cli
|
|
|
|
import (
|
|
"slices"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/devvmhq/devvm/internal/gitprotect"
|
|
"github.com/devvmhq/devvm/internal/msg"
|
|
"github.com/devvmhq/devvm/internal/vm"
|
|
)
|
|
|
|
// newGitProtectCmd is devvm git-protect [on|off].
|
|
func newGitProtectCmd(a *app) *cobra.Command {
|
|
var asJSON bool
|
|
cmd := &cobra.Command{
|
|
Use: "git-protect [on|off]",
|
|
Annotations: about{docs: "safety/git-protection#turning-it-on-or-off", group: groupChange}.annotations(),
|
|
Short: "Show or change .git protection for this folder's VM",
|
|
Long: "Show this folder's VM's .git protection, which locks the git hooks and config that run code on this machine, " +
|
|
"as on, off, or clone for a --clone VM. " +
|
|
"devvm git-protect on or off changes it at once on a stopped VM, and on a running one when it next stops, so in a terminal devvm offers to restart it now.",
|
|
Example: examples(
|
|
"devvm git-protect", "show the VM's .git protection",
|
|
"devvm git-protect off", "unlock .git, once the VM is stopped",
|
|
"devvm git-protect on", "lock .git again as it is then",
|
|
),
|
|
Args: showsOnly(cobra.MatchAll(noStrict, maxArgs(1))),
|
|
ValidArgs: gitprotect.Modes,
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
var mode string
|
|
err := a.env(func(env *vm.Env) error {
|
|
s := vm.Settings{Env: env}
|
|
if len(args) == 1 {
|
|
return s.SetGitProtect(cmd.Context(), args[0])
|
|
}
|
|
var err error
|
|
mode, err = s.GitProtect(cmd.Context())
|
|
return err
|
|
}, msg.GitProtectFailed)
|
|
if err != nil || len(args) == 1 {
|
|
return err
|
|
}
|
|
return printValue(a.u, asJSON, "mode", mode)
|
|
},
|
|
}
|
|
jsonFlag(cmd, &asJSON)
|
|
return addFolderFlags(cmd, a)
|
|
}
|
|
|
|
// noStrict refuses strict, which an earlier devvm's devvm git-protect
|
|
// took, naming devvm clone on instead.
|
|
func noStrict(_ *cobra.Command, args []string) error {
|
|
if slices.Contains(args, gitprotect.Strict) {
|
|
return msg.GitProtectStrictRefused()
|
|
}
|
|
return nil
|
|
}
|