mirror of
https://github.com/devvmhq/devvm.git
synced 2026-12-01 06:23:17 -10:00
An export leaves out opencode's worktree folder, as a git repository's config can name a command that git runs, so the uncommitted changes in the worktrees opencode makes there (~/.local/share/opencode/worktree/<project>/<name>, opencode v2.4.1's worktree/strategies.ts) went with the VM's disk on devvm rm, and on a remake with --clean-home, with nothing to say so. The registry gets Worktrees (opencode's worktree/*/*, below its HistoryOwn, TestDefsComplete holding each to a plain pattern there). rm's unsaved-work check, cloneCheck, becomes workCheck: the clone (cloneLost, as before) and the agents' worktrees (agentTrees), one question for both. agentTreesState reads them in the running VM in one command, in its home folder, each * of a pattern sent as *, .[!.]* and ..?* (treeGlobs), so a name that starts with a dot is found: each folder with a .git, its git status and a detached HEAD's commit nothing else has; past 100 it says more. devvm reads at most 8 MiB of the answer, cancelling the command past it, and waits at most treesWait (2 minutes). parseAgentTrees takes the answer as untrusted: whole records of a glob asked for, each path clean, matched by its pattern and once, at most 100, each with one status. Dirty, detached, unreadable (wfail) and more are shown and asked about (delete_anyway or remake_anyway), which --force never answers; refused without a terminal (no_terminal_delete_trees, naming --no-worktree-check, or the clone's no_terminal_delete when it has work too). --no-worktree-check skips the check and warns (trees_check_skipped). A failed, late or hostile answer keeps the VM (trees_check_failed), and so does a VM that doesn't start for it (trees_check_not_started). Rm and rmGone run it, and rmGone refuses a stopped VM with opencode, which starting would make its folder again (rm_gone_stopped_trees, naming --no-export --no-worktree-check); stillGone takes the refusal to give. remaking.checkWork runs it before a remake with --clean-home (remakeTo, Update, SetClone, whose clone check it joins), its messages naming the command as typed (remakeInVM.typed), with --clean-home, which --no-worktree-check needs (needs), so devvm clone off --clean-home's clone check messages name it too. A remake's flag has its own help. The stub guest runs the read in vmhome-<vm>. Tests that fail without the check: TestRmAgentTrees, TestRmVMAgentTrees, TestRemakeAgentTrees, TestUpdateAgentTrees, TestSetCloneAgentTrees and the rm_agent_trees script; TestAgentTreesBounded (an endless answer cancelled, a silent VM given up on, in a synctest bubble); TestAgentTreesState runs the script under sh on real worktrees, a dot-named one included; TestParseAgentTrees holds the answer's checks, TestCloneGit the stub's. TestSetCloneOff names the command with --clean-home. e2e's network section makes a worktree in opencode's folder in the real VM, and devvm rm refuses, then --no-worktree-check deletes it. Docs: coding agents' opencode's worktrees, How it works' opencode's worktrees, deleting a VM, a VM whose folder is gone, images, clone, the overview's remake, Commands, extending and rm's help; the changelog's What to do step 4 names --no-worktree-check for scripts and devvm rm VM of a stopped VM, and opencode's block gets the check; the release page has its section. Co-Authored-By: Claude Opus 5.5 <[email protected]>
173 lines
7.0 KiB
Go
173 lines
7.0 KiB
Go
package cli
|
|
|
|
import (
|
|
"io"
|
|
"strings"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/devvmhq/devvm/internal/msg"
|
|
"github.com/devvmhq/devvm/internal/vm"
|
|
)
|
|
|
|
// newCloneCmd is devvm clone [on|off] [--force] [--no-export]
|
|
// [--clean-home [--no-worktree-check]] [--no-clone-check] [--repos LIST],
|
|
// and its fetch and add PATH.
|
|
func newCloneCmd(a *app) *cobra.Command {
|
|
var o vm.RemakeOptions
|
|
var asJSON bool
|
|
cmd := &cobra.Command{
|
|
Use: "clone [on|off]",
|
|
Annotations: about{docs: "using/clone#switching-a-vm-that-exists", group: groupChange}.annotations(),
|
|
Short: "Show or change whether this folder's VM has its own clone of the repo",
|
|
Long: "Show whether this folder's VM has its own clone of the repo, or of each repo in a folder of repos, on, or mounts this folder, off. " +
|
|
"devvm clone on and devvm clone off make the VM again from its image, after asking in a terminal without --no-input, then open a shell in it, and devvm keeps its home folder and its settings, while everything else on its disk is lost. " +
|
|
"On copies the repos' branches into the new clone, and off first offers to fetch the clone's work into this machine's repos, unless you give --no-clone-check.",
|
|
Example: examples(
|
|
"devvm clone", "show whether the VM has its own clone of the repo",
|
|
"devvm clone on", "make the VM again with its own clone of the repo, after asking",
|
|
"devvm clone off", "fetch the clone's work, then make the VM again with this folder mounted",
|
|
"devvm clone on --force", "make it again without asking, as a script must",
|
|
"devvm clone fetch", "fetch the VM's branches into each repo on this machine",
|
|
),
|
|
Args: showsOnly(maxArgs(1), "force", "no-export", "clean-home", "no-clone-check", "repos"),
|
|
ValidArgs: []string{vm.CloneOn, vm.CloneOff},
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
if len(args) == 1 {
|
|
command := "devvm clone " + args[0]
|
|
return a.env(func(env *vm.Env) error {
|
|
return vm.Lifecycle{Env: env}.SetClone(cmd.Context(), command, args[0], o)
|
|
}, msg.RemakeFailed)
|
|
}
|
|
var mode string
|
|
if err := a.env(func(env *vm.Env) error {
|
|
var err error
|
|
mode, err = vm.Settings{Env: env}.Clone(cmd.Context())
|
|
return err
|
|
}, msg.CloneShowFailed); err != nil {
|
|
return err
|
|
}
|
|
return printValue(a.u, asJSON, "clone", mode)
|
|
},
|
|
}
|
|
addRemakeFlags(cmd, &o)
|
|
addNoCloneCheckFlag(cmd.Flags(), &o.NoCloneCheck)
|
|
addReposFlag(cmd.Flags(), &o.Repos)
|
|
needs(cmd, "repos", vm.CloneOn)
|
|
jsonFlag(cmd, &asJSON)
|
|
cmd.AddCommand(newCloneFetchCmd(a), newCloneAddCmd(a))
|
|
return addFolderFlags(cmd, a)
|
|
}
|
|
|
|
// cloneFetchOutput is devvm clone fetch --json: the branches new or moved
|
|
// in each repo's remote-tracking branches, by repo and by name.
|
|
type cloneFetchOutput struct {
|
|
Repos []cloneFetchRepo `json:"repos"`
|
|
Branches []cloneFetchBranch `json:"branches"`
|
|
}
|
|
|
|
// cloneFetchRepo is one repo's fetch: its path relative to the folder
|
|
// ("." for the folder's own), its remote, its new or moved branches, and
|
|
// the local branches the VM's own work went to: its detached HEAD's
|
|
// commits and its stash entries.
|
|
type cloneFetchRepo struct {
|
|
Path string `json:"path"`
|
|
Remote string `json:"remote"`
|
|
Branches []string `json:"branches"`
|
|
Saved []string `json:"saved,omitempty"`
|
|
}
|
|
|
|
// cloneFetchBranch is a branch name and the repos it's new or moved in.
|
|
type cloneFetchBranch struct {
|
|
Name string `json:"name"`
|
|
Repos []string `json:"repos"`
|
|
}
|
|
|
|
// newCloneFetchCmd is devvm clone fetch [--json].
|
|
func newCloneFetchCmd(a *app) *cobra.Command {
|
|
var asJSON bool
|
|
cmd := &cobra.Command{
|
|
Use: "fetch",
|
|
Annotations: about{docs: "using/clone#fetching-the-vms-work"}.annotations(),
|
|
Short: "Fetch the work of this folder's --clone VM into each of its repos",
|
|
Long: "Fetch every branch of each repo in this folder's --clone VM into the repo on this machine, as remote-tracking branches of its remote for the VM, such as devvm/main, starting the VM if it's stopped. " +
|
|
"When a repo's HEAD in the VM is detached at commits no branch has, fetch those into a new local branch, saved/devvm/HEAD, and each stash entry the VM made into saved/devvm/stash-N. " +
|
|
"Then list the branches that are new or moved, each with the repos it's in.",
|
|
Example: examples(
|
|
"devvm clone fetch", "fetch every repo's branches, and list what's new",
|
|
"devvm clone fetch --json", "list what's new for a script",
|
|
),
|
|
Args: noArgs,
|
|
RunE: func(cmd *cobra.Command, _ []string) error {
|
|
var got vm.Fetched
|
|
if err := a.env(func(env *vm.Env) error {
|
|
var err error
|
|
got, err = vm.Clone{Env: env}.Fetch(cmd.Context())
|
|
return err
|
|
}, msg.CloneFetchRunFailed); err != nil {
|
|
return err
|
|
}
|
|
return printFetched(a, got, asJSON)
|
|
},
|
|
}
|
|
jsonFlag(cmd, &asJSON)
|
|
return addFolderFlags(cmd, a)
|
|
}
|
|
|
|
// printFetched prints what devvm clone fetch brought: with asJSON, a
|
|
// cloneFetchOutput; else each branch name and its repos on a line, the
|
|
// VM's names each on one line (msg.OneLine), then how to review them, or that there was
|
|
// nothing new.
|
|
func printFetched(a *app, got vm.Fetched, asJSON bool) error {
|
|
byBranch := got.ByBranch()
|
|
if asJSON {
|
|
out := cloneFetchOutput{Repos: []cloneFetchRepo{}, Branches: []cloneFetchBranch{}}
|
|
for _, r := range got.Repos {
|
|
out.Repos = append(out.Repos, cloneFetchRepo{Path: r.Path, Remote: r.Remote, Branches: append([]string{}, r.Branches...), Saved: append([]string(nil), r.Saved...)})
|
|
}
|
|
for _, b := range byBranch {
|
|
out.Branches = append(out.Branches, cloneFetchBranch{Name: b.Name, Repos: b.Repos})
|
|
}
|
|
return printJSON(a.u, out)
|
|
}
|
|
if len(byBranch) == 0 {
|
|
// Every repo left alone has been named: nothing was looked at. A
|
|
// detached HEAD's commits saved were named as they were.
|
|
if got.Checked > 0 && len(got.Repos) == 0 {
|
|
a.u.Info(msg.CloneFetchedNone())
|
|
}
|
|
return nil
|
|
}
|
|
var b strings.Builder
|
|
for _, br := range byBranch {
|
|
b.WriteString(msg.OneLine(br.Name) + ": " + msg.OneLine(strings.Join(br.Repos, ", ")) + "\n")
|
|
}
|
|
if _, err := io.WriteString(a.u.Stdout(), b.String()); err != nil {
|
|
return err
|
|
}
|
|
a.u.Info(msg.CloneFetchedReview(got.Repos[0].Remote))
|
|
return nil
|
|
}
|
|
|
|
// newCloneAddCmd is devvm clone add PATH.
|
|
func newCloneAddCmd(a *app) *cobra.Command {
|
|
cmd := &cobra.Command{
|
|
Use: "add PATH",
|
|
Annotations: about{docs: "using/clone#adding-a-repo", noJSON: "it changes the VM"}.annotations(),
|
|
Short: "Copy one more repo into this folder's --clone VM",
|
|
Long: "Copy the repo at PATH, a folder in this folder's --clone VM's folder, into the VM at the same path, with its branches, tags and remote-tracking branches, checked out as it is here, starting the VM if it's stopped. " +
|
|
"The repo gets a remote for the VM, as each of the VM's repos has.",
|
|
Example: examples(
|
|
"devvm clone add tools", "copy the repo in ./tools into the VM",
|
|
"devvm clone add ../shared", "copy a repo beside this one, in the same folder of repos",
|
|
),
|
|
Args: exactArgs(1),
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
return a.env(func(env *vm.Env) error {
|
|
return vm.Clone{Env: env}.Add(cmd.Context(), args[0])
|
|
}, msg.CloneAddFailed)
|
|
},
|
|
}
|
|
return addFolderFlags(cmd, a)
|
|
}
|