#!/bin/sh
#
# gits - print detail infomation about current git repository

# shellcheck disable=SC2312

if ! command -v git >/dev/null 2>&1; then
  echo 'Error: Git is not installed' 2>&1
  exit 1
fi

if [ "$(git rev-parse --is-inside-work-tree 2>/dev/null)" != "true" ]; then
  echo 'Error: Not a git repository' 2>&1
  exit 1
fi

## Common
get_branch() {
  # HEAD branch
  head_branch=$(git symbolic-ref -q --short HEAD)
  if [ -z "${head_branch}" ]; then
    prehash=$(git rev-parse --short HEAD)
    echo "Not on any branch : prehash=${prehash}"
    return 1
  fi
  echo "${head_branch}"
}

get_remote_name() {
  head_branch="$1"
  # The remote name of HEAD branch (e.g. origin)
  remote_name=$(git config branch."${head_branch}".remote)
  if [ -z "${remote_name}" ]; then
    echo 'No remote repository'
    return 1
  fi
  echo "${remote_name}"
}

cd_git_toplevel() {
  cd "$1" || return 1
  if [ "$(git rev-parse --is-inside-work-tree 2>/dev/null)" != "true" ]; then
    return 1
  fi
  cd "$(git rev-parse --show-toplevel)" >/dev/null 2>&1 || exit
}

## Remote
print_remote_update_info() {
  (
    dir="$1"
    cd_git_toplevel "${dir}" || return 1
    # local status
    # local url
    # local branch
    # local merge_branch
    # local remote_branch
    branch=$(get_branch)
    ret="$?"
    if [ "${ret}" -eq 0 ]; then
      remote_name=$(get_remote_name "${branch}")
      if [ "${ret}" -eq 0 ]; then
        merge_branch=$(git config branch."${branch}".merge | sed -E s/^.\{11\}//g)
        if [ -z "${merge_branch}" ]; then
          status='No merge branch'
        else
          remote_show=$(git remote show "${remote_name}")
          status=$(echo "${remote_show}" \
            | grep "^ *${merge_branch} *pushes" | sed 's/.*(\(.*\)).*/\1/')
          url=$(echo "${remote_show}" | grep '^ *Push' | sed 's/^.*URL: \(.*\)$/\1/')
          remote_branch=$(echo "${remote_show}" | grep "^ *${merge_branch} *pushes" \
            | sed 's/.*pushes to \(.*\) (.*)/\1/' | tr -d ' ')
        fi
      else
        status=${remote_name}
      fi
    else
      status=${branch}
      branch=''
    fi

    if [ "${status}" != "up to date" ]; then
      status="\033[0;31m${status}\033[0;39m"
    fi

    branch_info=''
    if [ -n "${branch}" ]; then
      branch_info=${branch}
      if [ -n "${merge_branch}" ]; then
        branch_info="${branch_info}:${merge_branch} -> ${remote_branch}"
      fi
      branch_info="(${branch_info})"
    fi

    update_info="(${status}) [${dir%/}]${branch_info}${url:+ URL: ${url}}"
    printf '%b\n' "${update_info}"

    if [ "${status}" != "up to date" ]; then
      exit 1
    fi
  )
}

print_update_info() {
  dir="$1"
  if [ ! -e "${dir}" ]; then
    printf '%b\n' "\033[0;31mDirectory not found!! : ${dir}\033[0;39m"
    return 1
  elif [ ! -d "${dir}" ]; then
    return 0
  fi
  print_remote_update_info "${dir}"
}

## Main
cd_git_toplevel

# printf '%b\n' "===== \033[0;32mStatus (git status -sb)\033[0;39m ====="
# git status -sb

# sub_status=$(git submodule status)
# if [ -n "${sub_status}" ]; then
#   printf '%b\n' "===== \033[0;33mSubmodule Status (git submodule status)\033[0;39m ====="
#   echo "${sub_status}"
# fi

printf '%b\n' "===== \033[0;32mRemote Update Info\033[0;39m ====="
print_update_info '.'

# printf '%b\n' "===== \033[0;34mRemote List (git remote -v)\033[0;39m ====="
# git remote -v
#
# printf '%b\n' "===== \033[0;35mBranch List (git branch -av)\033[0;39m ====="
# git branch -av
#
# printf '%b\n' "===== \033[0;36mStash List (git stash list)\033[0;39m ====="
# git stash list
