Skip to content

About

No description, website, or topics provided.

Resources

Stars

4 stars

Watchers

1 watching

Forks

Latest commit

 

History

36 Commits

Folders and files

Repository files navigation

add-on registry tests last commit release

ddev-worktree

A global DDEV add-on that clones an existing DDEV project (files + database) into a new directory. If the project is a git repo, it uses git worktree to create a linked worktree; otherwise it copies files with rsync.

The new directory gets its own DDEV project name via config.worktree.local.yaml, so both instances can run simultaneously without collision.

Installation

ddev add-on install elabx/ddev-worktrees

Or install from a local clone:

ddev add-on install /path/to/ddev-worktree

Once installed, the ddev worktree and ddev worktree-remove commands are available in every DDEV project.

Quick Start

cd ~/projects/my-site

# Create a worktree for a new branch
ddev worktree feature-login

# Both projects now run side-by-side
ddev list
# my-site          running
# my-site-feature-login  running

# Done with the worktree? Clean up
ddev worktree-remove feature-login

Usage

ddev worktree

ddev worktree <branch-or-name> [--profile=NAME] [--no-db] [--no-start] [--force]
ddev worktree [<name>] --path=DIR [--profile=NAME] [--no-db] [--no-start]

Creates a clone of the current DDEV project in a sibling directory.

What it does:

  1. Exports the database from the source project
  2. Creates the target directory via git worktree add (or rsync if not a git repo)
  3. Copies .ddev/ if it's gitignored
  4. Copies any files specified in the profile's copy list
  5. Creates .ddev/config.worktree.local.yaml with a unique project name
  6. Starts DDEV and imports the database
  7. Runs any post_create commands from the profile

Flags:

Flag Description
--profile=NAME Which profile from .ddev/worktree-hooks.yaml to use
--no-db Skip database export/import
--no-start Create files only — don't start DDEV or run post_create
--force, -f Overwrite an existing target directory/project
--path=DIR Set up DDEV in an existing worktree another tool created (see below)

Target directory: ../<source-dirname>-<sanitized-name> Target project name: <source-project>-<sanitized-name>

Branch names are sanitized so the project name is a valid hostname: they are lowercased and every character other than a-z, 0-9 and - becomes - (e.g., feature/Foo_bar becomes feature-foo-bar, release/1.2 becomes release-1-2). If two branches sanitize to the same name, the command refuses to overwrite the other branch's worktree, even with --force.

Worktrees created by another tool (--path)

IDEs and other tools can create git worktrees themselves. --path sets up DDEV inside one of those instead of creating a new one:

git worktree add ../anywhere/my-feature feature/x   # or your IDE does this
ddev worktree --path=../anywhere/my-feature          # run from the source project

The directory must already be a linked git worktree of the source project. The command skips git worktree add and the branch checks, then does everything else as usual (.ddev/ copy, profile files, database, post_create). The name defaults to the directory's name, so the project above is <source-project>-my-feature; a directory named <source-dirname>-<name> gives <name>. Pass a name to choose a different one.

The worktree belongs to the tool that created it:

  • --force is not allowed with --path, and ddev worktree <name> --force refuses to replace an adopted worktree.
  • ddev worktree-remove removes only the DDEV project and config.worktree.local.yaml (plus .ddev/ if it was gitignored and copied in during setup). It never deletes the directory or the git worktree.
  • If the DDEV project can't be deleted (for example, Docker isn't running), nothing is removed, so you can run the command again later.

ddev worktree-remove

ddev worktree-remove <name> [--list]
ddev worktree-remove --path=DIR

List clones:

ddev worktree-remove --list

Remove a clone:

ddev worktree-remove feature-login

This stops the DDEV project, removes the git worktree (or deletes the directory), and cleans up. It refuses to remove directories that don't have a config.worktree.local.yaml as a safety measure (the older config.worktree.yaml is still accepted, so worktrees created before v1.1.0 remain removable).

For a worktree set up with ddev worktree --path, removal (by name or with --path=DIR) deletes only the DDEV project and config.worktree.local.yaml (and a .ddev/ copied in during setup), and leaves the files and the git worktree in place. Run it from the project the worktree was created from. --list shows these worktrees wherever they are, marked (external).

Configuration: .ddev/worktree-hooks.yaml

Create this file in your project's .ddev/ directory to configure what gets copied and what commands run after clone creation. Commit it to git so your team shares the same setup.

# .ddev/worktree-hooks.yaml
default_profile: full

profiles:
  full:
    copy:
      - site/assets
      - .env
    post_create:
      - ddev composer install
      - ddev npm install

  minimal:
    copy:
      - .env
    post_create:
      - ddev composer install

  frontend:
    copy:
      - .env
    post_create:
      - ddev npm install

Fields:

Field Description
default_profile Which profile to use when --profile is not specified
profiles.<name>.copy Files/directories to copy from source to target (relative to project root). Use this for things not in git: uploads, .env, etc.
profiles.<name>.post_create Shell commands to run in the target directory after DDEV is started

If no .ddev/worktree-hooks.yaml exists, the command works fine — it just skips the copy and post_create steps.

Profile Examples

Note: Only the ProcessWire profile has been tested. The other profiles are untested examples — adapt them to your project's needs.

ProcessWire:

default_profile: full

profiles:
  full:
    copy:
      - site/assets
      - .env
    post_create:
      - ddev composer install

  minimal:
    copy:
      - .env
    post_create:
      - ddev composer install

  frontend:
    copy:
      - .env
      - site/assets
    post_create:
      - ddev npm install

