Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Executing git commands via PHP

Tags:

git

php

exec

How can git commands like git add . and git commit -m be executed using php?

It is for a project that creates a centralized repository facility for maintaining academic projects.

exec() didn't seem to work.

<?php
$path = "/var/www/repos/$_POST[project]"; 
$a='';
chdir($path);
exec("git add .");  
exec("git commit -m'message'");
echo "<h3 align = center> Succesfully commited all the files.</h3>";
?>
like image 251
rjv Avatar asked Dec 19 '11 14:12

rjv


People also ask

Can I use Git with PHP?

PHP allows you to create web apps that run pretty much anywhere, including on cheap hosting. Instead, git allows you to keep track of the changes in your source code. You can use Git in your PHP projects to leverage the best of both.

How do I run a git command?

All you have to do is load Command Prompt (Load the Start menu, then click "Run", type cmd and hit enter), then you can use Git commands as normal.

What is mkdir command in git?

The mkdir (make directory) command in the Unix, DOS, DR FlexOS, IBM OS/2, Microsoft Windows, and ReactOS operating systems is used to make a new directory. It is also available in the EFI shell and in the PHP scripting language. In DOS, OS/2, Windows and ReactOS, the command is often abbreviated to md .


2 Answers

It is definetly possible. I implemented it in one of my projects. However you should be careful about permissions.

On linux, usually, the exec command will execute using the www-data user. So you should allow www-data to write and read on your work directory.

One quick and dirty way to do it is : chmod o+rw -R git_directory

like image 119
Oli Avatar answered Sep 23 '22 13:09

Oli


There is another solution, which is to simply use php backticks like so

$message = `git log -1 --pretty=%B`; // get commit message

or

$deploy_tag = `git describe --tags`; // get commit tags

or

$hash = `git log -1 --pretty=%h`; // get the hash
like image 38
Tim Hallman Avatar answered Sep 20 '22 13:09

Tim Hallman