Created
April 9, 2024 00:07
-
-
Save shaleh/e93581c6edf3baec0508c74232dbbaf5 to your computer and use it in GitHub Desktop.
github-browse-file
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/sh | |
# Executed from helix using :pipe-to github-browse-file %{dirname} %{filename} %{linenumber} | |
OPEN_URL_COMMAND=open | |
DIR_NAME=$1 | |
FILE_NAME=$2 | |
LINE_NUMBER=$3 | |
TOP_LEVEL=$(cd $DIR_NAME && git rev-parse --show-toplevel) | |
GITHUB_URL=$(cd $DIR_NAME && git config remote.origin.url) | |
REF=$(cd $DIR_NAME && git symbolic-ref -q HEAD) | |
UPSTREAM_NAME=$(cd $DIR_NAME && git for-each-ref --format='%(upstream:short)' $REF) | |
# Need to strip the remote name like "origin" from the upstream name. | |
UPSTREAM_NAME=${UPSTREAM_NAME#*/} | |
RELATIVE_FILE_NAME=$(echo "$FILE_NAME" | sed -e "s|${TOP_LEVEL}/||") | |
DESTINATION_URL="$GITHUB_URL/tree/$UPSTREAM_NAME/$RELATIVE_FILE_NAME#L$LINE_NUMBER" | |
$OPEN_URL_COMMAND "$DESTINATION_URL" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment