config/fish/functions/clone.fish

30 lines
1.0 KiB
Fish

function clone --argument-names giturl
if test (count $argv) -gt 1
echo "Expected a Git URL but got "(count $argv)" arguments: '$argv'"
return 1
end
# These regular expressions are far from comprehensive, but they work for me.
#
# Examples:
# https://github.com/NixOS/nix
# https://git.sr.ht/~sircmpwn/git.sr.ht
# git@git.sr.ht:~crg/home
# ssh://git@gitlab.haskell.org:ghc/ghc.git
#
set --local giturl_format1 '^https:\/\/(?<githost>[^\/]+)\/(?<gituser>[^\/]+)\/(?<gitrepo>.+)$'
set --local giturl_format2 '^(ssh:\/\/)?[a-z]+?@(?<githost>[^:]+):([0-9]+\/)?(?<gituser>[^\/]+)\/(?<gitrepo>.+)$'
string match -rq $giturl_format1 $giturl; or string match -rq $giturl_format2 $giturl
set gituser (string replace '~' '' $gituser)
set gitrepo (string replace -r '.git$' '' $gitrepo)
set --local gitdir "$HOME/Code/$githost/$gituser/$gitrepo"
if confirm "Clone to $gitdir?"
git clone $giturl "$gitdir"
cd "$gitdir"
end
end