Skip to content

feat(snapshot): offer snapshots from sibling Git worktrees, by name, latest, or --list (#8733) [skip ci] - #8733

Merged
rfay merged 6 commits into
ddev:mainfrom
rfay:20260824_rfay_worktree_snapshot_restore
Aug 28, 2026
Merged

rfay merged 6 commits into
ddev:mainfrom
rfay:20260824_rfay_worktree_snapshot_restore

Conversation

@rfay

@rfay rfay commented Aug 25, 2026 •

Copy link
Copy Markdown
Member

Short Summary (TL;DR)

Teaches ddev snapshot restore — interactively, by name, and via --latest — and ddev snapshot --list to also see snapshots from the same project checked out in other Git worktrees of the same repository, building on the path-based restore already on main instead 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 restore in 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 main since it was opened (#8704, #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 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 the ddev snapshot restore command line and to ddev snapshot --list, which the first commit left out.
  • 81cda38bf and 8ab7d6451 - documentation for the above, plus a wording fix for a textlint terminology warning.

ddev snapshot restore and ddev snapshot restore --latest already support restoring from an arbitrary host path via resolveSnapshotSource. 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 (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.

The bug, fixed in 0f4556f54: the feature commit computed a 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 t.TempDir() in the new test) commonly lives under /var/folders/..., 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 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 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.

916466694 extends the same discovery to two places the first commit didn't reach. DdevApp.ResolveSnapshotRestoreTarget() handles a bare name given directly on the ddev 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. SnapshotRestoreTarget now also carries Size/DBVersion/Compression, so ddev snapshot --list can call ListSnapshotRestoreTargets() instead of the project's own ListSnapshots() and render sibling rows with the same detail as the project's own, in a Worktree column 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:

  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):

    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. ddev snapshot restore shared-snapshot in repo-b (the bare name, no picker, no --latest) - should also restore it directly, since repo-b has no snapshot of its own by that name.

  4. A separate git clone of repo at the same relative depth should not have its snapshots offered from repo-b, by any of the above.

  5. ddev snapshot --list in repo-b - should show shared-snapshot alongside repo-b's own snapshots, with a Worktree column reading repo. ddev snapshot --list in repo-a - should show no Worktree column at all, since none of repo-a's siblings have snapshots of their own yet.

  6. Add a third worktree, git -C repo worktree add ../repo-c HEAD, give it its own snapshot also named shared-snapshot (ddev snapshot --name=shared-snapshot after ddev config/ddev start there), and make sure it's newer than repo-a's. ddev snapshot restore shared-snapshot in repo-b should now restore repo-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

TestListSnapshotRestoreTargetsWorktrees in pkg/ddevapp/snapshot_test.go (0f4556f54) sets up a real Git repo, a worktree of it, and an unrelated clone (all via the git binary), and asserts that ListSnapshotRestoreTargets and GetLatestSnapshotRestoreTarget find the worktree's snapshot by absolute path, keep the current project's own snapshot addressable by bare name, exclude the unrelated clone, and (as of 916466694) carry the right DBVersion. It reproduces the symlink bug described above under a real t.TempDir() and fails without 0f4556f54's fix.

TestResolveSnapshotRestoreTargetWorktrees (916466694, same file, sharing the fixture through a new newSnapshotWorktreeFixture helper) covers ResolveSnapshotRestoreTarget(): 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.

TestListSnapshotsNoSnapshots and TestListSnapshotsWorktrees (916466694, new file cmd/ddev/cmd/snapshot_list_test.go) exercise listSnapshots() directly against filesystem/Git fixtures with no Docker involved, covering the empty-project case and the Worktree column appearing only when a sibling snapshot actually exists.

TestCmdSnapshotRestore and TestCmdSnapshotRestoreFile (existing, covering --latest and the path argument) still pass. Also manually verified end-to-end with real DDEV projects sharing one Git repo via git 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 - and ddev snapshot --list now 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 --latest silently 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's Worktree column - 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 example repo/web), every sibling worktree shows the same label and can't be told apart.

@github-actions

github-actions Bot commented Aug 25, 2026 •

Copy link
Copy Markdown

Download this PR's build for your OS and architecture with:

ddev utility download-ddev --pr 8733

Or download the artifact for your OS and architecture directly:

See Testing a PR.

Open in GitHub Codespaces

@rfay
rfay force-pushed the 20260824_rfay_worktree_snapshot_restore branch from d9db688 to 0f4556f Compare August 25, 2026 02:49
@rfay
rfay requested review from stasadev and tyler36 August 25, 2026 17:27
@rfay

rfay commented Aug 25, 2026

Copy link
Copy Markdown
Member Author

@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.

@rfay rfay changed the title feat(snapshot): offer snapshots from sibling git worktrees in ddev snapshot restore feat(snapshot): offer snapshots from sibling Git worktrees, by name, latest, or --list Aug 25, 2026
@rfay
rfay force-pushed the 20260824_rfay_worktree_snapshot_restore branch from 8ab7d64 to c903445 Compare August 25, 2026 17:58
@rfay
rfay marked this pull request as ready for review August 25, 2026 17:59
@rfay
rfay requested review from a team as code owners August 25, 2026 17:59

@stasadev stasadev left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread pkg/ddevapp/snapshot.go
Comment on lines +243 to +290
// 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
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

rfay and others added 6 commits August 27, 2026 11:43
…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.
@rfay
rfay force-pushed the 20260824_rfay_worktree_snapshot_restore branch from c903445 to 7c37ee4 Compare August 27, 2026 17:53

@stasadev stasadev left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me.

@rfay rfay changed the title feat(snapshot): offer snapshots from sibling Git worktrees, by name, latest, or --list feat(snapshot): offer snapshots from sibling Git worktrees, by name, latest, or --list (#8733) [skip ci] Aug 28, 2026
@rfay
rfay merged commit fa36829 into ddev:main Aug 28, 2026
44 checks passed
@rfay
rfay deleted the 20260824_rfay_worktree_snapshot_restore branch August 28, 2026 03:25

@tyler36 tyler36 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was a busy month so didn't get a change to check before merge.

Followed the testing from my original PR (#8623) and this works as describe here and achieving the same thing as desired in #8623.

Thank you for the improved version!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants