-
Notifications
You must be signed in to change notification settings - Fork 10.6k
feat(extensions): port git extension scripts to Python #3400
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
marcelsafin
wants to merge
3
commits into
github:main
Choose a base branch
from
marcelsafin:feat/3282-port-git-extension
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,179 @@ | ||
| #!/usr/bin/env python3 | ||
| """Git extension: auto_commit.py | ||
|
|
||
| Automatically commit changes after a Spec Kit command completes. | ||
| Python port of ``auto-commit.sh`` / ``auto-commit.ps1``. | ||
| Checks per-command config keys in git-config.yml before committing. | ||
|
|
||
| Usage: auto_commit.py <event_name> | ||
| e.g.: auto_commit.py after_specify | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import re | ||
| import shutil | ||
| import subprocess | ||
| import sys | ||
| from pathlib import Path | ||
|
|
||
|
|
||
| def _find_project_root(start: Path) -> Path | None: | ||
| current = start | ||
| while True: | ||
| if (current / ".specify").is_dir() or (current / ".git").exists(): | ||
| return current | ||
| if current.parent == current: | ||
| return None | ||
| current = current.parent | ||
|
|
||
|
|
||
| def _value_after_colon(line: str) -> str: | ||
| return re.sub(r"^[^:]*:\s*", "", line) | ||
|
|
||
|
|
||
| def _strip_quotes(value: str) -> str: | ||
| """Strip one leading quote and all trailing quotes, mirroring the bash sed.""" | ||
| value = re.sub(r"^[\"']", "", value) | ||
| return re.sub(r"[\"']*$", "", value) | ||
|
|
||
|
|
||
| def _parse_auto_commit_config( | ||
| config_file: Path, event_name: str | ||
| ) -> tuple[bool, str]: | ||
| """Parse the auto_commit section for this event, mirroring the bash line parser. | ||
|
|
||
| Returns (enabled, commit_msg). Looks for auto_commit.<event_name>.enabled | ||
| and .message, with auto_commit.default as fallback. | ||
| """ | ||
| enabled = False | ||
| commit_msg = "" | ||
| default_enabled = False | ||
| in_auto_commit = False | ||
| in_event = False | ||
|
|
||
| content = config_file.read_text(encoding="utf-8") | ||
| for line in content.splitlines(): | ||
| if line.startswith("auto_commit:"): | ||
| in_auto_commit = True | ||
| in_event = False | ||
| continue | ||
|
|
||
| # Exit auto_commit section on next top-level key | ||
| if in_auto_commit and re.match(r"^[a-z]", line): | ||
| break | ||
|
|
||
| if not in_auto_commit: | ||
| continue | ||
|
|
||
| if re.match(r"^\s+default:\s", line): | ||
| value = re.sub(r"\s", "", _value_after_colon(line)).lower() | ||
| if value == "true": | ||
| default_enabled = True | ||
|
|
||
| if re.match(rf"^\s+{re.escape(event_name)}:", line): | ||
| in_event = True | ||
| continue | ||
|
|
||
| if in_event: | ||
| # Exit on next sibling key (same indent level as event name) | ||
| if re.match(r"^\s{2}[a-z]", line) and not re.match(r"^\s{4}", line): | ||
| in_event = False | ||
| continue | ||
| if re.search(r"\s+enabled:", line): | ||
| value = re.sub(r"\s", "", _value_after_colon(line)).lower() | ||
| if value == "true": | ||
| enabled = True | ||
| elif value == "false": | ||
| enabled = False | ||
| if re.search(r"\s+message:", line): | ||
| commit_msg = _strip_quotes(_value_after_colon(line)) | ||
|
|
||
| # If event-specific key not found, use default — but only if the event | ||
| # section didn't exist at all (an explicit false must win). | ||
| if not enabled and default_enabled: | ||
| if not re.search(rf"^\s*{re.escape(event_name)}:", content, re.MULTILINE): | ||
| enabled = True | ||
|
|
||
| return enabled, commit_msg | ||
|
|
||
|
|
||
| def main(argv: list[str]) -> int: | ||
| event_name = argv[0] if argv else "" | ||
| if not event_name: | ||
| print(f"Usage: {Path(sys.argv[0]).name} <event_name>", file=sys.stderr) | ||
| return 1 | ||
|
|
||
| script_dir = Path(__file__).resolve().parent | ||
| repo_root = _find_project_root(script_dir) or Path.cwd() | ||
|
|
||
| if shutil.which("git") is None: | ||
| print("[specify] Warning: Git not found; skipped auto-commit", file=sys.stderr) | ||
| return 0 | ||
|
|
||
| probe = subprocess.run( | ||
| ["git", "rev-parse", "--is-inside-work-tree"], | ||
| cwd=repo_root, | ||
| capture_output=True, | ||
| text=True, | ||
| ) | ||
| if probe.returncode != 0: | ||
| print( | ||
| "[specify] Warning: Not a Git repository; skipped auto-commit", | ||
| file=sys.stderr, | ||
| ) | ||
| return 0 | ||
|
|
||
| config_file = repo_root / ".specify" / "extensions" / "git" / "git-config.yml" | ||
| if not config_file.is_file(): | ||
| # No config file — auto-commit disabled by default | ||
| return 0 | ||
|
|
||
| enabled, commit_msg = _parse_auto_commit_config(config_file, event_name) | ||
| if not enabled: | ||
| return 0 | ||
|
|
||
| # Check if there are changes to commit | ||
| def _quiet(*args: str) -> bool: | ||
| return ( | ||
| subprocess.run( | ||
| ["git", *args], cwd=repo_root, capture_output=True, text=True | ||
| ).returncode | ||
| == 0 | ||
| ) | ||
|
|
||
| untracked = subprocess.run( | ||
| ["git", "ls-files", "--others", "--exclude-standard"], | ||
| cwd=repo_root, | ||
| capture_output=True, | ||
| text=True, | ||
| ).stdout.strip() | ||
| if _quiet("diff", "--quiet", "HEAD") and _quiet("diff", "--cached", "--quiet") and not untracked: | ||
| print(f"[specify] No changes to commit after {event_name}", file=sys.stderr) | ||
| return 0 | ||
|
|
||
| # Derive a human-readable command name from the event | ||
| # e.g., after_specify -> specify, before_plan -> plan | ||
| command_name = re.sub(r"^(after_|before_)", "", event_name) | ||
| phase = "before" if event_name.startswith("before_") else "after" | ||
|
|
||
| if not commit_msg: | ||
| commit_msg = f"[Spec Kit] Auto-commit {phase} {command_name}" | ||
|
|
||
| steps = [ | ||
| (["git", "add", "."], "git add"), | ||
| (["git", "commit", "-q", "-m", commit_msg], "git commit"), | ||
| ] | ||
| for cmd, label in steps: | ||
| result = subprocess.run(cmd, cwd=repo_root, capture_output=True, text=True) | ||
| if result.returncode != 0: | ||
| output = (result.stdout + result.stderr).strip() | ||
| print(f"[specify] Error: {label} failed: {output}", file=sys.stderr) | ||
| return 1 | ||
|
|
||
| print(f"[OK] Changes committed {phase} {command_name}", file=sys.stderr) | ||
| return 0 | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| raise SystemExit(main(sys.argv[1:])) | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed. read_text is now wrapped in try/except OSError and returns (False, ""), so an unreadable config disables auto-commit the same way a missing one does. Added test_unreadable_config_skips_auto_commit (chmod 000, asserts exit 0, no traceback, no commit). 5241529