Created
April 12, 2026 17:44
-
-
Save ulgens/a2791b902792385d029674d6389675b7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # /// script | |
| # dependencies = [ | |
| # # TODO: Is there a min version requirement? | |
| # "niquests", | |
| # "ipdb", | |
| # ] | |
| # /// | |
| # .gitignore file header sample: | |
| # # Source: https://raw.githubusercontent.com/github/gitignore/7bfab2ec5b8a7576811b4342991bb6764fe725cf/Python.gitignore | |
| # # Revision: 1 | |
| import argparse | |
| from pathlib import Path | |
| import niquests | |
| API_URL = "https://api.github.com/repos/github/gitignore/commits?path=Python.gitignore&per_page=1" | |
| REVISION = 1 | |
| GITIGNORE_FILE = Path(".gitignore") | |
| def get_latest_commit_hash(): | |
| headers = {"Accept": "application/vnd.github.v3+json"} | |
| resp = niquests.get(API_URL, headers=headers) | |
| resp.raise_for_status() | |
| data = resp.json() | |
| commit_hash = data[0]["sha"] | |
| return commit_hash | |
| def get_source(content_lines): | |
| source_line = content_lines[0].strip() | |
| try: | |
| source = source_line.split("Source: ")[1] | |
| except IndexError: | |
| source = None | |
| return source | |
| def get_revision(content_lines): | |
| revision_line = content_lines[1].strip() | |
| try: | |
| revision = revision_line.split("Revision: ")[1] | |
| revision = int(revision) | |
| except (IndexError, ValueError): | |
| revision = None | |
| return revision | |
| def main(revision): | |
| with GITIGNORE_FILE.open() as existing_gitignore: | |
| existing_content = existing_gitignore.readlines() | |
| latest_commit_hash = get_latest_commit_hash() | |
| source = f"https://raw.githubusercontent.com/github/gitignore/{latest_commit_hash}/Python.gitignore" | |
| existing_source = get_source(existing_content) | |
| existing_revision = get_revision(existing_content) | |
| if existing_source == source: | |
| if existing_revision != REVISION: | |
| print("Your .gitignore source is up to date, but the revision number is outdated.") | |
| else: | |
| print("Your .gitignore is already up to date.") | |
| return | |
| new_content_resp = niquests.get(source) | |
| new_content_resp.raise_for_status() | |
| with GITIGNORE_FILE.open("w") as f: | |
| f.write(f"# Source: {source}\n") | |
| f.write(f"# Revision: {revision}\n") | |
| f.write("\n") | |
| f.write(new_content_resp.text) | |
| print("Updated .gitignore to the latest version.") | |
| print(f"Source: {source}") | |
| print(f"Revision: {revision}") | |
| if __name__ == "__main__": | |
| parser = argparse.ArgumentParser() | |
| parser.add_argument( | |
| "-r", | |
| "--revision", | |
| type=int, | |
| default=REVISION, | |
| ) | |
| args = parser.parse_args() | |
| main(revision=args["revision"]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment