summaryrefslogtreecommitdiffstats
path: root/src/GitDeploy/GitDeploy.php
diff options
context:
space:
mode:
authorRob Loach <robloach@gmail.com>2015-02-08 09:36:53 -0500
committerRob Loach <robloach@gmail.com>2015-02-08 09:36:53 -0500
commit56942fbeb71c41a074e293c58dd2633eda016198 (patch)
treea820ce826a9abd8225f6b1215422131564b3bb16 /src/GitDeploy/GitDeploy.php
parentbc0f13b93a3b20f1640c75c9f28fe77b5a4c89ca (diff)
downloadgit-deploy-origin/master.zip
git-deploy-origin/master.tar.gz
git-deploy-origin/master.tar.bz2
Update to allow multiple branchesHEAD0.0.5origin/masterorigin/HEADmaster
Diffstat (limited to 'src/GitDeploy/GitDeploy.php')
-rw-r--r--src/GitDeploy/GitDeploy.php99
1 files changed, 74 insertions, 25 deletions
diff --git a/src/GitDeploy/GitDeploy.php b/src/GitDeploy/GitDeploy.php
index d1c2cf6..a8c420c 100644
--- a/src/GitDeploy/GitDeploy.php
+++ b/src/GitDeploy/GitDeploy.php
@@ -7,11 +7,55 @@ use GitWrapper\GitWorkingCopy;
class GitDeploy
{
- protected $repositories;
+ protected $projects = array();
- public function __construct($repositories = array())
+ public function __construct(array $projects = array())
{
- $this->repositories = $repositories;
+ if (!empty($projects)) {
+ $this->setProjects($projects);
+ }
+ }
+
+ /**
+ * Parses the given projects configuration array into valid configuration.
+ */
+ public function setProjects(array $projects = array())
+ {
+ $output = array();
+
+ foreach ($projects as $name => $project) {
+ if (is_string($project)) {
+ $output[$name]['repo'] = $project;
+ }
+ elseif (is_array($project)) {
+ $output[$name] = $project;
+ }
+
+ // Construct the default branches array if one is not given.
+ if (!isset($output[$name]['branches']) || !is_array($output[$name]['branches'])) {
+ $output[$name]['branches'] = array(
+ 'gh-pages' => $name,
+ );
+ }
+ else {
+ $branches = array();
+ foreach ($output[$name]['branches'] as $source => $destination) {
+ if (is_numeric($source)) {
+ $source = $destination;
+ $destination = $name . '-' . $source;
+ }
+ $branches[$source] = $destination;
+ }
+ $output[$name]['branches'] = $branches;
+ }
+ }
+
+ $this->projects = $output;
+ }
+
+ public function getProjects()
+ {
+ return $this->projects;
}
public static function fromFile($file = 'git-deploy.json')
@@ -20,41 +64,46 @@ class GitDeploy
if (is_file($file)) {
$contents = file_get_contents($file);
- $results = json_decode($contents);
+ $results = json_decode($contents, true);
+ if ($results === NULL) {
+ throw new \UnexpectedValueException("The given JSON file could not be parsed.");
+ }
}
return new GitDeploy($results);
}
- public function update()
+ public function deploy()
{
// Create the wrapper.
$wrapper = new GitWrapper();
$wrapper->streamOutput();
- foreach ($this->repositories as $dir => $repo) {
- // Build our git interface.
- $git = null;
- if (!is_dir($dir)) {
- $git = $wrapper->cloneRepository($repo, $dir);
- }
- else {
- $git = new GitWorkingCopy($wrapper, $dir);
- }
+ // Iterate through each project.
+ foreach ($this->projects as $name => $project) {
+ // Check out all branches.
+ foreach ($project['branches'] as $branch => $destination) {
+ // Build our git interface.
+ $git = null;
+ if (!is_dir($destination)) {
+ $git = $wrapper->cloneRepository($project['repo'], $destination);
+ }
+ else {
+ $git = new GitWorkingCopy($wrapper, $destination);
+ }
- // Fetch all the latest.
- $git->fetch('--all');
+ // Fetch the latest.
+ $git->fetch('origin');
- // Reset over to the gh-pages branch.
- $git->reset('origin/gh-pages', array('hard' => true));
+ // Checkout the desired branch.
+ $git->checkout($branch, array('force' => true));
- // Remove any extra files.
- $git->clean('-d', '-f', '-x');
- }
- }
+ // Reset any local changes.
+ $git->reset(array('hard' => true));
- public function getRepositories()
- {
- return $this->repositories;
+ // Pull the latest from the branch.
+ $git->pull('origin', $branch, array('force' => true));
+ }
+ }
}
}