Merge driver consists of three things:
1. .gitattributes definition that tells which files to process with which driver
2. Section in git/config that maps the logical merge driver name to a concrete script on a disk
3. The actual script files(s) - PHP or Bash impl in our case
It's a bit tricky because:
- .gitattributes is committed in the repo. The paths must therefore be relative.
- git/config is not committed in the repo so it must be created dynamically on actions
like activating VersionPress or restoring / cloning a site. Furthermore, the paths
must be absolute because Git's cwd can be different.
- We need cross-platform scripts so we detect the OS and install the correct driver.
The driver impl can be forced using the $driver parameter.
public static installMergeDriver ( string $rootDir, string $pluginDir, string $vpdbDir, string $driver = self::DRIVER_AUTO ) | ||
$rootDir | string | Where to install the driver |
$pluginDir | string | Path to VersionPress (plugin) - used to look up templates and merge drivers |
$vpdbDir | string | Location of the VPDB dir (where the INI files are) |
$driver | string | DRIVER_BASH | DRIVER_PHP | DRIVER_AUTO (default; will use PHP driver for Windows, Bash otherwise) |
/** * Finishes clone operation * * @subcommand finish-restore-site * * @when before_wp_load * */ public function finishRestore($args, $assoc_args) { define('SHORTINIT', true); $wpConfigPath = \WP_CLI\Utils\locate_wp_config(); require_once $wpConfigPath; require ABSPATH . WPINC . '/formatting.php'; require ABSPATH . WPINC . '/link-template.php'; require ABSPATH . WPINC . '/shortcodes.php'; require ABSPATH . WPINC . '/taxonomy.php'; wp_plugin_directory_constants(); require_once WP_PLUGIN_DIR . '/versionpress/bootstrap.php'; $versionPressContainer = DIContainer::getConfiguredInstance(); /** @var ActionsDefinitionRepository $actionsDefinitionRepository */ $actionsDefinitionRepository = $versionPressContainer->resolve(VersionPressServices::ACTIONS_DEFINITION_REPOSITORY); $actionsDefinitionRepository->restoreAllDefinitionFilesFromHistory(); // Truncate tables /** @var Database $database */ $database = $versionPressContainer->resolve(VersionPressServices::DATABASE); /** @var DbSchemaInfo $dbSchema */ $dbSchema = $versionPressContainer->resolve(VersionPressServices::DB_SCHEMA); $tables = array_map(function ($entityName) use($dbSchema) { return $dbSchema->getPrefixedTableName($entityName); }, array_merge($dbSchema->getAllEntityNames(), array_map(function ($referenceDetails) { return $referenceDetails['junction-table']; }, $dbSchema->getAllMnReferences()))); $tables = array_filter($tables, function ($table) use($database) { return $table !== $database->options; }); foreach ($tables as $table) { $truncateCmd = "TRUNCATE TABLE `{$table}`"; @$database->query($truncateCmd); // Intentional @ - not existing table is ok for us but TRUNCATE ends with error } // Create VersionPress tables /** @var \VersionPress\Initialization\Initializer $initializer */ $initializer = $versionPressContainer->resolve(VersionPressServices::INITIALIZER); $initializer->createVersionPressTables(); WP_CLI::success("VersionPress tables created"); // Install Custom merge driver MergeDriverInstaller::installMergeDriver(VP_PROJECT_ROOT, VERSIONPRESS_PLUGIN_DIR, VP_VPDB_DIR); WP_CLI::success("Git merge driver added"); // Run synchronization /** @var SynchronizationProcess $syncProcess */ $syncProcess = $versionPressContainer->resolve(VersionPressServices::SYNCHRONIZATION_PROCESS); $syncProcess->synchronizeAll(); WP_CLI::success("Database synchronized"); VPCommandUtils::runWpCliCommand('vp-internal', 'flush-regenerable-values', ['require' => __FILE__]); }
/** * @param string $driver See MergeDriverInstaller::installMergeDriver()'s $driver parameter */ private function installMergeDriver($driver) { MergeDriverInstaller::installMergeDriver(self::$repositoryDir, __DIR__ . '/../..', self::$repositoryDir, $driver); }
private function createGitRepository() { if (!$this->repository->isVersioned()) { $this->reportProgressChange(InitializerStates::CREATING_GIT_REPOSITORY); $this->repository->init(); } $this->installGitignore(); MergeDriverInstaller::installMergeDriver(VP_PROJECT_ROOT, VERSIONPRESS_PLUGIN_DIR, VP_VPDB_DIR); }