Created
July 3, 2026 11:59
-
-
Save rometsch/219380e87c6d0d6baba492039e03b8e5 to your computer and use it in GitHub Desktop.
Shell script for .(bash|zsh)rc: file path -> github link
This file contains hidden or 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
| # print the github url for a file at the current branch | |
| ghlink() { | |
| local file="$1" | |
| if [ -z "$file" ]; then | |
| echo "usage: ghlink <path>" >&2 | |
| return 1 | |
| fi | |
| # remote url -> owner/repo, handling both ssh and https | |
| local slug | |
| slug=$(git config --get remote.origin.url | sed -E ' | |
| s#^git@[^:]+:##; | |
| s#^https?://[^/]+/##; | |
| s#\.git$## | |
| ') | |
| local branch rel | |
| branch=$(git rev-parse --abbrev-ref HEAD) | |
| rel=$(git ls-files --full-name "$file" 2>/dev/null \ | |
| || realpath --relative-to="$(git rev-parse --show-toplevel)" "$file") | |
| echo "https://github.com/$slug/blob/$branch/$rel" | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment