diff --git a/README.md b/README.md index 5fbc099..0a7ab3d 100644 --- a/README.md +++ b/README.md @@ -28,11 +28,18 @@ or run the following command: php composer.phar require tivie/php-git-log-parser ## Quick guide -To parse the current git repository log, you can simply +To parse a git repository log, you can simply ```php +$repositoryPath = '/path_to_your_repo'; +$revision = 'fe4f1105e10a8fedeb0b4f2f8c4ea43bec56a256..' + $parser = new \Tivie\GitLogParser\Parser(); +$parser->setGitDir($repositoryPath); +$parser->setRevision($revision); +echo $parser->getCommand(); $logArray = $parser->parse(); + ``` ## License diff --git a/src/Parser.php b/src/Parser.php index 49c3401..39cc029 100644 --- a/src/Parser.php +++ b/src/Parser.php @@ -60,8 +60,6 @@ public function __construct(Format $format = null, Command $command = null, $rev $this->gitDir = __DIR__; $this->branch = 'HEAD'; - - $this->command = ($command) ? $command : $this->buildCommand(); } /** @@ -106,6 +104,8 @@ public function setCommand(Command $command) */ public function getCommand() { + if (!$this->command) $this->command = $this->buildCommand(); + return $this->command; } @@ -145,7 +145,7 @@ public function setGitDir($dir, $check = true) throw new Exception("Directory $dir does not exist"); } $this->gitDir = $dir; - $this->command->chdir($dir); + if ($this->command) $this->command->chdir($dir); return $this; } @@ -164,13 +164,16 @@ public function setBranch($branch) throw new InvalidArgumentException('string', 0); } - $oldBranch = $this->branch; - $oldArg = $this->command->searchArgument($oldBranch); - if (!$oldArg) { - throw new Exception("Couldn't change the command to new branch. Was the Command object modified?"); + if ($this->command) { + $oldBranch = $this->branch; + $oldArg = $this->command->searchArgument($oldBranch); + if (!$oldArg) { + throw new Exception("Couldn't change the command to new branch. Was the Command object modified?"); + } + + $newArg = new Argument($branch); + $this->command->replaceArgument($oldArg, $newArg); } - $newArg = new Argument($branch); - $this->command->replaceArgument($oldArg, $newArg); $this->branch = $branch; return $this; @@ -183,7 +186,8 @@ public function setBranch($branch) */ public function parse() { - $result = $this->command->run(); + $command = $this->getCommand(); + $result = $command->run(); $log = $result->getStdOut(); $buffer = array();