Skip to content

Instantly share code, notes, and snippets.

@fritz-c
Last active November 9, 2022 19:46
Show Gist options
  • Save fritz-c/d5b755aeacbd6cf68b7a6463f8dbe1c5 to your computer and use it in GitHub Desktop.
Save fritz-c/d5b755aeacbd6cf68b7a6463f8dbe1c5 to your computer and use it in GitHub Desktop.
Open up diff on Github
#!/usr/bin/env bash
# Opens the comparison of two commits on Github (only for repositories on Github)
# Download this script as "git-hubdiff" (no extension), chmod it to be executable and put it in your
# path somewhere (e.g. /usr/bin). You can then use it via `git hubdiff` from inside any git repo.
usage()
{
echo "USAGE"
echo "compare commit to head : git hubdiff <commit>"
echo "compare two commits : git hubdiff <commit> <commit>"
}
while getopts "h:" opt; do
case $opt in
h)
usage
exit 1
;;
\?)
usage
exit 1
;;
esac
done
# Parse the following patterns for repo urls to get the github repo url
# https://github.com/owner/repo-name.git
# git@github.com:owner/repo-name.git
BASE_URL="https://github.com/""$(git config --get remote.origin.url | sed 's/.*github\.com[/:]\(.*\).git/\1/')""/compare"
if [[ "$#" -eq 1 ]]; then
if [[ "$1" =~ .*\.\..* ]]; then
# Handle "git hubdiff fromcommit..tocommit"
open "${BASE_URL}/$(git rev-parse "${1/\.\.*/}")...$(git rev-parse ${1/*\.\./})"
else
# Handle "git hubdiff fromcommit"
open "${BASE_URL}/$(git rev-parse "$1")...$(git rev-parse HEAD)"
fi
elif [[ "$#" -eq 2 ]]; then
# Handle "git hubdiff fromcommit tocommit"
open "${BASE_URL}/$(git rev-parse "$1")...$(git rev-parse "$2")"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment