private function drush_pm_version_control_bzr::sync

3.x bzr.inc private drush_pm_version_control_bzr::sync($project)
4.x bzr.inc private drush_pm_version_control_bzr::sync($project)

Automatically add any unversioned files to Bzr and remove any files that have been deleted on the file system

File

commands/pm/version_control/bzr.inc, line 55
Drush pm BZR extension

Class

drush_pm_version_control_bzr

Code

private function sync($project) {
  if (drush_get_option('bzrsync')) {
    $errors = '';

    // All paths returned by bzr status are relative to the repository root.
    $root = bzr_get_repository_root($project['full_project_path']);

    if (drush_shell_exec('bzr status -S %s', $project['full_project_path'])) {
      $output = drush_shell_exec_output();

      // Parse the output of bzr status to get the list of added and removed
      // files.
      $new_files = array();
      $removed_files = array();
      foreach ($output as $line) {
        if (preg_match('/^\? *(.*)/', $line, $matches)) {
          $new_files[] = $root . '/' . $matches[1];
        }
        if (preg_match('/^\ D *(.*)/', $line, $matches)) {
          $removed_files[] = $root . '/' . $matches[1];
        }
      }

      // Proceed adding new files.
      foreach (array_chunk($new_files, 100) as $chunk) {
        $chunk = array_map('escapeshellarg', $chunk);
        if (!drush_shell_exec('bzr add ' . implode(' ', $chunk))) {
          $errors .= implode("\n", drush_shell_exec_output());
        }
      }

      // Proceed removing old files.
      foreach (array_chunk($removed_files, 100) as $chunk) {
        $chunk = array_map('escapeshellarg', $chunk);
        if (!drush_shell_exec('bzr remove ' . implode(' ', $chunk))) {
          $errors .= implode("\n", drush_shell_exec_output());
        }
      }

      if (!empty($errors)) {
        return drush_set_error('DRUSH_PM_BZR_SYNC_PROBLEMS', dt("Problems were encountered adding or removing files to/from Bazaar.\nThe specific errors are below:\n!errors", array('!errors' => $errors)));
      }
    }
    else {
      return drush_set_error('DRUSH_PM_BZR_NOT_FOUND', dt("Drush was unable to get the bzr status. Check that you have Bazaar \ninstalled and that the site is a Bazaar working copy.\nThe specific errors are below:\n!errors", array('!errors' => implode("\n", drush_shell_exec_output()))));
    }
    return TRUE;
  }
}