Skip to content

Instantly share code, notes, and snippets.

@fidergo-stephane-gourichon
Last active August 14, 2019 15:45
Show Gist options
  • Save fidergo-stephane-gourichon/6a6fac6610a5a530ea145725ca1fea3b to your computer and use it in GitHub Desktop.
Save fidergo-stephane-gourichon/6a6fac6610a5a530ea145725ca1fea3b to your computer and use it in GitHub Desktop.
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