4

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 Answer 1

4

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)

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks, that helped. Used this: <br> co = !bash -c '(compgen -A function vxzExecuteGitCheckout >/dev/null && vxzExecuteGitCheckout "$@" ) || git checkout "$@"' - You might want to explain the importance of trailing dash, because without it the git co command seemed to do nothing at all.
I finally ditched this approach because I wanted the called function vxz... to change the environment variables of the shell I am working in. But since this function gets called in new shell of its own, the environment variable changes do not affect the calling shell.
@gurjeet: any chance we might know the problem you're trying to solve? maybe there's another way around it
does anyone know what the trailing dash is for, beacuase without it, it doesn't work?
@Steve: from man bash: »If there are arguments after the command_string, the first argument is assigned to $0 and any remaining arguments are assigned to the positional parameters. The assignment to $0 sets the name of the shell, which is used in warning and error messages.«
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.