diff --git a/README.md b/README.md index 1d825c1..8da260a 100644 --- a/README.md +++ b/README.md @@ -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 - **"Authentication failed"**: Check your Personal Access Token and repository 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 diff --git a/download_and_compare.py b/download_and_compare.py index 737fccf..1ff94b0 100644 --- a/download_and_compare.py +++ b/download_and_compare.py @@ -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