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

46 lines
1.3 KiB
Fish
Executable file

#!/usr/bin/env fish
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
else if string match -q '*gitlab.com*' $remote_url
set provider gitlab
else
echo "Error: Unsupported git provider."
return 1
end
# Extract the repo path and branch
set repo_path (echo $remote_url | perl -nE 'say $2 if /\.com(:|\/)(.*)\.git/')
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"
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]