Thursday, March 11, 2021

Executing Git Commnads from PHP

 This a continuation of my previous post about the new project I have in mind. I am currently using Git as my version control software and commands are usually entered at the cmd environment or the git-bash software.

I already know that PHP has the command shell_exec to run batch files or external executable files so it should be easy to accomplish this task. I am going to show how I did it:

  • shell_exec can not work directly to execute the git commands,  but it has already proven that it can run .exe and .bat file. 
  • I opted to create a .bat file but the problem with this file is that I could not capture its output. It has no control over the process being created which means it does not have to wait for a line to finish before going to the next, fortunately, vb6 has this feature that's why vb6 is an indispensable tool  for me.
  • So I created a .exe file from vb6. What this program does is to run the .bat file and capture its output and save it to a text file
  • And after creating this, the php scritpt will run the .exe file and then display the content of the text file.    
It should be noted that this is for offline applications. I encountered a lot of  difficulties but finally I was able to make it work. 

More details of the programs I created.

1. VB6 .Exe File
The vb6 program consists of a module with a few api declarations and a function. The api declarations are necessary to activate the detection of the ending of the process created when the program executes the .bat file. The vb6 startup is the subroutine main().

Here's api declaration:

The subroutine main() calls the function Shellsync with 2 parameters, the batch file name and vbHide(a vb6 system variable that instructs the shell command to hide the window that the process being created when it executes the batch file. And the function Shellsync the one responsible for executing the batch file and catch the output to a text file. The actual command to save the output to a text file is a common dos command:


 2. The batch file.

I created a .bat file to handle 2 git commands and its very very simple:


 

 3. And lastly the php script is also very simple:

 


 4. Before I forget, all three files are located on the same folder:


 

 

 


No comments:

Post a Comment

Executing Git Commnads from PHP

 This a continuation of my previous post about the new project I have in mind. I am currently using Git as my version control software and c...