---
- name: Fetch TensorFlow source archive
  when: tensorflow_version != 'git' and not prebuild_release
  block:
    - name: Fetch TensorFlow{{ variant_name }} {{ tensorflow_version }} source
      ansible.builtin.get_url:
        url: "https://github.com/tensorflow/tensorflow/archive/v{{ tensorflow_version }}.tar.gz"
        dest: "{{ build_dir }}/tensorflow-{{ tensorflow_version }}.tar.gz"
        mode: 0600

    - name: Unpack TensorFlow{{ variant_name }} {{ tensorflow_version }} source
      ansible.builtin.unarchive:
        src: "{{ build_dir }}/tensorflow-{{ tensorflow_version }}.tar.gz"
        dest: "{{ build_dir }}/"
        remote_src: true

    - name: Rename source dir for oneDNN build
      ansible.builtin.command: "mv {{ build_dir }}/tensorflow-{{ tensorflow_version }} {{ tensorflow_build_dir }}"
      args:
        creates: "{{ tensorflow_build_dir }}"
        removes: "{{ build_dir }}/tensorflow-{{ tensorflow_version }}"
      when: onednn_build

- name: Fetch TensorFlow{{ variant_name }} from git
  when: tensorflow_version == 'git'
  ansible.builtin.git:
    repo: "https://github.com/tensorflow/tensorflow.git"  # noqa latest[git]
    dest: "{{ tensorflow_build_dir }}/"
    depth: 1
    recursive: true
    force: true

- name: Fetch TensorFlow{{ variant_name }} from git at commit {{ prebuild_hash }}
  when: prebuild_release
  ansible.builtin.git:
    repo: "https://github.com/tensorflow/tensorflow.git"
    dest: "{{ tensorflow_build_dir }}/"
    version: "{{ prebuild_hash }}"
    depth: 1
    recursive: true
    force: true

- name: Prepare TensorFlow source from git
  when: tensorflow_version == 'git' or prebuild_release
  block:
    - name: Set world write access to source
      ansible.builtin.file:
        path: "{{ tensorflow_build_dir }}/"
        state: directory
        mode: 0777

    - name: Get oneline version of git log for this checkout  # noqa no-changed-when
      ansible.builtin.command:
        cmd: "git log --pretty=oneline -1"
        chdir: "{{ tensorflow_build_dir }}/"
      register: git_log

    - name: Show git log
      ansible.builtin.debug:
        var: git_log.stdout

    - name: Record git commit hash to variable
      ansible.builtin.set_fact:
        git_commit: "{{ git_log.stdout | regex_search('^[0-9a-f]+') }}"

    - name: Save git commit hash to file
      ansible.builtin.template:
        src: "git_commit_hash.j2"
        dest: "{{ wheels_dir }}/git_commit_hash"
        owner: buildslave
        group: buildslave
        mode: 0444
        force: true

