dotfiles/.config/helix/scripts/get_git_provider_url.fish

63 lines
1.9 KiB
Fish
Executable file

#!/usr/bin/env fish
# Original script from Reddit (forget URL)
# sr.ht support added by ficd
function get_git_provider_url --argument filename line_number
# Check if inside a git repository
if not git rev-parse --is-inside-work-tree >/dev/null 2>&1
echo "Error: Not inside a git repository."
return 1
end
# Get the remote URL
set remote_url (git config --get remote.origin.url)
if test -z "$remote_url"
echo "Error: No remote origin found."
return 1
end
# Determine the provider (GitHub, GitLab, etc.)
if string match -q '*github.com*' $remote_url
set provider github
set repo_path (echo $remote_url | perl -nE 'say $2 if /\.com(:|\/)(.*)\.git/')
else if string match -q '*gitlab.com*' $remote_url
set provider gitlab
set repo_path (echo $remote_url | perl -nE 'say $2 if /\.com(:|\/)(.*)\.git/')
else if string match -q '*git.sr.ht*' $remote_url
set provider srht
set repo_path (string match -r -g '.*:(~.+)' $remote_url)
else
echo "Error: Unsupported git provider."
return 1
end
# Extract the repo path and branch
set branch (git rev-parse --abbrev-ref HEAD)
# Generate the URL based on the provider
switch $provider
case github
echo "https://github.com/$repo_path/blob/$branch/$filename#L$line_number"
case gitlab
echo "https://gitlab.com/$repo_path/-/blob/$branch/$filename#L$line_number"
case srht
echo "https://git.sr.ht/$repo_path/tree/$branch/item/$filename#L$line_number"
end
end
# Ensure correct number of arguments
if [ (count $argv) -ne 2 ]
echo "Usage: script.fish <filename> <line_number>"
return 1
end
# get_git_provider_url $argv[1] $argv[2]
# set -l output (get_git_provider_url $argv[1] $argv[2])
get_git_provider_url $argv[1] $argv[2]
# echo $output
# echo $output | wl-copy
# echo $output
# wl-copy $output