Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>One possibility is to use PHP's SSH library to perform those actions by connecting back to the web server?</p> <p>Or I found <a href="http://www.phpclasses.org/package/5310-PHP-Retrieve-project-files-from-GIT-repositories.html" rel="nofollow">this set of classes</a> which allow you to you to clone and read other metadata over HTTP, but not push nor pull. However it could be a starting point if you're brave enough to extend them to do that. I can imagine it would be a lot of work to replicate these processes and keep them compliant with various server versions etc.</p> <h2>[UPDATED 23/03/2014, after receiving an upvote - thanks!]</h2> <p>I did get some way trying to implement the above (I was searching for an implementation, drew a blank so tried to write my own), and it <em>was</em> hard as I thought! I actually abandoned it as I found a simpler way to achieve this in a different architecture, but here's the class I wrote trying. It essentially works, but was brittle to the environmental variations I was working in (i.e. it doesn't cope very well with errors or problems).</p> <p>It uses:</p> <ol> <li>herzult/php-ssh</li> <li>an ssh config file - a trick to simplify setting up authentication credentials in code</li> </ol> <p>(Note - I had to quickly strip out a few details from the code to post it. So you'll need to fiddle about a bit to integrate it into your app/namespace etc.) </p> <pre><code>&lt;?php /** * @author: scipilot * @since: 31/12/2013 */ /** * Class GitSSH allows you to perform Git functions over an SSH session. * i.e. you are using the command-line Git commands after SSHing to a host which has the Git client. * * You don't need to know about the SSH to use the class, other than the fact you will need access * to a server via SSH which has the Git client installed. Its likely this is the local web server. * * This was made because PHP has no good native Git client. * * Requires herzult/php-ssh * * Example php-ssh config file would be * * &lt;code&gt; * Host localhost * User git * IdentityFile id_rsa * * Host your.host.domain.com * User whoever * IdentityFile ~/.ssh/WhoeverGit *&lt;/code&gt; */ class GitSSH { protected $config; protected $session; protected $sPath; /** * @var string */ protected $sConfigPath = '~/.ssh/config'; /** * Connects to the specified host, ready for further commands. * * @param string $sHost Host (entry in the config file) to connect to. * @param string $sConfigPath Optional; config file path. Defaults to ~/.ssh/config, * which is probably inaccessible for web apps. */ function __construct($sHost, $sConfigPath=null){ \Log::info('New GitSSH '.$sHost.', '.$sConfigPath); if(isset($sConfigPath)) $this-&gt;sConfigPath = $sConfigPath; $this-&gt;config = new \Ssh\SshConfigFileConfiguration($this-&gt;sConfigPath, $sHost); $this-&gt;session = new \Ssh\Session($this-&gt;config, $this-&gt;config-&gt;getAuthentication()); } public function __destruct() { $this-&gt;disconnect(); } /** * Thanks to Steve Kamerman, as there isn't a native disconnect. */ public function disconnect() { $this-&gt;exec('echo "EXITING" &amp;&amp; exit;'); $this-&gt;session = null; } /** * Run a command (in the current working directory set by cd) * @param $sCommand * @return string */ protected function exec($sCommand) { //echo "\n".$sCommand."\n"; $exec = $this-&gt;session-&gt;getExec(); $result = $exec-&gt;run('cd '.$this-&gt;sPath.'; '.$sCommand); // todo: parse/scrape the result, return a Result object? return $result; } /** * CD to a folder. (This not an 'incremental' cd!) * Devnote: we don't really execute the cd now, it's appended to other commands. Each command seems to re-login? * * @param string $sPath Absolute filesystem path, or relative from user home */ public function cd($sPath){ $this-&gt;sPath = $sPath; // @todo this is useless! each command seems to run in a separate login? //$result = $this-&gt;exec('cd'); // /; ls'); //return $result; } /** * @return string */ public function ls(){ $result = $this-&gt;exec('ls '); return $result; } public function gitAdd($sOptions=null, array $aFiles=null){ $result = $this-&gt;exec('git add ' .(empty($sOptions) ? '' : ' '.$sOptions) .(empty($aFiles) ? '' : ' '.implode(' ', $aFiles)) ); return $result; } public function gitClone($sRepo, $sBranch=null, $sTarget=null){ \Log::info('GitSSH::clone '.$sRepo.', '.$sBranch.', '.$sTarget); $result = $this-&gt;exec('git clone ' .(empty($sBranch) ? '' : ' --branch '.$sBranch) .' '.$sRepo .' '.$sTarget); return $result; } public function gitCommit($sMessage, $sOptions=null, array $aFiles=null){ $result = $this-&gt;exec('git commit ' .'-m "'.addcslashes($sMessage, '"').'"' .(empty($sOptions) ? '' : ' '.$sOptions) .(empty($aFiles) ? '' : ' '.implode(' ', $aFiles)) ); return $result; } public function gitPull($sOptions=null, $sRepo=null, $sRefspec=null){ $result = $this-&gt;exec('git pull ' .(empty($sOptions) ? '' : ' '.$sOptions) .(empty($sRepo) ? '' : ' '.$sRepo) .(empty($sRefspec) ? '' : ' '.$sRefspec) ); return $result; } public function gitPush($sOptions=null, $sRepo=null, $sRefspec=null){ $result = $this-&gt;exec('git push ' .(empty($sOptions) ? '' : ' '.$sOptions) .(empty($sRepo) ? '' : ' '.$sRepo) .(empty($sRefspec) ? '' : ' '.$sRefspec) ); return $result; } /** * @return string the raw result from git status */ public function gitStatus(){ $result = $this-&gt;exec('git status'); return $result; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload