This commit is contained in:
2025-08-30 20:31:00 +10:00
parent 3df2538080
commit 2b62ce0e28
2 changed files with 66 additions and 3 deletions

View File

@@ -49,6 +49,20 @@ def setup_git_repo():
if not run_git_command(f"git checkout -b {git_branch}"):
return False
print("Pulling latest changes from remote repository...")
if not run_git_command("git pull origin main --allow-unrelated-histories"):
print("Warning: Could not pull from remote, continuing with local changes")
else:
if github_token and git_repo_url.startswith('https://'):
print("Updating remote URL with authentication...")
auth_url = git_repo_url.replace('https://', f'https://{git_username}:{github_token}@')
if not run_git_command(f"git remote set-url origin {auth_url}"):
return False
print("Pulling latest changes from remote repository...")
if not run_git_command("git pull origin main"):
print("Warning: Could not pull from remote, continuing with local changes")
run_git_command(f"git config user.name '{git_username}'")
run_git_command(f"git config user.email '{git_email}'")
@@ -72,6 +86,27 @@ def verify_git_remote():
print(f"Git remote configuration: {remote_output}")
return True
def handle_git_conflicts():
print("Handling potential Git conflicts...")
try:
print("Stashing any local changes...")
run_git_command("git stash")
print("Pulling latest changes from remote...")
if not run_git_command("git pull origin main"):
print("Warning: Could not pull from remote")
return False
print("Applying stashed changes...")
if not run_git_command("git stash pop"):
print("Warning: Could not apply stashed changes")
return True
except Exception as e:
print(f"Error handling Git conflicts: {e}")
return False
def commit_and_push_changes():
if not os.path.exists("/app/.git"):
return False
@@ -96,9 +131,25 @@ def commit_and_push_changes():
print("Updated Git remote configuration:")
run_git_command("git remote -v")
run_git_command("git push origin HEAD")
print("Changes committed and pushed to Git repository")
return True
print("Attempting to push changes...")
push_result = subprocess.run("git push origin HEAD", shell=True, capture_output=True, text=True, cwd="/app")
if push_result.returncode == 0:
print("Changes committed and pushed to Git repository")
return True
else:
print(f"Push failed: {push_result.stderr}")
if "rejected" in push_result.stderr and "fetch first" in push_result.stderr:
print("Remote has changes, attempting to resolve conflicts...")
if handle_git_conflicts():
print("Retrying push after resolving conflicts...")
if run_git_command("git push origin HEAD"):
print("Changes successfully pushed after resolving conflicts")
return True
return False
except Exception as e:
print(f"Error pushing to Git: {e}")
return False