如何使用Git别名启动Bash函数 [英] How to launch a Bash function using Git alias

查看:102
本文介绍了如何使用Git别名启动Bash函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在〜/ .gitconfig中使用Git别名,以便它调用bash函数(如果已定义),否则调用常规的 git checkout



这是我设计的:

  cat〜/ .gitconfig 
...
[别名]
...
co =!(compgen -A函数vxzExecuteGitCheckout> / dev / null&& vxzExecuteGitCheckout)|| git checkout

问题是Git使用/ bin / sh(恰好是破折号),因为它是 bash ,所以它在 compgen 上倒刺



以任何方式确保Git使用 bash 执行此命令?



请注意, vxzExecuteGitCheckout()是在尚不包含在〜/ .bashrc中的文件中定义的。



另一个问题是,如果我使用 git co -b param1 param2 ... ,上述别名定义是否会传递给 -b param1 param2 git checkout 如果找不到此Bash函数?

解决方案

明确使用 bash

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

另一种可能性是编写带有正确shebang行<$ c的shell脚本$ c>#!/ bin / bash 并为 alias.co 调用该脚本(请参阅问题如何将bash脚本直接嵌入git别名内


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?

解决方案

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)

这篇关于如何使用Git别名启动Bash函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