Skip to content

Instantly share code, notes, and snippets.

@Tremeschin
Created August 23, 2025 02:25
Show Gist options
  • Select an option

  • Save Tremeschin/f35d1e47b2019cc43046d969c0dc5096 to your computer and use it in GitHub Desktop.

Select an option

Save Tremeschin/f35d1e47b2019cc43046d969c0dc5096 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
# fmt: off
# Todo: Find a new name, write proper package
from pathlib import Path
DIVISOR = "# <!-- AUTOMATIC -->"
HOME = Path().home()
def main():
target = (HOME/".gitignore").read_text().split(DIVISOR)[0].splitlines()
target = filter(lambda line: not line.startswith("#"), target)
target = list(map(Path, filter(None, target)))
ignore = list(map(str, sorted(target)))
ignore.append(f"\n{DIVISOR}")
ignore.append("*")
for file in sorted(target):
for path in reversed([file] + list(file.parents)[:-1]):
ignore.append(f"!{path}")
(HOME/".gitignore").write_text("\n".join(ignore))
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment