Test: Added auto-commit documentation and test file
This commit is contained in:
@@ -1,186 +1,130 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Final auto-commit solution that tries multiple methods:
|
||||
1. First try: Use standard git if available
|
||||
2. Second try: Use Go-Git with proper environment
|
||||
3. Third try: Use Gitea API directly
|
||||
Auto-commit script for LightRAG project.
|
||||
Usage: python auto_commit_final.py "Commit message describing changes"
|
||||
"""
|
||||
|
||||
import os
|
||||
import sys
|
||||
import subprocess
|
||||
import sys
|
||||
import os
|
||||
from datetime import datetime
|
||||
|
||||
def check_git_in_path():
|
||||
"""Check if git is available in PATH"""
|
||||
def run_command(cmd, cwd=None):
|
||||
"""Run a shell command and return output."""
|
||||
try:
|
||||
result = subprocess.run(
|
||||
["git", "--version"],
|
||||
capture_output=True,
|
||||
text=True
|
||||
)
|
||||
return result.returncode == 0
|
||||
except:
|
||||
return False
|
||||
|
||||
def check_gogit():
|
||||
"""Check if Go-Git is available"""
|
||||
git_exe = r"C:\Program Files\gitea\git.exe"
|
||||
return os.path.exists(git_exe)
|
||||
|
||||
def run_standard_git(description):
|
||||
"""Commit using standard git in PATH"""
|
||||
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||||
commit_message = f"{description}\n\nAuto-commit at: {timestamp}"
|
||||
|
||||
commands = [
|
||||
["git", "add", "."],
|
||||
["git", "commit", "-m", commit_message],
|
||||
["git", "push", "origin", "main"]
|
||||
]
|
||||
|
||||
print("Using standard Git...")
|
||||
for cmd in commands:
|
||||
print(f"Running: {' '.join(cmd)}")
|
||||
try:
|
||||
result = subprocess.run(cmd, capture_output=True, text=True)
|
||||
if result.returncode != 0:
|
||||
print(f"Error: {result.stderr}")
|
||||
return False
|
||||
except Exception as e:
|
||||
print(f"Exception: {e}")
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
def run_gogit(description):
|
||||
"""Commit using Go-Git"""
|
||||
git_exe = r"C:\Program Files\gitea\git.exe"
|
||||
gitea_dir = os.path.dirname(git_exe)
|
||||
|
||||
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||||
commit_message = f"{description}\n\nAuto-commit at: {timestamp}"
|
||||
|
||||
print("Using Go-Git...")
|
||||
|
||||
# Try with gogit.bat wrapper if it exists
|
||||
if os.path.exists("gogit.bat"):
|
||||
print("Using gogit.bat wrapper...")
|
||||
commands = [
|
||||
["gogit.bat", "add", "."],
|
||||
["gogit.bat", "commit", "-m", commit_message],
|
||||
["gogit.bat", "push", "origin", "main"]
|
||||
]
|
||||
else:
|
||||
# Try direct git.exe with environment
|
||||
print(f"Using git.exe directly from {gitea_dir}...")
|
||||
env = os.environ.copy()
|
||||
env["GITEA_WORK_DIR"] = gitea_dir
|
||||
env["GITEA_CUSTOM_DIR"] = os.path.join(gitea_dir, "custom")
|
||||
|
||||
commands = [
|
||||
[git_exe, "add", "."],
|
||||
[git_exe, "commit", "-m", commit_message],
|
||||
[git_exe, "push", "origin", "main"]
|
||||
]
|
||||
|
||||
for cmd in commands:
|
||||
print(f"Running: {' '.join(cmd)}")
|
||||
try:
|
||||
result = subprocess.run(
|
||||
cmd,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
cwd=gitea_dir if "gogit.bat" not in cmd[0] else None
|
||||
)
|
||||
print(f"Output: {result.stdout}")
|
||||
if result.returncode != 0:
|
||||
print(f"Error: {result.stderr}")
|
||||
except Exception as e:
|
||||
print(f"Exception: {e}")
|
||||
|
||||
return True
|
||||
|
||||
def run_gitea_api(description):
|
||||
"""Commit using Gitea API"""
|
||||
print("Using Gitea API...")
|
||||
try:
|
||||
result = subprocess.run(
|
||||
[sys.executable, "final_gitea_commit.py", description],
|
||||
capture_output=True,
|
||||
text=True
|
||||
)
|
||||
print(result.stdout)
|
||||
if result.returncode == 0:
|
||||
return True
|
||||
else:
|
||||
print(f"API Error: {result.stderr}")
|
||||
return False
|
||||
result = subprocess.run(cmd, shell=True, capture_output=True, text=True, cwd=cwd)
|
||||
return result.returncode, result.stdout, result.stderr
|
||||
except Exception as e:
|
||||
print(f"Exception running API: {e}")
|
||||
return False
|
||||
return 1, "", str(e)
|
||||
|
||||
def main():
|
||||
if len(sys.argv) < 2:
|
||||
print("Usage: python auto_commit_final.py \"Description of changes\"")
|
||||
print("Example: python auto_commit_final.py \"Added document download endpoint\"")
|
||||
sys.exit(1)
|
||||
def auto_commit(commit_message=None):
|
||||
"""Perform automatic commit and push to Gitea."""
|
||||
|
||||
description = sys.argv[1]
|
||||
|
||||
print("=" * 60)
|
||||
print("AUTO-COMMIT SYSTEM")
|
||||
print("=" * 60)
|
||||
print(f"Commit: {description}")
|
||||
print(f"Time: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
|
||||
print()
|
||||
|
||||
# Check available methods
|
||||
has_standard_git = check_git_in_path()
|
||||
has_gogit = check_gogit()
|
||||
|
||||
print("Available methods:")
|
||||
print(f" Standard Git: {'✓' if has_standard_git else '✗'}")
|
||||
print(f" Go-Git: {'✓' if has_gogit else '✗'}")
|
||||
print(f" Gitea API: ✓ (always available)")
|
||||
print()
|
||||
|
||||
# Try methods in order of preference
|
||||
success = False
|
||||
|
||||
if has_standard_git:
|
||||
print("Trying standard Git first...")
|
||||
success = run_standard_git(description)
|
||||
if success:
|
||||
print("✓ Success with standard Git")
|
||||
|
||||
if not success and has_gogit:
|
||||
print("\nStandard Git failed, trying Go-Git...")
|
||||
success = run_gogit(description)
|
||||
if success:
|
||||
print("✓ Success with Go-Git")
|
||||
|
||||
if not success:
|
||||
print("\nBoth Git methods failed, using Gitea API...")
|
||||
success = run_gitea_api(description)
|
||||
if success:
|
||||
print("✓ Success with Gitea API")
|
||||
|
||||
print()
|
||||
print("=" * 60)
|
||||
print("RESULT")
|
||||
print("=" * 60)
|
||||
if success:
|
||||
print(f"✅ SUCCESS: Changes committed to repository")
|
||||
print(f"📁 Repository: https://git.mtrcompute.com/jleu3482/railseek6")
|
||||
# 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:
|
||||
print(f"❌ FAILED: Could not commit changes")
|
||||
print("Please check:")
|
||||
print("1. Git installation and PATH")
|
||||
print("2. Gitea credentials")
|
||||
print("3. Network connection")
|
||||
# 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__":
|
||||
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)
|
||||
Reference in New Issue
Block a user