Skip to content

Instantly share code, notes, and snippets.

@Anoesj
Last active October 5, 2020 21:08
Show Gist options
  • Save Anoesj/300077068c0df1f0b70081f3e78f6e93 to your computer and use it in GitHub Desktop.
Save Anoesj/300077068c0df1f0b70081f3e78f6e93 to your computer and use it in GitHub Desktop.
Run `repo` in the CLI to go to the repository on GitHub/Bitbucket on the active branch
# Add in .bashrc/.zshrc etc.
# Print/open link to repository, preferably to currently active branch
function repo () {
if [ "$(git rev-parse --is-inside-work-tree)" = "true" ]; then
REPO_URL=$(git config --get remote.origin.url | sed "s~git@\(.*\):\(.*\).git~https:\/\/\1\/\2~")
BRANCH=$(git branch --show-current)
# BRANCH=$(git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/') # if the line above doesn't work
URL=""
if [[ $REPO_URL = *"github.com"* ]]; then
URL="$REPO_URL/tree/$BRANCH"
elif [[ $REPO_URL = *"bitbucket.org"* ]]; then
URL="$REPO_URL/branch/$BRANCH"
else
URL="$REPO_URL"
fi
echo "Opening $URL"
# open $URL # macOS
xdg-open $URL &> /dev/null # Ubuntu
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment