Shayde Nofziger
Posted on April 18, 2017
I find myself frequently needing to go to the homepage of the projects I contribute to on GitHub. I have bookmarked the important ones, and a quick Google search of the project name and "github.com" should take me to the right place, but that can be time-consuming and requires a non-negligible amount of context switching. I decided to automate this process so as to not interrupt my workflow.
To do so, I wrote up a quick bash function to perform the automation, placed it in my .bashrc
file, and assigned it to the alias github
:
alias github=GitHub
function GitHub()
{
if [ ! -d .git ] ;
then echo "ERROR: This isnt a git directory" && return false;
fi
git_url=`git config --get remote.origin.url`
if [[ $git_url != https://github* ]] ;
then echo "ERROR: Remote origin is invalid" && return false;
fi
url=${git_url%.git}
open $url
}
Here's what that code is doing, in layman's terms:
- First, we check to see if we are at the root of a git repository by checking for the existence of a
.git/
directory. - Next, we get the
remote.origin.url
property from thegit config
. This is the URL of the remote git repo with which the project is associated. For GitHub projects, this takes the form:https://github.com/[USERNAME]/[PROJECT_NAME].git
- If the
remote.origin.url
is not from GitHub, we can't guarantee our ability to open it, and throw an error. - Finally, we remove
.git
from the URL and use theopen
command in macOS to open a browser at that given URL.
Now, any time I'm in the base directory of a project cloned from GitHub, I can open its project page straight from Terminal by running github
.
This is my first time really exploring the power of incorporating bash functions and aliases into my workflow. I'm now actively looking out for tasks I perform regularly that could be automated, and will try to create bash functions for them as needed.
This code snippet and another I've created to get the size of the working directory are on GitHub, and I plan to publish all the future ones I create as well! Feel free to contribute and share your own!
Some questions to inspire discussion:
What actions do developers perform regularly that could benefit from automation?
Have you used bash functions to automate your workflow? If so, feel free to share examples so others may learn! If not, could you see yourself using them in the future?
Posted on April 18, 2017
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.