#!/usr/bin/env bash set -euo pipefail # Parse -L start,end while [[ "${1:-}" == -* ]]; do case "$1" in -L) IFS=',' read -r start end <<< "$2"; shift 2 ;; *) shift ;; esac done file="$1" # Run git from the file's directory so it works with any CWD root=$(git -C "$(dirname "$file")" rev-parse --show-toplevel) hash=$(git -C "$(dirname "$file")" rev-parse HEAD) remote=$(git -C "$(dirname "$file")" remote get-url origin) # Normalise SSH or HTTPS remote → owner/repo remote="${remote#git@github.com:}" remote="${remote#https://github.com/}" remote="${remote%.git}" # Make file path relative to repo root relpath="${file#$root/}" # Build fragment: single line vs range if [[ "$start" == "$end" ]]; then fragment="#L${start}" else fragment="#L${start}-L${end}" fi url="https://github.com/${remote}/blob/${hash}/${relpath}${fragment}" if command -v pbcopy &>/dev/null; then echo "$url" | pbcopy elif command -v xclip &>/dev/null; then echo "$url" | xclip -selection clipboard elif command -v xsel &>/dev/null; then echo "$url" | xsel --clipboard --input fi echo "$url"