Skip to content

Commit

Permalink
named exceptions, better api for creating tags, branches and commits
Browse files Browse the repository at this point in the history
  • Loading branch information
matteosister committed May 31, 2013
1 parent 1779e22 commit 6ddec60
Show file tree
Hide file tree
Showing 16 changed files with 254 additions and 24 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ $commit->count();
// as well as
count($commit);

// remotes (thanks to @davidneimeyer)
$repo->getRemote('origin'); // a Remote object
$repo->getRemotes(); // array of Remote objects

// Log contains a collection of commit objects
// syntax: getLog(<tree-ish>, path = null, limit = 15, offset = null)
$log = $repo->getLog();
Expand Down Expand Up @@ -167,6 +171,9 @@ $repo->stage(); // stage all
$repo->commit('my first commit');
$repo->commit('my first commit', true); // commit and stage every pending changes in the working tree

// remotes
$repo->addRemote('awesome', 'git://github.com/matteosister/GitElephant.git');

// checkout
$repo->checkout($this->getCommit('v1.0')); // checkout a tag
$repo->checkout('master'); // checkout master
Expand Down
4 changes: 3 additions & 1 deletion ROADMAP.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ todo
* git blame
* blobs management
* submodules management
* remotes
* remotes DONE
* better status handling with --porcelain
* tag messages and signed tags
* named exceptions

17 changes: 17 additions & 0 deletions src/GitElephant/Command/CatFileCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,21 @@ public function content(Object $object, $treeish)

return $this->getCommand();
}

/**
* output an object content given it's sha
*
* @param string $sha
*
* @return string
*/
public function contentBySha($sha)
{
$this->clearAll();
$this->addCommandName(static::GIT_CAT_FILE);
$this->addCommandArgument('-p');
$this->addCommandSubject($sha);

return $this->getCommand();
}
}
8 changes: 4 additions & 4 deletions src/GitElephant/Command/MainCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,21 +92,21 @@ public function add($what = '.')
/**
* Commit
*
* @param string $message the commit message
* @param bool $commitAll commit all changes
* @param string $message the commit message
* @param bool $stageAll commit all changes
*
* @throws \InvalidArgumentException
* @return string
*/
public function commit($message, $commitAll = false)
public function commit($message, $stageAll = false)
{
$this->clearAll();
if (trim($message) == '' || $message == null) {
throw new \InvalidArgumentException(sprintf('You can\'t commit whitout message'));
}
$this->addCommandName(self::GIT_COMMIT);

if ($commitAll) {
if ($stageAll) {
$this->addCommandArgument('-a');
}

Expand Down
10 changes: 5 additions & 5 deletions src/GitElephant/Command/RemoteCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ public static function getInstance(Repository $repository = null)
* in practice you will likely pass a SubCommandCommand object. This
* class provide "convenience" methods that do this for you.
*
* @param \GitElephant\SubCommandCommand $subcommand A subcommand object
* @param array $options Options for the main git-remote command
* @param \GitElephant\Command\SubCommandCommand $subcommand A subcommand object
* @param array $options Options for the main git-remote command
*
* @return string Command string to pass to caller
*/
Expand Down Expand Up @@ -121,9 +121,9 @@ public function show($name = null)
/**
* git-remote add [options] <name> <url>
*
* @param unknown $name remote name
* @param unknown $url URL of remote
* @param unknown $options options for the add subcommand
* @param string $name remote name
* @param string $url URL of remote
* @param array $options options for the add subcommand
*
* @return string
*/
Expand Down
11 changes: 8 additions & 3 deletions src/GitElephant/Command/TagCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,15 @@ public function create($name, $startPoint = null, $message = null)
$this->clearAll();
$this->addCommandName(self::TAG_COMMAND);
if ($message != null) {
$this->addCommandArgument(sprintf('-m %s', $message));
$this->addCommandArgument('-m');
$this->addCommandArgument($message);
}
if (null !== $startPoint) {
$this->addCommandArgument($name);
$this->addCommandSubject($startPoint);
} else {
$this->addCommandSubject($name);
}
$subject = $startPoint == null ? $name : $name . ' ' . $startPoint;
$this->addCommandSubject($subject);

return $this->getCommand();
}
Expand Down
20 changes: 20 additions & 0 deletions src/GitElephant/Exception/InvalidBranchNameException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
/**
* User: matteo
* Date: 01/06/13
* Time: 0.00
* Just for fun...
*/


namespace GitElephant\Exception;

/**
* Class InvalidBranchNameException
*
* @package GitElephant\Exception
*/
class InvalidBranchNameException extends \Exception
{
protected $message = 'The name provided is not a valid branch name';
}
19 changes: 18 additions & 1 deletion src/GitElephant/Objects/Branch.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

use GitElephant\Command\BranchCommand;
use GitElephant\Command\MergeCommand;
use GitElephant\Exception\InvalidBranchNameException;
use GitElephant\Objects\TreeishInterface;
use GitElephant\Repository;

Expand Down Expand Up @@ -69,6 +70,22 @@ class Branch extends Object implements TreeishInterface
*/
private $fullRef;

/**
* Creates a new branch on the repository and returns it
*
* @param \GitElephant\Repository $repository repository instance
* @param string $name branch name
* @param string $startPoint branch to start from
*
* @return \GitElephant\Objects\Branch
*/
public static function create(Repository $repository, $name, $startPoint = null)
{
$repository->getCaller()->execute(BranchCommand::getInstance()->create($name, $startPoint));

return $repository->getBranch($name);
}

/**
* static generator to generate a single commit from output of command.show service
*
Expand Down Expand Up @@ -128,7 +145,7 @@ private function createFromCommand()
return;
}
}
throw new \InvalidArgumentException(sprintf('The %s branch doesn\'t exists', $this->name));
throw new InvalidBranchNameException(sprintf('The %s branch doesn\'t exists', $this->name));
}

/**
Expand Down
15 changes: 15 additions & 0 deletions src/GitElephant/Objects/Commit.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

namespace GitElephant\Objects;

use GitElephant\Command\MainCommand;
use GitElephant\Objects\Author,
GitElephant\Objects\TreeishInterface,
GitElephant\Objects\Commit\Message,
Expand Down Expand Up @@ -96,6 +97,20 @@ class Commit implements TreeishInterface, \Countable
*/
private $datetimeCommitter;

/**
* @param Repository $repository repository instance
* @param string $message commit message
* @param bool $stageAll automatically stage the dirty working tree. Alternatively call stage() on the repo
*
* @return Commit
*/
public static function create(Repository $repository, $message, $stageAll = false)
{
$repository->getCaller()->execute(MainCommand::getInstance()->commit($message, $stageAll));

return $repository->getCommit();
}

/**
* static generator to generate a single commit from output of command.show service
*
Expand Down
13 changes: 13 additions & 0 deletions src/GitElephant/Objects/Remote.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,19 @@ public function __construct(Repository $repository, $name = null)
return $this;
}

/**
* Static constructor
*
* @param \GitElephant\Repository $repository repository instance
* @param string $name remote name
*
* @return \GitElephant\Objects\Remote
*/
public static function create(Repository $repository, $name = null)
{
return new self($repository, $name);
}

/**
* get output lines from git-remote --verbose
*
Expand Down
19 changes: 19 additions & 0 deletions src/GitElephant/Objects/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@

namespace GitElephant\Objects;

use GitElephant\Command\CatFileCommand;
use GitElephant\Repository;
use GitElephant\Command\TagCommand;
use GitElephant\Command\RevListCommand;
use Symfony\Component\Filesystem\Filesystem;


/**
Expand Down Expand Up @@ -53,6 +55,23 @@ class Tag extends Object
*/
private $sha;

/**
* Creates a new tag on the repository and returns it
*
* @param \GitElephant\Repository $repository repository instance
* @param string $name branch name
* @param string $startPoint branch to start from
* @param string $message tag message
*
* @return \GitElephant\Objects\Branch
*/
public static function create(Repository $repository, $name, $startPoint = null, $message = null)
{
$repository->getCaller()->execute(TagCommand::getInstance()->create($name, $startPoint, $message));

return $repository->getTag($name);
}

/**
* static generator to generate a single commit from output of command.show service
*
Expand Down
52 changes: 43 additions & 9 deletions src/GitElephant/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@
namespace GitElephant;

use GitElephant\Command\FetchCommand;
use GitElephant\Command\RemoteCommand;
use GitElephant\Exception\InvalidBranchNameException;
use GitElephant\GitBinary;
use GitElephant\Command\Caller;
use GitElephant\Objects\Remote;
use GitElephant\Objects\Tree;
use GitElephant\Objects\Branch;
use GitElephant\Objects\Tag;
Expand Down Expand Up @@ -230,7 +233,7 @@ public function getStatusOutput()
*/
public function createBranch($name, $startPoint = null)
{
$this->caller->execute(BranchCommand::getInstance()->create($name, $startPoint));
Branch::create($this, $name, $startPoint);
}

/**
Expand Down Expand Up @@ -327,11 +330,9 @@ function (Branch $branch) {
*/
public function getBranch($name)
{
foreach ($this->getBranches() as $branch) {
/** @var $branch Branch */
if ($branch->getName() == $name) {
return $branch;
}
try {
return Branch::checkout($this, $name);
} catch (InvalidBranchNameException $e) {
}

return null;
Expand Down Expand Up @@ -383,7 +384,7 @@ public function merge(Branch $branch)
*/
public function createTag($name, $startPoint = null, $message = null)
{
$this->caller->execute(TagCommand::getInstance()->create($name, $startPoint, $message));
Tag::create($this, $name, $startPoint, $message);
}

/**
Expand Down Expand Up @@ -411,7 +412,7 @@ public function addSubmodule($gitUrl, $path = null)
/**
* Gets an array of Tag objects
*
* @return array An array of Tag objects
* @return array
*/
public function getTags()
{
Expand Down Expand Up @@ -556,7 +557,7 @@ public function getObjectLog(Object $obj, $branch = null, $limit = 1, $offset =
* Checkout a branch
* This function change the state of the repository on the filesystem
*
* @param string|TreeishInterface $ref the ref to checkout
* @param string|TreeishInterface $ref the reference to checkout
*/
public function checkout($ref)
{
Expand Down Expand Up @@ -609,6 +610,39 @@ public function cloneFrom($url, $to = null)
$this->caller->execute(CloneCommand::getInstance()->cloneUrl($url, $to));
}

/**
* @param string $name remote name
* @param string $url remote url
*/
public function addRemote($name, $url)
{
$this->caller->execute(RemoteCommand::getInstance()->add($name, $url));
}

/**
* @param string $name remote name
*
* @return \GitElephant\Objects\Remote
*/
public function getRemote($name)
{
return Remote::create($this, $name);
}

/**
* gets a list of remote objects
*
* @return array
*/
public function getRemotes()
{
$remoteNames = $this->caller->execute(RemoteCommand::getInstance()->show())->getOutputLines(true);

return array_map(function($name) {
return $this->getRemote($name);
}, $remoteNames);
}

/**
* get the humanish name of the repository
*
Expand Down
Loading

0 comments on commit 6ddec60

Please sign in to comment.