---
# Pinned to 2.36.1 because the unofficial win_git module hangs when using
# 2.37.3, this is either a breaking change in 2.37.x or an incompatibility with
# the win_git module. The git chocolatey package does not respect the version
# argument and always installs the most recent version, so instead download the
# installer from GitHub and install manually.
- set_fact:
    git_version: 2.36.1
    git_cli_exe: '{{ansible_env.ProgramFiles}}/Git/cmd/git.exe'
    git_run_installer: false

- name: detect if Git for Windows is installed
  win_stat:
    path: '{{git_cli_exe}}'
  register: git_cli_stat

- when: not git_cli_stat.stat.exists
  set_fact:
    git_run_installer: true

- name: check installed version
  when: git_cli_stat.stat.exists
  win_command: '"{{git_cli_exe}}" --version'
  register: git_cli_version
  changed_when: false

- when: git_cli_stat.stat.exists and git_version not in git_cli_version.stdout
  set_fact:
    git_run_installer: true

- include_tasks: Windows-installer.yaml
  when: git_run_installer

- name: clone config repos
  win_git:
    repo: '{{item.repo}}'
    dest: '{{ansible_env.USERPROFILE}}/.config/{{item.name}}'
    version: master
  with_items: '{{git_config_repos}}'

# - TODO: install pip packages
#   win_pip:
#     name: '{{git_pip_packages}}'
#     state: latest