Files
w3cjandClaude Opus 5.5 20d2fe1db7 feat(clone): workspace clones of a folder of repos
devvm --clone in a folder of git repos gives one VM a clone of each repo
the user chooses, each at the same path as on the host, so paths between
them (file:../pkg, ../Lib/Lib.csproj) work in the VM, and a copy of the
folder's own files outside the repos, made once.

- Discovery by .git protection's walk (4 folders down, the same skipped
  folders, 100 repos); each repo checked as --clone checks one; a linked
  worktree of a repo in the folder isn't a repo of its own; a repo whose
  folder, or common dir, another VM mounts is refused, naming it, as is
  one whose name a terminal can't show. A picker in a terminal (every
  copyable repo ticked, the others shown with why); --repos
  all|none|LIST answers without one, and the question is refused naming
  it. A folder that's a repo keeps today's single clone unless --repos
  is given; one inside another repo's working tree is refused
  (workspace_inside_repo). The choice is recorded in the VM's state.
- A single --clone, or devvm clone on, in a folder another VM mounts is
  refused too (clone_folder_mounted), at plan time and before the seed's
  push.
- Each repo seeded with its current and default branch (origin/HEAD,
  else main, else master); a detached HEAD checks out the default. One
  VM command makes every repo, leaving one an earlier push reached as it
  is; each repo is checked again just before its push, and its remote
  chosen once the one before is added. The folder's own repo's clone
  excludes the other clones (escaped, anchored lines) and the host's
  info/exclude, at the seed and at devvm clone add.
- The folder's own files: walked folder by folder beneath the folder
  (safefile.Root.OpenDir), no links followed, no hard-linked files, no
  bare repos, no set-id bits, bounded; refused folders (checkUnsafe)
  apply.
- devvm clone fetch [--json]: every repo's branches into its remote's
  remote-tracking branches (--refmap=, one refspec), listed by branch
  name with their repos; nothing new is said only when a repo was
  fetched from. devvm clone add PATH: one more repo; one whose copy
  didn't finish is copied again.
- rm, devvm clone off and the switch loop over the repos; rm's check
  reads every repo in one VM command (cloneState), parsed strictly and
  capped (8 MiB, 10,000 branches in all), counting only worktrees inside
  the folder, a VM-side folder never made read as every repo gone; a
  clone missing in the VM is asked about, an unfinished repo checked
  when it's there; a host repo that changed refuses the check, and one
  another VM mounts, a single clone's too, gets no host git and is asked
  about; each repo is checked again before rm's fetch.
- The finder opens a workspace clone VM inside one of its repos; --here
  still makes the repo's own. --repos's dependencies go through needs(),
  which now takes a first argument too (devvm clone on).
- State is additive: clone.workspace, clone.repos, clone.made.files; a
  0.1.0 clone reads as one repo at ".". CHANGELOG: an Unreleased section.

Messages: flag_needs_flag, repos_value, repos_not_workspace,
workspace_repo_unknown, workspace_pick_help, workspace_pick_cannot,
workspace_reason_*, workspace_inside_repo, workspace_repos_past,
workspace_skipped, workspace_repo_dropped, workspace_detached,
copying_repo, uncommitted_not_copied_repos, workspace_ready,
workspace_files_failed, workspace_files_skipped, unfetched_commits_in,
clone_repo_dirty, clone_repo_gone, clone_repo_changed,
clone_repo_mounted, clone_repo_unchecked, clone_folder_mounted,
clone_check_repo_changed, fetch_before_delete_repos,
no_terminal_fetch_repos, clone_fetch_*, fetching_repo, clone_fetched_*,
clone_add_*, clone_added.

Tests: TestDefaultBranch, TestBranchPlan, TestIsWorkspace,
TestChooseRepos, TestPickRepos, TestFindRepos, TestRepoExcludes,
TestWorkspaceFiles, TestCloneChangesAcrossRepos, TestFetchedByBranch,
TestCloneRepos, TestV010CloneReadsAsOneRepo, TestCoversRepo,
TestParseCloneState, TestCloneStateScript, TestWorktreePathPins,
TestOpenDir; the clone_workspace, clone_workspace_repo, subfolder and
nested_replaced scripts; fixture_v010_clone (0.1.0's web clone fetched,
checked by rm, made a workspace clone by devvm clone add, switched off;
its scratch switched on); the e2e clone section's cloneWorkspace.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-08 21:38:23 -07:00

201 lines
7.5 KiB
Go

package cli
import (
"context"
"slices"
"github.com/spf13/cobra"
"github.com/devvmhq/devvm/internal/build"
"github.com/devvmhq/devvm/internal/image"
"github.com/devvmhq/devvm/internal/msg"
"github.com/devvmhq/devvm/internal/netpolicy"
"github.com/devvmhq/devvm/internal/paths"
"github.com/devvmhq/devvm/internal/ui"
"github.com/devvmhq/devvm/internal/vm"
)
// upFlags are devvm's flags, all of them for what a new VM gets.
type upFlags struct {
tools, agents string // --tools and --agents: lists, or none
layers string // --layers: a list, or none
from string
network string // --network: a tier
size vm.Size
ports string
unsafeFolder bool
noGitProtect bool
clone bool
repos string // --repos: a workspace clone's repos
sshAgent bool
noDevcontainer bool
sshConfig bool
}
// addUpFlags adds devvm's flags to the root command, as its own: no
// subcommand takes them but devvm setup, whose --agents chooses new VMs'
// default, and the commands on this folder's VM, which take --here and
// --parent too (a's).
func addUpFlags(root *cobra.Command, a *app, uf *upFlags) {
f := root.Flags()
f.StringVar(&uf.tools, "tools", "", "the `LIST` of toolchains a new VM gets, such as node,python, or none (default: those the folder's files call for)")
addAgentsFlag(f, &uf.agents, "the `LIST` of coding agents a new VM gets, such as claude,codex, or none (default: devvm setup's)")
addLayersFlag(f, &uf.layers, "the `LIST` of layers a new VM's image gets, bottom first, such as docker,mine, or none (default: devvm setup's)")
addFromFlag(f, &uf.from)
f.StringVar(&uf.network, "network", "", "the network `TIER` a new VM gets: open, internet, allowlist or none, or learn for internet while it learns the hosts it needs (default: devvm network -g's, else allowlist)")
f.StringVar(&uf.size.CPUs, "cpus", "", "the `N` CPUs the VM gets (default: devvm size -g, else the image's)")
f.StringVar(&uf.size.Memory, "memory", "", "the VM's memory in `GiB`, such as 16 or 16G (default: devvm size -g, else the image's)")
f.StringVar(&uf.size.Disk, "disk", "", "the VM's disk in `GiB`, at least the image's (default: devvm size -g, else the image's)")
f.StringVar(&uf.ports, "ports", "", "the `PORTS` the VM forwards: all, none or a list like 5173,8000-8010 (default: devvm ports -g, else all)")
f.BoolVar(&uf.clone, "clone", false, "create the VM with its own clone of this repo, or of each repo in this folder, instead of mounting the folder")
addReposFlag(f, &uf.repos)
f.BoolVar(&uf.unsafeFolder, "unsafe-folder", false, "mount a folder devvm refuses, such as your home folder")
f.BoolVar(&uf.noGitProtect, "no-git-protect", false, "create the VM without .git protection")
f.BoolVar(&uf.sshAgent, "ssh-agent", false, "create the VM with your SSH agent forwarded, so anything in it can use your keys while you're connected")
f.BoolVar(&uf.noDevcontainer, "no-devcontainer", false, "create the VM without reading the folder's devcontainer.json")
addFolderFlags(root, a)
exclusive(root, "clone", "parent")
needs(root, "repos", "--clone")
addSSHConfigFlag(f, &uf.sshConfig)
}
// options are what the flags ask of up.
func (uf *upFlags) options() vm.Options {
tier, learn, _ := netpolicy.Choice(uf.network)
if uf.network == "" {
tier = ""
}
return vm.Options{
Toolchains: partFlag(uf.tools), Agents: partFlag(uf.agents), Layers: partFlag(uf.layers),
Network: tier, Learn: learn, Size: uf.size, Ports: uf.ports,
From: fromRef(uf.from), FromSet: uf.from != "",
UnsafeFolder: uf.unsafeFolder, NoGitProtect: uf.noGitProtect, Clone: uf.clone, Repos: uf.repos, SSHAgent: uf.sshAgent,
NoDevcontainer: uf.noDevcontainer,
}
}
// up opens a shell in this folder's VM, making it first.
func up(ctx context.Context, a *app, uf *upFlags) error {
o := uf.options()
defer releaseNotice(ctx, a)()
return a.env(func(env *vm.Env) error {
env.AddInclude = uf.sshConfig
return vm.Launch{Env: env}.Up(ctx, o)
}, msg.UpFailed)
}
// pickParts returns what shows the tools picker in u's terminal, each
// part under its heading; without one, or where it couldn't show, the
// rows stay as they are.
func pickParts(u *ui.UI) func(msg.M, []vm.PickPart) []vm.PickPart {
pick := pickMany(u)
return func(help msg.M, parts []vm.PickPart) []vm.PickPart {
out, _ := pick(help, parts)
return out
}
}
// pickMany is the VM environment's PickMany: the UI's Pick over the
// parts, each under its heading, with why it couldn't show or didn't
// end at Enter, the parts as they were then.
func pickMany(u *ui.UI) func(msg.M, []vm.PickPart) ([]vm.PickPart, error) {
return func(help msg.M, parts []vm.PickPart) ([]vm.PickPart, error) {
sections := make([]ui.Section, len(parts))
for i, p := range parts {
sections[i].Heading = p.Heading
for _, r := range p.Rows {
sections[i].Choices = append(sections[i].Choices, ui.Choice{Name: r.Name, Desc: u.Text(r.Desc), On: r.On, Fixed: r.Fixed})
}
}
picked, err := u.Pick(help, sections)
out := make([]vm.PickPart, len(parts))
for i, p := range parts {
out[i] = vm.PickPart{Heading: p.Heading, Rows: slices.Clone(p.Rows)}
if err != nil {
continue
}
for j := range out[i].Rows {
out[i].Rows[j].On = picked[i].Choices[j].On
}
}
return out, err
}
}
// pickOne is the VM environment's PickOne: the UI's PickOne over rows.
func pickOne(u *ui.UI) func(msg.M, []vm.PickRow) int {
return func(help msg.M, rows []vm.PickRow) int {
choices := make([]ui.Choice, len(rows))
for i, r := range rows {
choices[i] = ui.Choice{Name: r.Name, Desc: u.Text(r.Desc), On: r.On}
}
return u.PickOne(help, choices)
}
}
// partsNone is the value of --tools, --agents and --layers that chooses
// none of the part.
const partsNone = "none"
// partFlag is a part's flag as up takes it: nil when it's not given, its
// list otherwise, empty for none.
func partFlag(value string) *[]string {
switch value {
case "":
return nil
case partsNone:
return &[]string{}
}
names := splitList(value)
return &names
}
// checkPartFlag checks the value of --tools, --agents or --layers (flag,
// as given): none alone, or a comma-separated list of the part's names.
// A name of another part's is refused naming that part's flag, and any
// other with a did-you-mean. The layers are devvm's own and the user's.
func checkPartFlag(flag, value string) error {
names := splitList(value)
if slices.Contains(names, partsNone) && len(names) > 1 || len(names) == 0 {
return msg.PartNoneAlone(flag)
}
if value == partsNone {
return nil
}
parts := partNames()
for _, n := range names {
if slices.Contains(parts[flag], n) {
continue
}
if other := partFlagOf(parts, n); other != "" {
return msg.PartOtherFlag(flag, n, other)
}
}
return vm.CheckPart(flag, names, parts[flag])
}
// partFlags are the flags of a new VM's parts, in the order a command
// line names them.
func partFlags() []string { return []string{"--tools", "--agents", "--layers"} }
// partNames is each part's flag and the names it takes: the shared
// tools' toolchains and agents, and the layers, devvm's own and the
// user's.
func partNames() map[string][]string {
layers := image.Builtins
if dirs, err := paths.FromEnv(); err == nil {
layers = (&build.Env{Dirs: dirs}).Layers()[1:]
}
return map[string][]string{"--tools": image.Toolchains, "--agents": image.Agents, "--layers": layers}
}
// partFlagOf is the flag that takes name, or "" when no part has it.
func partFlagOf(parts map[string][]string, name string) string {
for _, f := range partFlags() {
if slices.Contains(parts[f], name) {
return f
}
}
return ""
}