Skip to content

Instantly share code, notes, and snippets.

@ashishb
Created November 16, 2018 19:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ashishb/867443359a6367baf81e33725ca2ba11 to your computer and use it in GitHub Desktop.
Save ashishb/867443359a6367baf81e33725ca2ba11 to your computer and use it in GitHub Desktop.
Opens github.com url of a file corresponding to a local file
# Usage: ghurl <filepath>
# Opens the file in github corresponding to a local path. Respects repo name, branch name (defaults to master).
function ghurl() {
domain='http://github.com' &&
repo=$(git config --get remote.origin.url | cut -d: -f2 | cut -d. -f1) &&
# Fallback to master branch
remoteBranch=$(git rev-parse --abbrev-ref --symbolic-full-name @{u} 2>/dev/null || echo 'master') &&
# Remove origin prefix
remoteBranch=${remoteBranch#origin/} &&
currentDir=$PWD &&
gitTopLevel=$(git rev-parse --show-toplevel) &&
pathRelativeToBaseRepo=${currentDir#${gitTopLevel}}/${1} &&
# echo Remote branch is ${remoteBranch} &&
# echo current Dir is ${currentDir} &&
# echo gitTopLevel is ${gitTopLevel} &&
# echo pathRelativeToBaseRepo is ${pathRelativeToBaseRepo}
url=${domain}/${repo}/tree/${remoteBranch}/${pathRelativeToBaseRepo} &&
# echo url is ${url} &&
open ${url}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment