From 0cfad1e418017d8c4982214830332752396c2db6 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Johan=20Bj=C3=B6rk?= <phb@spotify.com>
Date: Sun, 20 Feb 2011 17:26:12 +0100
Subject: [PATCH] CTest+git: Use --recursive with gitsubmodule support. Bail if there are submodules present and the users git version is <1.6.5.0

---
 Source/CTest/cmCTestGIT.cxx |   37 ++++++++++++++++++++++++++++++++++++-
 Source/CTest/cmCTestGIT.h   |    4 ++++
 2 files changed, 40 insertions(+), 1 deletions(-)

diff --git a/Source/CTest/cmCTestGIT.cxx b/Source/CTest/cmCTestGIT.cxx
index aa9e55b..e783ef2 100644
--- a/Source/CTest/cmCTestGIT.cxx
+++ b/Source/CTest/cmCTestGIT.cxx
@@ -28,6 +28,7 @@ cmCTestGIT::cmCTestGIT(cmCTest* ct, std::ostream& log):
   cmCTestGlobalVC(ct, log)
 {
   this->PriorRev = this->Unknown;
+  this->CurrentGitVersion = 0;
 }
 
 //----------------------------------------------------------------------------
@@ -262,8 +263,22 @@ bool cmCTestGIT::UpdateImpl()
     }
 
   std::string top_dir = this->FindTopDir();
+  const char* recursive = "--recursive";
   const char* git = this->CommandLineTool.c_str();
-  char const* git_submodule[] = {git, "submodule", "update", 0};
+
+  if(!this->CurrentGitVersion) {
+    this->UpdateGitVersion();
+  }
+  //Git <1.6.5.0 did not support --recursive
+  if(this->CurrentGitVersion < this->GitVersion(1,6,5,0)) {
+    recursive = NULL;
+    //No need to require >1.6.5.0 if the user is not using submodules.
+    if(cmSystemTools::FileExists((top_dir + "/.gitmodules").c_str())) {
+      this->Log << "submodule-err> Git version >= 1.6.5.0 required for git-submodule support.";
+      return false;
+    }
+  }
+  char const* git_submodule[] = {git, "submodule", "update", recursive, 0};
   OutputLogger submodule_out(this->Log, "submodule-out> ");
   OutputLogger submodule_err(this->Log, "submodule-err> ");
   return this->RunChild(git_submodule, &submodule_out, &submodule_err,
@@ -271,6 +286,26 @@ bool cmCTestGIT::UpdateImpl()
 }
 
 //----------------------------------------------------------------------------
+bool cmCTestGIT::UpdateGitVersion()
+{
+  std::string version;
+  std::string git = this->CommandLineTool + " --version";
+  cmsys::RegularExpression re_ver("git version ([1-9]+)\\.([1-9]+)\\.([1-9]+)\\.([1-9]+)");
+  if(!cmSystemTools::RunSingleCommand(git.c_str(), &version,0,0,false))
+    return false;
+  if(re_ver.find(version))
+    {
+    this->CurrentGitVersion = this->GitVersion(
+      atoi(re_ver.match(1).c_str()),
+      atoi(re_ver.match(2).c_str()),
+      atoi(re_ver.match(3).c_str()),
+      atoi(re_ver.match(4).c_str())
+    );
+    return true;
+    }
+  return false;
+}
+//----------------------------------------------------------------------------
 /* Diff format:
 
    :src-mode dst-mode src-sha1 dst-sha1 status\0
diff --git a/Source/CTest/cmCTestGIT.h b/Source/CTest/cmCTestGIT.h
index 1765340..734790c 100644
--- a/Source/CTest/cmCTestGIT.h
+++ b/Source/CTest/cmCTestGIT.h
@@ -27,6 +27,10 @@ public:
   virtual ~cmCTestGIT();
 
 private:
+  //1.6.5.0 maps to 10605000
+  int GitVersion(int epic,int major,int minor,int fix ) { return fix+minor*1000 + major*100000 + epic * 10000000; };
+  int CurrentGitVersion;
+  bool UpdateGitVersion();
   std::string GetWorkingRevision();
   virtual void NoteOldRevision();
   virtual void NoteNewRevision();
-- 
1.7.2.1

