MCPcopy Create free account
hub / github.com/cocoindex-io/cocoindex-code / add_to_gitignore

Function add_to_gitignore

src/cocoindex_code/cli.py:273–293  ·  view source on GitHub ↗

Add ``/.cocoindex_code/`` to ``.gitignore`` if ``.git`` exists. Creates ``.gitignore`` if it doesn't exist. Skips if the entry is already present.

(project_root: Path)

Source from the content-addressed store, hash-verified

271
272
273def add_to_gitignore(project_root: Path) -> None:
274 """Add ``/.cocoindex_code/`` to ``.gitignore`` if ``.git`` exists.
275
276 Creates ``.gitignore`` if it doesn't exist. Skips if the entry is already
277 present.
278 """
279 if not (project_root / ".git").is_dir():
280 return
281
282 gitignore = project_root / ".gitignore"
283 if gitignore.is_file():
284 content = gitignore.read_text()
285 if _GITIGNORE_ENTRY in content.splitlines():
286 return # already present
287 # Ensure a trailing newline before appending
288 if content and not content.endswith("\n"):
289 content += "\n"
290 content += f"{_GITIGNORE_COMMENT}\n{_GITIGNORE_ENTRY}\n"
291 gitignore.write_text(content)
292 else:
293 gitignore.write_text(f"{_GITIGNORE_COMMENT}\n{_GITIGNORE_ENTRY}\n")
294
295
296def remove_from_gitignore(project_root: Path) -> None:

Calls

no outgoing calls