Drupal:

default_profile: full

profiles:
  full:
    copy:
      - sites/default/files
      - .env
    post_create:
      - ddev composer install
      - ddev drush cr

Laravel:

default_profile: full

profiles:
  full:
    copy:
      - storage/app
      - .env
    post_create:
      - ddev composer install
      - ddev npm install
      - ddev artisan key:generate

WordPress:

default_profile: full

profiles:
  full:
    copy:
      - wp-content/uploads
      - .env
    post_create:
      - ddev composer install

How It Works

Project Isolation

The clone gets a config.worktree.local.yaml with override_config: true and a unique name field. This overrides the project name from config.yaml without modifying any git-tracked files. Both projects can run simultaneously with their own containers, databases, and URLs.

Git Worktree Behavior

When the source project is a git repo:

  • Existing local branch — git worktree add <path> <branch>
  • Remote branch only — creates a local tracking branch
  • No such branch — creates a new branch from HEAD

Non-Git Projects

If there's no git repo, the command uses rsync to copy all files, excluding DDEV runtime artifacts.

Keeping git clean

The generated config is named config.worktree.local.yaml, and the .local. is deliberate: DDEV's own generated .ddev/.gitignore already ignores /config.*.local.y*ml. The file stays out of git status without this add-on writing to .gitignore, .git/info/exclude, or any other git-owned file.

One wrinkle: .ddev/.gitignore is itself untracked, so a fresh worktree starts without it and the config is briefly visible to git status. The first ddev start regenerates the ignore file and it disappears. With --no-start, it stays visible until you start the project.

Database transfer

With a database export (the default), the dump is written to a private temp directory created with mktemp -d under $TMPDIR (falling back to /tmp), and removed by an EXIT/INT/TERM trap — so an interrupted or failed run leaves nothing behind, and concurrent runs never collide.

Troubleshooting

Updates leave an older command installed

Older releases omitted the #ddev-generated marker from the global commands. DDEV treats those files as user-managed and reports NOT overwriting during installation, so an upgrade can leave the old behavior in place.

Back up any local command changes, then add #ddev-generated on its own line below the shebang in both ~/.ddev/commands/host/worktree and ~/.ddev/commands/host/worktree-remove. Reinstall the add-on from a DDEV project directory. The commands now ship with this marker so subsequent upgrades can replace them normally.

Many untracked files under .ddev/ in an older worktree

Older commands created .ddev/.gitignore containing only config.worktree.yaml. That prevents DDEV from generating its normal ignore rules. If the file contains only that entry, replace it with #ddev-generated and run ddev start. Preserve any custom rules if you have edited the file yourself.

Worktrees with _, . or uppercase letters in their names

Versions up to v1.1.2 kept _, . and uppercase letters, so a branch like feat_x produced ../site-feat_x. Newer versions name it ../site-feat-x. ddev worktree-remove feat_x still finds and removes the old directory. Running ddev worktree feat_x while the old clone exists stops with a message pointing to it; remove the old clone first, then recreate it.

For an older config.worktree.yaml, rename it to config.worktree.local.yaml before starting DDEV. The current command uses this name, which DDEV ignores automatically. Keep only one of these config files.

mktemp: mkstemp failed on /tmp/ddev-worktree-db-XXXXXX.sql.gz: File exists

Affects v1.1.0 and earlier on macOS. Those versions built the dump path with the X placeholders mid-string (ddev-worktree-db-XXXXXX.sql.gz). BSD mktemp only substitutes a trailing run of Xs, so the name was used literally — the same fixed path on every run. There was also no cleanup trap, so any failure between export and import left the file behind, and every later run then failed on it.

Unblock an affected machine:

rm -f /tmp/ddev-worktree-db-*.sql.gz

Then upgrade to v1.1.1 or later, which uses a temp directory plus a cleanup trap. Note that reinstalling or updating the add-on overwrites ~/.ddev/commands/host/worktree, so patch the repo rather than the installed copy.

Development

Tests use bats, the framework of DDEV's add-on template.

Unit tests (tests/unit/) run both commands against a fake ddev, so they need no Docker and take seconds:

brew install bats-core shellcheck   # or: apt-get install bats shellcheck
bats tests/unit
shellcheck commands/host/worktree commands/host/worktree-remove tests/unit/*.bash

End-to-end test (tests/test.bats) installs the add-on into a real DDEV project, creates a clone, checks that it serves the site with the copied database, and removes it. It installs the commands into ~/.ddev, so it only runs in CI unless you opt in. It needs bats-assert, bats-file and bats-support (brew install bats-core/bats-core/bats-assert bats-core/bats-core/bats-file bats-core/bats-core/bats-support):

DDEV_WORKTREE_E2E=1 bats tests/test.bats --filter-tags '!release'

GitHub Actions runs shellcheck and the unit tests (Linux and macOS) plus the end-to-end test (DDEV stable and HEAD, amd64 and arm64) on every pull request, on every push to main and nightly.

Dependencies

  • DDEV >= v1.24.10
  • yq (recommended) — for full YAML parsing of .ddev/worktree-hooks.yaml. Install via brew install yq or see yq docs. Without yq, the command falls back to basic grep/awk parsing with a warning.
  • rsync — for file copying (pre-installed on macOS and most Linux distributions)

Uninstall

ddev add-on remove ddev-worktree

License

MIT, see LICENSE.

About

No description, website, or topics provided.

Resources

Stars

4 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages