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?

Gurjeet Singh
  • 2,635
  • 2
  • 27
  • 22

1 Answers1

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)

Community
  • 1
  • 1
knittl
  • 246,190
  • 53
  • 318
  • 364
  • 1
    Thanks, that helped. Used this:
    `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.
    – Gurjeet Singh Jun 26 '11 at 15:38
  • 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 Singh Jun 26 '11 at 15:45
  • @gurjeet: any chance we might know the problem you're trying to solve? maybe there's another way around it – knittl Jun 26 '11 at 15:58
  • I work with PostgreSQL sources quite a lot and have a few bash functions that let me do common things. I want these functions to do the right thing everytime I switch to different a Git branch of Postgres sources. To do the right thing, these functions depend on some env. variables, and I want to update these env vars with every checkout. I finally ended up adding a function call to bash special variable $PROMPT_COMMAND. PROMPT_COMMAND=vxzDetectBranchChange. And now everytime I change to a new branch, this function updates all relevant environment variables, including $PATH. – Gurjeet Singh Jun 27 '11 at 16:39
  • does anyone know what the trailing dash is for, beacuase without it, it doesn't work? – Steve Feb 25 '21 at 20:33
  • @Steve: from [`man bash`](https://linux.die.net/man/1/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.« – knittl Feb 25 '21 at 20:58