this repo has no description
at main 1.2 kB view raw
1#!/bin/bash 2 3filename=$1 4linerange=$2 # This is now optional 5 6git_config="$(git rev-parse --show-toplevel)/.git/config" 7 8# Git the repo URL 9repo_url=$(grep "url =" $git_config | sed 's/ url = //g') 10 11# Convert SSH URL to HTTPS if needed 12if [[ $repo_url == git@* ]]; then 13 repo_domain=$(echo $repo_url | awk -F':' '{print $1}' | sed 's/git@//') 14 repo_path=$(echo $repo_url | awk -F':' '{print $2}') 15 repo_url="https://$repo_domain/$repo_path" 16fi 17 18# Get the current commit 19current_commit=$(git rev-parse HEAD) 20 21# Handle github vs gitlab 22if [[ $repo_url == *"github.com"* ]]; then 23 platform="github" 24elif [[ $repo_url == *"gitlab.com"* ]]; then 25 platform="gitlab" 26else 27 echo "Unknown platform" 28 exit 1 29fi 30 31# Generate the final URL 32if [[ $platform == "github" ]]; then 33 final_url="${repo_url%.git}/blob/$current_commit/${filename}" 34 # Format line range for GitHub 35 if [ ! -z "$linerange" ]; then 36 linerange="L${linerange%-*}-L${linerange#*-}" 37 fi 38elif [[ $platform == "gitlab" ]]; then 39 final_url="${repo_url%.git}/-/blob/$current_commit/${filename}" 40fi 41 42# Add line range if it exists 43if [ ! -z "$linerange" ]; then 44 final_url="${final_url}#L${linerange}" 45fi 46 47echo -n $final_url