name: Synchronize to Private Repository
on:
  push:
    branches:
      - main
permissions:
  contents: write
jobs:
  cherry-pick-commits:
    runs-on: ubuntu-latest
    if: github.repository == 'nunchaku-tech/nunchaku'
    steps:
      - name: Clone private repository
        run: |
          git clone https://x-access-token:${{ secrets.GH_TOKEN }}@github.com/nunchaku-tech/nunchaku-dev.git
      - name: Add public remote and fetch
        run: |
          cd nunchaku-dev
          git remote add public https://x-access-token:${{ secrets.GH_TOKEN }}@github.com/nunchaku-tech/nunchaku.git
          git fetch public main
      - name: Cherry-pick latest commit from public/main
        run: |
          set -e
          cd nunchaku-dev
          COMMIT=$(git rev-parse public/main)
          COMMIT_MSG=$(git log -1 --pretty=%B $COMMIT)

          echo "Latest commit: $COMMIT"
          echo "Commit message: $COMMIT_MSG"

          if [[ "$COMMIT_MSG" == "[Auto Sync]"* ]]; then
            echo "Skipping [Auto Sync] commit."
            exit 0
          fi

          if [[ "$COMMIT_MSG" == *"[Dont Sync]"* ]]; then
            echo "Skipping [Dont Sync] commit."
            exit 0
          fi

          # Preserve original author and amend commit message
          GIT_AUTHOR_NAME=$(git log --format='%aN' -n 1 $COMMIT)
          GIT_AUTHOR_EMAIL=$(git log --format='%aE' -n 1 $COMMIT)

          git config --global user.name "$GIT_AUTHOR_NAME"
          git config --global user.email "$GIT_AUTHOR_EMAIL"

          NEW_MSG="[Auto Sync] ${COMMIT_MSG}"

          PARENTS=$(git rev-list --parents -n 1 $COMMIT)
          NUM_PARENTS=$(echo $PARENTS | wc -w)

          IGNORED_FILES=(
            ".github/workflows/sync-to-private.yml"
          )

          echo "Attempting cherry-pick..."

          if [ "$NUM_PARENTS" -gt 2 ]; then
            echo "Merge commit detected. Using -m 1"
            git cherry-pick --allow-empty -m 1 $COMMIT || (
              echo "Cherry-pick failed. Removing ignored files and continuing..."
              for file in "${IGNORED_FILES[@]}"; do
                git rm -rf "$file" || true
                rm -rf "$file" || true
              done
              git commit --allow-empty -m "$COMMIT_MSG"
              echo "Conflict resolved."
            )
          else
            echo "Normal commit. Cherry-picking directly."
            git cherry-pick --allow-empty $COMMIT || (
              echo "Cherry-pick failed. Removing ignored files and continuing..."
              for file in "${IGNORED_FILES[@]}"; do
                git rm -rf "$file" || true
                rm -rf "$file" || true
              done
              git commit --allow-empty -m "$COMMIT_MSG"
              echo "Conflict resolved."
            )
          fi

          for file in "${IGNORED_FILES[@]}"; do
            git rm -rf "$file" || true
            rm -rf "$file" || true
          done

          git commit --amend --allow-empty -m "$NEW_MSG" --author="$GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL>"
      - name: Push to the private main branch
        run: |
          cd nunchaku-dev
          git push
