git alias for php not completely working in windows - Stack Overflow

I am not able to set a PHP alias in the git bash installed in windows (I'm using Herd).Context:If

I am not able to set a PHP alias in the git bash installed in windows (I'm using Herd).

Context:

  • If I open Git Bash from windows menu, I can see the alias working (php -v returns correct info)
  • In my PHP project, I set a pre-commit hook to run some tests using php command

I added this aliases in both .bashrc and .bash_profile files:

export PATH=$PATH:"/c/Users/MyUser/.config/herd/bin/"

# Laravel Herd Aliases - please do not remove these lines
alias php="php.bat"
alias herd="herd.bat"
alias laravel="laravel.bat"
alias composer="composer.bat"

Now, when I execute git commit something, I get this error:

which: no php in (/mingw64/libexec/git-core:/mingw64/libexec/git-core:/mingw64/bin:/usr/bin: ...etc other paths)
/c/ProgramData/ComposerSetup/bin/composer: line 14: php: command not found

What I do not understand is that the alias seems to work If I use in git bash, but it's not working in normal git flow.

I am not able to set a PHP alias in the git bash installed in windows (I'm using Herd).

Context:

  • If I open Git Bash from windows menu, I can see the alias working (php -v returns correct info)
  • In my PHP project, I set a pre-commit hook to run some tests using php command

I added this aliases in both .bashrc and .bash_profile files:

export PATH=$PATH:"/c/Users/MyUser/.config/herd/bin/"

# Laravel Herd Aliases - please do not remove these lines
alias php="php.bat"
alias herd="herd.bat"
alias laravel="laravel.bat"
alias composer="composer.bat"

Now, when I execute git commit something, I get this error:

which: no php in (/mingw64/libexec/git-core:/mingw64/libexec/git-core:/mingw64/bin:/usr/bin: ...etc other paths)
/c/ProgramData/ComposerSetup/bin/composer: line 14: php: command not found

What I do not understand is that the alias seems to work If I use in git bash, but it's not working in normal git flow.

Share Improve this question edited Mar 24 at 7:32 Giacomo M asked Mar 24 at 7:23 Giacomo MGiacomo M 4,7258 gold badges31 silver badges68 bronze badges 8
  • How did you define the alias? – grawity_u1686 Commented Mar 24 at 7:28
  • Make sure that in the bash process which is executed by git, not only the alias definition is present, but also a shopt expand_aliases is done. Otherwise bash would ignore the alias. I don't know which file you have to place this settings into. Perhaps you have to create a separate file for this and do an export BASH_ENV=/abs/path/to/this/file before calling git. See the section called INVOCATION in the bash man page. – user1934428 Commented Mar 24 at 14:24
  • @user1934428 here says that windows does not have same function: stackoverflow/questions/13826189/… – Giacomo M Commented Mar 24 at 17:20
  • @GiacomoM : This is weird. There is no logical reason why exactly this should not work when porting a bash to a different platform. Perhaps the answer given by Armali is simply wrong? If you do a shopt, does it show the string expand_aliases in the output? If it does, your bash should be able to get it set in a script as well. But in your case, I think you didn't put them into the right files. If git itself executes bash code, it will likely do it using a bash child process, and I don't see why such a process should source .bash_profile or .bashrc. – user1934428 Commented Mar 25 at 10:48
  • @user1934428 you right, the answer is wrong. If I execute shopt command in git bash, I can see that expand_aliases is on. But why it does not work? – Giacomo M Commented Mar 26 at 11:07
 |  Show 3 more comments

1 Answer 1

Reset to default 3

There's a whole list of reasons:

  1. Aliases are internal to the shell they were defined in; they don't automatically propagate "down" into programs (even if those programs are additional instances of Bash).

  2. Bash only loads ~/.bashrc when running in "interactive" mode. If run in non-interactive mode (like for a .sh script or a system() command), it doesn't load any startup scripts at all, and therefore won't load your alias definitions.

    So although your interactive shell has the aliases, any additional shell process run by git (like a hook script) won't have them.

  3. Bash only expands aliases when running in "interactive" mode. If run in non-interactive mode (like for a .sh script or a "system()" command), it ignores all alias definitions it may have – though per #1, in this mode it won't have any alias definitions in the first place.

  4. Not all command invocations even go through Bash in the first place. Yes, things like "system()" go through the shell, but the parent program can just as well execute another program directly (using the equivalent of PHP's "pcntl_fork()" + "pcntl_exec()".)

  5. Related to #3: As your message shows, the error occurs not when trying to execute the php command, but when trying to find it. That is, the failing script tries to do an early check using which to make sure that such a command exists in $PATH – and aliases do not exist in $PATH, so the script's pre-check cannot succeed. (That is, assuming if the script had any alias definitions, which it doesn't per #1.)

    • This is in part because the check is done using the external which instead of the shell built-in command -v. Aliases are completely internal to the shell they were defined in, so even if the shell in question had any alias definitions (which per #1 it doesn't), those wouldn't be visible to the which program as it is external to the shell.

Recommendation: Replace the alias with a "real" shell script, e.g. ~/bin/php. Then you will only need to ensure that ~/bin is in PATH, and it's much easier to have PATH propagate down into programs (it's automatic) than to make the same happen with aliases.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744256247a4565412.html

相关推荐

  • git alias for php not completely working in windows - Stack Overflow

    I am not able to set a PHP alias in the git bash installed in windows (I'm using Herd).Context:If

    8天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信