1

I want to push a folder with data to an empty repository in atlassian Git stash. The repository is created via the rest api. The remote origin is set. When I enter

$ git push origin master

in the shell it works all fine. When I use the PHP interactive shell and enter

exec('git push origin master');

it also works. But when the code is called in my PHP file nothing happens. Of course the code is the same. I am in the right working directory (I change it with chdir() before the shell_exec). No errors are reported (even when error_reporting is set to E_ALL) and when i set the output argument in the exec() I get an empty array.

Can anyone help?

Kara
  • 6,115
  • 16
  • 50
  • 57
Nafta
  • 71
  • 1
  • 7
  • Could you do `echo exec('git push origin master');` – Daniel Gelling Oct 30 '14 at 15:25
  • I added an answer regarding shell/user environment but in order to better understand your question could you please post how you're executing your php code non-interactively? – AndrewPK Oct 30 '14 at 15:27
  • This is all the important code for this step: `chdir($url->path); exec('git push origin master');` – Nafta Oct 30 '14 at 15:32
  • Are you still executing the code using your user? As in, interactively you can run it and it works but when you call `php myGitScript.php` then it fails? My concern is that it's running via fastcgi or mod_php and not having the proper ssh/git config to have permission to push. It is quite odd that you'd be getting zero output from that though, can you try using `shell_exec` for your git command so that you capture all output? (UPDATE - nevermind, just read you tried using the output param for `exec`) – AndrewPK Oct 30 '14 at 15:39
  • when i try `$result = shell_exec('git push origin master);` $result is null – Nafta Oct 30 '14 at 15:44

2 Answers2

1

When you execute code interactively, or commands via the shell, you're executing under your user account and your shell environment.

I would assume that the reason this isn't working is that you're executing the PHP script as another user (and possibly lacking your ssh/git environment config).

More information is needed to better answer your question, but you may want to start by verifying the user you're executing your PHP file as is also able to perform a git push origin master by doing something like:

sudo su -s /bin/bash -c "cd /my/git/repo/dir; git push origin master" MYPHPEXECUTEUSER

AndrewPK
  • 6,100
  • 3
  • 32
  • 36
  • I also have a lot of other shell_exec() in my whole script and they all work. I even add the remote origin of my git repository via an exec and it works fine... – Nafta Oct 30 '14 at 15:31
0

I found out that i have to add '2>&1' to the end of a shell command to get an error output when using shell_exec(). It returned "fatal: could not read Password for"url": device not configured". i just had to add the password to the url in a http basic auth fashion. Now it all works fine. Thanks for the answers :)

Nafta
  • 71
  • 1
  • 7