Example #1
0
 private function revert($commits, $method)
 {
     if (!$this->canRevert()) {
         return RevertStatus::NOT_CLEAN_WORKING_DIRECTORY;
     }
     vp_commit_all_frequently_written_entities();
     uasort($commits, function ($a, $b) {
         return $this->repository->wasCreatedAfter($b, $a);
     });
     $modifiedFiles = [];
     $vpIdsInModifiedFiles = [];
     foreach ($commits as $commitHash) {
         $commitHashForDiff = $method === "undo" ? sprintf("%s~1..%s", $commitHash, $commitHash) : $commitHash;
         $modifiedFiles = array_merge($modifiedFiles, $this->repository->getModifiedFiles($commitHashForDiff));
         $modifiedFiles = array_unique($modifiedFiles, SORT_REGULAR);
         $vpIdsInModifiedFiles = array_merge($vpIdsInModifiedFiles, $this->getAllVpIdsFromModifiedFiles($modifiedFiles));
         $vpIdsInModifiedFiles = array_unique($vpIdsInModifiedFiles, SORT_REGULAR);
         if ($method === "undo") {
             $status = $this->revertOneCommit($commitHash);
         } else {
             $status = $this->revertToCommit($commitHash);
         }
         if ($status !== RevertStatus::OK) {
             return $status;
         }
         vp_force_action('versionpress', $method, $commitHash, [], [["type" => "path", "path" => "*"]]);
     }
     if (!$this->repository->willCommit()) {
         return RevertStatus::NOTHING_TO_COMMIT;
     }
     $affectedPosts = $this->getAffectedPosts($modifiedFiles);
     $this->updateChangeDateForPosts($affectedPosts);
     $this->committer->commit();
     $vpIdsInModifiedFiles = array_merge($vpIdsInModifiedFiles, $this->getAllVpIdsFromModifiedFiles($modifiedFiles));
     $vpIdsInModifiedFiles = array_unique($vpIdsInModifiedFiles, SORT_REGULAR);
     $this->synchronizationProcess->synchronize($vpIdsInModifiedFiles);
     do_action('vp_revert', $modifiedFiles);
     return RevertStatus::OK;
 }
Example #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");
 }
 /**
  * Used before pull
  *
  * @subcommand commit-frequently-written-entities
  */
 public function commitFrequentlyWrittenEntities($args = [], $assoc_args = [])
 {
     vp_commit_all_frequently_written_entities();
 }