#!/bin/sh
set -eu

usage () {
    cat <<'EOF'
usage: git remote-tracking-branch [-h] [<branch>]

Prints the fully qualified name of the remote tracking
branch for the given local branch name.

Options:
-h    Show this help
EOF
}

while getopts h flag; do
    case "$flag" in
        h) usage; exit 0;;
    esac
done
shift $(($OPTIND - 1))

if [ $# -gt 1 ]; then
  usage >&2
  exit 2
fi

branch="${1:-}"

# This result is constructed so that it only prints the output of
# git-rev-parse if the command succeeded, and is silent otherwise
result="$(git rev-parse --symbolic-full-name --abbrev-ref "${branch}@{u}" 2>/dev/null)"
echo "$result"
