1

So I am attempting to create list of modified files between two git commands:

shell_exec("
  git diff --name-only $remoteCommitId $localCommitId > diff &&
  SET /p DIFF=<diff &&
  git archive --output=$zipName HEAD %DIFF%
");

However, no zip file is created I also tried separating commands with & and ;. If a run these commands in batch file, everything works.

I am on windows and the reason why I am doing it that way is here.

I just want to create zip file between two git commits via shell_exec so any other method which works via shell_exec is also welcome.

Update

If I run these commands directly on console:

git diff --name-only 207b606c270fc14a7161647c2fbe8f9a7d8e05a1 17dcc2233678df4ce86c8713e0349d68e45c3c96 > diff&& SET /p DIFF=<diff&& git archive --output=deployment.zip HEAD %DIFF%

It does create deployment.zip file but problem is that deployment.zip file only contains single root path file in it not ones from other folder (vendor in this case):

Here is diff file:

composer.json
vendor/anlutro/l4-settings/.gitignore
vendor/anlutro/l4-settings/.travis.yml
vendor/anlutro/l4-settings/CONTRIBUTING.md
vendor/anlutro/l4-settings/LICENSE
vendor/anlutro/l4-settings/README.md
vendor/anlutro/l4-settings/composer.json
vendor/anlutro/l4-settings/phpunit.xml
vendor/anlutro/l4-settings/src/ArrayUtil.php
vendor/anlutro/l4-settings/src/DatabaseSettingStore.php
vendor/anlutro/l4-settings/src/Facade.php
vendor/anlutro/l4-settings/src/JsonSettingStore.php
vendor/anlutro/l4-settings/src/MemorySettingStore.php

But deployment.zip file only contains composer.json file.

dev02
  • 1,776
  • 3
  • 24
  • 45
  • CA you try `...> diff&& set... diff&& git ...`: ie, no space after the last character of one command, and its separator `&&`. I just want to be sure you are no create `diff ` instead of `diff` – VonC Aug 13 '17 at 13:26
  • Are you sure all your commands are succeeding? You tagged this with `bash` but some of those are windows cmd commands/syntax, not bash – Eric Renouf Aug 13 '17 at 13:35
  • @VonC: Please see updated question, i tried that way. – dev02 Aug 13 '17 at 13:36
  • @EricRenouf: Yes when i run individually/directly on console, I don't see any errors but even with this method it only generates deployment file with single composer.json file in it, not all files. Please see updated question. – dev02 Aug 13 '17 at 13:38
  • Can you try `SET /p "DIFF= – VonC Aug 13 '17 at 13:38
  • @VonC: Tried, it prompts back on cmd with ` – dev02 Aug 13 '17 at 13:40
  • And `"SET /p DIFF= – VonC Aug 13 '17 at 13:41
  • @VonC: `The system cannot find the path specified.` – dev02 Aug 13 '17 at 13:43
  • OK. Back to your original question: would a `shell_exec("echo a&& echo b")` work? – VonC Aug 13 '17 at 13:44
  • @VonC: not sure what you mean by a and b here. The problem is that generated deployment.zip file only contains single file not all files even though diff file lists all files correctly. – dev02 Aug 13 '17 at 13:45
  • Just checking if two commands can be chained inside a shell_exec: just printing anything – VonC Aug 13 '17 at 13:47
  • @VonC: yes `echo shell_exec("echo a&& echo b");` – dev02 Aug 13 '17 at 13:48
  • Then try your first diff, followed by `&& type diff`, to see if you can read it's content. – VonC Aug 13 '17 at 13:51
  • @VonC: all files get listed on console correctly – dev02 Aug 13 '17 at 13:53
  • https://stackoverflow.com/a/3069068/6309 is interesting: set will only put the first line in the variable – VonC Aug 13 '17 at 14:12
  • @VonC: bash/batch stuff is too advanced for me but thanks for pointing out, i will see if I can use that in my existing script. Thanks – dev02 Aug 13 '17 at 14:18
  • @VonC: Also found this but not sure how to run via php exec as it creates zip file of all files instead: https://stackoverflow.com/questions/21639415/create-archive-of-modified-files-in-git-via-batch-file – dev02 Aug 13 '17 at 14:43
  • Is it possible to put all that in a .bat script, run it first to check that it is working, and then call that same script from php? – VonC Aug 13 '17 at 16:14
  • @VonC: Yes everything works via batch/bash files but not from php because in php we have to give each statement separately. – dev02 Aug 13 '17 at 17:17
  • Why not give only one statement, with that statement being the .bat script which in turn executes all your original commands? – VonC Aug 13 '17 at 17:19
  • @VonC: That's how I am doing for now but it opens up console window every time. Until solution is found, I am going this route. Thanks – dev02 Aug 13 '17 at 17:35
  • Oh, you didn't mention that. https://stackoverflow.com/a/33685422/6309 might help – VonC Aug 13 '17 at 17:42
  • @VonC: Thanks that should do the trick then :) – dev02 Aug 13 '17 at 17:49

1 Answers1

1

Try instead:

 shell_exec("cmd /C \"ascript.bat\"");

with ascript.bat a script which list all the necessary commands.
See "how not to open a Cmd window when running a batch file"

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250