Skip to content

feat: add 'copier adopt' command for adopting templates in existing projects#2487

Open
ichoosetoaccept wants to merge 1 commit intocopier-org:masterfrom
detailobsessed:feat/adopt-command
Open

feat: add 'copier adopt' command for adopting templates in existing projects#2487
ichoosetoaccept wants to merge 1 commit intocopier-org:masterfrom
detailobsessed:feat/adopt-command

Conversation

@ichoosetoaccept
Copy link
Copy Markdown

Summary

This PR adds a new copier adopt subcommand that allows users to adopt a template for an existing project that was not originally created with Copier.

Closes #2486

Motivation

Many users have existing projects they want to bring under Copier management. Currently, the only option is copier copy --overwrite, which destroys existing customizations. The adopt command provides a safer alternative by merging template files with existing files using conflict markers.

What it does

Unlike copier copy, the adopt command:

  • Creates new files from the template (files that don't exist in the project)
  • Skips identical files (no changes needed)
  • Merges differing files with git-style conflict markers (<<<<<<< existing / >>>>>>> template)
  • Writes a valid .copier-answers.yml for future copier update calls
  • Skips tasks by default since conflicts need resolution first (with a helpful message)
  • Handles binary files by creating .rej files instead of corrupting them with conflict markers

Usage

copier adopt <template_src> [destination_path]

Options

  • --conflict {rej,inline} - Behavior on conflict (default: inline)
    • inline: Add git-style conflict markers to the file
    • rej: Create a .rej file with the template version
  • --defaults - Use default answers to questions

Example workflow

# Adopt a template for an existing project
copier adopt gh:org/template ./my-project

# Review and resolve conflicts
git diff
# ... edit files to resolve conflicts ...

# Run template tasks manually
uv sync  # or whatever tasks the template defines

# Commit the adoption
git add -A
git commit -m "chore: adopt org/template"

# Future updates now work with proper 3-way merge!
copier update

Implementation

The implementation:

  1. Renders the template into a temporary directory using the existing run_copy machinery
  2. Iterates through rendered files and compares with the destination
  3. Uses git merge-file with an empty base for text files (creates conflict markers)
  4. Creates .rej files for binary files (detected by null bytes in content)
  5. Writes .copier-answers.yml directly (not from a template source)

Tests

Added 13 comprehensive tests covering:

  • Core functionality (create, skip, merge, answers file generation)
  • CLI interface
  • Both conflict modes (inline and rej)
  • Binary file handling (uses .rej to avoid corruption)
  • Exclude patterns
  • Non-git destination projects
  • Deep nested directory structures
  • Templates with _subdirectory
  • Pretend mode
  • Enabling future updates

All existing tests continue to pass (215 total).

Checklist

  • Code follows project style (ruff, editorconfig)
  • Tests added and passing
  • Pre-commit hooks pass
  • Conventional commit message

…rojects

This adds a new 'copier adopt' subcommand that allows users to adopt a template
for an existing project that was not originally created with Copier.

Unlike 'copier copy', this command:
- Creates new files from the template
- Merges existing files with conflict markers (git-style) instead of overwriting
- Writes a valid .copier-answers.yml for future 'copier update' calls
- Skips tasks by default since conflicts need resolution first
- Handles binary files by creating .rej files (avoids corruption)

Usage:
  copier adopt <template_src> [destination_path]

Options:
  --conflict {rej,inline}  Behavior on conflict (default: inline)
  --defaults               Use default answers to questions

After adoption, users can resolve conflicts manually, then run template tasks.
Future 'copier update' calls will work with proper 3-way merge.

Includes 13 comprehensive tests covering:
- Core functionality (create, skip, merge, answers file)
- CLI interface and both conflict modes
- Binary file handling (uses .rej to avoid corruption)
- Edge cases: non-git destinations, deep nesting, _subdirectory, exclude patterns

Closes copier-org#2486
ichoosetoaccept added a commit to detailobsessed/copier-uv-bleeding that referenced this pull request Feb 6, 2026
…#61)

## Summary
Add Quick Start section to README with uv tool install, uvx, and copier adopt commands.

## Changes
- **README.md**: Expand Quick Start with install, create, uvx, and adopt workflows
- **README.md**: Link to upstream copier adopt [PR #2487](copier-org/copier#2487) and [issue #2486](copier-org/copier#2486)
- **README.md**: Update Scaffold Prompts table with `repository_provider` and GitLab CI references

Fixes #55
<!-- devin-review-badge-begin -->

---

<a href="https://app.devin.ai/review/detailobsessed/copier-uv-bleeding/pull/61" target="_blank">
  <picture>
    <source media="(prefers-color-scheme: dark)" srcset="https://static.devin.ai/assets/gh-open-in-devin-review-dark.svg?v=1">
    <img src="https://static.devin.ai/assets/gh-open-in-devin-review-light.svg?v=1" alt="Open with Devin">
  </picture>
</a>
<!-- devin-review-badge-end -->
Copy link
Copy Markdown
Member

@sisp sisp left a comment

Choose a reason for hiding this comment

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

Thanks for contributing this valuable feature to Copier, @ichoosetoaccept! 🙇

I think this is a great idea. Before reviewing the changes in detail, a few rather general remarks:

  • How about naming the command onboard? For example, Renovate creates onboarding PRs, it sounds like a more familiar term in a somewhat similar context to me.
  • Once #2376 lands, .rej files will be dropped. I imagine nobody uses .rej files anymore because we have had more familiar inline conflict markers for quite a long time and they have also been the default setting for a while. How about omitting .rej files support for this command right from the beginning? This would reduce complexity and avoid code that will likely be removed in the (hopefully) near future again.
  • Long-term, I think we'll want more flags like --data. I assume you didn't add them in this PR to keep the scope of the PR manageable. We can add more useful flags in follow-up PRs.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature: copier adopt — first-class workflow for adopting a template in an existing project

2 participants