git url 作为命令

git url as a command

当我在终端输入 git 回购 url 时,它说:

% https://github.com/chmln/sd.git        
zsh: no such file or directory: https://github.com/chmln/sd.git

我希望https://github.com/chmln/sd.git作为一个命令,它将

git clone https://github.com/chmln/sd.git
cd sd

我该怎么做?

OP 提供了有关用例的更多详细信息。值得专门解决这个用例。当一个命令(在这种情况下是 git 克隆)应该假设时,考虑使用 BASH_COMMAND(或 $_)

的 ERR 陷阱
trap 'try_gc' ERR
function try_gc {
    local x=$BASH_COMMAND
    if [[ "$x" = https://* ]] ; then git clone "$x"; fi;
}

OP 在这里,对我有用的 zsh 特定解决方案是

function _accept-line-with-url {

    if  [[ $BUFFER =~ ^https.*git ]]
    then

        echo $BUFFER >> $HISTFILE
        fc -R

        BUFFERz="git clone $BUFFER && cd $(basename $BUFFER .git)"
        zle .kill-whole-line
        BUFFER=$BUFFERz
        zle .accept-line
        # zle reset-prompt  
    else 
        zle .accept-line
    fi 
}

zle -N accept-line _accept-line-with-url

有一个更简单的解决方案。但是,它不会 cd:

alias -s git="git clone"