stages:
  - deploy

create_composer_tag:
  stage: deploy
  tags:
    - manage-git-repo
  rules:
    # Trigger only on tags
    - if: $CI_COMMIT_TAG
  before_script:
    - git config user.email "support@math.uzh.ch"
    - git config user.name "GitLab CI"
    - git merge --abort || true
    - git clean -ffdx
  script:
    # 1. Create the -full tag locally (pointing to the current "dirty" commit)
    - FULL_TAG="${CI_COMMIT_TAG}-full"
    - echo "Creating archival tag $FULL_TAG"
    - git tag $FULL_TAG $CI_COMMIT_SHA

    # 2. Setup the dist branch and clean it
    - git checkout -f -B dist $CI_COMMIT_SHA
    - rm -rf example hooks index.php php/tests js/tests nginx phpunit
    
    # 3. Commit the clean state
    - git add .
    - |
      if git diff --staged --quiet; then
        echo "No changes to commit for dist branch."
      else
        git commit -m "release: clean build for $CI_COMMIT_TAG [skip ci]"
      fi

    # 4. Move the original tag to this clean commit
    - git tag -f $CI_COMMIT_TAG

    # 5. Push everything back to GitLab
    # -o ci.skip is the key to preventing recursive pipeline triggers
    - echo "Pushing tags and dist branch with ci.skip..."
    - git push https://oauth2:${GL_TOKEN}@git.math.uzh.ch/${CI_PROJECT_PATH}.git dist $CI_COMMIT_TAG $FULL_TAG --force -o ci.skip