Exemplo n.º 1
0
 /**
  * Creates a commit.
  *
  * Uses a temporary file to construct the commit message because on Windows, multi-line
  * commit message cannot be created on CLI and it's generally a more flexible solution
  * (very long commit messages, etc.).
  *
  * @param CommitMessage|string $message
  * @param string $authorName
  * @param string $authorEmail
  */
 public function commit($message, $authorName, $authorEmail)
 {
     if (is_string($message)) {
         $commitMessage = $message;
         $body = null;
     } else {
         $subject = $message->getSubject();
         $body = $message->getBody();
         $commitMessage = $this->commitMessagePrefix . $subject;
     }
     if ($body != null) {
         $commitMessage .= "\n\n" . $body;
     }
     $tempCommitMessageFilename = md5(rand());
     $tempCommitMessagePath = $this->tempDirectory . '/' . $tempCommitMessageFilename;
     file_put_contents($tempCommitMessagePath, $commitMessage);
     // Unfortunatelly, `git commit --author=...` is not enough.
     // It doesn't work with empty both local and global config.
     $localConfigUserName = $this->runShellCommandWithStandardOutput('git config --local user.name');
     $localConfigUserEmail = $this->runShellCommandWithStandardOutput('git config --local user.email');
     $this->runShellCommand('git config --local user.name %s', $authorName);
     $this->runShellCommand('git config --local user.email %s', $authorEmail);
     $this->runShellCommand("git commit --file=%s", $tempCommitMessagePath);
     FileSystem::remove($tempCommitMessagePath);
     if ($localConfigUserName === null) {
         $this->runShellCommand('git config --local --unset user.name');
     } else {
         $this->runShellCommand('git config --local user.name %s', $localConfigUserName);
     }
     if ($localConfigUserEmail === null) {
         $this->runShellCommand('git config --local --unset user.email');
     } else {
         $this->runShellCommand('git config --local user.email %s', $localConfigUserEmail);
     }
     $gitConfigPath = $this->workingDirectoryRoot . '/.git/config';
     GitConfig::removeEmptySections($gitConfigPath);
 }
Exemplo n.º 2
0
 /**
  * Pushes changes to another clone
  *
  * ## OPTIONS
  *
  * [--to=<name|path>]
  * : Name of the clone or a path to it. Defaults to 'origin' which, in a clone,
  * points to its original site.
  *
  * ## EXAMPLES
  *
  * Push is a similar command to 'pull' but does not create a merge. To push from clone
  * to the original site, run:
  *
  *     wp vp push
  *
  * To push from the original site to the clone, use the '--to' parameter:
  *
  *     wp vp push --to=clonename
  *
  *
  */
 public function push($args = [], $assoc_args = [])
 {
     if (!VersionPress::isActive()) {
         WP_CLI::error('This site is not tracked by VersionPress. Please run "wp vp activate" before cloning / merging.');
     }
     $remoteName = isset($assoc_args['to']) ? $assoc_args['to'] : 'origin';
     $remotePath = $this->getRemoteUrl($remoteName);
     if ($remotePath === null) {
         $remotePath = $remoteName;
         if (!is_dir($remotePath)) {
             WP_CLI::error("'{$remotePath}' is not a valid path to a WP site");
         }
     }
     $this->switchMaintenance('on', $remoteName);
     vp_commit_all_frequently_written_entities();
     $currentPushType = trim(VPCommandUtils::exec('git config --local push.default')->getOutput());
     VPCommandUtils::exec('git config --local push.default simple');
     // hardcoded branch name until we support custom branches
     $pushCommand = "git push --set-upstream {$remoteName} master";
     $process = VPCommandUtils::exec($pushCommand);
     if ($process->isSuccessful()) {
         WP_CLI::success("Changes successfully pushed");
     } else {
         $this->switchMaintenance('off', $remoteName);
         WP_CLI::error("Changes couldn't be pushed. Details:\n\n" . $process->getConsoleOutput());
     }
     if ($currentPushType === '') {
         // implicit value
         VPCommandUtils::exec("git config --local --unset push.default");
     } else {
         VPCommandUtils::exec("git config --local push.default {$currentPushType}");
     }
     $gitConfigPath = VP_PROJECT_ROOT . '/.git/config';
     GitConfig::removeEmptySections($gitConfigPath);
     $process = $this->runVPInternalCommand('finish-push', [], $remotePath);
     if ($process->isSuccessful()) {
         WP_CLI::success("Remote database synchronized");
     } else {
         WP_CLI::error("Push couldn't be finished. Details:\n\n" . $process->getConsoleOutput());
     }
     $this->switchMaintenance('off', $remoteName);
     WP_CLI::success("All done");
 }