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]>
184 lines
5.5 KiB
Go
184 lines
5.5 KiB
Go
package vm
|
|
|
|
import (
|
|
"archive/tar"
|
|
"bytes"
|
|
"io/fs"
|
|
"path"
|
|
"path/filepath"
|
|
|
|
"github.com/devvmhq/devvm/internal/hostsafety"
|
|
"github.com/devvmhq/devvm/internal/platform"
|
|
"github.com/devvmhq/devvm/internal/safefile"
|
|
)
|
|
|
|
// The bounds of a workspace clone's copy of the folder's files: each file
|
|
// at most wsFileMax bytes, all of them at most wsFilesMax, and at most
|
|
// wsFilesCount files, folders and links.
|
|
const (
|
|
wsFileMax = 10 << 20
|
|
wsFilesMax = 64 << 20
|
|
wsFilesCount = 5000
|
|
)
|
|
|
|
// workspaceFiles is a tar of the files in the folder dir outside every
|
|
// repo in it, for a workspace clone's VM: the folder's own files, which
|
|
// no repo's clone has, such as a Makefile or a compose file at its top.
|
|
// It walks dir one folder at a time, each opened from its parent's
|
|
// (safefile.Root.OpenDir) and each file by its own name in it, so no
|
|
// link on the way is followed, even one swapped in as it walks, and stays
|
|
// on dir's file system. It leaves out every folder with a .git (a repo,
|
|
// whether the clone has it or not) or shaped like a git dir (a bare
|
|
// repo), every .git, the folders of packages and build tools
|
|
// (hostsafety.NoRepos), which the VM fetches for itself, and, counting
|
|
// them (skipped), what it won't copy: a link that leads out of dir (it
|
|
// keeps one that stays in, as a link), a file with another hard link,
|
|
// anything but files, folders and links, a bare repo, a folder that
|
|
// changed as it was opened, and what's past its bounds. Files keep their
|
|
// permissions but set-id bits. nil when there's nothing to copy.
|
|
func workspaceFiles(dir string) (tarball []byte, skipped int, err error) {
|
|
root, err := safefile.OpenRoot(dir)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
defer func() { _ = root.Close() }()
|
|
top, err := root.Stat()
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
w := &filesTar{top: top}
|
|
w.tw = tar.NewWriter(&w.buf)
|
|
if err := w.folder(root, "."); err != nil {
|
|
return nil, 0, err
|
|
}
|
|
if err := w.tw.Close(); err != nil {
|
|
return nil, 0, err
|
|
}
|
|
if w.n == 0 {
|
|
return nil, w.skipped, nil
|
|
}
|
|
return w.buf.Bytes(), w.skipped, nil
|
|
}
|
|
|
|
// filesTar is one workspaceFiles's tar as it's made.
|
|
type filesTar struct {
|
|
top fs.FileInfo
|
|
buf bytes.Buffer
|
|
tw *tar.Writer
|
|
n int // entries written
|
|
size int64 // their files' bytes
|
|
skipped int
|
|
}
|
|
|
|
// folder adds what's in the folder d, at rel ("." for the top), each
|
|
// entry by its name in d.
|
|
func (w *filesTar) folder(d *safefile.Root, rel string) error {
|
|
entries, err := d.ReadDir()
|
|
if err != nil {
|
|
w.skipped++
|
|
return nil //nolint:nilerr // an unreadable folder: left out, and counted
|
|
}
|
|
for _, e := range entries {
|
|
name, p := e.Name(), path.Join(rel, e.Name())
|
|
var err error
|
|
switch {
|
|
case hostsafety.IsGitName(name):
|
|
case w.n >= wsFilesCount:
|
|
w.skipped++
|
|
case e.IsDir():
|
|
err = w.sub(d, name, p)
|
|
case e.Type()&fs.ModeSymlink != 0:
|
|
err = w.link(d, name, p)
|
|
case e.Type().IsRegular():
|
|
err = w.file(d, name, p)
|
|
default:
|
|
w.skipped++
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// sub is folder's for the folder name in d, at p: a package or build
|
|
// tool's, a repo's or one shaped like a git dir isn't entered, nor one on
|
|
// another file system or that changed as it was opened.
|
|
func (w *filesTar) sub(d *safefile.Root, name, p string) error {
|
|
if hostsafety.NoRepos(name) {
|
|
return nil
|
|
}
|
|
s, err := d.OpenDir(name)
|
|
if err != nil {
|
|
w.skipped++
|
|
return nil
|
|
}
|
|
defer func() { _ = s.Close() }()
|
|
if _, err := s.Lstat(".git"); err == nil {
|
|
return nil
|
|
}
|
|
fi, err := s.Stat()
|
|
if err != nil || !platform.SameDevice(w.top, fi) || gitDirShaped(s) {
|
|
w.skipped++
|
|
return nil
|
|
}
|
|
w.n++
|
|
if err := w.tw.WriteHeader(&tar.Header{Typeflag: tar.TypeDir, Name: p + "/", Mode: int64(fi.Mode().Perm() | 0o700), ModTime: fi.ModTime()}); err != nil {
|
|
return err
|
|
}
|
|
return w.folder(s, p)
|
|
}
|
|
|
|
// gitDirShaped reports whether the folder d has a git dir's HEAD, objects
|
|
// and refs, as a bare repo has (hostsafety.IsGitDir's test, by name).
|
|
func gitDirShaped(d *safefile.Root) bool {
|
|
for _, n := range []string{"HEAD", "objects", "refs"} {
|
|
if _, err := d.Lstat(n); err != nil {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
// link is folder's for the symbolic link name in d, at p: kept, as a
|
|
// link, when it leads to a path in the folder; else left out.
|
|
func (w *filesTar) link(d *safefile.Root, name, p string) error {
|
|
target, err := d.Readlink(name)
|
|
if err != nil || filepath.IsAbs(target) || !filepath.IsLocal(filepath.Join(filepath.Dir(p), target)) {
|
|
w.skipped++
|
|
return nil //nolint:nilerr // a link that can't be read: left out, and counted
|
|
}
|
|
w.n++
|
|
return w.tw.WriteHeader(&tar.Header{Typeflag: tar.TypeSymlink, Name: p, Linkname: target, Mode: 0o777})
|
|
}
|
|
|
|
// file is folder's for the regular file name in d, at p: never through a
|
|
// link, nor one with another hard link, which can be a file's anywhere on
|
|
// its disk (safefile's OneLink), within the bounds.
|
|
func (w *filesTar) file(d *safefile.Root, name, p string) error {
|
|
f, err := d.Open(name, safefile.Opts{OneLink: true})
|
|
if err != nil {
|
|
w.skipped++
|
|
return nil
|
|
}
|
|
defer func() { _ = f.Close() }()
|
|
size := f.Info.Size()
|
|
if size > wsFileMax || w.size+size > wsFilesMax {
|
|
w.skipped++
|
|
return nil
|
|
}
|
|
b, err := f.ReadAll(wsFileMax)
|
|
if err != nil || w.size+int64(len(b)) > wsFilesMax {
|
|
w.skipped++
|
|
return nil
|
|
}
|
|
w.n++
|
|
w.size += int64(len(b))
|
|
h := &tar.Header{Typeflag: tar.TypeReg, Name: p, Size: int64(len(b)), Mode: int64(f.Info.Mode().Perm()), ModTime: f.Info.ModTime()}
|
|
if err := w.tw.WriteHeader(h); err != nil {
|
|
return err
|
|
}
|
|
_, err = w.tw.Write(b)
|
|
return err
|
|
}
|