Create or backfill ``.gitignore`` so older workspaces pick up new patterns.
(directory: Path)
| 170 | |
| 171 | |
| 172 | def _ensure_gitignore(directory: Path) -> None: |
| 173 | """Create or backfill ``.gitignore`` so older workspaces pick up new patterns.""" |
| 174 | gi = directory / ".gitignore" |
| 175 | if not gi.exists(): |
| 176 | gi.write_text(_GITIGNORE_CONTENT) |
| 177 | return |
| 178 | |
| 179 | existing = gi.read_text() |
| 180 | existing_lines = set(existing.splitlines()) |
| 181 | missing = [ |
| 182 | line |
| 183 | for line in _GITIGNORE_CONTENT.splitlines() |
| 184 | if line and not line.startswith("#") and line not in existing_lines |
| 185 | ] |
| 186 | if not missing: |
| 187 | return |
| 188 | |
| 189 | sep = "" if existing.endswith("\n") else "\n" |
| 190 | gi.write_text(existing + sep + "\n".join(missing) + "\n") |
| 191 | |
| 192 | |
| 193 | @click.group(invoke_without_command=True) |