#!/usr/bin/env python3 """ Auto-commit script for LightRAG project. Usage: python auto_commit_final.py "Commit message describing changes" """ import subprocess import sys import os from datetime import datetime def run_command(cmd, cwd=None): """Run a shell command and return output.""" # On Windows, if cmd starts with 'git', we need to use the full path to avoid conflict with git.py if sys.platform == "win32" and cmd.strip().startswith("git "): # Use full path to git.exe with proper quoting git_exe = r'"C:\Program Files\Git\bin\git.exe"' if os.path.exists(git_exe.strip('"')): # Replace 'git' with the full path parts = cmd.split(" ", 1) if parts[0] == "git": cmd = f'{git_exe} {parts[1]}' if len(parts) > 1 else git_exe try: result = subprocess.run(cmd, shell=True, capture_output=True, text=True, cwd=cwd) return result.returncode, result.stdout, result.stderr except Exception as e: return 1, "", str(e) def auto_commit(commit_message=None): """Perform automatic commit and push to Gitea.""" # Get commit message from command line or generate one if commit_message: message = commit_message elif len(sys.argv) > 1: message = sys.argv[1] else: # Generate a timestamp-based message timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S") message = f"Auto-commit: {timestamp}" print(f"Auto-commit starting with message: {message}") print("=" * 60) # Step 1: Check git status print("1. Checking git status...") code, out, err = run_command("git status --porcelain") if code != 0: print(f"Error checking git status: {err}") return False if not out.strip(): print("No changes to commit.") return True print(f"Changes detected:\n{out}") # Step 2: Add all changes print("\n2. Adding all changes...") code, out, err = run_command("git add -A") if code != 0: print(f"Error adding changes: {err}") return False print("Changes added.") # Step 3: Commit print(f"\n3. Committing with message: '{message}'") code, out, err = run_command(f'git commit -m "{message}"') if code != 0: print(f"Error committing: {err}") return False print(f"Commit successful: {out.strip()}") # Step 4: Push to remote print("\n4. Pushing to remote repository...") code, out, err = run_command("git push origin master") if code != 0: print(f"Error pushing: {err}") # Try with credentials in URL print("Trying with credentials...") remote_url = "http://jleu3482:jleu1212@localhost:8467/jleu3482/railseek6.git" code, out, err = run_command(f'git push {remote_url} master') if code != 0: print(f"Push failed: {err}") return False print("Push successful!") # Step 5: Show git log print("\n5. Latest commit:") code, out, err = run_command("git log --oneline -3") if code == 0: print(out) print("\n" + "=" * 60) print("Auto-commit completed successfully!") return True def setup_git_config(): """Ensure git config is properly set.""" print("Checking git configuration...") # Set user name if not set code, out, err = run_command("git config user.name") if not out.strip(): run_command('git config user.name "jleu3482"') print("Set user.name to jleu3482") # Set user email if not set code, out, err = run_command("git config user.email") if not out.strip(): run_command('git config user.email "slclabs@gmail.com"') print("Set user.email to slclabs@gmail.com") # Set remote URL remote_url = "http://localhost:8467/jleu3482/railseek6.git" code, out, err = run_command("git remote get-url origin") if code != 0 or not out.strip(): run_command(f'git remote add origin {remote_url}') print(f"Set remote origin to {remote_url}") else: print(f"Remote origin already set to: {out.strip()}") if __name__ == "__main__": print("LightRAG Auto-Commit Script") print("=" * 60) # Setup git config setup_git_config() # Run auto-commit success = auto_commit() if success: sys.exit(0) else: print("Auto-commit failed!") sys.exit(1)