How to launch a Bash function using Git alias

拈花ヽ惹草 提交于 2019-12-10 20:27:43

问题


I want to use a Git alias in ~/.gitconfig so that it calls a bash function, if it is defined, otherwise call the regular git checkout.

This is what I have devised:

cat ~/.gitconfig
...
[alias]
...
    co = !(compgen -A function vxzExecuteGitCheckout >/dev/null && vxzExecuteGitCheckout ) || git checkout

The problem is that Git uses /bin/sh (which happens to be dash in my case) and it barfs on compgen since it is a bash builtin.

Any way making sure that Git uses bash to execute this command?

Note that vxzExecuteGitCheckout() is defined in a file which is not included in ~/.bashrc, yet.

Another question is, if I were to use git co -b param1 param2 ..., would the above alias definition pass on the -b param1 param2 to git checkout if this Bash function is not found?


回答1:


use bash explicitely:

co = !bash -c '( compgen -A function vxzExecuteGitCheckout >/dev/null && vxzExecuteGitCheckout ) || git checkout' -

another possibility would be to write a shell script with the correct shebang line #!/bin/bash and call that script for alias.co (see the question How to embed bash script directly inside a git alias)



来源:https://stackoverflow.com/questions/6483443/how-to-launch-a-bash-function-using-git-alias

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!