#!/usr/bin/bash

# DESCRIPTION:
#
# Get remote for a branch. Prefer remote configured for pushing. Fallback to origin eventually.
#
# env: GIT_ROOT, GIT_BRANCH

cd "$GIT_ROOT"

remote="$(/usr/bin/git config --get branch."$GIT_BRANCH".pushRemote)"

if [ -z "$remote" ]; then
    remote="$(/usr/bin/git config --get remote.pushDefault)"
fi

if [ -z "$remote" ]; then
    remote="$(/usr/bin/git config --get branch."$GIT_BRANCH".remote)"
fi

if [ -z "$remote" ]; then
    remote="origin"
fi

if [ -n "$remote" ]; then
    echo "$remote"
fi
