Skip to content

Instantly share code, notes, and snippets.

@larzconwell
Created October 9, 2012 02:31
Show Gist options
  • Save larzconwell/3856234 to your computer and use it in GitHub Desktop.
Save larzconwell/3856234 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# Check if the Git directory exists up to 5 parent directories, exit if not
searchGitDir() {
local relPath=""
local pathLoc=""
for i in 1 2 3 4 5; do
pathLoc="$(pwd)/.git"
if [[ -d "$pathLoc" ]]; then
return
else
if [[ "$i" == 5 ]]; then
echo "fatal: Not a git repository (or any of the parent directories): .git"
exit 1
fi
relPath="$relPath../"
cd "$(pwd)/$relPath"
fi
done
}
run() {
searchGitDir
local commitSHA="$(git rev-parse $1)"
# Wow, this is fucking horrible. Works though.
local commitURL="$(git remote show origin -n |
grep -i "Fetch URL" |
sed -e 's/^.*git@/https:\/\//' -e 's/^.*git:/https:/' -e 's/com:/com\//' \
-e 's/\.git//')/commit/$commitSHA"
# Use pbcopy if on Darwin
if [[ "$MACHTYPE" == *darwin* ]]; then
if [[ -f "$(which pbcopy)" ]]; then
echo "$commitURL" | pbcopy
else
echo "Please install pbcopy to copy the URL to the clipboard."
exit 1
fi
# Use xsel if on Linux
elif [[ "$MACHTYPE" == *linux* ]]; then
if [[ -f "$(which xsel)" ]]; then
echo "$commitURL" | xsel --clipboard --input
else
echo "Please install xsel to copy the URL to the clipboard."
exit 1
fi
fi
}
run "$@"
# Git does some automagical stuff with PATH, that makes it so if any commands are prepended
# with "git-" it adds it to the main git command. So once installed you can simply call
# share like `git share`
PREFIX=/usr/local
install:
@cp -r git-share ${PREFIX}/bin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment