<?php
namespace Git;
use Git\Commit\Commit;
use Git\Model\Tree;
use Git\Model\Blob;
use Git\Model\Diff;
class Repository
{
protected $path;
protected $client;
public function __construct($path, Client $client)
{
$this->setPath($path);
$this->setClient($client);
}
public function setClient(Client $client)
{
$this->client = $client;
}
public function getClient()
{
return $this->client;
}
public function create()
{
mkdir($this->getPath());
$this->getClient()->run($this, 'init');
return $this;
}
public function getConfig($key)
{
$key = $this->getClient()->run($this, 'config ' . $key);
return trim($key);
}
public function setConfig($key, $value)
{
$this->getClient()->run($this, "config $key \"$value\"");
return $this;
}
/**
* Add untracked files
*
* @access public
* @param mixed $files Files to be added to the repository
*/
public function add($files = '.')
{
if(is_array($files)) {
$files = implode(' ', $files);
}
$this->getClient()->run($this, "add $files");
return $this;
}
/**
* Add all untracked files
*
* @access public
*/
public function addAll()
{
$this->getClient()->run($this, "add -A");
return $this;
}
/**
* Commit changes to the repository
*
* @access public
* @param string $message Description of the changes made
*/
public function commit($message)
{
$this->getClient()->run($this, "commit -m '$message'");
return $this;
}
/**
* Checkout a branch
*
* @access public
* @param string $branch Branch to be checked out
*/
public function checkout($branch)
{
$this->getClient()->run($this, "checkout $branch");
return $this;
}
/**
* Pull repository changes
*
* @access public
*/
public function pull()
{
$this->getClient()->run($this, "pull");
return $this;
}
/**
* Update remote references
*
* @access public
* @param string $repository Repository to be pushed
* @param string $refspec Refspec for the push
*/
public function push($repository = null, $refspec = null)
{
$command = "push";
if($repository) {
$command .= " $repository";
}
if($refspec) {
$command .= " $refspec";
}
$this->getClient()->run($this, $command);
return $this;
}
/**
* Show a list of the repository branches
*
* @access public
* @return array List of branches
*/
public function getBranches()
{
$branches = $this->getClient()->run($this, "branch");
$branches = explode("\n", $branches);
$branches = array_filter(preg_replace('/[\*\s]/', '', $branches));
return $branches;
}
/**
* Show the current repository branch
*
* @access public
* @return string Current repository branch
*/
public function getCurrentBranch()
{
$branches = $this->getClient()->run($this, "branch");
$branches = explode("\n", $branches);
foreach($branches as $branch) {
if($branch[0] == '*') {
return substr($branch, 2);
}
}
}
/**
* Check if a specified branch exists
*
* @access public
* @param string $branch Branch to be checked
* @return boolean True if the branch exists
*/
public function hasBranch($branch)
{
$branches = $this->getBranches();
$status = in_array($branch, $branches);
return $status;
}
/**
* Create a new repository branch
*
* @access public
* @param string $branch Branch name
*/
public function createBranch($branch)
{
$this->getClient()->run($this, "branch $branch");
}
/**
* Show a list of the repository tags
*
* @access public
* @return array List of tags
*/
public function getTags()
{
$tags = $this->getClient()->run($this, "tag");
$tags = explode("\n", $tags);
if (empty($tags[0])) {
return NULL;
}
return $tags;
}