Skip to content

Commit 4f3c9cd

Browse files
scopmsullivan
authored andcommitted
Automatically write .gitignore to cache dir, ignoring everything (#8193)
1 parent 0e6e9c6 commit 4f3c9cd

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

mypy/build.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,12 +1080,31 @@ def _cache_dir_prefix(options: Options) -> str:
10801080
return base
10811081

10821082

1083+
def add_catch_all_gitignore(target_dir: str) -> None:
1084+
"""Add catch-all .gitignore to an existing directory.
1085+
1086+
No-op if the .gitignore already exists.
1087+
"""
1088+
gitignore = os.path.join(target_dir, ".gitignore")
1089+
try:
1090+
with open(gitignore, "x") as f:
1091+
print("# Automatically created by mypy", file=f)
1092+
print("*", file=f)
1093+
except FileExistsError:
1094+
pass
1095+
1096+
10831097
def create_metastore(options: Options) -> MetadataStore:
10841098
"""Create the appropriate metadata store."""
1099+
# Add catch-all .gitignore to cache dir if we created it
1100+
cache_dir_existed = os.path.isdir(options.cache_dir)
10851101
if options.sqlite_cache:
1086-
return SqliteMetadataStore(_cache_dir_prefix(options))
1102+
mds = SqliteMetadataStore(_cache_dir_prefix(options)) # type: MetadataStore
10871103
else:
1088-
return FilesystemMetadataStore(_cache_dir_prefix(options))
1104+
mds = FilesystemMetadataStore(_cache_dir_prefix(options))
1105+
if not cache_dir_existed and os.path.isdir(options.cache_dir):
1106+
add_catch_all_gitignore(options.cache_dir)
1107+
return mds
10891108

10901109

10911110
def get_cache_names(id: str, path: str, options: Options) -> Tuple[str, str, Optional[str]]:

0 commit comments

Comments
 (0)