Alias ZSH con parámetro


Estoy intentando crear un alias con un parámetro para mi simple git add/commit/push.

He visto que la función podría usarse como alias, así que lo intento, pero no lo hice ..

Antes tenía:

alias gitall="git add . ; git commit -m 'update' ; git push"

Pero quiero poder modificar mis commits:

function gitall() {
    "git add ."
    if [$1 != ""]
        "git commit -m $1"
    else
        "git commit -m 'update'"
    fi
    "git push"
}

(sé que es una terrible práctica de git)

Author: ale-batt, 2015-12-17

4 answers

No se puede hacer un alias con argumentos*, tiene que ser una función. Su función está cerca, solo necesita citar ciertos argumentos en lugar de los comandos completos, y agregar espacios dentro de [].

function gitall() {
    git add .
    if [ "$1" != "" ] # or better, if [ -n "$1" ]
    then
        git commit -m "$1"
    else
        git commit -m update
    fi
    git push
}

*: La mayoría de los shells no permiten argumentos en alias, creo que csh y derivados sí, pero no deberías usarlos de todos modos.

 43
Author: Kevin,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-08-19 00:48:23

Si realmente necesita usar un alias con un parámetro por alguna razón, puede hackearlo incrustando una función en su alias y ejecutándola inmediatamente:

alias example='f() { echo Your arg was $1. };f'

Veo que este enfoque se utiliza mucho en .gitconfig alias.

 33
Author: joelpt,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-09-08 16:10:46

Utilicé esta función en .archivo zshrc:

function gitall() {
    git add .
    if [ "$1" != "" ]
    then
        git commit -m "$1"
    else
        git commit -m update # default commit message is `update`
    fi # closing statement of if-else block
    git push origin HEAD
}

Aquí git push origin HEAD es responsable de empujar su rama actual en remoto.

Desde el símbolo del sistema ejecute este comando: gitall "commit message goes here"

Si simplemente ejecutamos gitall sin ningún mensaje de confirmación, entonces el mensaje de confirmación será update como dijo la función.

 6
Author: Hasan Abdullah,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-09-11 12:59:45

"git add ." y los otros comandos entre " son solo cadenas para bash, eliminar la "s.

Es posible que desee utilizar [ -n "$1" ] en su lugar en su cuerpo if.

 4
Author: Alberto Zaccagni,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-12-17 17:24:43