Frage

I want to alias mv/rm to 'git mv'/'git rm' ONLY IF current path is a git work copy. Otherwise mv/rm is just normal mv/rm.

Could zsh archive that?

War es hilfreich?

Lösung

This cannot (or really should not) be done with aliases. Aliases are just replaced by their value before execution and thus do not provide any control structures.

If you need more control, functions are much better suited:

function rm mv () {
    if git rev-parse --is-inside-work-tree &> /dev/null
    then
        git $0 "$@"
    else
        command $0 "$@"
    fi
}

This creates two functions - rm and mv - with the same body. Depending on being inside a work-tree either git is called with the used command name ($0) or the command itself is called.

It is important to use command (which explicitly calls the external command from $PATH instead of a function or alias) or to call the executables with a their complete path (/bin/$0), else the function will be called recursively.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top