0

So I want to run a piece of AppleScript before and after my call to git (not the AppleScript below, different AppleScript, but this should get the point through). The issue I am having is that the git command has a variable number of arguments, so I can't just use $1 to give it the first one, it has to be the number that were passed in. Here is my code that is in my .bash_profile:

function git() {
    osascript -e 'display alert "before git command"';
    git "$@";
    osascript -e 'display alert "after git command"';
}

The expected result is that the first piece of applescript runs, git runs with whatever amount of arguments has been provided, then the last piece of applescript runs. What really happens is that the first piece of applescript runs over and over indefinitely. I think the issue might be that "$@" after the git. From what I understand using "$@" will dump all of the arguments in. But the first piece of applescript just runs in an infinite loop. What gives?

Community
  • 1
  • 1

1 Answers1

1

You have clobbered the git name. Your function is git and you call git in the function. Congratulations, you have recursion.

You need to tell the shell to call the real git binary in the middle there.

Either use command git or /full/path/to/git, etc.

There is also \git (but I think this just avoids aliases and not functions).

Also, as pointed out by @ryenus, in a comment. The env binary /usr/bin/env or /bin/env can also be used for this purpose (though it does more than just avoid the function and requires an extra external process to be spawned).

Etan Reisner
  • 68,917
  • 7
  • 78
  • 118
  • 1
    @ryenus Also an option but I wouldn't say "better". `env` is an external binary with a distinct purpose (that just so happens to overlap this purpose). But this is exactly what `command` (a shell built-in) is for. – Etan Reisner Jan 26 '15 at 02:38
  • @BrianTkatch Read the bash man page definition of `command` and the man page for `env`. They do different (but in this case overlapping) things. The bit about external vs. built-in is just a time thing. A built-in will perform better than an external tool as it doesn't require spawning a second/third/etc. process. This doesn't matter in simple cases but it loops/etc. and with large data it can be huge. – Etan Reisner Jan 26 '15 at 02:44
  • @EtanReisner, agreed, in this case command is slightly better, especially regarding the extra process thing involved with `env`. – ryenus Jan 26 '15 at 02:45