---
- name: Test just the versions-pull pre_tasks from site.yml
  hosts: localhost
  gather_facts: false
  tasks:
    - name: Fetch origin/main (non-fatal -- a self-heal nicety, not a hard requirement)
      ansible.builtin.command:
        cmd: git fetch origin main
        chdir: /home/jpaul/Escritorio/plevion/plevion-k8s
      register: versions_fetch
      changed_when: false
      failed_when: false

    - name: Warn and skip the versions-file pull if the network/fetch is unavailable
      ansible.builtin.debug:
        msg: >-
          Could not fetch origin/main ({{ versions_fetch.stderr | default('unknown error') }})
          -- continuing with whatever's already on disk for
          environments/local/versions/kustomization.yml.
      when: versions_fetch.rc != 0

    - name: Pull the latest versions file from origin/main if it's safe to
      when: versions_fetch.rc == 0
      block:
        - name: Check for an uncommitted local edit to the versions file
          ansible.builtin.command:
            cmd: git status --porcelain -- environments/local/versions/kustomization.yml
            chdir: /home/jpaul/Escritorio/plevion/plevion-k8s
          register: versions_dirty
          changed_when: false

        - name: Find the local commit that last touched the versions file
          ansible.builtin.command:
            cmd: git log -1 --format=%H -- environments/local/versions/kustomization.yml
            chdir: /home/jpaul/Escritorio/plevion/plevion-k8s
          register: versions_last_commit
          changed_when: false

        - name: Check whether that commit is an ancestor of origin/main (i.e. local is just behind, not diverged)
          ansible.builtin.command:
            cmd: git merge-base --is-ancestor {{ versions_last_commit.stdout }} origin/main
            chdir: /home/jpaul/Escritorio/plevion/plevion-k8s
          register: versions_is_ancestor
          changed_when: false
          failed_when: false
          when: versions_last_commit.stdout | length > 0

        - name: Pull origin/main's version -- local has no uncommitted edit and hasn't diverged
          ansible.builtin.command:
            cmd: git checkout origin/main -- environments/local/versions/kustomization.yml
            chdir: /home/jpaul/Escritorio/plevion/plevion-k8s
          when: >-
            versions_dirty.stdout | length == 0 and
            versions_last_commit.stdout | length > 0 and
            versions_is_ancestor.rc == 0

        - name: Warn and skip -- local has an uncommitted edit to the versions file
          ansible.builtin.debug:
            msg: "SKIPPED: uncommitted local edit"
          when: versions_dirty.stdout | length > 0

        - name: Warn and skip -- local has a committed change origin/main doesn't have
          ansible.builtin.debug:
            msg: "SKIPPED: local commit not on origin/main"
          when: >-
            versions_dirty.stdout | length == 0 and
            versions_last_commit.stdout | length > 0 and
            versions_is_ancestor.rc != 0
