Last active
April 19, 2019 16:20
-
-
Save bazzargh/dfcc599d6d28b6b237af57b522f35085 to your computer and use it in GitHub Desktop.
open code in github at a specific revision and optionally at a line
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash -x | |
IFS=$'\n' | |
url_encode() { | |
if command -v jq >/dev/null 2>&1; then | |
jq -srR 'sub("\n";"")|@uri' | |
else | |
sed -e 's/ /%20/g' | |
fi | |
} | |
if [ -n "$1" ]; then | |
cd $(dirname "$1") || exit 1 | |
fi | |
# the checked-out branch | |
branch=$(git symbolic-ref -q --short HEAD) | |
: "${branch:=master}" | |
# the remote for this branch, with fallbacks | |
remote=$(git config branch.$branch.remote) | |
: "${remote:=$(git config branch.master.remote)}" | |
: "${remote:=$(git remote -v|head -n 1|cut -f1)}" | |
: "${remote:=origin}" | |
remote_url=$(git config remote.$remote.url) | |
forge_url=$(echo "$remote_url"|sed -Ee 's,git@([^:]+):,https://\1/,;s,\.git$,,') | |
# this should always contain a SHA or a tag, but default to branch otherwise | |
commitish=$(git name-rev --tags --name-only --always --no-undefined HEAD) | |
if [ -n "$1" ] ; then | |
# quoting here looks dodgy | |
path="blob/${commitish:-${branch}}/$(git rev-parse --show-prefix)$(basename "$1"|url_encode)${2:+#L$2${3:+-L$3}}" | |
else | |
path="tree/${commitish:-${branch}}" | |
fi | |
url="$forge_url/$path" | |
echo $url | |
if command -v xdg-open >/dev/null 2>&1; then | |
# linux | |
xdg-open "$url" | |
else | |
# osx | |
open "$url" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment