From 8d949c75edf1e0990ef9c52e833558546e43f6fb Mon Sep 17 00:00:00 2001
From: Marco Villegas <git@marvil07.net>
Date: Wed, 1 May 2013 03:08:17 -0500
Subject: [PATCH] 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.
---
 includes/VersioncontrolGitRepository.php           |   27 +++++++++++++++-----
 ...trolGitRepositoryManagerWorkerDefault.class.php |    6 ++---
 ...tRepositoryHistorySynchronizerDefault.class.php |   23 +++++++++++++++++
 versioncontrol_git.install                         |   16 +++++++++++-
 4 files changed, 61 insertions(+), 11 deletions(-)

diff --git a/includes/VersioncontrolGitRepository.php b/includes/VersioncontrolGitRepository.php
index 2e9f58b..20f11ef 100644
--- a/includes/VersioncontrolGitRepository.php
+++ b/includes/VersioncontrolGitRepository.php
@@ -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;
   }
 
   /**
diff --git a/includes/plugins/repomgr/VersioncontrolGitRepositoryManagerWorkerDefault.class.php b/includes/plugins/repomgr/VersioncontrolGitRepositoryManagerWorkerDefault.class.php
index 8cb450d..442750e 100644
--- a/includes/plugins/repomgr/VersioncontrolGitRepositoryManagerWorkerDefault.class.php
+++ b/includes/plugins/repomgr/VersioncontrolGitRepositoryManagerWorkerDefault.class.php
@@ -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) {
diff --git a/includes/plugins/reposync/VersioncontrolGitRepositoryHistorySynchronizerDefault.class.php b/includes/plugins/reposync/VersioncontrolGitRepositoryHistorySynchronizerDefault.class.php
index 32f8fce..b0c747e 100644
--- a/includes/plugins/reposync/VersioncontrolGitRepositoryHistorySynchronizerDefault.class.php
+++ b/includes/plugins/reposync/VersioncontrolGitRepositoryHistorySynchronizerDefault.class.php
@@ -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];
+  }
 }
diff --git a/versioncontrol_git.install b/versioncontrol_git.install
index 029243d..981c3a1 100644
--- a/versioncontrol_git.install
+++ b/versioncontrol_git.install
@@ -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);
+}
-- 
1.7.10.4

