Skip to content

Instantly share code, notes, and snippets.

@xkr47
Last active September 11, 2018 11:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xkr47/91a8ecea42b54355f6e83e90aeb9a681 to your computer and use it in GitHub Desktop.
Save xkr47/91a8ecea42b54355f6e83e90aeb9a681 to your computer and use it in GitHub Desktop.
Handy script for generating a github "diff" url between two refs

Example:

$ github-diff origin/master 04ed4b
https://github.com/example/repo/compare/a418bcece3d540ad1d4864030635c2d4ae2628b3...04ed4b34bee0a2b2003f3744b772473a4c8b604b

Requires the commits to be available locally, so you might have to git fetch first.

#!/bin/bash
if [ $# -lt 2 -o $# -gt 3 ]; then
echo "usage: $0 [<remote>] <fromref> <toref>"
echo ".. where <remote> can be a remote name or url"
exit 1
fi
if [ $# = 3 ]; then
remote="$1"
shift
else
branch="`git symbolic-ref --short HEAD`"
if [ ! "${branch}" ]; then
echo "Could not figure out current branch"
exit 2
fi
for key in branch.$branch.pushremote remote.pushdefault branch.$branch.remote ; do
remote="`git config $key ||:`"
[ "$remote" ] && break
done
if [ ! "$remote" ]; then
remote=$(git remote | head -n 1)
if [ ! "$remote" ]; then
echo "No remotes found"
exit 2
fi
echo "NOTE: Could not find upstream remote for local branch '$branch', using first remote '$remote' by default"
fi
fi
case "$remote" in
*:*)
url="$remote"
;;
*)
url="$(git config remote.$remote.url)"
if [ ! "$url" ]; then
echo "ERROR: No such remote '$remote'"
exit 2
fi
;;
esac
case "$url" in
http*)
:
;;
*.*:*)
url="${url#git@}"
url="${url%/}"
url="${url%.git}"
url="https://${url/://}"
;;
*)
echo "UNSUPPORTED '$url'"
exit 3
esac
echo "${url%/}/compare/$(git rev-parse $1)...$(git rev-parse $2)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment