Skip to content

Commit

Permalink
Merge pull request phingofficial#414 from siad007/patch-5
Browse files Browse the repository at this point in the history
Fixes phingofficial#1162 - add depth param to GitCloneTask
  • Loading branch information
mrook committed Apr 3, 2015
2 parents d78e699 + f8caa98 commit a1ef309
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 5 deletions.
79 changes: 74 additions & 5 deletions classes/phing/tasks/ext/git/GitCloneTask.php
Expand Up @@ -32,6 +32,12 @@
*/
class GitCloneTask extends GitBaseTask
{
/**
* Whether --depth key should be set for git-clone
* @var int
*/
private $depth = 0;

/**
* Whether --bare key should be set for git-init
* @var string
Expand Down Expand Up @@ -70,22 +76,85 @@ public function main()
$client = $this->getGitClient(false, getcwd());

try {
$client->createClone(
$this->getRepository(),
$this->isBare(),
$this->getTargetPath()
);
if ($this->hasDepth()) {
$this->doShallowClone($client);
} else {
$client->createClone(
$this->getRepository(),
$this->isBare(),
$this->getTargetPath()
);
}
} catch (Exception $e) {
throw new BuildException('The remote end hung up unexpectedly', $e);
}

$msg = 'git-clone: cloning '
. ($this->isBare() ? '(bare) ' : '')
. ($this->hasDepth() ? ' (depth="' . $this->getDepth() . '") ' : '')
. '"' . $this->getRepository() . '" repository'
. ' to "' . $this->getTargetPath() . '" directory';
$this->log($msg, Project::MSG_INFO);
}

/**
* Create a shallow clone with a history truncated to the specified number of revisions.
*
* @param VersionControl_Git $client
*
* @throws VersionControl_Git_Exception
*/
protected function doShallowClone(VersionControl_Git $client)
{
$command = $client->getCommand('clone')
->setOption('depth', $this->getDepth())
->setOption('q')
->addArgument($this->getRepository())
->addArgument($this->getTargetPath());

if (is_dir($this->getTargetPath()) && version_compare('1.6.1.4', $client->getGitVersion(), '>=')) {
$isEmptyDir = true;
$entries = scandir($this->getTargetPath());
foreach ($entries as $entry) {
if ('.' !== $entry && '..' !== $entry) {
$isEmptyDir = false;

break;
}
}

if ($isEmptyDir) {
@rmdir($this->getTargetPath());
}
}

$command->execute();
}

/**
* @return int
*/
public function getDepth()
{
return $this->depth;
}

/**
* @param int $depth
*/
public function setDepth($depth)
{
$this->depth = $depth;
}

/**
* @return bool
*/
public function hasDepth()
{
return (bool) $this->depth;
}

/**
* Get path to target direcotry repo
*
Expand Down
10 changes: 10 additions & 0 deletions docs/docbook5/en/source/appendixes/optionaltasks.xml
Expand Up @@ -1435,6 +1435,16 @@
<entry><literal>false</literal></entry>
<entry>No</entry>
</row>
<row>
<entry><literal>depth</literal></entry>
<entry><literal role="type">Integer</literal></entry>
<entry>Create a shallow clone with a history truncated to the specified number of revisions.
See --depth option of <link
xlink:href="http://www.kernel.org/pub/software/scm/git/docs/git-clone.html"
>git-clone</link>.</entry>
<entry><literal>0</literal></entry>
<entry>No</entry>
</row>
</tbody>
</tgroup>
</table>
Expand Down

0 comments on commit a1ef309

Please sign in to comment.