Skip to content

Commit bde9fc0

Browse files
authored
Add .gitignore to .pytask to avoid committing it. (#646)
1 parent b235ae9 commit bde9fc0

File tree

5 files changed

+25
-9
lines changed

5 files changed

+25
-9
lines changed

.python-version

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3.12.2
1+
3.12.3

docs/source/changes.md

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ releases are available on [PyPI](https://pypi.org/project/pytask) and
88
## 0.5.2 - 2024-09-15
99

1010
- {pull}`640` stops the live display when an exception happened during the execution.
11+
- {pull}`646` adds a `.gitignore` to the `.pytask/` folder to exclude it from version
12+
control.
1113

1214
## 0.5.1 - 2024-07-20
1315

src/_pytask/clean.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import itertools
77
import shutil
88
import sys
9-
from pathlib import Path
109
from typing import TYPE_CHECKING
1110
from typing import Any
1211
from typing import Generator
@@ -36,6 +35,7 @@
3635
from _pytask.tree_util import tree_leaves
3736

3837
if TYPE_CHECKING:
38+
from pathlib import Path
3939
from typing import NoReturn
4040

4141

@@ -64,7 +64,12 @@ def pytask_extend_command_line_interface(cli: click.Group) -> None:
6464
@hookimpl
6565
def pytask_parse_config(config: dict[str, Any]) -> None:
6666
"""Parse the configuration."""
67-
config["exclude"] = to_list(config["exclude"]) + _DEFAULT_EXCLUDE
67+
config["exclude"] = (
68+
to_list(config["exclude"])
69+
+ _DEFAULT_EXCLUDE
70+
# Adding the cache folder to the exclude list.
71+
+ [config["root"].joinpath(".pytask", "*").as_posix()]
72+
)
6873

6974

7075
@click.command(cls=ColoredCommand)
@@ -116,10 +121,9 @@ def clean(**raw_config: Any) -> NoReturn: # noqa: C901, PLR0912
116121
session.hook.pytask_collect(session=session)
117122

118123
known_paths = _collect_all_paths_known_to_pytask(session)
119-
exclude = session.config["exclude"]
120124
include_directories = session.config["directories"]
121125
unknown_paths = _find_all_unknown_paths(
122-
session, known_paths, exclude, include_directories
126+
session, known_paths, session.config["exclude"], include_directories
123127
)
124128
common_ancestor = find_common_ancestor(
125129
*unknown_paths, *session.config["paths"]
@@ -189,10 +193,6 @@ def _collect_all_paths_known_to_pytask(session: Session) -> set[Path]:
189193
known_paths.add(session.config["config"])
190194
known_paths.add(session.config["root"])
191195

192-
database_url = session.config["database_url"]
193-
if database_url.drivername == "sqlite" and database_url.database:
194-
known_paths.add(Path(database_url.database))
195-
196196
# Add files tracked by git.
197197
if is_git_installed():
198198
git_root = get_root(session.config["root"])

src/_pytask/config.py

+6
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,12 @@ def pytask_parse_config(config: dict[str, Any]) -> None:
7979
"""Parse the configuration."""
8080
config["root"].joinpath(".pytask").mkdir(exist_ok=True, parents=True)
8181

82+
# Ensure a .gitignore file exists in the .pytask directory to avoid accidentally
83+
# committing the cache.
84+
gitignore_path = config["root"].joinpath(".pytask", ".gitignore")
85+
if not gitignore_path.exists():
86+
gitignore_path.write_text("# Automatically added by pytask.\n*\n")
87+
8288
config["paths"] = parse_paths(config["paths"])
8389

8490
config["markers"] = {

tests/test_config.py

+8
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,11 @@ def test_paths_are_relative_to_configuration_file(tmp_path):
123123
result = run_in_subprocess(("python", "script.py"), cwd=tmp_path)
124124
assert result.exit_code == ExitCode.OK
125125
assert "1 Succeeded" in result.stdout
126+
127+
128+
@pytest.mark.end_to_end
129+
def test_create_gitignore_file_in_pytask_directory(tmp_path):
130+
session = build(paths=tmp_path)
131+
132+
assert session.exit_code == ExitCode.OK
133+
assert tmp_path.joinpath(".pytask", ".gitignore").exists()

0 commit comments

Comments
 (0)