public function deploy() { // Create the wrapper. $wrapper = new GitWrapper(); $wrapper->streamOutput(); // 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 the latest. $git->fetch('origin'); // Checkout the desired branch. $git->checkout($branch, array('force' => true)); // Reset any local changes. $git->reset(array('hard' => true)); // Pull the latest from the branch. $git->pull('origin', $branch, array('force' => true)); } } }
public function execute(GitWorkingCopy $git, LocalPackage $package) { $git->add('.'); $this->dispatchEvent(StepsEvents::GIT_ADDED, new GitEventMade($git)); $git->commit($this->getCommitMessage($git)); $this->dispatchEvent(StepsEvents::GIT_COMMITTED, new GitEventMade($git)); $git->push('origin', $package->getLocalBranch(), array('f' => true)); $this->dispatchEvent(StepsEvents::GIT_PUSHED, new GitEventMade($git)); return null === $git->getStatus() ? true : false; }
/** * Pushes the deployment build to the temporary git repository. */ public function main() { // Check if all required properties are present. $this->checkRequiredProperties(); // Add the remote for the starterkit if it doesn't exist yet. $remote_branch = 'remotes/' . $this->starterkitRemote . '/' . $this->starterkitBranch; $remote_exists = $this->subsiteRepository->hasRemote($this->starterkitRemote); if (!$remote_exists) { $this->log('Adding remote repository.'); // Only track the given branch, and don't download any tags. $options = ['--no-tags' => TRUE, '-t' => [$this->starterkitBranch]]; $this->subsiteRepository->addRemote($this->starterkitRemote, $this->starterkitRepository, $options); } // Check if the tracking branch exists and create it if it doesn't. try { $this->subsiteRepository->run(array('rev-parse', $remote_branch)); } catch (GitException $e) { $this->log('Adding tracking branch.'); $this->subsiteRepository->remote('set-branches', '--add', $this->starterkitRemote, $this->starterkitBranch); } // Fetch the latest changes. $this->log('Fetching latest changes.'); $this->subsiteRepository->fetch($this->starterkitRemote); // Check if the latest commit on the remote is merged into the current // branch. $this->subsiteRepository->clearOutput(); $latest_commit = (string) $this->subsiteRepository->run(array('rev-parse', $remote_branch)); $merge_base = (string) $this->subsiteRepository->run(array('merge-base @ ' . $remote_branch)); // If the latest commit on the remote is not merged into the current branch, // the repository is not up-to-date. if ($merge_base !== $latest_commit) { throw new \BuildException('The current branch is not up to date with the starterkit.'); } $this->log('The starterkit is up to date.'); }
/** * @return array */ public function getRefs() { if (empty($this->refs)) { $this->git->fetch(); foreach (explode(PHP_EOL, trim($this->wrapper->git('show-ref --abbrev'))) as $row) { list($hash, $ref) = explode(' ', $row, 2); $this->assignHashByRef($this->refs, $ref, $hash); } } return $this->refs['refs']; }
/** * Set up the git wrapper and the temporary directory. */ protected function setup() { // @TODO: Allow setting up in an existing dierectory. if (!$this->dir) { // Greate a temporary directory. $tempfile = tempnam(sys_get_temp_dir(), ''); mkdir($tempfile . '.git'); if (file_exists($tempfile)) { unlink($tempfile); } $this->dir = $tempfile . '.git'; $this->git = $this->wrapper->init($this->dir); } $this->git->config('user.name', 'GitMerge')->config('user.email', '*****@*****.**')->config('merge.conflictStyle', 'diff3'); $this->strategy = null; }
/** * Deploy a version to an environment. * * @param $version * A git branch, tag, or sha. */ public function deploy($version) { // Checkout the branch $wrapper = new GitWrapper(); $wrapper->streamOutput(); $git = new GitWorkingCopy($wrapper, $this->getSourcePath()); $git->checkout($version); $git->pull(); // Reload config so any changes get picked up. $this->reloadConfig(); // Run the deploy hooks, if there are any. if (isset($this->config['hooks']['deploy']) && !empty($this->config['hooks']['deploy'])) { chdir($this->getSourcePath()); $process = new Process($this->config['hooks']['deploy']); $process->run(function ($type, $buffer) { if (Process::ERR === $type) { // Error echo $buffer; } else { // OK echo $buffer; } }); } // @TODO: Save the environment // @TODO: Create EnvironmentFactory->save(); // Returning the branch for now. The command is saving the info. return $this->getRepo()->getCurrentBranch(); }
protected function assertNoRemoteBranch(GitWorkingCopy $repository, $branch) { $branches = $repository->getBranches()->remote(); $this->assertArrayNotHasKey($branch, array_flip($branches)); }
/** * Returns currently active branch (HEAD) of the working copy. * * @return string */ public function head() { return trim((string) $this->git->run(array('rev-parse --abbrev-ref HEAD'))); }
/** * Deploy a version to an environment. * * @param $version * A git branch, tag, or sha. */ public function deploy($version) { // Checkout the branch $wrapper = new GitWrapper(); $wrapper->streamOutput(); $git = new GitWorkingCopy($wrapper, $this->getSourcePath()); $git->checkout($version); $git->pull(); // Reload config so any changes get picked up. $this->reloadConfig(); // Run the deploy hooks chdir($this->getSourcePath()); $process = new Process($this->config['hooks']['deploy']); $process->run(function ($type, $buffer) { if (Process::ERR === $type) { // Error echo $buffer; } else { // OK echo $buffer; } }); // Save new branch to yml $this->director->config['apps'][$this->app]['environments'][$this->name]['git_ref'] = $this->getRepo()->getCurrentBranch(); $this->director->saveData(); }
/** * Get an array of all files in the current repo * * @return array */ protected function getAllFiles() { $files = $this->repository->run(['ls-files', '--full-name'])->getOutput(); return array_map('trim', explode("\n", $files)); }
/** * Deploy the current contents of our working copy. * * @param GitWorkingCopy $workingCopy */ protected function deployWithGit(GitWorkingCopy $workingCopy) { if (!$workingCopy->hasChanges()) { return $this->output->writeln('<comment>No changes to deploy!</comment>'); } $this->output->writeln('<info>Ready to deploy changes:</info>'); $this->output->write($workingCopy->getStatus()); if (!$this->input->getOption('force')) { if (!$this->askForChangesConfirmation($workingCopy)) { return $this->output->writeln('<error>Aborted!</error>'); } } $this->output->writeln('<info>Deploying...</info>'); $this->output->write($workingCopy->add('.')->commit($this->getCommitMessage())->push()->getOutput(), OutputInterface::VERBOSITY_VERBOSE); }
private function getGitWorking($gitWrapper, $package) { $git = new GitWorkingCopy($gitWrapper, $package->getFolder()); $git->config('user.name', $this->githubUserName)->config('user.email', $this->githubEmail); return $git; }