Skip to content

Commit 66a051a

Browse files
authored
Ignore git dir and others (#26)
* ignore git and others * Update .pre-commit-config.yaml
1 parent 457445f commit 66a051a

4 files changed

Lines changed: 55 additions & 4 deletions

File tree

.pre-commit-config.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ repos:
99
- id: check-yaml
1010
- id: detect-private-key
1111
- repo: https://github.com/tox-dev/pyproject-fmt
12-
rev: "v2.16.2"
12+
rev: "v2.19.0"
1313
hooks:
1414
- id: pyproject-fmt
1515
- repo: https://github.com/citation-file-format/cffconvert
1616
rev: 5295f87c0e261da61a7b919fc754e3a77edd98a7
1717
hooks:
1818
- id: validate-cff
1919
- repo: https://github.com/codespell-project/codespell
20-
rev: v2.4.1
20+
rev: v2.4.2
2121
hooks:
2222
- id: codespell
2323
exclude: |
@@ -39,7 +39,7 @@ repos:
3939
- id: yamllint
4040
exclude: pre-commit-config.yaml
4141
- repo: https://github.com/astral-sh/ruff-pre-commit
42-
rev: "v0.15.4"
42+
rev: "v0.15.6"
4343
hooks:
4444
- id: ruff-format
4545
- id: ruff-check
@@ -53,7 +53,7 @@ repos:
5353
- id: pre-commit-update
5454
args: ["--keep", "mdformat", "--keep", "pre-commit-update", "--keep", "cffconvert"]
5555
- repo: https://github.com/jendrikseipp/vulture
56-
rev: 'v2.14'
56+
rev: 'v2.15'
5757
hooks:
5858
- id: vulture
5959
- repo: https://github.com/aristanetworks/j2lint

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)