#!/usr/bin/env bash
{ set +x; } 2>/dev/null

function git-url-fullname() {
	{ set +x; } 2>/dev/null

	local url="$1"
	git-url-validate "$url" 1>&2 || return
	# git@github.com:owner/repo.git
	[[ $url == "git@"*".git" ]] && {
		IFS=:;set $1;IFS=/;set $2;IFS=
		local owner="$1"
		local repo="${2::${#2}-4}"
	}
	# https://github.com/owner/repo.git
	[[ $url == "https://"*".git" ]] && {
		IFS=/;set $url;IFS=
		local owner="$4"
		local repo="${5::${#5}-4}"
	}
	# https://github.com/owner/repo
	[[ $url == "https://"* ]] && [[ $url != *.git ]] && {
		IFS=/;set $url;IFS=
		local owner="$4"
		local repo="$5"
	}
	echo "$owner/$repo"
}

[[ $# == 0 ]] && [ -s /dev/stdin ] && set -- "$(cat -)" # stdin
[[ $# != 1 ]] || [[ $1 == "--help" ]] && {
	echo "usage: ${BASH_SOURCE[0]##*/} url"
	exit 1
} 

git-url-fullname "$@"

