#!/bin/sh
#
# gpu - push to current upstream

git_push() {
  branch="$(git branch | grep '\*' | sed s:^..::g)"
  if [ "$1" = "--force-with-lease" ] \
    || [ "$1" = "--force" ] || [ "$1" = "-f" ]; then
    echo "Use gpuf for force pushing"
    return 1
  fi
  git push origin "${branch}"
}

git_push_force() {
  branch="$(git branch | grep '\*' | sed s:^..::g)"
  [ "${branch}" = "master" ] \
    && echo "I won't force push to master." \
    && return 1
  [ "${branch}" = "main" ] \
    && echo "I won't force push to main." \
    && return 1
  echo 'Pushing with --force-with-lease'
  git push origin "${branch}" --force-with-lease
}

case "$1" in
  f | force)
    git_push_force
    ;;
  -h | --help)
    printf "%s\n" "Usage: $(basename "$0") [-h|--help] [f|force]"
    exit 0
    ;;
  *)
    git_push "$@"
    ;;
esac
