From a94af2014a6679e12d21a52c36f12ef2c2af45b7 Mon Sep 17 00:00:00 2001
From: Marco Villegas <marvil07@gmail.com>
Date: Tue, 23 Nov 2010 14:08:16 -0500
Subject: [PATCH] various

- Process commits before tags.
  A tag in git in a reference to a commit, so we should process new commits
  to before to make sure we can process the tags correctly.

- Remove no more used _versioncontrol_git_log_get_commit() function.

- Add type to commit operation objects.

- Pass parameter to VersioncontrolGitBackend constructor as it parent.

- Pass --reverse on commit fetching to process commits in a more natural way.
---
 includes/VersioncontrolGitBackend.php    |    4 +-
 includes/VersioncontrolGitRepository.php |    2 +-
 versioncontrol_git.log.inc               |   53 +++++++++++-------------------
 3 files changed, 22 insertions(+), 37 deletions(-)

diff --git includes/VersioncontrolGitBackend.php includes/VersioncontrolGitBackend.php
index bc67004..d1d9978 100644
--- includes/VersioncontrolGitBackend.php
+++ includes/VersioncontrolGitBackend.php
@@ -9,8 +9,8 @@ class VersioncontrolGitBackend extends VersioncontrolBackend {
       'item' => 'VersioncontrolGitItem',
     );
 
-  public function __construct() {
-    parent::__construct();
+  public function __construct($backend = NULL) {
+    parent::__construct($backend);
     $this->name = 'Git';
     $this->description = t('Git is a fast, scalable, distributed revision control system with an unusually rich command set that provides both high-level operations and full access to internals.');
     $this->capabilities = array(
diff --git includes/VersioncontrolGitRepository.php includes/VersioncontrolGitRepository.php
index 6fdc584..898e8a8 100644
--- includes/VersioncontrolGitRepository.php
+++ includes/VersioncontrolGitRepository.php
@@ -98,7 +98,7 @@ class VersioncontrolGitRepository extends VersioncontrolRepository {
   }
 
   public function fetchCommits($branch_name = NULL) {
-    $logs = $this->exec('git rev-list ' . (empty($branch_name) ? '--all' : $branch_name));
+    $logs = $this->exec('git rev-list --reverse ' . (empty($branch_name) ? '--all' : $branch_name));
     $commits = array();
     while (($line = next($logs)) !== FALSE) {
       $commits[] = trim($line);
diff --git versioncontrol_git.log.inc versioncontrol_git.log.inc
index 78678af..9d23972 100644
--- versioncontrol_git.log.inc
+++ versioncontrol_git.log.inc
@@ -69,24 +69,7 @@ function _versioncontrol_git_log_update_repository(VersioncontrolGitRepository &
     $branch->delete();
   }
 
-  // 2. Process tags
-
-  // Insert new tags in the database.
-  $tags_in_repo = $repository->fetchTags();
-  $tags_in_db = $repository->loadTags();
-  $tags_in_db_by_name = array();
-  foreach ($tags_in_db as $tag) {
-    $tags_in_db_by_name[$tag->name] = $tag;
-  }
-  unset($tags_in_db);
-  // Deleting tags is *not* supported. Read the manual if you want to know why...
-  // Check for new tags.
-  $tags_new = array_diff_key($tags_in_repo, $tags_in_db_by_name);
-  if (!empty($tags_new)) {
-    _versioncontrol_git_log_process_tags($repository, $tags_new);
-  }
-
-  // 3. Process commits
+  // 2. Process commits
 
   // Fetch commits from the repo and load them from the db.
   $commits_in_repo_hashes = $repository->fetchCommits();
@@ -112,6 +95,23 @@ function _versioncontrol_git_log_update_repository(VersioncontrolGitRepository &
     _versioncontrol_git_log_attach_branch_to_commits($branches_in_db_by_name[$branch_new->name], $commits);
   }
 
+  // 3. Process tags
+
+  // Insert new tags in the database.
+  $tags_in_repo = $repository->fetchTags();
+  $tags_in_db = $repository->loadTags();
+  $tags_in_db_by_name = array();
+  foreach ($tags_in_db as $tag) {
+    $tags_in_db_by_name[$tag->name] = $tag;
+  }
+  unset($tags_in_db);
+  // Deleting tags is *not* supported. Read the manual if you want to know why...
+  // Check for new tags.
+  $tags_new = array_diff_key($tags_in_repo, $tags_in_db_by_name);
+  if (!empty($tags_new)) {
+    _versioncontrol_git_log_process_tags($repository, $tags_new);
+  }
+
   // Update repository updated field. Displayed on administration interface for documentation purposes.
   $repository->updated = time();
   $repository->locked = 0;
@@ -137,22 +137,6 @@ function _versioncontrol_git_log_exec($command) {
 /// All commit related function
 
 /**
- * Return one commit op.
- * @param VersioncontrolRepository $repository
- * @param string $revision
- * @return VersioncontrolOperation
- */
-//function _versioncontrol_git_log_get_commit($repository, $revision) {
-//  $constraints = array(
-//    'repo_ids' => array($repository->repo_id),
-//    'types' => array(VERSIONCONTROL_OPERATION_COMMIT),
-//    'revisions' => array($revision),
-//  );
-//  $commit_op = VersioncontrolOperationCache::getInstance()->getOperations($constraints);
-//  return array_pop($commit_op);
-//}
-
-/**
  * Returns an array of all branches a given commit is in.
  * @param string $revision
  * @param array $branch_label_list
@@ -348,6 +332,7 @@ function _versioncontrol_git_log_parse_and_insert_commit($repository, $logs, $br
 
   // rebuild the data array to be used for the commit op
   $data = new stdClass();
+  $data->type = VERSIONCONTROL_OPERATION_COMMIT;
   $data->revision = $revision;
   $data->author = $author;
   $data->committer = $committer;
-- 
1.7.2.3

