Created
April 13, 2018 20:18
-
-
Save pganssle/c3460412047da3825d926f5fffcd5c79 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# Parse CLI arguments | |
REGEXES=() | |
POSITIONAL=() | |
while [[ $# -gt 0 ]]; do | |
key=$1 | |
case $key in | |
-r|--regex) | |
REGEXES+=("$2") | |
shift; shift; ;; | |
-b|--branch) | |
BRANCHES+=("$2") | |
shift; shift; ;; | |
*) | |
# First positional argument is the remote name | |
if [ -z $remote ]; then | |
remote=$1 | |
else | |
BRANCHES+=("$1") | |
fi | |
shift; ;; | |
esac | |
done; | |
# Add default values to the CLI | |
BRANCHES+=('upstream') | |
BRANCHES+=('origin') | |
REGEXES+=('([email protected]:)[^\/]+') | |
REGEXES+=('(https:\/\/github.com\/)[^\/]+') | |
# Find the base url of the first valid branch | |
for rname in ${BRANCHES[@]}; do | |
if url=$(git remote get-url $rname 2>/dev/null); then | |
base_url=$url | |
break | |
fi | |
done | |
if [ -z $base_url ]; then | |
echo "Failed to find a valid branch" | |
exit -1 | |
fi | |
for regex in ${REGEXES[@]}; do | |
output=$(echo $base_url | sed -rn s/$regex/\\1$remote/p) | |
if [ ! -z $output ]; then | |
echo $output | |
exit 0 | |
fi | |
done | |
echo "No matching regular expressions for $base_url" | |
exit -2 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
BTW, the "hub" tool from GitHub is really good at these sort of shortcuts too.