Commit 8d949c75 authored by Marco Villegas's avatar Marco Villegas
Browse files

Issue #1796382: Set default branch from reposync default plugin.

- Lets {versioncontrol_git_repositories}.default_branch to be NULL.
- Renames VersioncontrolGitRepository default_branch data member to
  defaultBranch. Also changes its visibility to public to be able to
  change it from outside (i.e. reposync plugin).
- Adds a getDefaultBranch() method to
  VersioncontrolGitRepositoryHistorySynchronizerDefault to retrieve the
  default branch from git.
- Set the default branch to database on reposync default plugin.
- Adds a note to remove VersioncontrolGitRepository::getDefaultBranch()
  in the next major version.
parent 900c77ff
Loading
Loading
Loading
Loading
+20 −7
Changes for includes/VersioncontrolGitRepository.php: 20 added lines, 7 removed lines.
Original line number Diff line number Diff line
@@ -3,10 +3,20 @@
class VersioncontrolGitRepository extends VersioncontrolRepository {

  /**
   * The branch name of the default (HEAD) branch or empty if this information
   * The branch name of the default (HEAD) branch or NULL if this information
   * is not available.
   */
  public $default_branch = 'master';
  public $defaultBranch = NULL;

  public function build($args = array()) {
    parent::build($args);
    // Map db field to data member.
    if (isset($this->default_branch)) {
      $this->defaultBranch = $this->default_branch;
    }
    unset($this->default_branch);
  }


  protected function backendDelete($options) {
    db_delete('versioncontrol_git_repositories')
@@ -29,7 +39,7 @@ class VersioncontrolGitRepository extends VersioncontrolRepository {
  protected function backendUpdate($options) {
    db_update('versioncontrol_git_repositories')
      ->condition('repo_id', $this->repo_id)
      ->fields(array('default_branch' => $this->default_branch))
      ->fields(array('default_branch' => $this->defaultBranch))
      ->execute();
  }

@@ -37,7 +47,7 @@ class VersioncontrolGitRepository extends VersioncontrolRepository {
    db_insert('versioncontrol_git_repositories')
      ->fields(array(
        'repo_id' => $this->repo_id,
        'default_branch' => $this->default_branch,
        'default_branch' => $this->defaultBranch,
      ))
      ->execute();
  }
@@ -136,11 +146,14 @@ class VersioncontrolGitRepository extends VersioncontrolRepository {
  /**
   * Get the default (HEAD) branch of the repository.
   *
   * @return
   *   The name of the default branch or empty if there is none.
   * @todo Remove this in the next major version. default branch is now a
   * public data member.
   *
   * @return mixed
   *   The name of the default branch or NULL if there is none.
   */
  public function getDefaultBranch() {
    return $this->default_branch;
    return $this->defaultBranch;
  }

  /**
+3 −3
Changes for includes/plugins/repomgr/VersioncontrolGitRepositoryManagerWorkerDefault.class.php: 3 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -93,7 +93,7 @@ class VersioncontrolGitRepositoryManagerWorkerDefault implements VersioncontrolG

  public function setDefaultBranch($branch_name) {
    $this->passthru('symbolic-ref --quiet HEAD ' . escapeshellarg('refs/heads/' . $branch_name), TRUE);
    $this->repository->default_branch = $branch_name;
    $this->repository->defaultBranch = $branch_name;
  }

  public function fetchDefaultBranch() {
@@ -144,8 +144,8 @@ class VersioncontrolGitRepositoryManagerWorkerDefault implements VersioncontrolG
    }

    // Set it on the repository object and return it.
    $this->repository->default_branch = $match[1];
    return $this->repository->default_branch;
    $this->repository->defaultBranch = $match[1];
    return $this->repository->defaultBranch;
  }

  public function passthru($command, $exception = FALSE) {
+23 −0
Changes for includes/plugins/reposync/VersioncontrolGitRepositoryHistorySynchronizerDefault.class.php: 23 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -88,6 +88,12 @@ class VersioncontrolGitRepositoryHistorySynchronizerDefault implements Versionco
      $branch->delete();
    }

    // Set the default branch if needed.
    if (!$this->repository->defaultBranch) {
      $this->repository->defaultBranch = $this->getDefaultBranch();
      $this->repository->save(array('nested' => FALSE));
    }

    // 2. Process commits

    // Fetch commits from the repo and load them from the db.
@@ -681,4 +687,21 @@ class VersioncontrolGitRepositoryHistorySynchronizerDefault implements Versionco
    }
    return TRUE;
  }

  protected function getDefaultBranch() {
    $exec = 'symbolic-ref --quiet HEAD';
    $logs = $this->execute($exec);
    $default_branch = next($logs);

    if (empty($default_branch)) {
      return NULL;
    }

    // Stdout should be refs/heads/<branchname>.
    if (!preg_match('/^refs\/heads\/(.*)$/', $default_branch, $match)) {
      return NULL;
    }

    return $match[1];
  }
}
+15 −1
Changes for versioncontrol_git.install: 15 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -210,7 +210,7 @@ function versioncontrol_git_schema() {
        'description' => 'The default (HEAD) branch of the git repository.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'not null' => FALSE,
        'default' => '',
      ),
    ),
@@ -553,3 +553,17 @@ function versioncontrol_git_update_6209() {
  db_add_primary_key('versioncontrol_git_event_data', array('elid', 'refname', 'reftype'));
  return t('Added reftype as part of the {versioncontrol_git_event_data} PK.');
}

/**
 * Allows {versioncontrol_git_repositories}.default_branch to be NULL.
 */
function versioncontrol_git_update_7100() {
  $default_branch_shema = array(
    'description' => 'The default (HEAD) branch of the git repository.',
    'type' => 'varchar',
    'length' => 255,
    'not null' => FALSE,
    'default' => '',
  );
  db_change_field('versioncontrol_git_repositories', 'default_branch', 'default_branch', $default_branch_shema);
}