Skip to content

Commit

Permalink
Merge pull request #232 from sburba/master
Browse files Browse the repository at this point in the history
Skip deploy if no files were changed
  • Loading branch information
mnapoli committed Apr 16, 2019
2 parents 1330121 + 4cbdab8 commit 53fe638
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 3 deletions.
14 changes: 13 additions & 1 deletion src/CommandRunner/Git.php
Expand Up @@ -56,8 +56,20 @@ public function getRemoteUrl($remote = 'origin')
return trim($this->commandRunner->run($command));
}

/**
* Check if the git repository in the provided directory has any uncommitted changes
*
* @param string $directory A directory containing a git repository
* @return bool True if there are changes, false otherwise
*/
public function hasUncommittedChanges($directory)
{
$changes = $this->run($directory, "git diff-index --name-only HEAD");
return !(ctype_space($changes) || $changes = '');
}

private function run($directory, $command)
{
$this->commandRunner->run("cd \"$directory\" && $command");
return $this->commandRunner->run("cd \"$directory\" && $command");
}
}
8 changes: 6 additions & 2 deletions src/Deployer.php
Expand Up @@ -51,9 +51,13 @@ public function deploy(Project $project, OutputInterface $output, $repositoryUrl

$this->copyGeneratedFiles($output, $directory, $tmpDirectory);

$this->commitChanges($output, $tmpDirectory);
if ($this->git->hasUncommittedChanges($tmpDirectory)) {
$this->commitChanges($output, $tmpDirectory);

$this->pushBranch($output, $branch, $tmpDirectory);
$this->pushBranch($output, $branch, $tmpDirectory);
} else {
$output->writeln('<comment>Skipping deploy, no changes detected</comment>');
}

$this->deleteTempDirectory($tmpDirectory);
}
Expand Down
28 changes: 28 additions & 0 deletions tests/UnitTest/CommandRunner/GitTest.php
Expand Up @@ -91,6 +91,34 @@ public function get_remote_url_should_return_url()
$this->assertEquals('the-remote', $remote);
}

/**
* @test
*/
public function has_uncommitted_changes_should_detect_changes()
{
$directory = 'directory';
$changes = '
changed_file_one.txt
changed_file_two.txt
';
$this->expectCommandIsRun('cd "directory" && git diff-index --name-only HEAD', $changes);

$this->assertTrue($this->git->hasUncommittedChanges($directory));
}

/**
* @test
*/
public function has_uncommitted_changes_should_detect_no_changes()
{
$directory = 'directory';
$changes = '
';
$this->expectCommandIsRun('cd "directory" && git diff-index --name-only HEAD', $changes);

$this->assertFalse($this->git->hasUncommittedChanges($directory));
}

private function expectCommandIsRun($command, $return = null)
{
$this->commandRunner->expects($this->once())
Expand Down

0 comments on commit 53fe638

Please sign in to comment.