Skip to content

Instantly share code, notes, and snippets.

@cljoly
Created June 11, 2023 13:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cljoly/cbe931d6e0bb31cf9dc113774bb87f3f to your computer and use it in GitHub Desktop.
Save cljoly/cbe931d6e0bb31cf9dc113774bb87f3f to your computer and use it in GitHub Desktop.
Quick and dirty alternative to https://github.com/x-motemen/ghq. It only has the features I care about and is much less robust, but it automatically changes the current directory
function ghq
# Simplified https://github.com/x-motemen/ghq, so that it’s always available
# Reference for git URLs: https://git-scm.com/docs/git-fetch#_git_urls
# * ssh://[user@]host.xz[:port]/path/to/repo.git/
# * git://host.xz[:port]/path/to/repo.git/
# * http[s]://host.xz[:port]/path/to/repo.git/
# * ftp[s]://host.xz[:port]/path/to/repo.git/
# * [user@]host.xz:path/to/repo.git/
set -f url $argv[1]
set -f path
set -f host
string match -rq '^([a-z]+://([^@]+@)?(?<host>[^:/]+)(:\d+)?/)(?<path>.+?)(\.git/?)?$' $url || string match -rq '^(([\w-]+@)?(?<host>[^/:@]+):)(?<path>.+?)(\.git/?)?$' $url
set -f hqpath "$HOME/ghq/$host/$path"
if test ! -d $hqpath
echo "Cloning to $hqpath"
git clone $url $hqpath
end
cd $hqpath
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment