|
| 1 | +#!/usr/bin/env python3 |
| 2 | +import re |
| 3 | +import subprocess |
| 4 | +import sys |
| 5 | +from pathlib import Path |
| 6 | + |
| 7 | +from packaging import version |
| 8 | + |
| 9 | + |
| 10 | +def run_command(command, check=True): |
| 11 | + """Run a command and return its output""" |
| 12 | + try: |
| 13 | + result = subprocess.run(command, check=check, capture_output=True, text=True) |
| 14 | + return result.stdout.strip() |
| 15 | + except subprocess.CalledProcessError as e: |
| 16 | + print(f"Error running command {' '.join(command)}: {e}") |
| 17 | + print(f"Error output: {e.stderr}") |
| 18 | + sys.exit(1) |
| 19 | + |
| 20 | +def get_modified_files(): |
| 21 | + """Get list of modified files""" |
| 22 | + # Get both staged and unstaged changes |
| 23 | + staged = run_command(['git', 'diff', '--staged', '--name-only']).split('\n') |
| 24 | + unstaged = run_command(['git', 'diff', '--name-only']).split('\n') |
| 25 | + # Combine and remove duplicates while preserving order |
| 26 | + all_files = [] |
| 27 | + for f in staged + unstaged: |
| 28 | + if f and f not in all_files: |
| 29 | + all_files.append(f) |
| 30 | + return all_files |
| 31 | + |
| 32 | +def check_unstaged_changes(): |
| 33 | + """Check for unstaged changes""" |
| 34 | + result = subprocess.run(['git', 'diff', '--quiet'], capture_output=True) |
| 35 | + return result.returncode != 0 |
| 36 | + |
| 37 | +def commit_changes(files, message): |
| 38 | + """Commit specified files with a message""" |
| 39 | + try: |
| 40 | + # Add files explicitly first |
| 41 | + for file in files: |
| 42 | + print(f"Adding file: {file}") |
| 43 | + subprocess.run(['git', 'add', file], check=True) |
| 44 | + |
| 45 | + # Show what's being committed |
| 46 | + print("\nFiles staged for commit:") |
| 47 | + subprocess.run(['git', 'status', '--short'], check=True) |
| 48 | + |
| 49 | + # Commit |
| 50 | + print(f"\nCommitting with message: {message}") |
| 51 | + subprocess.run(['git', 'commit', '-m', message], check=True) |
| 52 | + |
| 53 | + # Now pull with rebase (after changes are committed) |
| 54 | + print("\nPulling latest changes...") |
| 55 | + subprocess.run(['git', 'pull', '--rebase', 'origin', 'main'], check=True) |
| 56 | + |
| 57 | + # Push |
| 58 | + print("\nPushing to origin main...") |
| 59 | + subprocess.run(['git', 'push', 'origin', 'main'], check=True) |
| 60 | + |
| 61 | + print("Changes committed and pushed successfully!") |
| 62 | + |
| 63 | + except subprocess.CalledProcessError as e: |
| 64 | + print(f"Git operation failed: {e}") |
| 65 | + print(f"Error output: {e.stderr if e.stderr else 'None'}") |
| 66 | + sys.exit(1) |
| 67 | + |
| 68 | +def update_version(): |
| 69 | + """Run the version update script""" |
| 70 | + try: |
| 71 | + subprocess.run([sys.executable, 'scripts/update_version.py'], check=True) |
| 72 | + except subprocess.CalledProcessError as e: |
| 73 | + print(f"Error updating version: {e}") |
| 74 | + sys.exit(1) |
| 75 | + |
| 76 | +def create_release(): |
| 77 | + """Run the release script""" |
| 78 | + try: |
| 79 | + subprocess.run([sys.executable, 'scripts/release.py'], check=True) |
| 80 | + except subprocess.CalledProcessError as e: |
| 81 | + print(f"Error creating release: {e}") |
| 82 | + sys.exit(1) |
| 83 | + |
| 84 | +def main(): |
| 85 | + # 1. Get modified files |
| 86 | + modified_files = [f for f in get_modified_files() if f] |
| 87 | + |
| 88 | + # Handle existing changes if any |
| 89 | + if modified_files: |
| 90 | + print("\nModified files:") |
| 91 | + for f in modified_files: |
| 92 | + print(f"- {f}") |
| 93 | + |
| 94 | + response = input("\nCommit these changes before release? [y/N] ") |
| 95 | + if response.lower() == 'y': |
| 96 | + commit_message = input("\nEnter commit message: ") |
| 97 | + if not commit_message: |
| 98 | + print("Commit message cannot be empty.") |
| 99 | + sys.exit(1) |
| 100 | + |
| 101 | + print("\nCommitting changes...") |
| 102 | + commit_changes(modified_files, commit_message) |
| 103 | + |
| 104 | + # Always proceed with version update and release |
| 105 | + print("\nUpdating version...") |
| 106 | + update_version() |
| 107 | + |
| 108 | + print("\nCreating release...") |
| 109 | + create_release() |
| 110 | + |
| 111 | + print("\nProcess completed successfully!") |
| 112 | + |
| 113 | +if __name__ == "__main__": |
| 114 | + main() |
0 commit comments