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

@@ -135,3 +135,15 @@ If you encounter authentication errors like "could not read Username", try these
- **"No such device or address"**: Usually means Git is trying to prompt for credentials interactively - **"No such device or address"**: Usually means Git is trying to prompt for credentials interactively
- **"Authentication failed"**: Check your Personal Access Token and repository permissions - **"Authentication failed"**: Check your Personal Access Token and repository permissions
- **"Repository not found"**: Verify the repository URL and your access permissions - **"Repository not found"**: Verify the repository URL and your access permissions
- **"Updates were rejected because the remote contains work"**: The script now automatically handles this by:
1. Stashing local changes
2. Pulling latest changes from remote
3. Applying stashed changes
4. Retrying the push
### Conflict Resolution
The script automatically handles common Git conflicts:
- **Remote ahead of local**: Automatically pulls latest changes before pushing
- **Merge conflicts**: Uses git stash to preserve changes, then pulls and reapplies
- **Unrelated histories**: Uses `--allow-unrelated-histories` flag for initial setup

View File

@@ -50,6 +50,20 @@ def setup_git_repo():
if not run_git_command(f"git checkout -b {git_branch}"): if not run_git_command(f"git checkout -b {git_branch}"):
return False 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.name '{git_username}'")
run_git_command(f"git config user.email '{git_email}'") 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}") print(f"Git remote configuration: {remote_output}")
return True 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(): def commit_and_push_changes():
if not os.path.exists("/app/.git"): if not os.path.exists("/app/.git"):
return False return False
@@ -96,9 +131,25 @@ def commit_and_push_changes():
print("Updated Git remote configuration:") print("Updated Git remote configuration:")
run_git_command("git remote -v") run_git_command("git remote -v")
run_git_command("git push origin HEAD") print("Attempting to push changes...")
print("Changes committed and pushed to Git repository") push_result = subprocess.run("git push origin HEAD", shell=True, capture_output=True, text=True, cwd="/app")
return True
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: except Exception as e:
print(f"Error pushing to Git: {e}") print(f"Error pushing to Git: {e}")
return False return False