Skip to content

Commit 9bcd3a7

Browse files
committed
Successfully ignored git files we don't care about
1 parent f711828 commit 9bcd3a7

File tree

4 files changed

+107
-95
lines changed

4 files changed

+107
-95
lines changed

ccds/hook_utils/configure_vcs.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,16 @@
1010
# ---------------------------------------------------------------------------- #
1111

1212

13-
def init_local_git_repo(directory: str | Path, _make_initial_commit: bool = True) -> bool:
13+
def init_local_git_repo(
14+
directory: str | Path, _make_initial_commit: bool = True
15+
) -> bool:
1416
"""
1517
Initialize a local git repository without any GitHub integration.
16-
18+
1719
Args:
1820
directory: Directory where the repository will be created
1921
_make_initial_commit: Whether to make initial commit (for testing)
20-
22+
2123
Returns:
2224
bool: True if initialization was successful, False otherwise
2325
"""
@@ -28,24 +30,26 @@ def init_local_git_repo(directory: str | Path, _make_initial_commit: bool = True
2830
directory = Path(directory)
2931
if not directory.is_dir():
3032
raise ValueError(f"Directory '{directory}' does not exist.")
31-
33+
3234
os.chdir(directory)
33-
35+
3436
if not (directory / ".git").is_dir():
3537
_git("init")
3638
if _make_initial_commit:
3739
_git("add .")
3840
_git("commit -m 'Initial commit'")
39-
41+
4042
return True
4143
except Exception as e:
4244
print(f"Error during repository initialization: {e}")
4345
return False
4446

47+
4548
def _git(command: str, **kwargs) -> subprocess.CompletedProcess:
4649
"""Run a git command and return the result."""
4750
return subprocess.run(f"git {command}", shell=True, check=True, **kwargs)
4851

52+
4953
def _check_git_cli_installed() -> bool:
5054
"""Check whether git cli is installed"""
5155
try:
@@ -54,10 +58,12 @@ def _check_git_cli_installed() -> bool:
5458
except subprocess.CalledProcessError:
5559
return False
5660

61+
5762
# ---------------------------------------------------------------------------- #
5863
# Git + Github #
5964
# ---------------------------------------------------------------------------- #
6065

66+
6167
def configure_github_repo(
6268
directory: str | Path,
6369
repo_name: str,
@@ -237,4 +243,4 @@ def _set_branch_protection(username: str, repo_name: str, branch: str) -> None:
237243
f"-f enforce_admins={protection_data['enforce_admins']} "
238244
f"-f required_pull_request_reviews='{protection_data['required_pull_request_reviews']}' "
239245
f"-f restrictions={protection_data['restrictions']}"
240-
)
246+
)

hooks/post_gen_project.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22
from copy import copy
33
from pathlib import Path
44

5+
from ccds.hook_utils.configure_vcs import configure_github_repo, init_local_git_repo
6+
57
# https://github.com/cookiecutter/cookiecutter/issues/824
68
# our workaround is to include these utility functions in the CCDS package
79
from ccds.hook_utils.custom_config import write_custom_config
810
from ccds.hook_utils.dependencies import basic, packages, scaffold, write_dependencies
9-
from ccds.hook_utils.configure_vcs import configure_github_repo, init_local_git_repo
1011

1112
#
1213
# TEMPLATIZED VARIABLES FILLED IN BY COOKIECUTTER
@@ -83,15 +84,12 @@
8384
# {% endif %}
8485

8586
# {% if cookiecutter.version_control == "git (local)" %}
86-
init_local_git_repo(
87-
directory=Path.cwd(),
88-
_make_initial_commit=False
89-
)
87+
init_local_git_repo(directory=Path.cwd(), _make_initial_commit=True)
9088
# {% elif cookiecutter.version_control == "git (github)" %}
9189
configure_github_repo(
9290
directory=Path.cwd(),
9391
repo_name="{{ cookiecutter.repo_name }}",
9492
protection_type="main_and_dev",
9593
no_github=False,
9694
)
97-
# {% endif %}
95+
# {% endif %}

tests/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"module_name": "project_module",
2020
"author_name": "DrivenData",
2121
"description": "A test project",
22-
"version_control": "git (local)"
22+
"version_control": "git (local)",
2323
}
2424

2525

tests/test_creation.py

Lines changed: 89 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -72,40 +72,46 @@ def verify_folders(root, config):
7272
"reports/figures",
7373
config["module_name"],
7474
}
75+
ignored_dirs = set()
7576

7677
if config["include_code_scaffold"] == "Yes":
7778
expected_dirs.add(f"{config['module_name']}/modeling")
7879

7980
if config["docs"] == "mkdocs":
8081
expected_dirs.add("docs/docs")
81-
82+
8283
if config["version_control"] in ("git (local)", "git (github)"):
8384
# Expected after `git init`
84-
expected_dirs.update({
85-
".git",
86-
".git/hooks",
87-
".git/info",
88-
".git/objects",
89-
".git/objects/info",
90-
".git/objects/pack",
91-
".git/refs",
92-
".git/refs/heads",
93-
".git/refs/tags",
94-
})
85+
expected_dirs.update(
86+
{
87+
".git",
88+
".git/hooks",
89+
".git/info",
90+
".git/objects",
91+
".git/refs",
92+
".git/refs/heads",
93+
".git/refs/tags",
94+
}
95+
)
9596
# Expected after initial git commit
96-
# expected_dirs += [
97-
# ".git/logs",
98-
# ".git/logs/refs",
99-
# ".git/logs/refs/heads",
100-
# ]
97+
expected_dirs.update(
98+
{
99+
".git/logs",
100+
".git/logs/refs",
101+
".git/logs/refs/heads",
102+
}
103+
)
104+
ignored_dirs.update(
105+
{d.relative_to(root) for d in root.glob(".git/objects/**/*") if d.is_dir()}
106+
)
101107

102108
expected_dirs = {Path(d) for d in expected_dirs}
103109

104110
existing_dirs = {
105111
d.resolve().relative_to(root) for d in root.glob("**") if d.is_dir()
106112
}
107113

108-
assert sorted(existing_dirs) == sorted(expected_dirs)
114+
assert sorted(existing_dirs - ignored_dirs) == sorted(expected_dirs)
109115

110116

111117
def verify_files(root, config):
@@ -130,88 +136,90 @@ def verify_files(root, config):
130136
f"{config['module_name']}/__init__.py",
131137
}
132138

139+
ignored_files = set()
140+
133141
# conditional files
134142
if not config["open_source_license"].startswith("No license"):
135143
expected_files.add("LICENSE")
136144

137145
if config["include_code_scaffold"] == "Yes":
138-
expected_files.update({
139-
f"{config['module_name']}/config.py",
140-
f"{config['module_name']}/dataset.py",
141-
f"{config['module_name']}/features.py",
142-
f"{config['module_name']}/modeling/__init__.py",
143-
f"{config['module_name']}/modeling/train.py",
144-
f"{config['module_name']}/modeling/predict.py",
145-
f"{config['module_name']}/plots.py",
146-
})
146+
expected_files.update(
147+
{
148+
f"{config['module_name']}/config.py",
149+
f"{config['module_name']}/dataset.py",
150+
f"{config['module_name']}/features.py",
151+
f"{config['module_name']}/modeling/__init__.py",
152+
f"{config['module_name']}/modeling/train.py",
153+
f"{config['module_name']}/modeling/predict.py",
154+
f"{config['module_name']}/plots.py",
155+
}
156+
)
147157

148158
if config["docs"] == "mkdocs":
149-
expected_files.update({
150-
"docs/mkdocs.yml",
151-
"docs/README.md",
152-
"docs/docs/index.md",
153-
"docs/docs/getting-started.md",
154-
})
155-
156-
if config["version_control"] in ("git (local)", "git (github)"):
157-
# Expected after `git init`
158-
expected_files.update({
159-
".git/config",
160-
".git/description",
161-
".git/HEAD",
162-
".git/hooks/applypatch-msg.sample",
163-
".git/hooks/commit-msg.sample",
164-
".git/hooks/fsmonitor-watchman.sample",
165-
".git/hooks/post-update.sample",
166-
".git/hooks/pre-applypatch.sample",
167-
".git/hooks/pre-commit.sample",
168-
".git/hooks/pre-merge-commit.sample",
169-
".git/hooks/pre-push.sample",
170-
".git/hooks/pre-rebase.sample",
171-
".git/hooks/pre-receive.sample",
172-
".git/hooks/prepare-commit-msg.sample",
173-
".git/hooks/push-to-checkout.sample",
174-
".git/hooks/sendemail-validate.sample",
175-
".git/hooks/update.sample",
176-
".git/info/exclude",
177-
})
178-
# Expected after initial git commit
179-
# expected_files += [
180-
# ".git/COMMIT_EDITMSG",
181-
# ".git/FETCH_HEAD",
182-
# ".git/index",
183-
# ".git/logs/HEAD",
184-
# ".git/logs/refs/heads/main",
185-
# ".git/refs/heads/main",
186-
# ]
159+
expected_files.update(
160+
{
161+
"docs/mkdocs.yml",
162+
"docs/README.md",
163+
"docs/docs/index.md",
164+
"docs/docs/getting-started.md",
165+
}
166+
)
187167

188168
expected_files.add(config["dependency_file"])
189-
190-
ignored_files = set()
191-
169+
192170
if config["version_control"] in ("git (local)", "git (github)"):
193-
ignored_files.update({
194-
f.relative_to(root)
195-
for f in root.glob(".git/objects/*")
196-
if f.is_file()
197-
})
171+
# Expected after `git init`
172+
expected_files.update(
173+
{
174+
".git/config",
175+
".git/description",
176+
".git/HEAD",
177+
".git/hooks/applypatch-msg.sample",
178+
".git/hooks/commit-msg.sample",
179+
".git/hooks/fsmonitor-watchman.sample",
180+
".git/hooks/post-update.sample",
181+
".git/hooks/pre-applypatch.sample",
182+
".git/hooks/pre-commit.sample",
183+
".git/hooks/pre-merge-commit.sample",
184+
".git/hooks/pre-push.sample",
185+
".git/hooks/pre-rebase.sample",
186+
".git/hooks/pre-receive.sample",
187+
".git/hooks/prepare-commit-msg.sample",
188+
".git/hooks/push-to-checkout.sample",
189+
".git/hooks/sendemail-validate.sample",
190+
".git/hooks/update.sample",
191+
".git/info/exclude",
192+
}
193+
)
194+
# Expected after initial git commit
195+
expected_files.update(
196+
{
197+
".git/COMMIT_EDITMSG",
198+
".git/index",
199+
".git/logs/HEAD",
200+
".git/logs/refs/heads/main",
201+
".git/refs/heads/main",
202+
}
203+
)
204+
ignored_files.update(
205+
{f.relative_to(root) for f in root.glob(".git/objects/**/*") if f.is_file()}
206+
)
198207

199208
expected_files = {Path(f) for f in expected_files}
200209

201210
existing_files = {f.relative_to(root) for f in root.glob("**/*") if f.is_file()}
202211

203-
assert sorted(existing_files - ignored_files) == sorted(expected_files)
204-
212+
checked_files = existing_files - ignored_files
213+
214+
assert sorted(checked_files) == sorted(expected_files)
215+
205216
# Ignore files where curlies may exist but aren't unrendered jinja tags
206217
ignore_curly_files = {
207-
".git/hooks/fsmonitor-watchman.sample"
218+
Path(".git/hooks/fsmonitor-watchman.sample"),
219+
Path(".git/index"),
208220
}
209221

210-
assert all(
211-
no_curlies(root / f)
212-
for f in existing_files
213-
if str(f) not in ignore_curly_files
214-
)
222+
assert all(no_curlies(root / f) for f in checked_files - ignore_curly_files)
215223

216224

217225
def verify_makefile_commands(root, config):

0 commit comments

Comments
 (0)