2

I'm trying to execute multiple git commands with PHP, exec and a batch file. When I execute the bat file from my cmd windows, it runs perfectly. But for some reason, when I call the bat file with exec (in PHP) it only executes the until call git add -A. Commit commando doesn't get executed. When I execute it manually(the commit command) and then run the script again, the files get pushed to the repo... The problem seems to be that the script doens't want to commit the files if it gets executed via exec in PHP.

This is my php:

exec('C:\wamp\www\git.bat "C:/wamp/www/project" "project"');

And this is my bat file:

@echo off
set drupal_root=%1
set repo_name=%~2

pushd %drupal_root%
call git init
call git remote add origin https://repo-url/%repo_name%.git
call git add -A
call git commit -m "Initial commit"
call git push origin master

Anyone has a clue what the reason could be?

Robin
  • 133
  • 7
  • set `@echo on` temporarily and check the output for errors? – Aleksei Matiushkin Dec 01 '14 at 10:57
  • the script doesn't generate errors. When I execute it from command line, it works... When I execeute it from php, only the first command gets executed. – Robin Dec 01 '14 at 10:59
  • After a closer look, it seems that the script gets executed untill `call git add -A`. So the comit and push commando's are ignored... – Robin Dec 01 '14 at 11:03
  • Why do you have forward slashes in your drupal_root argument `"C:/wamp/www/project"`? If I try to `pushd c:/users/username/Desktop` I get a "The syntax of the command is incorrect" error. It could be that your script is failing at the `pushd` line because you aren't using backslashes. Try changing your PHP exec line to `exec('C:\wamp\www\git.bat "C:\wamp\www\project" "project"');`. – rojo Dec 01 '14 at 13:03
  • No it's not failing, for the 100000th time. The root argument is wrapped with quotes, therefore it does not fail. The script is legit and it works – Robin Dec 01 '14 at 13:53
  • Whether the script works from the console is not being disputed. I simply wondered whether the command line arguments were being entered correctly with backslashes via `cmd` console, but incorrectly with forward slashes from your PHP `exec` call. Here's a test. Instead of using `%1` for drupal_root, try `pushd "c:\wamp\www\project"` as a static line within your batch script and see whether PHP still fails. If it does, then it's a problem with the argument. If it doesn't, then it's a problem with the PHP environment. This will hopefully at least help you narrow down the cause. – rojo Dec 01 '14 at 15:08

1 Answers1

0

Following "Windows shortcut to run git bash script", try instead to execute a bash script (using the Git for Windows bash shell) instead of a bat script:

exec('C:\Git\bin\sh.exe" --login -i C:\wamp\www\myscript.sh "C:/wamp/www/project" "project"');

(Replace C:\Git\bin\sh.exe with the path where you have installed Git for Windows.)

With myscript.sh:

#!/bin/bash

drupal_root=$1
repo_name=$2

cd $drupal_root
git init
git remote add origin https://repo-url/${repo_name}.git
git add -A
git commit -m "Initial commit"
git push -u origin master
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250