Skip to content

Instantly share code, notes, and snippets.

@bryankimani
Last active April 29, 2023 19:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bryankimani/3ab8ace69b0d1c552ec1c8047d6ea599 to your computer and use it in GitHub Desktop.
Save bryankimani/3ab8ace69b0d1c552ec1c8047d6ea599 to your computer and use it in GitHub Desktop.
This shell function streamlines the process of visiting repository URLs for faster navigation.
# This shell function streamlines the process of visiting repository URLs for faster navigation.
# With a simple command, "openrepo", you can easily visit the repository URL associated with the current folder you're in.
# This is particularly useful when you need to quickly access the repository of the project you're working on, without
# having to navigate to the URL manually. By executing the "openrepo" command from the command line, the script will
# automatically open the associated repository URL in your default browser. This saves time and eliminates the need for
# manual navigation, making it an ideal solution for developers and anyone else who regularly interacts with repositories.
openrepo() {
regex='^(https?|ftp|file)://[-A-Za-z0-9\+&@#/%?=~_|!:,.;]*[-A-Za-z0-9\+&@#/%=~_|]\.[-A-Za-z0-9\+&@#/%?=~_|!:,.;]*[-A-Za-z0-9\+&@#/%=~_|]$'
remote_url=$(git remote get-url origin)
if [[ "${remote_url}" == *"github"* ]]; then
open_url=$(echo "${remote_url}" | sed 's,git@github.com:,https://github.com/,')
elif [[ "${remote_url}" == *"bitbucket"* ]]; then
open_url=$(echo "${remote_url}" | sed 's,git@bitbucket.org:,https://bitbucket.org/,')
else
open_url='The folder you are currently in is not a git repository'
fi
# Optional: set default web browser
# Use xdg-open to open the url if you are on a linux OS
# On MacOS, use: open $url
# On Windows, use: start $url
if [[ $open_url =~ $regex ]]; then
xdg-settings set default-web-browser chromium-browser.desktop
xdg-open $open_url
else
echo $open_url
fi
}
# End of open git repo url from the command line
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment