#!/usr/bin/env zsh
#
# Copyright (c) 2016-2020 Sebastian Gniazdowski and contributors
# Copyright (c) 2021-2022 zdharma-continuum and contributors

emulate -L zsh -o extendedglob -o warncreateglobal -o typesetsilent

[[ -z "${ICE[submods]}" ]] && return 0

if [[ "$1" = plugin ]]; then
  local type="$1" user="$2" plugin="$3" id_as="$4" dir="$5"
else
  local type="$1" url="$2" id_as="$3" dir="$4"
fi

local -a mods parts lines
local mod

mods=( ${(@s.;.)ICE[submods]} )
for mod in "${mods[@]}"; do
  parts=( "${(@s:->:)mod}" )
  # Remove only leading and trailing whitespace
  # parts=( "${parts[@]//+([[:space:]])/}" )
  parts=( "${parts[@]//( (#s)[[:space:]]##|[[:space:]]##(#e) )/}" )

  print -r -- "zinit-annex-submods: ${ZINIT[col-bar]}@@${ZINIT[col-rst]} Updating submodule ${ZINIT[col-bar]}@@${ZINIT[col-rst]}: ${ZINIT[col-pname]}${parts[1]}${ZINIT[col-rst]} (directory: ${ZINIT[col-info]}${parts[2]}${ZINIT[col-rst]})"
  if [[ "${parts[1]}" != (http|https|ftp|ftps|scp)://* ]]; then
    parts[1]="https://github.com/${parts[1]}"
  fi

  command git -C "$dir/${parts[2]}" fetch
  lines=( ${(f)"$(command git -C "$dir/${parts[2]}" log --color --date=short --pretty=format:'%Cgreen%cd %h %Creset%s %Cred%d%Creset%n' ..FETCH_HEAD)"} )

  # Remove the (origin/master ...) segments, to expect only tags to appear
  lines=( "${(S)lines[@]//\(([,[:blank:]]#(origin|HEAD|master)[^a-zA-Z]##(HEAD|origin|master)[,[:blank:]]#)#\)/}" )

  if (( $#lines )); then
    print -rl -- "${lines[@]}"
  fi
  command git -C "$dir/${parts[2]}" pull --no-stat ${=ICE[pullopts]:---ff-only} origin |& { command egrep -v '(FETCH_HEAD|up to date\.|From.*://)' || : }
done
