186 lines
5.5 KiB
Python
186 lines
5.5 KiB
Python
#!/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
|
|
"""
|
|
|
|
import os
|
|
import sys
|
|
import subprocess
|
|
from datetime import datetime
|
|
|
|
def check_git_in_path():
|
|
"""Check if git is available in PATH"""
|
|
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
|
|
except Exception as e:
|
|
print(f"Exception running API: {e}")
|
|
return False
|
|
|
|
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)
|
|
|
|
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")
|
|
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")
|
|
|
|
print("=" * 60)
|
|
|
|
if __name__ == "__main__":
|
|
main() |