Skip to content

Instantly share code, notes, and snippets.

@ku
Created February 17, 2012 22:23
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ku/1855779 to your computer and use it in GitHub Desktop.
Save ku/1855779 to your computer and use it in GitHub Desktop.
open corresponding URL of the repository of current directory
#!/bin/sh
GIT='git'
if test `uname` = "Darwin" ; then
SED_OPT='-E'
OPEN='open'
else
SED_OPT='-r'
OPEN=''
fi
FETCH_URL=`$GIT remote show origin -n | sed -n '/Fetch URL/p' | awk '{print $3}' | sed $SED_OPT 's/[:@]/ /g' `
PROTOCOL=`echo $FETCH_URL | cut -f 1 -d" "`
echo $PROTOCOL | 'grep' http > /dev/null
if [ $? -eq 0 ];then
# http
echo $FETCH_URL
HOST=`echo $FETCH_URL | sed $SED_OPT 's/\/+/ /g' | awk '{print $2}'`
REPOS=`echo $FETCH_URL | sed $SED_OPT 's/\/\// /' | sed $SED_OPT 's/\// /' | awk '{print $3}' | sed 's/.git$//'`
else
# git
HOST=`echo $FETCH_URL | awk '{print $2}' | sed $SED_OPT 's/^\/+//'`
REPOS=`echo $FETCH_URL | awk '{print $3}' | sed 's/.git$//'`
fi
ORIGIN="http://$HOST/"
BRANCH=`$GIT branch | fgrep '*' | sed 's/^* //'`
echo $HOST | 'egrep' -q '^gist'
HOST_STARTS_W_GIST=$? # gist.github.com
echo $REPOS | 'egrep' -q '^gist\b'
REPOS_STARTS_W_GIST=$? # enterprise
if test $HOST_STARTS_W_GIST = 0 -o $REPOS_STARTS_W_GIST = 0; then
GIST=YES
else
GIST=NO
fi
COMMIT=$1
if test "x$COMMIT" = "x" ; then
if test $GIST = "YES" ; then
URL="$ORIGIN$REPOS"
else
if test "x$BRANCH" = "x(no branch)"; then
URL="$ORIGIN$REPOS"
else
URL="$ORIGIN$REPOS/tree/$BRANCH/$COMMIT"
fi
fi
else
if test $GIST = "YES" ; then
URL="$ORIGIN$REPOS/$COMMIT"
else
URL="$ORIGIN$REPOS/commit/$COMMIT"
fi
fi
echo $URL
if test "x" != "x$OPEN" ; then
$OPEN "$URL"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment