From 3dbc398ee85fb2bc409345fca8ca54eb35f10759 Mon Sep 17 00:00:00 2001
From: Marco Villegas <marvil07@gmail.com>
Date: Tue, 7 Dec 2010 17:19:04 -0500
Subject: [PATCH] bug #486636: Detect if there is no git repo.

The validation is done at two places: at fetch logs and at repository
creation.
---
 includes/VersioncontrolGitRepository.php |   17 +++++++++++++++++
 versioncontrol_git.admin.inc             |   13 +++++++++++++
 versioncontrol_git.log.inc               |    6 +++++-
 3 files changed, 35 insertions(+), 1 deletions(-)

diff --git includes/VersioncontrolGitRepository.php includes/VersioncontrolGitRepository.php
index 7c84305..4d57cdb 100644
--- includes/VersioncontrolGitRepository.php
+++ includes/VersioncontrolGitRepository.php
@@ -111,4 +111,21 @@ class VersioncontrolGitRepository extends VersioncontrolRepository {
     reset($logs); // Reset the array pointer, so that we can use next().
     return $logs;
   }
+
+  /**
+   * Verify if the repository root points to a valid Git repository.
+   *
+   * @return boolean
+   *   TRUE for valid, FALSE for invalid
+   */
+  public function isValidGitRepo() {
+    // do not use exec() method to get the shell return code
+    if (!$this->envSet) {
+      $this->setEnv();
+    }
+    $logs = array();
+    exec('git ls-files', $logs, $shell_return);
+    return $shell_return == 0;
+  }
+
 }
diff --git versioncontrol_git.admin.inc versioncontrol_git.admin.inc
index 13aac1b..7b4ff86 100644
--- versioncontrol_git.admin.inc
+++ versioncontrol_git.admin.inc
@@ -43,6 +43,19 @@ function versioncontrol_git_repository_admin_form_alter(&$form, $form_state, $fo
     ),
   );
   $form['repository_information']['root']['#description'] = t('An absolute path to your bare repository.');
+  $form['#validate'][] = 'versioncontrol_git_repository_validate';
+}
+
+/**
+ * Validation of the repository root.
+ */
+function versioncontrol_git_repository_validate($form, &$form_state) {
+  // create a non-in-db-repo just for the validation
+  $repo = new VersioncontrolGitRepository(new VersioncontrolGitBackend());
+  $repo->root = $form_state['values']['root'];
+  if (!$repo->isValidGitRepo()) {
+    form_set_error('root', t('The specified path is not a valid Git bare repository.'));
+  }
 }
 
 /**
diff --git versioncontrol_git.log.inc versioncontrol_git.log.inc
index 1c45f61..43276fb 100644
--- versioncontrol_git.log.inc
+++ versioncontrol_git.log.inc
@@ -22,9 +22,13 @@
  * done
  */
 function _versioncontrol_git_log_update_repository(VersioncontrolGitRepository &$repository) {
+  if (!$repository->isValidGitRepo()) {
+    drupal_set_message(t('The repository %name at <code>@root</code> is not a valid Git bare repository.', array('%name' => $repository->name, '@root' => $repository->root)), 'error');
+    return FALSE;
+  }
+
   $root = escapeshellcmd($repository->root);
   putenv("GIT_DIR=$root");
-
   if ($repository->locked == TRUE) {
     drupal_set_message(t('This repository is locked, there is already a fetch in progress. If this is not the case, press the clear lock button.'), 'error');
     return FALSE;
-- 
1.7.2.3

