The take
command it's one of my favorite features about ZSH. Because with a single command you can create a new directory and automatically change into it.
So, why not do the same with git clone
?
That's why I search for a way to create cake
command, inspired on take
but for clone, that automatically cd
to the directory created after git clone
.
Add this script function on ~/.zshrc
file.
cake() {
[[ $# -eq 0 ]] && return
local last="${@: -1}"
local rest="${@:1:${#}-1}"
local cmd=""
local dir="$(basename "$1" .git)"
if [[ -z $rest ]]; then
cmd="$last"
else
cmd="$rest $last"
if [[ !"$last" =~ ^"-" ]]; then
dir="$last"
fi
fi
echo "\e[32m❯ \e[33mCloning...\e[0m"
git clone $cmd
echo "\e[32m❯ \e[33mChanging dir...\e[0m"
cd $dir
}
Once finish, reopen all terminals or update his source running source ~/.zshrc
command and now you can use it.
Works for repository links both with and without ".git" and also if you manually target the directory name at the end.
But I know you have angry for more. I have created cape
command that does the same as cake
but also installs the npm packages and opens the project on VScode.
cape() {
cake "$@"
echo "\e[32m❯ \e[33mInstalling...\e[0m"
npm i
echo "\e[32m❯ \e[33mOpening...\e[0m"
code .
}
Sources:
That’s All Folks!
Happy Coding 🖖
Discussion (7)
Largely incompatible with the common parameters
--recursive
and--branch
.I suggest using %* and figuring out how to scry %* to find the link.
You could also drop the quotes around the %1 and enforce the caller to wrap their arguments in quotes like,
cake "--branch master https://.../" FCC
Definitely need an improved version taking those aspects.
While that happens, it can be used in the most simple cloning way and run the other commands like
git branch
after that.@tilkinsc code was updated and can be used with the other parameters.
Only one condition, URI needs to be the first parameter right next to the
cake
orcape
command.I reviewed the code. Try out using a regex match out a basic url and isolate the repo name and supply the entire command after a shift to the git clone function. This is good for testing your skills.
Thanks for sharing.
Note that this works because it's a shell function. I tried
cd
before in a shell alias and it didn't work.I have some clone aliases here for git config. In particular I use the clone-s one to clone my own repos as SSH rules.
github.com/MichaelCurrin/dotfiles/...
Yes, I've tried the same with aliases but didn't work. That's why I've decided to use a function instead.
By the way, cool
undo
andreset
aliases. 😎Thanks.
You can use
code
instead ofcd
I think in an alias. And you can also define and use an alias with in a single git alias.