Skip to content

Instantly share code, notes, and snippets.

@shaleh
Created April 9, 2024 00:07
Show Gist options
  • Save shaleh/e93581c6edf3baec0508c74232dbbaf5 to your computer and use it in GitHub Desktop.
Save shaleh/e93581c6edf3baec0508c74232dbbaf5 to your computer and use it in GitHub Desktop.
github-browse-file
#!/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