How to use a bash function in a git alias?

杀马特。学长 韩版系。学妹 提交于 2019-11-28 09:35:52

问题


I want to use a bash function in a git alias. So I added this to my .bashrc:

fn() {
    echo "Hello, world!"
}
export -f fn

and to my .gitconfig:

[alias]
    fn = !fn

But then git fn produces an error:

fatal: cannot run fn: No such file or directory
fatal: While expanding alias 'fn': 'fn': No such file or directory

Is this a correct way to use a bash function in a git alias definition?


回答1:


Thats because git uses /bin/sh (so your .bashrc is not sourced).

You can call bash in a git alias as specified in this answer.

The thing is the bash shell started by the git command is not loading your .profile (which is the one responsible for including the .bashrc).

There may be other ways to do it but you can work around by doing:

[alias]
    fn = !bash -c 'source $HOME/.my_functions && fn'

With the file .my_functions like this:

#!/bin/bash
fn() {
    echo "Hello, world!"
}

You can even source .my_functions into your .bashrc if you want the functions to be available from regular shells.




回答2:


I don't know why, but if I just put a space before the function name in .gitconfig, it works without a problem and outputs the message:

[alias]
    fn = ! fn

It might be a bug in git, or I'm missing something in the documentation.




回答3:


If you want to be 100% certain that exported functions will be honored, ensure that the shell being called is bash, not /bin/sh (which will not honor them if it's implemented by ash or dash).

fn() { echo "hello, world"; }
export -f fn
git config --global alias.fn $'!bash -c \'fn "$@"\' _'
git fn

...properly emits:

hello, world

The relevant entry in .gitconfig:

[alias]
    fn = !bash -c 'fn \"$@\"'


来源:https://stackoverflow.com/questions/46435606/how-to-use-a-bash-function-in-a-git-alias

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