#!/bin/sh -e
#
# git-gud - the most awesomest git wrapper
#
# ---
# source: https://github.com/mitchweaver/bin

die() {
  >&2 printf "%s\n" "$*"
  exit 1
}
noargs='No arguments.'
confirm='Are you sure? (y\n) '
abort='Aborting.'

usage() {
  printf %s "\
  $(basename "$0")
    a simple git wrapper

  options: <optional> [mandatory]
    -a            - add <files>
    -b            - list branches [branch]
    -bc           - create branch [branch]
    -bd           - delete a branch [branch]
    -bort         - abort rebase in progress
    -c            - clone a repo [url]
    -cssh         - ssh clone a github repo [user/repo]
    -cont         - show repo contributors
    -d            - git diff <remote> <branch>
    -f            - fetch <remote> <branch>
    -gh           - clone a github repo [user/repo]
    -i            - initialize a repository
    -k            - checkout branch [branch]
    -l            - show git log
    -m            - commit <files> [message]
    -mv           - move a file
    -o            - push <remote> <branch>
    -p            - pull changes <remote> <branch>
    -r            - list remotes
    -ra           - add a remote
    -rb           - rebase [num]
    -reset        - hard reset a repo [num]
    -rm           - remove a file
    -u            - add+commit+push [message]
    -un           - list untracked files

  flags:
    -h, --help    - Display this message.
"
  exit 1
}

# returns whether a git repo is using "main" or "master"
main_or_master() {
  get_branch() { (git rev-parse --abbrev-ref HEAD 2>/dev/null) | tr -d '\n'; }
  branch="$(get_branch)"
  if ${branch} | grep '\* master' >/dev/null; then
    echo master
  elif ${branch} | grep '\* main' >/dev/null; then
    echo main
  fi
}

main() {
  case "$1" in
    -c | -gh | -cssh) ;;
    *) git status >/dev/null 2>&1 || usage ;;
  esac

  case "$1" in
    -h | --help)
      usage
      ;;
    -a)
      shift
      gadd "$@"
      ;;
    -b)
      shift
      git branch "$@"
      ;;
    -bc)
      shift
      git branch "$@"
      ;;
    -bd)
      shift
      branchdel "$@"
      ;;
    -bort)
      shift
      git rebase --abort "$@"
      ;;
    -c)
      shift
      git clone "$@"
      ;;
    -cssh)
      shift
      sshclone "$@"
      ;;
    -cont)
      git log --format='%aN' | sort \
        | uniq -c | sort -nr | sed 's|^[ ]*[^ ]*[ ]*||'
      ;;
    -d)
      shift
      gdiff "$@"
      ;;
    -f)
      shift
      git fetch "$@"
      ;;
    -gh)
      shift
      git clone https://github.com/"$*"
      ;;
    -i)
      shift
      git init "$@"
      ;;
    -k)
      shift
      git checkout "$@"
      ;;
    -l)
      shift
      git log --source "$@"
      ;;
    -m)
      shift
      gmit "$@"
      ;;
    -mv)
      shift
      git mv "$@"
      ;;
    -o)
      shift
      gout "$@"
      ;;
    -of)
      shift
      gout --force "$@"
      ;;
    -p)
      shift
      gpull "$@"
      ;;
    -pf)
      shift
      gpull --force "$@"
      ;;
    -r)
      shift
      git remote --verbose "$@"
      ;;
    -ra)
      shift
      git remote add "$@"
      ;;
    -rb)
      shift
      gbase "$@"
      ;;
    -rm)
      shift
      git rm "$@"
      ;;
    -reset)
      shift
      greset "$@"
      ;;
    -u)
      shift
      gup "$@"
      ;;
    -un)
      git ls-files --others --exclude-standard
      ;;
    *)
      git status -bs --show-stash
      ;;
  esac
}

# If no args, stage all files, (prompting for confirmation).
# Otherwise stage all arguments.
gadd() {
  if [ -n "$1" ]; then
    git add "$@"
  else
    printf "%s\n%s\n%s" "${noargs}" \
      'About to stage all files...' \
      "${confirm}"
    read -r ans
    [ "${ans}" = y ] || die "${abort}"
    git add --all
  fi
}

# If one argument, commit with message
# Otherwise, stage all arguments but last
# as files and then use last arg as commit message
# Ex: gud -m 'this is the commit message'
# Ex: gud -m file1 file2 file3 'added 3 files'
gmit() {
  case "$#" in
    0) die "${noargs}" ;;
    1) ;;
    *)
      while [ "$#" -ne 1 ]; do
        git add "$1"
        shift
      done
      ;;
  esac
  git commit --message="$1"
}

# If no arguments, push out to current branch.
# Otherwise, push to given branch.
# shellcheck disable=SC2312
gout() {
  [ -n "$1" ] \
    || set -- origin "$(git rev-parse --symbolic-full-name --abbrev-ref HEAD)"
  [ -n "$2" ] \
    || set -- origin "$1"
  git push --set-upstream "$@"
}

# If no arguments, git pull.
# Otherwise pull from given branch.
# shellcheck disable=SC2312
gpull() {
  [ -n "$1" ] || set -- origin "$(main_or_master)"
  [ -n "$2" ] || set -- origin "$1"
  git pull "$@"
}

# Interactively rebase current branch $1 number of commits.
gbase() {
  [ -n "$1" ] || die "${noargs}"
  count=$1
  shift
  git rebase --interactive HEAD~"${count}" "$@"
}

# Stage all, commit all, push all to current branch.
# Argument is the commit message.
gup() {
  [ -n "$1" ] || die "${noargs}"
  printf "%s\n%s" \
    'About to stage, commit, and push all changes.' \
    "${confirm}"
  read -r ans
  [ "${ans}" = y ] || die "${abort}"
  git add --all && git commit --message="$*" && gout
}

# Perform a hard reset of current branch $1 number of commits.
greset() {
  [ -n "$1" ] || die "${noargs}"
  printf "%s\n%s" "About to 'git reset --hard $1'..." "${confirm}"
  read -r ans
  [ "${ans}" = y ] || die "${abort}"
  count=$1
  shift
  git reset --hard HEAD~"${count}" "$@"
}

branchdel() {
  printf "%s\n%s" "About to delete branch $1..." "${confirm}"
  read -r ans
  [ "${ans}" = y ] || die "${abort}"
  git push --delete origin "$1"
}

# If no arguments, diff repo against remote main.
# Otherwise, diff repo against given remote branch.
# shellcheck disable=SC2312
gdiff() {
  [ -n "$1" ] || set -- origin "$(main_or_master)"
  [ -n "$2" ] || set -- origin "$1"
  git diff "$1"/"$2"
}

# because i can never remember the syntax for this
sshclone() {
  git clone "git@github.com:$1.git"
}

main "$@"
