Skip to content

Commit

Permalink
Fixed not correctly working in detached repo.
Browse files Browse the repository at this point in the history
AlexSkrypnyk authored Apr 24, 2018

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent d473c22 commit b89900a
Showing 2 changed files with 73 additions and 1 deletion.
40 changes: 39 additions & 1 deletion src/ArtefactTrait.php
Original file line number Diff line number Diff line change
@@ -257,7 +257,7 @@ protected function resolveOptions(array $options)
$srcPath = !empty($options['src']) ? $this->fsGetAbsolutePath($options['src']) : $this->fsGetRootDir();
$this->gitSetSrcRepo($srcPath);

$this->originalBranch = $this->gitGetCurrentBranch($this->src);
$this->originalBranch = $this->resolveOriginalBranch($this->src);
$this->setDstBranch($options['branch']);
$this->artefactBranch = $this->dstBranch.'-artefact';

@@ -388,6 +388,44 @@ public static function modeDiff()
return 'diff';
}

/**
* Resolve original branch to handle detached repositories.
*
* Usually, repository become detached when a tag is checked out.
*
* @param string $location
* Path to repository.
*
* @return null|string
* Branch or detachment source.
* @throws \Exception
* If neither branch nor detachment source is not found.
*/
protected function resolveOriginalBranch($location)
{
$branch = $this->gitGetCurrentBranch($location);

// Repository could be in detached state. If this the case - we need to
// capture the source of detachment, if exist.
if ($branch == 'HEAD') {
$branch = null;
$result = $this->gitCommandRun($location, 'branch');
$branchList = array_filter(preg_split('/\R/', $result->getMessage()));

foreach ($branchList as $branch) {
if (preg_match('/\* \(.*detached .* ([^\)]+)\)/', $branch, $matches)) {
$branch = $matches[1];
break;
}
}
if (empty($branch)) {
throw new \Exception('Unable to determine detachment source');
}
}

return $branch;
}

/**
* Set the branch in the remote repository where commits will be pushed to.
*
34 changes: 34 additions & 0 deletions tests/TagTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace IntegratedExperts\Robo\Tests;

/**
* Class TagTest.
*/
class TagTest extends AbstractTest
{

/**
* {@inheritdoc}
*/
public function setUp()
{
$this->mode = 'force-push';
parent::setUp();
}

public function testDetachedTag()
{
$this->gitCreateFixtureCommits(2);
$this->gitAddTag($this->src, 'tag1');
$this->gitCheckout($this->src, 'tag1');
$srcBranches = $this->runGitCommand('branch');

$output = $this->assertBuildSuccess();
$this->assertContains('Mode: force-push', $output);
$this->assertContains('Will push: Yes', $output);

$this->assertFixtureCommits(2, $this->dst, 'testbranch', ['Deployment commit']);
$this->assertEquals($srcBranches, $this->runGitCommand('branch'), 'Cleanup has correctly returned to the previous branch.');
}
}

0 comments on commit b89900a

Please sign in to comment.