Files
railseek6/gogit_commit.py

182 lines
5.4 KiB
Python

#!/usr/bin/env python3
"""
Commit the project using Go-Git (Gitea's git.exe)
This script uses GitPython to interface with the Go-Git executable.
"""
import os
import sys
import subprocess
from datetime import datetime
# Path to Gitea's git.exe
GIT_EXE = r"C:\Program Files\gitea\git.exe"
def check_git_exe():
"""Check if git.exe exists and is accessible"""
if not os.path.exists(GIT_EXE):
print(f"❌ Git executable not found at: {GIT_EXE}")
return False
print(f"✓ Found Go-Git executable: {GIT_EXE}")
return True
def run_git_command(args, cwd=None):
"""Run a git command using the Go-Git executable"""
if cwd is None:
cwd = os.getcwd()
cmd = [GIT_EXE] + args
print(f"Running: {' '.join(cmd)}")
try:
result = subprocess.run(
cmd,
cwd=cwd,
capture_output=True,
text=True,
encoding='utf-8',
errors='ignore'
)
if result.returncode == 0:
print(f"✓ Success: {result.stdout}")
return True, result.stdout
else:
print(f"❌ Error (code {result.returncode}): {result.stderr}")
return False, result.stderr
except Exception as e:
print(f"❌ Exception: {e}")
return False, str(e)
def initialize_repository():
"""Initialize git repository if not already initialized"""
# Check if .git directory exists
if os.path.exists(".git"):
print("✓ Git repository already initialized")
return True
print("Initializing git repository...")
success, output = run_git_command(["init"])
if success:
print("✓ Repository initialized")
# Configure user
run_git_command(["config", "user.name", "jleu3482"])
run_git_command(["config", "user.email", "jleu3482@mtrcompute.com"])
# Add remote
remote_url = "https://git.mtrcompute.com/jleu3482/railseek6.git"
run_git_command(["remote", "add", "origin", remote_url])
return True
else:
print("❌ Failed to initialize repository")
return False
def commit_changes(description):
"""Commit all changes with the given description"""
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
full_message = f"{description}\n\nAuto-commit at: {timestamp}"
print(f"Committing changes: {description}")
# Add all files
success, output = run_git_command(["add", "."])
if not success:
print("❌ Failed to add files")
return False
# Commit
success, output = run_git_command(["commit", "-m", full_message])
if not success:
print("❌ Failed to commit")
return False
print("✓ Changes committed")
return True
def push_to_remote():
"""Push changes to remote repository"""
print("Pushing to remote repository...")
# Try pushing to main branch
success, output = run_git_command(["push", "-u", "origin", "main"])
if not success:
# Try master branch
print("Trying master branch...")
success, output = run_git_command(["push", "-u", "origin", "master"])
if success:
print("✓ Changes pushed to remote")
return True
else:
print("❌ Failed to push to remote")
return False
def get_status():
"""Get git status"""
print("Getting git status...")
success, output = run_git_command(["status"])
return success, output
def main():
"""Main function"""
if len(sys.argv) < 2:
print("Usage: python gogit_commit.py \"Description of changes\"")
print("Example: python gogit_commit.py \"Added document download endpoint\"")
sys.exit(1)
description = sys.argv[1]
print("=" * 60)
print("GO-GIT AUTO-COMMIT")
print("=" * 60)
print(f"Description: {description}")
print(f"Git executable: {GIT_EXE}")
print(f"Working directory: {os.getcwd()}")
print()
# Check if git.exe exists
if not check_git_exe():
print("\n⚠ Please ensure Gitea is installed at: C:\\Program Files\\gitea")
print(" The git.exe should be in that directory.")
sys.exit(1)
# Initialize repository if needed
if not initialize_repository():
print("\n⚠ Could not initialize repository")
print(" You may need to initialize manually:")
print(f" {GIT_EXE} init")
print(f" {GIT_EXE} remote add origin https://git.mtrcompute.com/jleu3482/railseek6.git")
sys.exit(1)
# Get current status
get_status()
# Commit changes
if not commit_changes(description):
print("\n⚠ Could not commit changes")
print(" Check if there are any changes to commit.")
sys.exit(1)
# Push to remote
if not push_to_remote():
print("\n⚠ Could not push to remote")
print(" You may need to push manually:")
print(f" {GIT_EXE} push origin main")
print(" Or check your network connection and credentials.")
print()
print("=" * 60)
print("SUMMARY")
print("=" * 60)
print("✓ Repository: https://git.mtrcompute.com/jleu3482/railseek6")
print(f"✓ Commit: {description}")
print("✓ Timestamp:", datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
print()
print("View repository at: https://git.mtrcompute.com/jleu3482/railseek6")
print("=" * 60)
if __name__ == "__main__":
main()