|
| 1 | +#!/usr/bin/env python3 |
| 2 | +import subprocess |
| 3 | +import sys |
| 4 | + |
| 5 | + |
| 6 | +def run_command(command: list[str], check: bool = True) -> subprocess.CompletedProcess[str]: |
| 7 | + """Run a shell command and capture the output.""" |
| 8 | + print(f"Running command: {' '.join(command)}") |
| 9 | + result = subprocess.run(command, check=check, text=True, capture_output=True) |
| 10 | + if result.stdout: |
| 11 | + print(result.stdout, end="") |
| 12 | + if result.stderr: |
| 13 | + print(result.stderr, end="", file=sys.stderr) |
| 14 | + return result |
| 15 | + |
| 16 | + |
| 17 | +def stage_changes() -> None: |
| 18 | + """Stage all modified files.""" |
| 19 | + run_command(["git", "add", "-A"]) |
| 20 | + |
| 21 | + |
| 22 | +def amend_commit() -> None: |
| 23 | + """Amend the last commit to include the new changes.""" |
| 24 | + run_command(["git", "commit", "--amend", "--no-edit"]) |
| 25 | + |
| 26 | + |
| 27 | +def push_changes() -> None: |
| 28 | + """Push the amended commit to the remote repository.""" |
| 29 | + run_command(["git", "push", "--force-with-lease"]) |
| 30 | + |
| 31 | + |
| 32 | +def main() -> None: |
| 33 | + """Main function to automate the process of staging, amending, and pushing changes.""" |
| 34 | + # Stage the changes |
| 35 | + stage_changes() |
| 36 | + |
| 37 | + # Amend the last commit |
| 38 | + amend_commit() |
| 39 | + |
| 40 | + # Push the changes to the remote repository |
| 41 | + push_changes() |
| 42 | + |
| 43 | + |
| 44 | +if __name__ == "__main__": |
| 45 | + main() |
0 commit comments