ZSH alias z parametrem

Próbuję stworzyć alias z parametrem dla mojego prostego git add/commit / push.

Widziałem, że funkcja może być używana jako alias, więc staram się, ale nie udało mi się ..

Zanim miałem:

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

Ale chcę móc modyfikować swoje commity:

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

(i know it ' s a terrible Git practice)

Author: ale-batt, 2015-12-17

4 answers

Nie można utworzyć aliasu z argumentami*, musi to być funkcja. Twoja funkcja jest blisko, wystarczy cytować pewne argumenty zamiast całych poleceń i dodawać spacje wewnątrz [].

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

*: większość powłok nie pozwala na argumenty w aliasach, wierzę, że csh i pochodne tak, ale i tak nie powinieneś ich używać.

 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/doraprojects.net/template/agent.layouts/content.php on line 54
2017-08-19 00:48:23

Jeśli naprawdę potrzebujesz użyć aliasu z parametrem z jakiegoś powodu, możesz go zhakować, osadzając funkcję w aliasie i natychmiast ją wykonując:

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

Widzę, że to podejście jest często używane w .aliasy gitconfig.

 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/doraprojects.net/template/agent.layouts/content.php on line 54
2016-09-08 16:10:46

Użyłem tej funkcji w .plik 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
}

Tutaj git push origin HEAD jest odpowiedzialny za naciśnięcie bieżącej gałęzi na zdalnym.

Z wiersza polecenia Uruchom to polecenie: gitall "commit message goes here"

Jeśli po prostu uruchomimy gitall bez komunikatu o zatwierdzeniu, to Komunikat o zatwierdzeniu będzie update, jak powiedziała funkcja.

 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/doraprojects.net/template/agent.layouts/content.php on line 54
2018-09-11 12:59:45

"git add ." a inne polecenia pomiędzy " są tylko ciągami dla Basha, Usuń " s.

Możesz użyć [ -n "$1" ] zamiast w swoim ciele 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/doraprojects.net/template/agent.layouts/content.php on line 54
2015-12-17 17:24:43