This commit is contained in:
2025-08-30 20:36:30 +10:00
parent 2b62ce0e28
commit 26607897c8

View File

@@ -22,6 +22,29 @@ def run_git_command(command):
print(f"Error running git command: {e}")
return False
def clean_untracked_files():
print("Cleaning up untracked files that might cause conflicts...")
try:
status_result = subprocess.run("git status --porcelain", shell=True, capture_output=True, text=True, cwd="/app")
if status_result.stdout.strip():
untracked_files = []
for line in status_result.stdout.strip().split('\n'):
if line.startswith('??'):
untracked_files.append(line[3:])
if untracked_files:
print(f"Found {len(untracked_files)} untracked files, removing them...")
for file_path in untracked_files:
if os.path.exists(file_path):
os.remove(file_path)
print(f"Removed: {file_path}")
return True
except Exception as e:
print(f"Error cleaning untracked files: {e}")
return False
def setup_git_repo():
git_repo_url = os.environ.get('GIT_REPO_URL')
git_username = os.environ.get('GIT_USERNAME')
@@ -50,9 +73,24 @@ 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")
print("Checking if remote repository has content...")
fetch_result = subprocess.run("git fetch origin", shell=True, capture_output=True, text=True, cwd="/app")
if fetch_result.returncode == 0:
print("Remote repository found, checking for conflicts...")
ls_remote = subprocess.run("git ls-remote --heads origin main", shell=True, capture_output=True, text=True, cwd="/app")
if ls_remote.stdout.strip():
print("Remote main branch exists, resetting to remote state...")
clean_untracked_files()
run_git_command("git fetch origin")
run_git_command("git reset --hard origin/main")
else:
print("Remote main branch doesn't exist, creating initial commit...")
run_git_command("git add .")
run_git_command("git commit -m 'Initial commit'")
else:
print("Could not fetch from remote, creating initial commit...")
run_git_command("git add .")
run_git_command("git commit -m 'Initial commit'")
else:
if github_token and git_repo_url.startswith('https://'):
print("Updating remote URL with authentication...")
@@ -60,9 +98,19 @@ def setup_git_repo():
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")
print("Repository already exists, fetching latest changes...")
if not run_git_command("git fetch origin"):
print("Warning: Could not fetch from remote")
else:
print("Checking if we need to reset to remote state...")
local_commit = subprocess.run("git rev-parse HEAD", shell=True, capture_output=True, text=True, cwd="/app")
remote_commit = subprocess.run("git rev-parse origin/main", shell=True, capture_output=True, text=True, cwd="/app")
if local_commit.returncode == 0 and remote_commit.returncode == 0:
if local_commit.stdout.strip() != remote_commit.stdout.strip():
print("Local and remote are different, resetting to remote state...")
clean_untracked_files()
run_git_command("git reset --hard origin/main")
run_git_command(f"git config user.name '{git_username}'")
run_git_command(f"git config user.email '{git_email}'")
@@ -90,18 +138,17 @@ 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")
print("Fetching latest changes from remote...")
if not run_git_command("git fetch origin"):
print("Warning: Could not fetch from remote")
return False
print("Applying stashed changes...")
if not run_git_command("git stash pop"):
print("Warning: Could not apply stashed changes")
print("Resetting to remote state to resolve conflicts...")
if not run_git_command("git reset --hard origin/main"):
print("Warning: Could not reset to remote state")
return False
print("Re-applying language file changes...")
return True
except Exception as e:
print(f"Error handling Git conflicts: {e}")