Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
This bash script will transform a relative or absolute path to file or dir into a github URL you can browse and share.
#!/bin/bash
# Transform a relative or absolute path to file or dir into a github URL you can browse and share.
#
# usage: git_path_to_github_url.sh -
# usage: git_path_to_github_url.sh somedir/somefile
for ENTRY
do
(
FULLPATH=$([[ "$ENTRY" = /* ]] && echo "$ENTRY" || echo "$PWD/${ENTRY#./}")
cd "${FULLPATH%/*}/"
GITROOT="$( git rev-parse --show-toplevel )"
BRANCH="$( git rev-parse --abbrev-ref HEAD )"
if [[ "$ENTRY" == "-" ]]
then
RELPATH=""
KIND=tree
else
RELPATH="${FULLPATH/$GITROOT\//}"
if [[ -f "$FULLPATH" ]] ; then KIND=blob ; fi
if [[ -d "$FULLPATH" ]] ; then KIND=tree ; fi
fi
if [[ -z "$KIND" ]]
then
echo >&2 "Cannot figure out URL (does it even exist?): $FULLPATH"
continue
fi
while read -u 3 REMOTENAME REMOTEPATH
do
echo $REMOTEPATH/$KIND/$BRANCH/$RELPATH
done 3< <( git remote -v show | cut -f 1 -d " " | sort | uniq )
)
done | sed 's|^[^ ]*github.com:/*|https://github.com/|'
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.