/** * Build an object log command * * @param \GitElephant\Objects\Object $obj the Object to get the log for * @param \GitElephant\Objects\Branch|string|null $branch the branch to consider * @param int|null $limit limit to n entries * @param int|null $offset skip n entries * * @throws \RuntimeException * @return string */ public function showObjectLog(Object $obj, $branch = null, $limit = null, $offset = null) { $subject = null; if (null !== $branch) { if ($branch instanceof Branch) { $subject .= $branch->getName(); } else { $subject .= (string) $branch; } } return $this->showLog($subject, $obj->getFullPath(), $limit, $offset); }
/** * build a ls-tree command * * @param string|Branch $ref The reference to build the tree from * * @throws \RuntimeException * @return string */ public function fullTree($ref = 'HEAD') { $what = $ref; if ($ref instanceof TreeishInterface) { $what = $ref->getSha(); } $this->clearAll(); $this->addCommandName(self::LS_TREE_COMMAND); // recurse $this->addCommandArgument('-r'); // show trees $this->addCommandArgument('-t'); $this->addCommandArgument('-l'); $this->addCommandSubject($what); return $this->getCommand(); }
/** * Generate a merge command * * @param \GitElephant\Objects\Branch $with the branch to merge * @param string $message a message for the merge commit, if merge is 3-way * @param array $options option flags for git merge * * @throws \RuntimeException * @return string */ public function merge(Branch $with, $message = '', array $options = array()) { if (in_array(self::MERGE_OPTION_FF_ONLY, $options) && in_array(self::MERGE_OPTION_NO_FF, $options)) { throw new \Symfony\Component\Process\Exception\InvalidArgumentException("Invalid options: cannot use flags --ff-only and --no-ff together."); } $normalizedOptions = $this->normalizeOptions($options, $this->mergeCmdSwitchOptions()); $this->clearAll(); $this->addCommandName(static::MERGE_COMMAND); foreach ($normalizedOptions as $value) { $this->addCommandArgument($value); } if (!empty($message)) { $this->addCommandArgument('-m'); $this->addCommandArgument($message); } $this->addCommandSubject($with->getFullRef()); return $this->getCommand(); }
/** * clone url */ public function testPush() { $pc = PushCommand::getInstance(); $this->assertEquals("push 'origin' 'master'", $pc->push()); $this->assertEquals("push 'github' 'master'", $pc->push('github')); $this->assertEquals("push 'github' 'develop'", $pc->push('github', 'develop')); $this->getRepository()->addRemote('test-remote', 'git@github.com:matteosister/GitElephant.git'); $remote = m::mock('GitElephant\\Objects\\Remote')->shouldReceive('getName')->andReturn('test-remote')->getMock(); $this->assertEquals("push 'test-remote' 'develop'", $pc->push($remote, 'develop')); $branch = Branch::create($this->getRepository(), 'test-branch'); $this->assertEquals("push 'test-remote' 'test-branch'", $pc->push($remote, $branch)); }
/** * fetch test */ public function testFetch() { $fc = FetchCommand::getInstance(); $this->assertEquals("fetch", $fc->fetch()); $this->assertEquals("fetch 'github'", $fc->fetch('github')); $this->assertEquals("fetch 'github' 'develop'", $fc->fetch('github', 'develop')); $this->getRepository()->addRemote('test-remote', 'git@github.com:matteosister/GitElephant.git'); $remote = m::mock('GitElephant\\Objects\\Remote')->shouldReceive('getName')->andReturn('test-remote')->getMock(); $this->assertEquals("fetch 'test-remote' 'develop'", $fc->fetch($remote, 'develop')); $branch = Branch::create($this->getRepository(), 'test-branch'); $this->assertEquals("fetch 'test-remote' 'test-branch'", $fc->fetch($remote, $branch)); $this->assertEquals("fetch '--tags' 'test-remote' 'test-branch'", $fc->fetch($remote, $branch, array('--tags'))); }
/** * testTagFromStartPoint */ public function testTagFromStartPoint() { $this->getRepository()->init(); $this->addFile('foo'); $this->repository->commit('commit1', true); Tag::create($this->repository, 'tag1', $this->repository->getCommit()); $tag = new Tag($this->repository, 'tag1'); $this->assertInstanceOf('GitElephant\\Objects\\Tag', $tag); $this->assertEquals($tag->getSha(), $this->repository->getCommit()->getSha()); $branch = Branch::create($this->repository, 'test-branch'); Tag::create($this->repository, 'tag2', $branch); $tag = new Tag($this->repository, 'tag2'); $this->assertEquals($tag->getSha(), $branch->getSha()); }
/** * parents created by log */ public function testParents() { $log = $this->getRepository()->getLog(); $lastCommit = $this->repository->getCommit(); $lastLogCommit = $log[0]; $this->assertEquals($lastCommit->getParents(), $lastLogCommit->getParents()); Branch::create($this->repository, 'new-branch'); $this->getRepository()->checkout('new-branch'); $this->addFile('another file'); $this->repository->commit('another commit', true); $lastCommitOtherBranch = $this->getRepository()->getCommit(); $this->getRepository()->checkout('master'); $this->addFile('another file on master'); $this->getRepository()->commit('new commit on master', true); $lastCommitOnMaster = $this->getRepository()->getCommit(); $this->getRepository()->merge($this->getRepository()->getBranch('new-branch')); $log = $this->getRepository()->getLog(); $lastLogCommit = $log[0]; $this->assertContains($lastCommitOnMaster->getSha(), $lastLogCommit->getParents()); $this->assertContains($lastCommitOtherBranch->getSha(), $lastLogCommit->getParents()); }
/** * An array of Branch objects * * @param bool $namesOnly return an array of branch names as a string * @param bool $all lists also remote branches * * @throws \RuntimeException * @throws \Symfony\Component\Process\Exception\InvalidArgumentException * @throws \Symfony\Component\Process\Exception\LogicException * @throws \InvalidArgumentException * @throws \Symfony\Component\Process\Exception\RuntimeException * @return array */ public function getBranches($namesOnly = false, $all = false) { $branches = array(); if ($namesOnly) { $outputLines = $this->caller->execute(BranchCommand::getInstance($this)->listBranches($all, true))->getOutputLines(true); $branches = array_map(function ($v) { return ltrim($v, '* '); }, $outputLines); } else { $outputLines = $this->caller->execute(BranchCommand::getInstance($this)->listBranches($all))->getOutputLines(true); foreach ($outputLines as $branchLine) { $branches[] = Branch::createFromOutputLine($this, $branchLine); } } return $branches; }
/** * Checkout a treeish reference * * @param string|Branch $ref the reference to checkout * * @throws \RuntimeException * @return string */ public function checkout($ref) { $this->clearAll(); $what = $ref; if ($ref instanceof Branch) { $what = $ref->getName(); } elseif ($ref instanceof TreeishInterface) { $what = $ref->getSha(); } $this->addCommandName(self::GIT_CHECKOUT); $this->addCommandArgument('-q'); $this->addCommandSubject($what); return $this->getCommand(); }
/** * testCreate */ public function testCreate() { $this->getRepository()->init(); $this->addFile('test'); $this->repository->commit('test', true); $this->assertCount(1, $this->repository->getBranches(true)); Branch::create($this->repository, 'test-branch'); $this->assertCount(2, $this->repository->getBranches(true)); Branch::create($this->repository, 'test-branch2', 'test-branch'); $this->assertCount(3, $this->repository->getBranches(true)); }