Last active
December 3, 2021 16:17
-
-
Save wknapik/bd6e3c4c75c85ad4c3a8e6fea640792e to your computer and use it in GitHub Desktop.
Print link(s) to file(s) in repo(s) with optional line numbers (add to your shell profile; works with GitHub and GitLab)
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
# Requires: bash (I use it in zsh), coreutils, git. | |
# Print link(s) to file(s) in repo(s), with optional line numbers. | |
# $@ := file_path[:file_line] [ file_path2[:file_line2] [ ... ] ] | |
repolink() { | |
local branch f file host origin url_path | |
for f in "$@"; do | |
file="$(realpath "$f")" | |
checkout_path="$(git -C "$(dirname "$file")" rev-parse --show-toplevel)" | |
origin="$(git -C "$checkout_path" config --get remote.origin.url)" | |
branch="$(git -C "$checkout_path" rev-parse --abbrev-ref HEAD)" | |
host="${origin#*://}" # strip off scheme | |
host="${host#*@}" # strip off user | |
host="${host/:[0-9]*\//\/}" # strip off port | |
host="${host//://}" # replace : with / | |
host="${host%.git}" # strip off .git suffix | |
host="${host%/}" # strip off trailing / | |
url_path="${file#"$checkout_path"/}" | |
echo "https://$host/blob/$branch/${url_path/:/#L}" | |
done | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Works for me with GitHub and GitLab. Might need adjustments on line 20 for others.