Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Branch: master
Fetching contributors…

Cannot retrieve contributors at this time

executable file 130 lines (110 sloc) 2.91 kB
#!/usr/bin/env bash
#
# git-info
#
# License: MIT
# Copyright (c) 2012 Lennart C. L. Kats
set -e
WEB=
if [ "$1" == "-w" ]; then
WEB=-w
shift
fi
REV=
if [ "$1" != "" ] && git rev-parse --verify "$1" &>/dev/null && [ ! -e "$1" ]; then
REV=`git rev-parse $1`
shift
elif [ "$1" == "-r" ]; then
REV=`git rev-parse $2`
shift; shift
fi
if [ "$1" == "-w" ]; then
WEB=-w
shift
fi
if [ "$1" == "" ]; then
FILE=
else
FILE="$1"
FILE=`cd -P -- "$(dirname -- "$FILE")" && echo "$(pwd -P)/$(basename -- "$FILE")"`
while [ ! -e .git ] && [ "`pwd`" != "/" ]; do
cd ..
done
BASEPATH=`ls -d $(pwd -P)`
RELPATH=`echo "$FILE" | sed "s|$BASEPATH/||"`
fi
if ! git remote &>/dev/null; then
cd `dirname "$FILE"`
FILE="`basename "$FILE"`"
fi
REMOTE=$(
git remote -v | grep 'origin.*push' \
| grep -oE '(bitbucket.org|github.com)[:/][^/]+/[^ ]+' \
| sed -E 's/(.*)\.git/\1/' \
| sed -E 's/(bitbucket.org|github.com):/\1\//' \
) || (echo 'No remote for current directory' >&2 && exit 1)
if [ ! "$REMOTE" ]; then
echo Repository not recognized:
git remote -v
exit 1
fi
BRANCH=`git-get-branch 2>/dev/null || git rev-parse HEAD`
BRANCH_IN_URL=`echo $BRANCH | sed 's!/!%2F!g'`
if [ "$FILE" ] && [ ! -d "$FILE" ]; then
git status "$FILE"
fi
if echo $REMOTE | grep -q bitbucket.org; then
BASE_URL="https://$REMOTE"
if [ $REV ] && [ ! "$FILE" ]; then
URL="$BASE_URL/commits/$REV"
DIFFURL= # not supported
elif [ $REV ]; then
URL="$BASE_URL/src/$REV/$RELPATH?at=$BRANCH"
if [ ! -d "$FILE" ]; then
DIFFURL="$BASE_URL/diff/$RELPATH?diff1=master&diff2=$REV&at=$BRANCH"
fi
else
URL="$BASE_URL/src/$BRANCH_IN_URL/$RELPATH?at=$BRANCH"
DIFFURL="$BASE_URL/diff?diff1=master&diff2=$BRANCH"
fi
else
USER=`echo $REMOTE | perl -pe 's|github.com.([^/]+)/.*|$1|'`
REPO=`echo $REMOTE | perl -pe 's|github.com.[^/]+/(.*)|$1|'`
BASE_URL="https://github.com/$USER/$REPO"
if [ $REV ] && [ ! "$FILE" ]; then
URL="$BASE_URL/commit/$REV"
DIFFURL="$BASE_URL/compare/master...$REV"
elif [ $REV ]; then
if [ -d "$FILE" ]; then
URL="$BASE_URL/tree/$REV/$RELPATH"
else
URL="$BASE_URL/blob/$REV/$RELPATH"
fi
DIFFURL= # not supported
else
URL="$BASE_URL/tree/$BRANCH_IN_URL/$RELPATH"
DIFFURL= # not supported
fi
fi
open() {
if which start &>/dev/null && ! [[ `uname -s` =~ Darwin|Linux ]]; then
start "$1"
elif which xdg-open &>/dev/null && ! [[ `uname -s` =~ Darwin ]]; then
xdg-open "$1"
elif which gnome-open &>/dev/null && ! [[ `uname -s` =~ Darwin ]]; then
gnome-open "$1"
elif which kde-open &>/dev/null && ! [[ `uname -s` =~ Darwin ]]; then
kde-open "$1"
else
command open "$1"
fi
}
echo "# URL:"
echo $URL
if [ $DIFFURL ] && [ "$BRANCH" != "master" ]; then
echo "# Diff URL:"
echo $DIFFURL
fi
if [ $WEB ]; then
open $URL
fi
Jump to Line
Something went wrong with that request. Please try again.