mirror of
https://github.com/devvmhq/devvm.git
synced 2026-12-01 21:36:40 -10:00
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]>
137 lines
4.9 KiB
Go
137 lines
4.9 KiB
Go
package cli
|
|
|
|
import (
|
|
"os"
|
|
"slices"
|
|
|
|
"github.com/spf13/cobra"
|
|
"github.com/spf13/pflag"
|
|
|
|
"github.com/devvmhq/devvm/internal/from"
|
|
"github.com/devvmhq/devvm/internal/msg"
|
|
"github.com/devvmhq/devvm/internal/vm"
|
|
)
|
|
|
|
// addFromFlag adds --from, the container image a base starts from.
|
|
func addFromFlag(f *pflag.FlagSet, p *string) {
|
|
f.StringVar(p, "from", "", "the container `IMAGE` base starts from, such as node:24, instead of Ubuntu's cloud image (default: $DEVVM_FROM)")
|
|
}
|
|
|
|
// addAgentsFlag adds --agents, a list of coding agents: a new VM's, or
|
|
// new VMs' default with devvm setup. usage says whose.
|
|
func addAgentsFlag(f *pflag.FlagSet, p *string, usage string) {
|
|
f.StringVar(p, "agents", "", usage)
|
|
}
|
|
|
|
// addLayersFlag adds --layers, a list of layers: a new VM's, or new VMs'
|
|
// default with devvm setup. usage says whose.
|
|
func addLayersFlag(f *pflag.FlagSet, p *string, usage string) {
|
|
f.StringVar(p, "layers", "", usage)
|
|
}
|
|
|
|
// addForceFlag adds -f/--force, which only skips a confirmation.
|
|
func addForceFlag(f *pflag.FlagSet, p *bool) {
|
|
f.BoolVarP(p, "force", "f", false, "don't ask to confirm, which devvm needs without a terminal or with --no-input")
|
|
}
|
|
|
|
// addNoExportFlag adds --no-export, which skips exporting the agents'
|
|
// history before a VM's disk goes (devvm rm, devvm layer add, devvm
|
|
// layer rm, devvm update, devvm clone).
|
|
func addNoExportFlag(f *pflag.FlagSet, p *bool) {
|
|
f.BoolVar(p, "no-export", false, "don't export the agents' history first")
|
|
}
|
|
|
|
// addNoCloneCheckFlag adds --no-clone-check, which skips checking a
|
|
// --clone VM's clone before its disk goes (devvm rm, devvm clone off).
|
|
func addNoCloneCheckFlag(f *pflag.FlagSet, p *bool) {
|
|
f.BoolVar(p, "no-clone-check", false, "don't check a --clone VM's clone for unfetched commits and uncommitted changes first, which are lost")
|
|
}
|
|
|
|
// addReposFlag adds --repos, which answers the question of a --clone VM's
|
|
// repos in a folder of repos (devvm --clone, devvm clone on).
|
|
func addReposFlag(f *pflag.FlagSet, p *string) {
|
|
f.StringVar(p, "repos", "", "the repos a --clone VM of a folder of repos gets: all, none or a `LIST` of their paths, such as frontend,backend (default: a picker in a terminal)")
|
|
}
|
|
|
|
// addRemakeFlags adds the flags of a command that makes the folder's VM
|
|
// again (devvm layer add, devvm layer rm, devvm update, devvm clone):
|
|
// --force, --no-export and --clean-home.
|
|
func addRemakeFlags(f *pflag.FlagSet, o *vm.RemakeOptions) {
|
|
addForceFlag(f, &o.Force)
|
|
addNoExportFlag(f, &o.NoExport)
|
|
f.BoolVar(&o.CleanHome, "clean-home", false, "give the VM the image's home folder instead of keeping its own")
|
|
}
|
|
|
|
// addSSHConfigFlag adds --ssh-config, which answers the question whether
|
|
// to add devvm's Include line to ~/.ssh/config.
|
|
func addSSHConfigFlag(f *pflag.FlagSet, p *bool) {
|
|
f.BoolVar(p, "ssh-config", false, "add devvm's Include line to ~/.ssh/config without asking")
|
|
}
|
|
|
|
// folderFlags are --here and --parent: which VM a command on this
|
|
// folder's VM uses when a folder it's inside has one.
|
|
type folderFlags struct{ here, parent bool }
|
|
|
|
// rule is the VM environment's rule for them (vm.Env.Folder).
|
|
func (ff folderFlags) rule() vm.FolderRule {
|
|
switch {
|
|
case ff.here:
|
|
return vm.FolderHere
|
|
case ff.parent:
|
|
return vm.FolderParent
|
|
}
|
|
return vm.FolderAuto
|
|
}
|
|
|
|
// addFolderFlags adds --here and --parent to cmd, a command that acts on
|
|
// this folder's VM, into the run's (a.folder), which its VM environment
|
|
// finds the VM by. They can't go together, nor with -g on a command that
|
|
// has it, added before them. It returns cmd.
|
|
func addFolderFlags(cmd *cobra.Command, a *app) *cobra.Command {
|
|
f := cmd.Flags()
|
|
f.BoolVar(&a.folder.here, "here", false, "use this folder's own VM, never that of a folder it's inside")
|
|
f.BoolVar(&a.folder.parent, "parent", false, "use the VM of the nearest folder this one is inside, even across a git repo of its own")
|
|
names := []string{"here", "parent"}
|
|
if f.Lookup("global") != nil {
|
|
names = append(names, "global")
|
|
}
|
|
exclusive(cmd, names...)
|
|
return cmd
|
|
}
|
|
|
|
// fromRef is --from as given, else its default from the environment.
|
|
func fromRef(flag string) string {
|
|
if flag != "" {
|
|
return flag
|
|
}
|
|
return os.Getenv(from.Env)
|
|
}
|
|
|
|
// noBlanks refuses a flag given an empty value, and an empty argument: no
|
|
// flag or argument takes a blank to mean none or its default. A command
|
|
// that passes its arguments on as they are (limactl) is left alone.
|
|
func noBlanks(cmd *cobra.Command, args []string) error {
|
|
if cmd.DisableFlagParsing {
|
|
return nil
|
|
}
|
|
var err error
|
|
cmd.Flags().Visit(func(f *pflag.Flag) {
|
|
sv, slice := f.Value.(pflag.SliceValue)
|
|
switch {
|
|
case err != nil:
|
|
case f.Value.Type() == "string" && f.Value.String() == "":
|
|
err = msg.FlagBlank(f.Name)
|
|
case slice && (len(sv.GetSlice()) == 0 || slices.Contains(sv.GetSlice(), "")):
|
|
// --host "", or a comma too many: --host a.com,
|
|
err = msg.FlagBlank(f.Name)
|
|
}
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if slices.Contains(args, "") {
|
|
return msg.ArgBlank(cmd.CommandPath())
|
|
}
|
|
return nil
|
|
}
|