Skip to content

Commit db6e1ce

Browse files
committed
ignore git and others
1 parent 457445f commit db6e1ce

3 files changed

Lines changed: 51 additions & 0 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ Follow these steps to use this template:
1616
1. Follow the directions in your new repo's `README.md` and make sure to check each file for alignment with your project.
1717
1. Enjoy!
1818

19+
Copier already excludes `.git` from rendered output, so copying this template into an existing repository keeps that repository's history in place instead of importing this template's history. Copier also does not automatically run `git init` for a fresh destination. That could be done with a post-copy task, but Copier treats tasks as an unsafe feature, so this template leaves repository initialization as an explicit user step.
20+
1921
## What's included in the template
2022

2123
- Pre-configured `pyproject.toml` for Python project management

copier.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ github_url:
4343
and other places.
4444
4545
_exclude:
46+
- .git
47+
- .DS_Store
48+
- .svn
49+
- __pycache__
50+
- "*.py[co]"
51+
- "~*"
4652
- copier.yaml
4753
- tests/test_template.py
4854
- .github/workflows/test-template.yml

tests/test_template.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,22 @@
88
from copier import run_copy
99

1010

11+
def _git(*args: str, cwd: Path) -> str:
12+
return subprocess.check_output(["git", *args], cwd=cwd, text=True).strip()
13+
14+
15+
def _init_git_repo(path: Path) -> str:
16+
subprocess.run(["git", "init"], cwd=path, check=True)
17+
subprocess.run(["git", "config", "user.name", "Test User"], cwd=path, check=True)
18+
subprocess.run(
19+
["git", "config", "user.email", "test@example.com"], cwd=path, check=True
20+
)
21+
(path / "README.md").write_text("# Existing repo\n")
22+
subprocess.run(["git", "add", "README.md"], cwd=path, check=True)
23+
subprocess.run(["git", "commit", "-m", "Initial commit"], cwd=path, check=True)
24+
return _git("rev-parse", "HEAD", cwd=path)
25+
26+
1127
def test_template(tmp_path: Path) -> None:
1228
# Path to the Copier template root
1329
template_path = Path(__file__).resolve().parent.parent
@@ -33,6 +49,33 @@ def test_template(tmp_path: Path) -> None:
3349

3450
# Assert a file from the template was created
3551
assert (dst_path / "src/demo_project/main.py").exists()
52+
assert not (dst_path / ".git").exists()
3653

3754
# Run pytest from the copied template
3855
subprocess.run(["uv", "run", "pytest"], cwd=dst_path, check=True)
56+
57+
58+
def test_template_preserves_existing_git_repo(tmp_path: Path) -> None:
59+
template_path = Path(__file__).resolve().parent.parent
60+
dst_path = tmp_path / "existing-repo"
61+
dst_path.mkdir()
62+
original_head = _init_git_repo(dst_path)
63+
64+
run_copy(
65+
src_path=str(template_path),
66+
dst_path=dst_path,
67+
data={
68+
"project_name": "demo-project",
69+
"project_description": "A demo project for testing Copier templates.",
70+
"author_name": "Test Author",
71+
"author_orcid": "https://orcid.org/0000-0000-0000-0000",
72+
"github_url": "https://github.com/org/repo",
73+
},
74+
quiet=True,
75+
overwrite=True,
76+
vcs_ref="HEAD",
77+
)
78+
79+
assert (dst_path / "src/demo_project/main.py").exists()
80+
assert _git("rev-parse", "--is-inside-work-tree", cwd=dst_path) == "true"
81+
assert _git("rev-parse", "HEAD", cwd=dst_path) == original_head

0 commit comments

Comments
 (0)