Skip to content

Instantly share code, notes, and snippets.

@xkr47

xkr47/README.md

Last active Sep 11, 2018
Embed
What would you like to do?
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
You can’t perform that action at this time.