Files
w3cjandClaude Opus 5.5 2b176c92b3 feat(ui): every slow step shows a status line, and messages and questions never land on it
Any work that can take noticeable time now runs under the UI's status
line: a Reporter's Status with a label message, or a LoggedCmd step.

The UI:
- status lines are a stack: a Status or LoggedCmd inside another shows
  its own line, then the outer one is drawn again, where before it
  ended the outer one for good
- a message (Info, Warn, Note, Say, data on Stdout/Stderr) clears the
  line, prints and draws it again below; an unended line holds it
- a question (Ask, Confirm, Answer, Secret), the picker and
  ui.Terminal hold it until they're done
- no status line starts once the run is interrupted, as nothing would
  clear it
- the run log's path is read without the log's lock, as the status line
  reads it under outMu

Covered, by site and label:
- finder.target: finding_vm; Lima's limactl --version: checking_backend
- bringUp's checks (seeds, instance, recordFolder, hostChecks, ssh,
  requirements) and a running VM's go-on (proxy, pins, mount check):
  checking_vm; startOnce after the agent: starting; the self-check's
  probes: checking_network; pinHostKeys: reading_host_keys
- setUpVM (sync, ssh config, links, clone seed): setting_up_vm; each
  devcontainer command: running_script; makeMachine: creating;
  buildOnce: checking_image; paste's count and rm: reading_pasted
- volumeOf's lookup (diskutil), for launch, adopt, move, ls and rm:
  looking_up_volume
- vm.ListAll (devvm ls): reading_vms; devvm doctor's checks:
  doctor_checking
- devvm agent import: agent_import_reading (walk, links, opencode),
  agent_store_saving (copy, marketplace git checks, tmutil); devvm agent
  save: agent_save_reading, agent_save_comparing, agent_store_saving
- devvm stop (from its pins check on): checking_vm; a delete, an
  orphan's teardown and Lima's DeleteImage: deleting; devvm git-protect
  and clone on/off's .git work (leftBehind, mountFolder, unmountFolder):
  git_protecting; git-protect's re-read: finding_vm
- devvm clone add: clone_reading, clone_adding; clone fetch:
  clone_fetching; planClone: clone_planning; the clone check before rm
  or a remake: clone_checking; history export and import:
  history_exporting, history_importing; a remake's home: saving_home,
  restoring_home
- devvm upgrade's install: upgrade_installing; the shared tools' step
  back and .NET's downloads: resolving_tools; gc's caches: reading_space;
  the retention settings (daily and in gc): retaining

Work that takes the terminal stays off: the shell, ssh-add's
passphrase (checkAgent, Agent.Ensure, reloadAgent) and an agent's login.

Tests and guard:
- msg.Fake: OnStatus and Showing; its LoggedCmd shows its step
- backendtest.Fake: OnCall
- TestLaunchStatusLines: every backend call of a new, running and
  stopped VM's launch runs under a status line but the shell's and
  Running's (pid files)
- TestSSHAddOffStatusLine: ssh-add, on the user's agent or devvm's, never
  runs under one; chflagsCalls fails a .git lock or unlock with none
- the structure test's status-lines rule (statusSites, statusFree)
- TestStatusLinesNest, TestMessagesClearTheStatusLine,
  TestQuestionsHoldTheStatusLine, TestSecretAndPickerHoldTheStatusLine,
  TestTerminalHoldsTheStatusLine, TestNoStatusLineOnceInterrupted,
  TestFakeStatus, TestAgentImportStatus, TestAgentSaveStatus,
  TestDoctorReport, TestProblems, TestList, TestListLooksUpVolumesOnce,
  TestVolumeOfCache, TestWithSDKDownloads, TestRetainStatus,
  TestSetCloneOn, TestTeardownLeavesNothing

The rule is written in the cli skill (rule 14, checklist, review), the
codebase-standards skill (Show what devvm waits for, its checklist line
and rules.md's row), Contributing's CLI page (Status lines), its tests
and structure pages, and the troubleshooting page.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-19 11:13:13 -07:00

994 lines
34 KiB
Go

package vm
import (
"bytes"
"cmp"
"context"
"errors"
"path"
"path/filepath"
"slices"
"strconv"
"strings"
"github.com/devvmhq/devvm/internal/backend"
"github.com/devvmhq/devvm/internal/gitprotect"
"github.com/devvmhq/devvm/internal/hostsafety"
"github.com/devvmhq/devvm/internal/msg"
"github.com/devvmhq/devvm/internal/paths"
"github.com/devvmhq/devvm/internal/safefile"
"github.com/devvmhq/devvm/internal/state"
"github.com/devvmhq/devvm/internal/vmname"
)
// A workspace clone: devvm --clone in a folder of repos gives the VM a
// clone of each repo the user chooses, each at its own path, so paths
// between them (file:../lib, ../Lib/Lib.csproj) work as on the host, and
// a copy of the folder's files outside them, made once. Each repo gets a
// remote of its own for the VM, as one repo's clone does.
// The values of --repos besides a list of paths: every repo --clone can
// copy, or none of them.
const (
ReposAll = "all"
ReposNone = "none"
)
// wsRepo is a repo found in a folder of repos (findRepos): its folder,
// relative to the project folder ("." for its own), and why --clone
// can't copy it, the zero message when it can.
type wsRepo struct {
Path string
Reason msg.M
}
// ok reports whether --clone can copy the repo.
func (r wsRepo) ok() bool { return r.Reason.ID() == "" }
// findRepos finds the repos of the folder dir for a workspace clone of
// self, the VM: its own (".") when it has a .git, then those the walk
// finds below it (starter.walk: the walk .git protection makes, so the
// same depth, skipped folders and bound), each checked as --clone checks
// a repo (repoReason), and refused when another VM mounts it, or its
// common dir (mountingVM), or its path holds a character a terminal can't
// show, such as a line break (plainPath). A linked worktree of a repo in the folder isn't a repo of
// its own: its repo's clone gets that repo's current and default
// branches, not the worktree's own branch, which the user pushes with
// git push devvm BRANCH. more is how many more the walk found past its
// bound.
func (e clones) findRepos(ctx context.Context, dir string, self vmname.VM) (repos []wsRepo, more int) {
found := e.starter.walk(dir, false).Repos
var rels []string
if hasGit(dir) {
rels = append(rels, ".")
}
for _, rel := range append(rels, found.Dirs...) {
at := repoDir(dir, rel)
if worktreeIn(at, dir) || rel != "." && isSubmodule(ctx, at, dir) {
continue // its repo's clone has it
}
r := wsRepo{Path: rel, Reason: repoReason(ctx, at)}
if !plainPath(rel) {
r.Reason = msg.WorkspaceReasonName()
} else if vm := e.mountingVM(at, self); r.ok() && vm != "" {
r.Reason = msg.WorkspaceReasonMounted(vm)
}
repos = append(repos, r)
}
return repos, found.More
}
// worktreeIn reports whether the repo at at is a linked worktree of a repo
// in the folder dir, its common dir there (gitprotect.OwnRepo's).
func worktreeIn(at, dir string) bool {
gitDir, common, err := gitprotect.OwnRepo(at)
return err == nil && gitDir != common && paths.Within(filepath.Dir(common), dir)
}
// repoReason is why --clone can't copy the repo at dir, as the picker
// says it: not a repo's top folder, a .git that reads another repo's
// objects or refs (cloneSource), a grafts file (checkShape), or no
// commits; the zero message when it can. A detached
// HEAD is copied as it is.
func repoReason(ctx context.Context, dir string) msg.M {
err := checkRepoTop(ctx, dir)
m, _ := errors.AsType[msg.M](err)
switch {
case err == nil:
return msg.M{}
case m.ID() == msg.CloneNoCommits().ID():
return msg.WorkspaceReasonNoCommits()
case m.ID() == msg.CloneNotTop("").ID():
return msg.WorkspaceReasonNotRepo()
case m.ID() == msg.CloneGrafts("").ID():
return msg.WorkspaceReasonGrafts()
}
return msg.WorkspaceReasonElsewhere()
}
// isWorkspace reports whether repos make a folder a workspace clone's: a
// repo below it that --clone can copy. Without one, --clone copies the
// folder's own repo, as for a folder that holds no other.
func isWorkspace(repos []wsRepo) bool {
return slices.ContainsFunc(repos, func(r wsRepo) bool { return r.Path != "." && r.ok() })
}
// planClone is the clone section of a --clone VM, self, for the folder
// dir, as a new VM (creation.plan) or devvm clone on (canSwitch) makes it:
// in a folder of repos (isWorkspace), a workspace clone of the repos
// repos chooses (--repos: chooseRepos), else, once the folder is a repo
// --clone takes (checkCloneRepo), a clone of the folder's own. Only all
// is a value of --repos for that one. A folder that's a repo itself gets
// a clone of its own repo unless --repos is given, as devvm 0.1.0 made
// it, whatever repos are below it; one that isn't, but is in a repo's
// working tree, is refused (workspace_inside_repo), as the copy of its
// files would take that repo's.
func (e clones) planClone(ctx context.Context, dir, repos string, self vmname.VM) (cloneInVM, error) {
if err := needGit(); err != nil {
return cloneInVM{}, err
}
defer e.Status(msg.ClonePlanning(dir))()
if hasGit(dir) && repos == "" {
return e.ownClone(ctx, dir, self)
}
found, more := e.findRepos(ctx, dir, self)
if !isWorkspace(found) {
if repos != "" && repos != ReposAll {
return cloneInVM{}, msg.ReposNotWorkspace(dir)
}
return e.ownClone(ctx, dir, self)
}
if top, err := hostGit(ctx, dir, "rev-parse", "--show-toplevel"); !hasGit(dir) && err == nil && top != dir {
return cloneInVM{}, msg.WorkspaceInsideRepo(dir, top)
}
if more > 0 {
e.Warn(msg.WorkspaceReposPast(more, strconv.Itoa(hostsafety.MaxRepos)))
}
chosen, err := e.chooseRepos(dir, found, repos)
if err != nil {
return cloneInVM{}, err
}
c := cloneInVM{On: true, Workspace: true}
for _, p := range chosen {
c.Repos = append(c.Repos, cloneRepo{Path: p})
}
return c, nil
}
// ownClone is the clone section of self, a --clone VM of the folder dir's
// own repo, once --clone takes the repo (checkCloneRepo) and no other VM
// mounts it (mountingVM: clone_folder_mounted), as its seed runs host git
// there.
func (e clones) ownClone(ctx context.Context, dir string, self vmname.VM) (cloneInVM, error) {
if err := checkCloneRepo(ctx, dir); err != nil {
return cloneInVM{}, err
}
if vm := e.mountingVM(dir, self); vm != "" {
return cloneInVM{}, msg.CloneFolderMounted(dir, vm)
}
return cloneInVM{On: true}, nil
}
// chooseRepos returns the paths of the repos of found that a workspace
// clone of dir gets, in found's order: as flag (--repos) says, all, none
// or the paths it lists (pickedRepos), else in the picker, every repo
// --clone can copy ticked first and the others shown as they are, with
// why. Where the picker can't show, the question is refused, naming
// --repos. The repos asked for that --clone can't copy are left out and
// named.
func (e clones) chooseRepos(dir string, found []wsRepo, flag string) ([]string, error) {
var chosen, cannot []string
switch flag {
case "":
return e.pickRepos(dir, found)
case ReposNone:
return nil, nil
case ReposAll:
for _, r := range found {
if r.ok() {
chosen = append(chosen, r.Path)
} else {
cannot = append(cannot, r.Path)
}
}
default:
var err error
if chosen, cannot, err = pickedRepos(dir, found, flag); err != nil {
return nil, err
}
}
if len(cannot) > 0 {
e.Warn(msg.WorkspaceSkipped(quoted(cannot)))
}
return chosen, nil
}
// pickedRepos are the repos of found that list, --repos's comma-separated
// paths relative to dir, names, in found's order: those --clone can copy
// (chosen), and those it can't (cannot). A path that's none of found's
// is refused.
func pickedRepos(dir string, found []wsRepo, list string) (chosen, cannot []string, err error) {
asked := map[string]bool{}
for p := range strings.SplitSeq(list, ",") {
p = path.Clean(strings.TrimSpace(p))
if !slices.ContainsFunc(found, func(r wsRepo) bool { return r.Path == p }) {
return nil, nil, msg.WorkspaceRepoUnknown(dir, p)
}
asked[p] = true
}
for _, r := range found {
switch {
case !asked[r.Path]:
case r.ok():
chosen = append(chosen, r.Path)
default:
cannot = append(cannot, r.Path)
}
}
return chosen, cannot, nil
}
// pickRepos shows the picker of a workspace clone's repos: those --clone
// can copy, each ticked, then the others under a heading of their own,
// each with why, which can't be ticked. Where it can't show, the
// question is refused, naming --repos.
func (e clones) pickRepos(dir string, found []wsRepo) ([]string, error) {
q := msg.WorkspacePickHelp(dir).AnsweredBy("--repos")
if !e.CanPick() || e.PickMany == nil {
return nil, q.Refused(false)
}
parts := []PickPart{{}, {Heading: msg.WorkspacePickCannot()}}
for _, r := range found {
if r.ok() {
parts[0].Rows = append(parts[0].Rows, PickRow{Name: r.Path, On: true})
} else {
parts[1].Rows = append(parts[1].Rows, PickRow{Name: r.Path, Desc: r.Reason, Fixed: true})
}
}
parts = slices.DeleteFunc(parts, func(p PickPart) bool { return len(p.Rows) == 0 })
picked, err := e.PickMany(q.M(), parts)
if err != nil {
return nil, err
}
var chosen []string
for _, r := range picked[0].Rows {
if r.On && !r.Fixed {
chosen = append(chosen, r.Name)
}
}
return chosen, nil
}
// defaultBranchOf is the repo at dir's default branch's name and the
// ref it's copied from (defaultBranch), "" for none.
func defaultBranchOf(ctx context.Context, dir string) (def, from string) {
head, _ := hostGit(ctx, dir, "symbolic-ref", "-q", "refs/remotes/origin/HEAD")
list, _ := hostGit(ctx, dir, "for-each-ref", "--format=%(refname)", "refs/heads", "refs/remotes/origin")
def, from = defaultBranch(head, strings.Fields(list))
if def != "" && !validBranch(ctx, dir, def) {
def, from = "", ""
}
return def, from
}
// defaultBranch is a repo's default branch, from origin/HEAD (originHead,
// the ref it points to: "" for none) and the repo's branches and origin's
// remote-tracking branches (refs): origin/HEAD's branch, else main, else
// master, the first of them it has, as a local branch, else as origin's,
// with the ref it's copied from. "" for none.
func defaultBranch(originHead string, refs []string) (name, from string) {
var names []string
if n, ok := strings.CutPrefix(originHead, "refs/remotes/origin/"); ok && n != "HEAD" {
names = append(names, n)
}
for _, n := range append(names, "main", "master") {
for _, ref := range []string{"refs/heads/" + n, "refs/remotes/origin/" + n} {
if slices.Contains(refs, ref) {
return n, ref
}
}
}
return "", ""
}
// repoSeed is how a --clone VM's seed copies one repo into the VM: its
// path, relative to the project folder ("." for the folder's own), and
// folder, its object format, what its clone checks out (head: a branch's
// full name, "" for a detached HEAD, which it's detached at), the refs
// its seed sends (seedTips), and whether the repo is a linked worktree of
// a repo elsewhere, which sends less.
type repoSeed struct {
path, dir, format, head string
refs []refPair
worktree bool
// landed: the VM has the repo seeded already, by an earlier copy
// (initRepos), so only its remote is left to add.
landed bool
}
// planSeed is how the repo at dir, path in the project folder, is copied,
// once it's checked: what it has checked out (headOf), its object format,
// and the refs it sends (seedTips).
func planSeed(ctx context.Context, path, dir string) (repoSeed, error) {
return planSeedOf(ctx, path, dir, false)
}
// planSeedOf is planSeed, of a submodule of a --clone repo when sub
// (repoSource).
func planSeedOf(ctx context.Context, path, dir string, sub bool) (repoSeed, error) {
worktree, err := repoSource(ctx, dir, sub)
if err != nil {
return repoSeed{}, err
}
s := repoSeed{path: path, dir: dir, head: headOf(ctx, dir), worktree: worktree}
if s.format, err = objectFormat(ctx, dir); err != nil {
return repoSeed{}, err
}
if s.refs, err = seedTips(ctx, dir, s.head, worktree); err != nil {
return repoSeed{}, err
}
return s, nil
}
// prepareSeed checks the repo at path in t's folder as --clone checks a
// repo it copies (stillRepo), again, as the folder may have changed since
// the VM was made, and returns how it's copied (planSeed): a repo whose
// remote names are all taken is refused.
func (e seeder) prepareSeed(ctx context.Context, t *target, rel string) (repoSeed, error) {
dir := repoDir(t.f.Dir, rel)
if err := e.stillRepo(ctx, t, rel, false); err != nil {
return repoSeed{}, err
}
if _, err := freeRemote(ctx, t.f, dir); err != nil {
return repoSeed{}, err
}
return planSeed(ctx, rel, dir)
}
// sayHow says, with info, what's particular about how s is copied: a
// linked worktree's seed sends less (clone_worktree_tips, or
// workspace_worktree_tips for a workspace repo), and a workspace repo's
// detached HEAD is detached in the VM too (workspace_detached).
func sayHow(info func(msg.M), s repoSeed, workspace bool) {
switch {
case s.worktree && workspace:
info(msg.WorkspaceWorktreeTips(s.path))
case s.worktree:
info(msg.CloneWorktreeTips())
}
if workspace && s.head == "" {
info(msg.WorkspaceDetached(s.path))
}
}
// stillRepo checks, before host git reads or writes the repo at rel in
// t's workspace clone's folder, that it's still the top of a repo of its
// own (checkRepoTop: its error), and that no other VM mounts it, which
// could swap it (finder.mountsRepo: clone_repo_mounted); sub, a
// submodule of a --clone repo (checkRepoTopOf).
func (e clones) stillRepo(ctx context.Context, t *target, rel string, sub bool) error {
dir := repoDir(t.f.Dir, rel)
if err := checkRepoTopOf(ctx, dir, sub); err != nil {
return err
}
if vm := e.mountingVM(dir, t.f.Name); vm != "" {
return msg.CloneRepoMounted(rel, vm)
}
return nil
}
// mountingVM is the VM other than self that mounts the repo at dir (its
// own, or a parent folder's: finder.mountsRepo, unlike finder.mountedBy,
// a launch's warning, which looks at the parents alone), or,
// for a linked worktree, the repo its common dir is in, whose objects and
// refs its git reads and writes (finder.mountsRepo); "" for none.
func (e clones) mountingVM(dir string, self vmname.VM) vmname.VM {
if vm := e.finder.mountsRepo(dir, self); vm != "" {
return vm
}
if gitDir, common, err := gitprotect.OwnRepo(dir); err == nil && gitDir != common {
return e.finder.mountsRepo(filepath.Dir(common), self)
}
return ""
}
// initRepos makes the project folder and the repos of seeds, if any, in
// t's running VM (initRepos), its command carrying the VM's agent
// (target.exec), and, when the clone has the folder's own repo, adds
// exclude (repoExcludes) to that repo's info/exclude in the VM. Each seed
// the VM has seeded already is marked landed, so it isn't sent again
// (seedRepo). A VM whose git can't make a SHA-256 repo is refused, naming
// its git. An empty repo an earlier devvm made with another object
// format is made again once the user agrees (askRemake).
func (e seeder) initRepos(ctx context.Context, t *target, seeds []repoSeed, exclude []byte) error {
a, err := e.runInit(ctx, t, seeds, exclude, nil)
if err == nil && len(a.format) > 0 {
if err := e.askRemake(t.f.Name, a.format); err != nil {
return err
}
// exclude went in already, unless into the folder's own repo,
// which is made again.
again := exclude
if !slices.Contains(a.format, ".") {
again = nil
}
a, err = e.runInit(ctx, t, seeds, again, a.format)
if err == nil && len(a.format) > 0 {
err = msg.CloneSeedFailed(t.f.Name)
}
}
if err != nil {
return err
}
for i := range seeds {
seeds[i].landed = slices.Contains(a.landed, seeds[i].path)
}
return nil
}
// runInit runs initRepos in t's VM for seeds, remaking the empty repos at
// the paths remake names, and returns the VM's answer.
func (e seeder) runInit(ctx context.Context, t *target, seeds []repoSeed, exclude []byte, remake []string) (initAnswer, error) {
ex := "-"
if exclude != nil {
ex = "exclude"
}
argv := []string{"sh", "-c", initRepos, "sh", t.f.Dir, ex}
for _, s := range seeds {
how := "-"
if slices.Contains(remake, s.path) {
how = "remake"
}
argv = append(argv, s.path, s.format, how)
}
log := e.vmLog()
out := &bounded{max: cloneOutMax}
err := t.exec(ctx, argv, backend.ExecOpts{Stdin: bytes.NewReader(exclude), Stdout: out, Stderr: log})
a := parseInit(out.String(), seeds)
switch {
case ctx.Err() != nil:
return a, ctx.Err()
case a.oldGit != "":
return a, msg.CloneVMGitOld(a.oldGit, t.f.Name)
case err != nil:
return a, backend.Logged(msg.CloneSeedFailed(t.f.Name))
}
return a, nil
}
// excludeMax is the most of the host repo's info/exclude repoExcludes
// takes.
const excludeMax = 64 << 10
// repoExcludes is what the VM's clone of the folder's own repo, when the
// workspace clone c has it, gets in its info/exclude, so its git status
// leaves out what's the host's to leave out: each of the clone's other
// repos, of paths (all of them, or those added), which are its own
// clones, then, withHost, the host repo's own info/exclude, such as a
// .worktrees/ line. nil when c hasn't the folder's own repo.
func repoExcludes(ws string, c cloneInVM, rels []string, withHost bool) []byte {
if !slices.ContainsFunc(c.repos(), func(r cloneRepo) bool { return r.Path == "." }) {
return nil
}
var b bytes.Buffer
for _, p := range rels {
if p != "." && plainPath(p) {
b.WriteString(excludeLine(p))
}
}
if withHost {
if host, err := safefile.Read(filepath.Join(ws, ".git", "info", "exclude"), excludeMax, safefile.Opts{}); err == nil {
b.Write(host)
b.WriteString("\n")
}
}
return b.Bytes()
}
// excludeLine is the info/exclude line that leaves out the folder p,
// relative to the repo's top, and nothing else: anchored with a / at
// each end, so a ! or # can't start it and no space ends it, and git's
// pattern characters in it escaped, so lib[2] is lib[2] alone.
func excludeLine(p string) string {
var b strings.Builder
b.WriteString("/")
for _, r := range p {
if strings.ContainsRune(`\*?[`, r) {
b.WriteByte('\\')
}
b.WriteRune(r)
}
b.WriteString("/\n")
return b.String()
}
// plainPath reports whether p, a repo's path relative to the project
// folder, has only characters a terminal shows as they are (msg.Safe):
// no control character, such as a line break, which no line of a git
// exclude, nor of a message, can hold.
func plainPath(p string) bool {
return !strings.ContainsFunc(p, func(r rune) bool { return !msg.Safe(r) })
}
// seedRepo copies one repo into t's running VM, whose repo initRepos
// made: checked again just before (stillRepo), its remote chosen once the
// earlier repos' are added (freeRemote: two worktrees of one repo share a
// config), its branches sent (seed) unless an earlier copy landed
// (s.landed: the VM's branches may have moved on since), then its remote
// for the VM added, each recorded in the clone section as it's done, so a
// retry, rm and an interrupt find it. It returns the remote.
func (e seeder) seedRepo(ctx context.Context, t *target, s repoSeed) (string, error) {
vm, st := t.f.Name, t.st
if err := e.stillRepo(ctx, t, s.path, false); err != nil {
return "", err
}
remote, err := freeRemote(ctx, t.f, s.dir)
if err != nil {
return "", err
}
if err := update(ctx, st, cloneSection, func(c *cloneInVM) error {
c.setRepo(s.path, func(r *cloneRepo) { r.Remote = remote })
return nil
}); err != nil {
return "", err
}
if !s.landed {
if err := e.seed(ctx, t, s, msg.CopyingRepo(s.path)); err != nil {
return "", err
}
}
if _, ok := remoteURL(ctx, s.dir, remote); !ok {
if _, err := hostGit(ctx, s.dir, "remote", "add", remote, cloneURL(vm, s.dir)); err != nil {
return "", msg.RemoteAddFailed(remote)
}
}
// Before the repo is recorded as copied, so a try again copies those
// it didn't.
if err := e.seedSubmodules(ctx, t, s.path); err != nil {
return "", err
}
return remote, update(ctx, st, cloneSection, func(c *cloneInVM) error {
c.setRepo(s.path, func(r *cloneRepo) { r.Seeded = true })
return nil
})
}
// seedWorkspace makes a workspace clone in t's running VM, as seedClone
// makes one repo's: each repo not yet copied (prepareSeed: one that no
// longer passes is left out, named, and dropped from the clone section),
// made at once (initRepos), then copied one by one (seedRepo), the
// folder's files outside them copied once (copyFiles), and the clone
// recorded as made. Then it offers the ~/.ssh/config line that the
// remotes need.
func (e seeder) seedWorkspace(ctx context.Context, t *target) error {
st := t.st
c := sectionOf[cloneInVM](st, cloneSection)
var seeds []repoSeed
var dropped, all []string
for _, r := range c.repos() {
all = append(all, r.Path)
if r.Seeded {
continue
}
s, err := e.prepareSeed(ctx, t, r.Path)
if err != nil {
if ctx.Err() != nil {
return ctx.Err()
}
e.Warn(msg.WorkspaceRepoDropped(r.Path))
dropped = append(dropped, r.Path)
continue
}
seeds = append(seeds, s)
}
if err := update(ctx, st, cloneSection, func(c *cloneInVM) error {
c.Repos = slices.DeleteFunc(c.Repos, func(r cloneRepo) bool { return slices.Contains(dropped, r.Path) })
return nil
}); err != nil {
return err
}
c = sectionOf[cloneInVM](st, cloneSection)
if err := e.initRepos(ctx, t, seeds, repoExcludes(t.f.Dir, c, all, true)); err != nil {
return err
}
remote := ""
for _, s := range seeds {
if !s.landed {
sayHow(e.Info, s, true)
}
r, err := e.seedRepo(ctx, t, s)
if err != nil {
return err
}
remote = cmp.Or(remote, r)
}
if !c.Made.Files {
if err := e.copyFiles(ctx, t); err != nil {
return err
}
}
if err := update(ctx, st, cloneSection, func(c *cloneInVM) error { c.Made.Files, c.Made.Seeded = true, true; return nil }); err != nil {
return err
}
// git fetch and git push to a repo's remote need the Include line;
// devvm clone fetch doesn't.
if ok, err := e.sshConfig().EnsureInclude(e.Dirs.Home); err != nil {
return err
} else if !ok && remote != "" {
e.Warn(msg.FetchUntil(e.Dirs.SSHConfig(), remote))
}
e.Info(msg.WorkspaceReady(len(sectionOf[cloneInVM](st, cloneSection).Repos)))
return nil
}
// copyFiles copies the files of t's project folder outside every repo in
// it (workspaceFiles) into its running VM, at the same paths, once, and
// says how many it left out. A folder that's a repo itself has no files
// of its own outside it: its files are its repo's, chosen or not.
func (e seeder) copyFiles(ctx context.Context, t *target) error {
if hasGit(t.f.Dir) {
return nil
}
tarball, skipped, err := workspaceFiles(t.f.Dir)
if err != nil {
return msg.WorkspaceFilesFailed(t.f.Dir).Because(err)
}
if skipped > 0 {
e.Warn(msg.WorkspaceFilesSkipped(skipped))
}
if tarball == nil {
return nil
}
log := e.vmLog()
argv := []string{"sh", "-c", `exec tar -x -f - --no-same-owner -C "$1"`, "sh", t.f.Dir}
if err := t.exec(ctx, argv, backend.ExecOpts{Stdin: bytes.NewReader(tarball), Stdout: log, Stderr: log}); err != nil {
if ctx.Err() != nil {
return ctx.Err()
}
return backend.Logged(msg.CloneSeedFailed(t.f.Name))
}
return nil
}
// Clone is devvm clone fetch and devvm clone add: the work of a --clone
// VM's repos fetched into this machine's, and a repo added to a
// workspace clone.
type Clone struct{ *Env }
// Fetched is what devvm clone fetch brought into each repo of the clone:
// the VM's branches that are new or moved in its remote's
// remote-tracking branches, by repo.
type Fetched struct {
Repos []FetchedRepo
// Checked is how many repos it fetched from, those with nothing new
// too: none when it left every one alone (each named).
Checked int
}
// FetchedRepo is one repo's fetch: its path relative to the project
// folder ("." for the folder's own), its remote, the branches new or
// moved, as the VM named them, and the local branches it saved the VM's
// own work into, which no branch has: its detached HEAD's commits
// (saved/<remote>/HEAD) and its stash entries (saved/<remote>/stash-N).
type FetchedRepo struct {
Path, Remote string
Branches []string
Saved []string
}
// FetchedBranch is a branch name and the repos it's new or moved in.
type FetchedBranch struct {
Name string
Repos []string
}
// ByBranch is f by branch name, each with its repos in the clone's order,
// sorted by name: one name across repos is one piece of work.
func (f Fetched) ByBranch() []FetchedBranch {
at := map[string]int{}
var out []FetchedBranch
for _, r := range f.Repos {
for _, b := range r.Branches {
i, ok := at[b]
if !ok {
i = len(out)
at[b] = i
out = append(out, FetchedBranch{Name: b})
}
out[i].Repos = append(out[i].Repos, r.Path)
}
}
slices.SortFunc(out, func(a, b FetchedBranch) int { return strings.Compare(a.Name, b.Name) })
return out
}
// Fetch fetches the VM's branches into each repo of the folder's --clone
// VM, starting it if it's stopped: every branch into its remote's
// remote-tracking branches (fetchInto, as rm's fetch does), and returns
// those new or moved, its recorded submodules' too (allRepos), each by
// its path. A repo whose remote was removed gets it again; one whose
// remote points elsewhere now is left alone and named.
func (e Clone) Fetch(ctx context.Context) (Fetched, error) {
t, err := e.finder.target(ctx, need{has: hasMade})
if err != nil {
return Fetched{}, err
}
vm := t.f.Name
if !isClone(t.st) {
return Fetched{}, msg.CloneFetchMounted(vm)
}
if err := t.ensureRunning(ctx); err != nil {
return Fetched{}, err
}
defer e.Status(msg.CloneFetching(vm))()
var out Fetched
for _, r := range sectionOf[cloneInVM](t.st, cloneSection).allRepos() {
// A remote is recorded before its copy: one whose copy didn't
// finish may have the VM's work too.
if r.Remote == "" {
continue
}
got, err := e.fetchRepo(ctx, t, r)
if err != nil {
return out, err
}
if got.Path != "" { // fetched from, not left alone
out.Checked++
}
if len(got.Branches) > 0 || len(got.Saved) > 0 {
out.Repos = append(out.Repos, got)
}
}
return out, nil
}
// fetchRepo is Fetch's of the repo r.
func (e Clone) fetchRepo(ctx context.Context, t *target, r cloneRepo) (FetchedRepo, error) {
vm, dir := t.f.Name, repoDir(t.f.Dir, r.Path)
url := cloneURL(vm, dir)
// Fetched into only while it's still that repo, not one above it or
// put in its place, and no other VM can swap it.
if err := e.clones.stillRepo(ctx, t, r.Path, r.submodule); err != nil {
switch m, ok := errors.AsType[msg.M](err); {
case ok && m.ID() == msg.CloneRepoMounted("", "").ID():
e.Warn(m)
case r.submodule:
e.Warn(msg.CloneSubmoduleUnchecked(r.Path))
default:
e.Warn(msg.CloneRepoChanged(r.Path))
}
return FetchedRepo{}, nil
}
switch got, ok := remoteURL(ctx, dir, r.Remote); {
case !ok:
if _, err := hostGit(ctx, dir, "remote", "add", r.Remote, url); err != nil {
return FetchedRepo{}, msg.RemoteAddFailed(r.Remote)
}
case got != url:
e.Warn(msg.CloneRemoteElsewhere(r.Path, r.Remote))
return FetchedRepo{}, nil
}
before := trackingRefs(ctx, dir, r.Remote)
fail := msg.CloneFetchFailed(r.Path, vm)
if err := e.clones.fetchInto(ctx, t, dir, false, msg.FetchingRepo(r.Path), fail, "+refs/heads/*:refs/remotes/"+r.Remote+"/*"); err != nil {
return FetchedRepo{}, err
}
out := FetchedRepo{Path: r.Path, Remote: r.Remote}
for name, sha := range trackingRefs(ctx, dir, r.Remote) {
if before[name] != sha {
out.Branches = append(out.Branches, name)
}
}
slices.Sort(out.Branches)
saved, err := e.fetchOwn(ctx, t, dir, r.Remote, fail)
out.Saved = saved
return out, err
}
// ownWork is the script that prints what the clone at $1 has that no
// branch fetch brings: "SHA HEAD" for the commit its HEAD is detached at,
// when no branch has it, and "SHA stash@{N}" for each stash entry its
// seed didn't store (seedStashRecord), as cloneState does.
const ownWork = `if ! git -C "$1" symbolic-ref -q HEAD >/dev/null 2>&1 && [ -n "$(git -C "$1" rev-list -n1 HEAD --not --branches 2>/dev/null)" ]; then
printf '%s HEAD\n' "$(git -C "$1" rev-parse -q --verify HEAD)"
fi
if git -C "$1" rev-parse -q --verify refs/stash >/dev/null 2>&1; then
git -C "$1" reflog show --format=%H refs/stash -- | {
n=0
while read -r h; do
grep -qxF -- "$h" "$1/.git/` + seedStashRecord + `" 2>/dev/null || printf '%s stash@{%s}\n' "$h" "$n"
n=$((n+1))
done
}
fi`
// fetchOwn brings back what only the clone at dir in t's VM has, which a
// fetch of its branches doesn't (ownWork): when the repo on this machine
// lacks them, the commits of a detached HEAD into a new local branch,
// saved/<remote>/HEAD, and each of the VM's own stash entries into
// saved/<remote>/stash-N (savedPrefix, savedNames), never over one that
// exists, as rm does. It returns those branches, none when there was
// nothing to bring.
func (e Clone) fetchOwn(ctx context.Context, t *target, dir, remote string, fail msg.M) ([]string, error) {
out := &bounded{max: cloneOutMax}
if err := t.exec(ctx, []string{"sh", "-c", ownWork, "sh", dir}, backend.ExecOpts{Stdout: out, Stderr: e.vmLog()}); err != nil && ctx.Err() != nil {
return nil, ctx.Err()
}
if out.over {
return nil, fail
}
own, _ := savedNames(ctx, dir, unfetchedRefs(ctx, dir, ownLines(out.Bytes())))
if len(own) == 0 {
return nil, nil
}
prefix, ok := savedPrefix(ctx, dir, remote)
if !ok {
return nil, msg.SavedNamesTakenFetch(remote)
}
saved, err := e.clones.fetchSaved(ctx, t, dir, prefix, own, false, msg.Fetching(prefix), fail)
if len(saved) > 0 {
e.Info(msg.SavedBranches(quoted(saved)))
}
return saved, err
}
// ownLines are those of the lines ownWork printed that it prints: "SHA
// HEAD" and "SHA stash@{N}", so the VM can't have devvm clone fetch save
// a branch of any other name.
func ownLines(out []byte) []byte {
var b bytes.Buffer
for line := range bytes.Lines(out) {
_, ref, _ := strings.Cut(strings.TrimSpace(string(line)), " ")
if _, stash := stashIndex(ref); ref == "HEAD" || stash {
b.Write(line)
}
}
return b.Bytes()
}
// trackingRefs are remote's remote-tracking branches in the repo at dir,
// by branch name, each with its commit.
func trackingRefs(ctx context.Context, dir, remote string) map[string]string {
prefix := "refs/remotes/" + remote + "/"
out, _ := hostGit(ctx, dir, "for-each-ref", "--format=%(objectname) %(refname)", prefix)
refs := map[string]string{}
for line := range strings.Lines(out) {
sha, ref, _ := strings.Cut(strings.TrimSpace(line), " ")
if name, ok := strings.CutPrefix(ref, prefix); ok && name != "HEAD" {
refs[name] = sha
}
}
return refs
}
// Add copies the repo at dir, a folder in the --clone VM's project folder
// (or below the folder devvm runs in, when relative), into the VM, at its
// path, starting the VM if it's stopped, as a workspace clone's seed
// copies each (prepareSeed, then seedRepo), and returns its path relative
// to the project folder. A clone of the folder's own repo becomes a
// workspace clone of it. A repo the clone has already is said to be.
func (e Clone) Add(ctx context.Context, dir string) error {
t, err := e.finder.target(ctx, need{has: hasMade})
if err != nil {
return err
}
vm, ws := t.f.Name, t.f.Dir
if !isClone(t.st) {
return msg.CloneAddMounted(vm)
}
rel, err := e.addPath(ws, dir)
if err != nil {
return err
}
c := sectionOf[cloneInVM](t.st, cloneSection)
i := slices.IndexFunc(c.repos(), func(r cloneRepo) bool { return r.Path == rel })
// A submodule recorded below its repo is the clone's already, copied
// or to be copied with its repo, never a repo of its own besides.
sub := slices.ContainsFunc(c.allRepos(), func(r cloneRepo) bool { return r.submodule && r.Path == rel })
if i >= 0 && c.repos()[i].Seeded || sub {
e.Info(msg.CloneAddAlready(rel, vm))
return nil
}
if err := needGit(); err != nil {
return err
}
sd := seeder{e.clones}
stop := e.Status(msg.CloneReading(rel))
s, err := sd.prepareSeed(ctx, t, rel)
stop()
if err != nil {
return err
}
if err := t.ensureRunning(ctx); err != nil {
return err
}
// Once it runs: its start's agent may ask for a passphrase.
defer e.Status(msg.CloneAdding(rel, vm))()
// Recorded first, so rm checks it however far the copy gets; one
// recorded whose copy didn't finish is copied again.
if i < 0 {
if err := update(ctx, t.st, cloneSection, func(c *cloneInVM) error { addRepo(c, rel); return nil }); err != nil {
return err
}
}
c = sectionOf[cloneInVM](t.st, cloneSection)
// The folder's own repo added: its clone leaves out every other one,
// and what the host's leaves out; another added: that one.
exclude := repoExcludes(ws, c, []string{rel}, false)
if rel == "." {
var all []string
for _, r := range c.repos() {
all = append(all, r.Path)
}
exclude = repoExcludes(ws, c, all, true)
}
seeds := []repoSeed{s}
if err := sd.initRepos(ctx, t, seeds, exclude); err != nil {
return err
}
s = seeds[0]
if !s.landed {
sayHow(e.Info, s, true)
}
remote, err := sd.seedRepo(ctx, t, s)
if err != nil {
return err
}
e.Info(msg.CloneAdded(rel, remote))
return nil
}
// addRepo adds the repo at rel to the clone section c, unseeded: a clone
// of the folder's own repo becomes a workspace clone of it first, with
// its remote and seed, and its files made (it had none to copy).
func addRepo(c *cloneInVM, rel string) {
if !c.Workspace {
*c = cloneInVM{
On: c.On, Workspace: true, Repos: c.repos(),
Made: cloneMade{Seeded: c.Made.Seeded, Files: true},
}
}
c.Repos = append(c.Repos, cloneRepo{Path: rel})
}
// addPath is devvm clone add's folder, dir, relative to the project
// folder ws: resolved (from the folder devvm runs in, when relative), and
// in ws.
func (e Clone) addPath(ws, dir string) (string, error) {
if !filepath.IsAbs(dir) {
here, err := e.finder.dir()
if err != nil {
return "", err
}
dir = filepath.Join(here, dir)
}
abs, err := filepath.EvalSymlinks(dir)
if err != nil {
return "", msg.CloneAddMissing(dir)
}
if !paths.Within(abs, ws) {
return "", msg.CloneAddOutside(abs, ws)
}
rel, err := filepath.Rel(ws, abs)
if err != nil {
return "", msg.CloneAddOutside(abs, ws)
}
if !plainPath(rel) {
return "", msg.CloneAddName()
}
return filepath.ToSlash(rel), nil
}
// coversRepo reports whether st is a workspace clone's state, of the
// project folder ws, whose repos include the one at repo: the finder
// uses that VM in the repo's folders, as the clone has the repo at the
// same path.
func coversRepo(st *state.VM, ws, repo string) bool {
c := sectionOf[cloneInVM](st, cloneSection)
rel, err := filepath.Rel(ws, repo)
if !c.On || !c.Workspace || err != nil {
return false
}
return slices.ContainsFunc(c.Repos, func(r cloneRepo) bool { return r.Path == filepath.ToSlash(rel) })
}