Repository navigation
feat(snapshot): offer snapshots from sibling Git worktrees, by name, latest, or --list (#8733) [skip ci] - #8733
Conversation
|
Download this PR's build for your OS and architecture with: ddev utility download-ddev --pr 8733Or download the artifact for your OS and architecture directly:
See Testing a PR. |
d9db688 to
0f4556f
Compare
|
@tyler36 this was an attempt to do what you were hoping for in the other PR. The new snapshot features in this release made it far more possible. Please take it for a spin and see what you think. |
ddev snapshot restore8ab7d64 to
c903445
Compare
stasadev
left a comment
There was a problem hiding this comment.
I followed the manual testing instructions, and the logic looks pretty straightforward, including automatically selecting the newest snapshot with the same name in a diff.
I have a question about logging errors for git worktree.
| // otherWorktreeProjectRoots returns the directories where this same project | ||
| // would live in every other git worktree of its repository: the project's | ||
| // path relative to its own worktree root, re-joined onto each of the | ||
| // repository's other worktree roots. It returns nil if the project isn't in a | ||
| // git worktree, or the repository has no other worktrees - callers should | ||
| // treat that as "nothing extra to offer", not an error. | ||
| func (app *DdevApp) otherWorktreeProjectRoots() []string { | ||
| repoRootOutput, err := exec.RunHostCommand("git", "-C", app.AppRoot, "rev-parse", "--show-toplevel") | ||
| if err != nil { | ||
| return nil | ||
| } | ||
| repoRoot := strings.TrimSpace(repoRootOutput) | ||
| if repoRoot == "" { | ||
| return nil | ||
| } | ||
|
|
||
| // git's own idea of app.AppRoot's path relative to repoRoot, rather than | ||
| // filepath.Rel(repoRoot, app.AppRoot): on macOS in particular, app.AppRoot | ||
| // may reach the project through a symlink (e.g. /var/folders/... into | ||
| // /private/var/folders/...) that "rev-parse --show-toplevel" already | ||
| // resolved, so comparing the two paths directly can misjudge how deep the | ||
| // project sits under the repo root. | ||
| prefixOutput, err := exec.RunHostCommand("git", "-C", app.AppRoot, "rev-parse", "--show-prefix") | ||
| if err != nil { | ||
| return nil | ||
| } | ||
| relToRepo := strings.TrimSuffix(strings.TrimSpace(prefixOutput), "/") | ||
|
|
||
| worktreeListOutput, err := exec.RunHostCommand("git", "-C", repoRoot, "worktree", "list", "--porcelain") | ||
| if err != nil { | ||
| return nil | ||
| } | ||
|
|
||
| var projectRoots []string | ||
| for line := range strings.SplitSeq(strings.TrimSpace(worktreeListOutput), "\n") { | ||
| worktreeRoot, ok := strings.CutPrefix(line, "worktree ") | ||
| if !ok { | ||
| continue | ||
| } | ||
| worktreeRoot = strings.TrimSpace(worktreeRoot) | ||
| if worktreeRoot == "" || worktreeRoot == repoRoot { | ||
| continue | ||
| } | ||
| projectRoots = append(projectRoots, filepath.Join(worktreeRoot, relToRepo)) | ||
| } | ||
|
|
||
| return projectRoots | ||
| } |
There was a problem hiding this comment.
If git changes its command syntax, isn't installed, or something else happens, we don't have a good way to debug it or understand what went wrong without checking it line by line.
I don't think it's useful to show util.Warning everywhere, but what if we send all errors to util.Debug instead?
There was a problem hiding this comment.
Rebased and changed to util.Debug(), also did rethink about what happens if there's no git or no worktrees. Thanks for the good thinking.
…napshot restore` ## The Issue Related to ddev#8623 ("Allow worktrees to share DB snapshots" by @tyler36): a snapshot taken in one git worktree of a project isn't visible to `ddev snapshot restore` in another worktree of the same repository, so reusing a database across worktrees means manually copying snapshot files around. ddev#8623 predates the path-based snapshot restore/seed support that's landed on `main` since it was opened (ddev#8704, ddev#8705), and its `RestoreSnapshot(name, sourceRoot)` signature no longer matches `RestoreSnapshot(name, force)` on `main` - it needs a rewrite on top of current code either way. This is that rewrite, built directly on the newer mechanism instead of duplicating it. ## How This PR Solves The Issue `ddev snapshot restore` and `ddev snapshot restore --latest` already support restoring from an arbitrary host path via `resolveSnapshotSource`. This adds automatic discovery on top of that, rather than a parallel restore mechanism: - `DdevApp.ListSnapshotRestoreTargets()` lists the current project's own snapshots plus any snapshots found in the same project checked out in other worktrees of the same git repository (via `git worktree list`), newest first. A worktree match requires the same repository and the same relative project path - determined entirely from git and the filesystem, never from the `.ddev/config.yaml` project name (worktree projects don't usually share a name). A separate clone that happens to sit at the same relative path is not offered. - `DdevApp.GetLatestSnapshotRestoreTarget()` extends `--latest` the same way. - The interactive picker in `ddev snapshot restore` shows a worktree snapshot's name and source directory (`snapshot-name (worktree-dir)`); selecting one restores by its absolute path, reusing the existing path-based restore/bind-mount plumbing rather than a copy step. I considered going further with a repository-agnostic shared snapshot pool keyed by project name (so any two checkouts with the same name could share snapshots, not just worktrees of one repo). I left that out: project names collide too easily across unrelated projects, and a git worktree relationship is a precise, verifiable identity check that a name is not. The fully general case - "restore a snapshot from anywhere on disk" - is already covered by the existing path argument. ## Manual Testing Instructions 1. Create a git repo and a worktree of it, each with a DDEV project at the same relative path (project names deliberately differ, since that's the normal case for worktrees): ```bash git init repo && cd repo && git commit --allow-empty -m init ddev config --project-type=php --project-name=repo-a ddev start ddev snapshot --name=shared-snapshot cd .. && git -C repo worktree add ../repo-b HEAD cd repo-b ddev config --project-type=php --project-name=repo-b ddev start ``` 2. `ddev snapshot restore` in `repo-b` - the picker should list `shared-snapshot (repo)` alongside any of `repo-b`'s own snapshots. `ddev snapshot restore --latest` should restore it directly. 3. A separate `git clone` of `repo` at the same relative depth should *not* have its snapshots offered from `repo-b`. ## Automated Testing Overview Covered in the following commit, along with a fix for a real bug found while writing it. ## Release/Deployment Notes No config or behavior changes for projects outside a git worktree. Within a worktree, `ddev snapshot restore` and `--latest` now also see snapshots from sibling worktrees of the same project; this is additive and doesn't change what a bare snapshot name resolves to. Co-Authored-By: Claude Sonnet 5 <[email protected]>
…ee siblings The previous commit's otherWorktreeProjectRoots() computed the current project's path relative to its repo root with filepath.Rel(repoRoot, app.AppRoot), where repoRoot came from `git rev-parse --show-toplevel`. That silently breaks when app.AppRoot reaches the project through a symlink that show-toplevel already resolved - notably macOS, where a temp directory (and so, in the new test, t.TempDir()) commonly lives under /var/folders/... which is itself a symlink to /private/var/folders/.... filepath.Rel then compares a resolved path against an unresolved one and returns nonsense like "../../../../../var/folders/.../repo/web" instead of "web", which can make another worktree's project directory resolve back to the current project itself instead of to the sibling worktree. Fixed by asking git for the relative path instead of computing it with filepath.Rel: `git rev-parse --show-prefix` (run from app.AppRoot) returns the current directory's path relative to the repo's top level, computed entirely within git's own resolved view, so it never mixes resolved and unresolved forms of the same path. TestListSnapshotRestoreTargetsWorktrees, added here, reproduces this with a real git repo and worktree under t.TempDir() and fails without the fix. Co-Authored-By: Claude Sonnet 5 <[email protected]>
…name>` and `--list` ## Short Summary (TL;DR) `ddev snapshot restore <name>` and `ddev snapshot --list` only looked at a project's own `.ddev/db_snapshots`, so a snapshot living in a sibling git worktree of the same project - already offered by the interactive picker and `--latest` - was invisible to a bare name or the list table. This extends both to fall back to the same sibling-worktree lookup. ## The Issue Follow-up to the sibling-worktree snapshot discovery added in 8b7c395 and 0f4556f. That work wired `ListSnapshotRestoreTargets()` into the interactive picker and `--latest` only; a snapshot name given directly on the command line still resolved solely against the current project's own directory, and `--list` never looked past it either. ## How This PR Solves The Issue - `DdevApp.ResolveSnapshotRestoreTarget()` resolves a bare name given to `ddev snapshot restore <name>`: the project's own snapshot wins if one exists by that name (unchanged behavior), otherwise it falls back to the newest same-named snapshot in a sibling worktree, resolving to its absolute path so the existing path-based restore plumbing handles it. A path argument, or a name matching nothing, is returned unchanged. - `SnapshotRestoreTarget` now also carries `Size`/`DBVersion`/`Compression`, so `ddev snapshot --list` can render sibling-worktree rows with the same detail as the project's own. `listSnapshots()` switches from `ListSnapshots()` to `ListSnapshotRestoreTargets()` and adds a `Worktree` column naming the source directory - shown only when a sibling snapshot is actually present, so a project with no worktree siblings sees no change in output. ## Manual Testing Instructions 1. In a project checked out as a git worktree with a sibling worktree of the same project that has its own snapshots: - `ddev snapshot --list` should show the sibling's snapshots too, each row's `Worktree` column naming its source directory; the project's own rows leave that column blank. - `ddev snapshot restore <sibling-snapshot-name>` should restore it directly, without needing `--latest` or the interactive picker. 2. In a project with no sibling worktree snapshots, `ddev snapshot --list` should show no `Worktree` column at all. ## Automated Testing Overview - `pkg/ddevapp/snapshot_test.go`: `TestResolveSnapshotRestoreTargetWorktrees` covers own-name resolution, sibling-only fallback, an unresolvable name passing through unchanged, and a path never being treated as a lookup name. The existing worktree fixture is factored into `newSnapshotWorktreeFixture` and shared with `TestListSnapshotRestoreTargetsWorktrees`, which now also asserts the new `DBVersion` field. - `cmd/ddev/cmd/snapshot_list_test.go` (new): `TestListSnapshotsNoSnapshots` and `TestListSnapshotsWorktrees` exercise `listSnapshots()` directly against filesystem/git fixtures (no Docker), covering the empty case, the `Worktree` column appearing only when a sibling snapshot exists, and its labeling. ## Release/Deployment Notes No behavior change for projects outside a git worktree, or where a named snapshot already exists locally. Within a worktree, a bare snapshot name and `--list` now also see sibling-worktree snapshots, matching what the interactive picker and `--latest` already offered. Co-Authored-By: Claude Sonnet 5 <[email protected]>
## Short Summary (TL;DR) Updates the git-worktree snapshot docs to cover what 9164666 added: `ddev snapshot restore <name>` and `ddev snapshot --list` now also reach sibling-worktree snapshots, not just the interactive picker and `--latest`. ## The Issue No issue - the two hand-maintained docs pages for this feature (database-management.md and commands.md) still described only the picker and `--latest` behavior from 8b7c395, so they read as stale after 9164666 extended the same lookup to a bare name and to `--list`. ## How This PR Solves The Issue - database-management.md: notes that restoring by name and `--list` also see sibling-worktree snapshots, adds a `ddev snapshot restore before-migration` example, and documents that a same-named snapshot in more than one sibling resolves to the most recent one without asking which. - commands.md: updates the `snapshot restore` description and adds a note to the `snapshot --list` section about the `Worktree` column. ## Manual Testing Instructions Read the updated sections in docs/content/users/usage/database-management.md and docs/content/users/usage/commands.md against the behavior in 9164666. ## Automated Testing Overview None - documentation only. `markdownlint --fix` was run on both files. ## Release/Deployment Notes None. Co-Authored-By: Claude Sonnet 5 <[email protected]>
Fixes the "Incorrect term: 'git', use 'Git' instead" warnings reviewdog flagged on 81cda38's added prose in database-management.md and commands.md. The rest of those docs already capitalize "Git" when referring to the tool rather than the `git` command itself. Co-Authored-By: Claude Sonnet 5 <[email protected]>
…overy otherWorktreeProjectRoots() silently returns nil on any git failure, which is correct for the common case of a project that isn't in a git repo at all. But once "git rev-parse --show-toplevel" has already succeeded, a later failure in "git rev-parse --show-prefix" or "git worktree list" means something unexpected happened inside a real repo, which was previously undebuggable. Route just those two through util.Debug, per stasadev's review comment on ddev#8733.
c903445 to
7c37ee4
Compare
Short Summary (TL;DR)
Teaches
ddev snapshot restore— interactively, by name, and via--latest— andddev snapshot --listto also see snapshots from the same project checked out in other Git worktrees of the same repository, building on the path-based restore already onmaininstead of duplicating it. An alternative to #8623, which predates that path-based support.The Issue
Related to #8623 ("Allow worktrees to share DB snapshots" by @tyler36): a snapshot taken in one Git worktree of a project isn't visible to
ddev snapshot restorein another worktree of the same repository, so reusing a database across worktrees means manually copying snapshot files around.#8623 predates the path-based snapshot restore/seed support that's landed on
mainsince it was opened (#8704, #8705), and itsRestoreSnapshot(name, sourceRoot)signature no longer matchesRestoreSnapshot(name, force)onmain- it needs a rewrite on top of current code either way. This PR is that rewrite, built directly on the newer mechanism instead of duplicating it.How This PR Solves The Issue
This PR is five commits:
8b7c39593- the feature: sibling-worktree snapshots offered by the interactive picker and--latest.0f4556f54- a fix for a real bug the feature's own test caught, split out so it's reviewable on its own rather than buried in the feature diff. Details below.916466694- extends the same lookup to a bare name on theddev snapshot restorecommand line and toddev snapshot --list, which the first commit left out.81cda38bfand8ab7d6451- documentation for the above, plus a wording fix for a textlint terminology warning.ddev snapshot restoreandddev snapshot restore --latestalready support restoring from an arbitrary host path viaresolveSnapshotSource. The feature commit adds automatic discovery on top of that, rather than a parallel restore mechanism:DdevApp.ListSnapshotRestoreTargets()lists the current project's own snapshots plus any snapshots found in the same project checked out in other worktrees of the same Git repository (viagit worktree list), newest first. A worktree match requires the same repository and the same relative project path - determined entirely from Git and the filesystem, never from the.ddev/config.yamlproject name (worktree projects don't usually share a name). A separate clone that happens to sit at the same relative path is not offered.DdevApp.GetLatestSnapshotRestoreTarget()extends--latestthe same way.ddev snapshot restoreshows a worktree snapshot's name and source directory (snapshot-name (worktree-dir)); selecting one restores by its absolute path, reusing the existing path-based restore/bind-mount plumbing rather than a copy step.The bug, fixed in
0f4556f54: the feature commit computed a project's path relative to its repo root withfilepath.Rel(repoRoot, app.AppRoot), whererepoRootcame fromgit rev-parse --show-toplevel. That silently breaks whenapp.AppRootreaches the project through a symlink that--show-toplevelalready resolved - notably macOS, where a temp directory (and sot.TempDir()in the new test) commonly lives under/var/folders/..., itself a symlink to/private/var/folders/....filepath.Relthen compares a resolved path against an unresolved one and returns nonsense like../../../../../var/folders/.../repo/webinstead ofweb, which could make the "other worktree" project directory resolve right back to the current project instead of to the sibling worktree - i.e. cross-worktree discovery would have silently found nothing (or the wrong thing) on a real macOS checkout. The fix asks Git for the relative path instead of computing it withfilepath.Rel:git rev-parse --show-prefix(run fromapp.AppRoot) returns the current directory's path relative to the repo's top level, computed entirely within Git's own resolved view, so it never mixes resolved and unresolved forms of the same path.916466694extends the same discovery to two places the first commit didn't reach.DdevApp.ResolveSnapshotRestoreTarget()handles a bare name given directly on theddev snapshot restore <name>command line: the project's own snapshot wins if one exists by that name (unchanged behavior), otherwise it falls back to the newest same-named snapshot in a sibling worktree, resolved to its absolute path so the existing path-based restore plumbing handles it unchanged; a path argument, or a name matching nothing anywhere, passes through untouched.SnapshotRestoreTargetnow also carriesSize/DBVersion/Compression, soddev snapshot --listcan callListSnapshotRestoreTargets()instead of the project's ownListSnapshots()and render sibling rows with the same detail as the project's own, in aWorktreecolumn that's added only when a sibling snapshot is actually present - so a project outside a worktree, or one whose siblings have no snapshots, sees no change in output.I considered going further with a repository-agnostic shared snapshot pool keyed by project name (so any two checkouts with the same name could share snapshots, not just worktrees of one repo). I left that out: project names collide too easily across unrelated projects, and a Git worktree relationship is a precise, verifiable identity check that a name is not. The fully general case - "restore a snapshot from anywhere on disk" - is already covered by the existing path argument.
Manual Testing Instructions
Docs:
Create a Git repo and a worktree of it, each with a DDEV project at the same relative path (project names deliberately differ, since that's the normal case for worktrees):
ddev snapshot restoreinrepo-b- the picker should listshared-snapshot (repo)alongside any ofrepo-b's own snapshots.ddev snapshot restore --latestshould restore it directly.ddev snapshot restore shared-snapshotinrepo-b(the bare name, no picker, no--latest) - should also restore it directly, sincerepo-bhas no snapshot of its own by that name.A separate
git cloneofrepoat the same relative depth should not have its snapshots offered fromrepo-b, by any of the above.ddev snapshot --listinrepo-b- should showshared-snapshotalongsiderepo-b's own snapshots, with aWorktreecolumn readingrepo.ddev snapshot --listinrepo-a- should show noWorktreecolumn at all, since none ofrepo-a's siblings have snapshots of their own yet.Add a third worktree,
git -C repo worktree add ../repo-c HEAD, give it its own snapshot also namedshared-snapshot(ddev snapshot --name=shared-snapshotafterddev config/ddev startthere), and make sure it's newer thanrepo-a's.ddev snapshot restore shared-snapshotinrepo-bshould now restorerepo-c's copy instead - there's no prompt or on-screen indication of which sibling it came from beyond the absolute path in the final "was restored" message. This is expected today; see Release/Deployment Notes.Automated Testing Overview
TestListSnapshotRestoreTargetsWorktreesinpkg/ddevapp/snapshot_test.go(0f4556f54) sets up a real Git repo, a worktree of it, and an unrelated clone (all via thegitbinary), and asserts thatListSnapshotRestoreTargetsandGetLatestSnapshotRestoreTargetfind the worktree's snapshot by absolute path, keep the current project's own snapshot addressable by bare name, exclude the unrelated clone, and (as of916466694) carry the rightDBVersion. It reproduces the symlink bug described above under a realt.TempDir()and fails without0f4556f54's fix.TestResolveSnapshotRestoreTargetWorktrees(916466694, same file, sharing the fixture through a newnewSnapshotWorktreeFixturehelper) coversResolveSnapshotRestoreTarget(): the project's own snapshot resolves by bare name unchanged, a sibling-only name falls back to its absolute path, a name matching nothing anywhere passes through unchanged so the usual not-found error still fires, and a path is never treated as a name to look up.TestListSnapshotsNoSnapshotsandTestListSnapshotsWorktrees(916466694, new filecmd/ddev/cmd/snapshot_list_test.go) exerciselistSnapshots()directly against filesystem/Git fixtures with no Docker involved, covering the empty-project case and theWorktreecolumn appearing only when a sibling snapshot actually exists.TestCmdSnapshotRestoreandTestCmdSnapshotRestoreFile(existing, covering--latestand the path argument) still pass. Also manually verified end-to-end with real DDEV projects sharing one Git repo viagit worktree, per the steps above.Release/Deployment Notes
No config or behavior changes for projects outside a Git worktree, or where a named snapshot already exists locally. Within a worktree,
ddev snapshot restore- interactively, by name, or via--latest- andddev snapshot --listnow also see snapshots from sibling worktrees of the same project.Two things worth knowing before merging, neither fixed in this PR: if the same snapshot name exists in more than one sibling worktree,
ddev snapshot restore <name>and--latestsilently use the most recently created one, with no prompt and no indication afterward of which sibling it came from beyond the absolute path in the final "was restored" message. And the label shown for a sibling snapshot - in the picker and in--list'sWorktreecolumn - is the project's path relative to the repo root, not the worktree's own directory name; if the DDEV project lives in a subdirectory (for examplerepo/web), every sibling worktree shows the same label and can't be told apart.