From b7f45cc9daac70c8d6d57806007226c31c4f48af Mon Sep 17 00:00:00 2001
From: Pedro Navarro <pnavarro@netflix.com>
Date: Tue, 1 Oct 2013 18:49:47 -0700
Subject: [PATCH] Added support for Perforce

---
 Modules/CTest.cmake                   |    5 +
 Modules/DartConfiguration.tcl.in      |    7 +
 Source/CMakeLists.txt                 |    2 +
 Source/CTest/cmCTestP4.cxx            |  493 +++++++++++++++++++++++++++++++++
 Source/CTest/cmCTestP4.h              |   71 +++++
 Source/CTest/cmCTestUpdateCommand.cxx |    4 +
 Source/CTest/cmCTestUpdateHandler.cxx |   26 +-
 Source/CTest/cmCTestUpdateHandler.h   |    1 +
 8 files changed, 608 insertions(+), 1 deletion(-)
 create mode 100644 Source/CTest/cmCTestP4.cxx
 create mode 100644 Source/CTest/cmCTestP4.h

diff --git a/Modules/CTest.cmake b/Modules/CTest.cmake
index 5cd62f6..21a5b29 100644
--- a/Modules/CTest.cmake
+++ b/Modules/CTest.cmake
@@ -130,6 +130,7 @@ if(BUILD_TESTING)
   find_program(BZRCOMMAND bzr)
   find_program(HGCOMMAND hg)
   find_program(GITCOMMAND git)
+  find_program(P4COMMAND p4)
 
   if(NOT UPDATE_TYPE)
     if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/CVS")
@@ -161,6 +162,9 @@ if(BUILD_TESTING)
   elseif("${_update_type}" STREQUAL "git")
     set(UPDATE_COMMAND "${GITCOMMAND}")
     set(UPDATE_OPTIONS "${GIT_UPDATE_OPTIONS}")
+  elseif("${_update_type}" STREQUAL "p4")
+    set(UPDATE_COMMAND "${P4COMMAND}")
+    set(UPDATE_OPTIONS "${P4_UPDATE_OPTIONS}")
   endif()
 
   set(DART_TESTING_TIMEOUT 1500 CACHE STRING
@@ -256,6 +260,7 @@ if(BUILD_TESTING)
     CVS_UPDATE_OPTIONS
     DART_TESTING_TIMEOUT
     GITCOMMAND
+    P4COMMAND
     HGCOMMAND
     MAKECOMMAND
     MEMORYCHECK_COMMAND
diff --git a/Modules/DartConfiguration.tcl.in b/Modules/DartConfiguration.tcl.in
index 9e49ac7..f33e82d 100644
--- a/Modules/DartConfiguration.tcl.in
+++ b/Modules/DartConfiguration.tcl.in
@@ -52,6 +52,13 @@ GITCommand: @GITCOMMAND@
 GITUpdateOptions: @GIT_UPDATE_OPTIONS@
 GITUpdateCustom: @CTEST_GIT_UPDATE_CUSTOM@
 
+# Perforce options
+P4Command: @P4COMMAND@
+P4Client: @P4_CLIENT@
+P4Options: @P4_OPTIONS@
+P4UpdateOptions: @P4_UPDATE_OPTIONS@
+P4UpdateCustom: @CTEST_P4_UPDATE_CUSTOM@
+
 # Generic update command
 UpdateCommand: @UPDATE_COMMAND@
 UpdateOptions: @UPDATE_OPTIONS@
diff --git a/Source/CMakeLists.txt b/Source/CMakeLists.txt
index 8412e3e..b4fd883 100644
--- a/Source/CMakeLists.txt
+++ b/Source/CMakeLists.txt
@@ -475,6 +475,8 @@ set(CTEST_SRCS cmCTest.cxx
   CTest/cmCTestGIT.h
   CTest/cmCTestHG.cxx
   CTest/cmCTestHG.h
+  CTest/cmCTestP4.cxx
+  CTest/cmCTestP4.h
   )
 
 # Build CTestLib
diff --git a/Source/CTest/cmCTestP4.cxx b/Source/CTest/cmCTestP4.cxx
new file mode 100644
index 0000000..2b1764a
--- /dev/null
+++ b/Source/CTest/cmCTestP4.cxx
@@ -0,0 +1,493 @@
+/*============================================================================
+  CMake - Cross Platform Makefile Generator
+  Copyright 2000-2013 Kitware, Inc.
+
+  Distributed under the OSI-approved BSD License (the "License");
+  see accompanying file Copyright.txt for details.
+
+  This software is distributed WITHOUT ANY WARRANTY; without even the
+  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+  See the License for more information.
+============================================================================*/
+#include "cmCTestP4.h"
+
+#include "cmCTest.h"
+#include "cmSystemTools.h"
+#include "cmXMLSafe.h"
+
+#include <cmsys/RegularExpression.hxx>
+#include <cmsys/ios/sstream>
+#include <cmsys/Process.h>
+
+#include <sys/types.h>
+#include <time.h>
+#include <ctype.h>
+
+//----------------------------------------------------------------------------
+cmCTestP4::cmCTestP4(cmCTest* ct, std::ostream& log):
+  cmCTestGlobalVC(ct, log)
+{
+  this->PriorRev = this->Unknown;
+}
+
+//----------------------------------------------------------------------------
+cmCTestP4::~cmCTestP4()
+{
+}
+
+//----------------------------------------------------------------------------
+class cmCTestP4::IdentifyParser: public cmCTestVC::LineParser
+{
+public:
+  IdentifyParser(cmCTestP4* p4, const char* prefix,
+                 std::string& rev): Rev(rev)
+    {
+    this->SetLog(&p4->Log, prefix);
+    this->RegexIdentify.compile("^Change ([0-9]+) on");
+    }
+private:
+  std::string& Rev;
+  cmsys::RegularExpression RegexIdentify;
+
+  bool ProcessLine()
+    {
+    if(this->RegexIdentify.find(this->Line))
+      {
+      this->Rev = this->RegexIdentify.match(1);
+      return false;
+      }
+    return true;
+    }
+};
+
+//----------------------------------------------------------------------------
+class cmCTestP4::ChangesParser: public cmCTestVC::LineParser
+{
+public:
+  ChangesParser(cmCTestP4* p4, const char* prefix) : P4(p4)
+    {
+    this->SetLog(&P4->Log, prefix);
+    this->RegexIdentify.compile("^Change ([0-9]+) on");
+    }
+private:
+  cmsys::RegularExpression RegexIdentify;
+  cmCTestP4* P4;
+
+  bool ProcessLine()
+    {
+    if(this->RegexIdentify.find(this->Line))
+      {
+      P4->ChangeLists.push_back(this->RegexIdentify.match(1));
+      }
+    return true;
+    }
+};
+
+//----------------------------------------------------------------------------
+class cmCTestP4::UserParser: public cmCTestVC::LineParser
+{
+public:
+  UserParser(cmCTestP4* p4, const char* prefix) : P4(p4)
+    {
+    this->SetLog(&P4->Log, prefix);
+    this->RegexUser.compile("^(.+) <(.*)> \\((.*)\\) accessed (.*)$");
+    }
+private:
+  cmsys::RegularExpression RegexUser;
+  cmCTestP4* P4;
+
+  bool ProcessLine()
+    {
+    if(this->RegexUser.find(this->Line))
+      {
+      User NewUser;
+
+      NewUser.UserName = this->RegexUser.match(1);
+      NewUser.EMail = this->RegexUser.match(2);
+      NewUser.Name = this->RegexUser.match(3);
+      NewUser.AccessTime = this->RegexUser.match(4);
+      P4->Users[this->RegexUser.match(1)] = NewUser;
+
+      return false;
+      }
+    return true;
+    }
+};
+
+//----------------------------------------------------------------------------
+class cmCTestP4::DiffParser: public cmCTestVC::LineParser
+{
+public:
+  DiffParser(cmCTestP4* p4, const char* prefix) : P4(p4)
+    {
+    this->SetLog(&P4->Log, prefix);
+    }
+private:
+  cmCTestP4* P4;
+
+  bool ProcessLine()
+    {
+    if(!this->Line.empty())
+      {
+        P4->DoModification(PathModified, Line);
+      }
+    return true;
+    }
+};
+
+//----------------------------------------------------------------------------
+cmCTestP4::User cmCTestP4::GetUserData(const std::string& username)
+{
+  std::map<std::string, cmCTestP4::User>::const_iterator it =
+          Users.find(username);
+
+  if(it == Users.end())
+    {
+    std::vector<char const*> p4_users;
+    SetP4Options(p4_users);
+    p4_users.push_back("users");
+    p4_users.push_back("-m");
+    p4_users.push_back("1");
+    p4_users.push_back(username.c_str());
+    p4_users.push_back(0);
+
+    UserParser out(this, "users-out> ");
+    OutputLogger err(this->Log, "users-err> ");
+    RunChild(&p4_users[0], &out, &err);
+
+    // The user should now be added to the map. Search again.
+    it = Users.find(username);
+    if(it == Users.end())
+      {
+      return cmCTestP4::User();
+      }
+    }
+
+  return it->second;
+}
+
+//----------------------------------------------------------------------------
+/* Commit format:
+
+Change 1111111 by user@client on 2013/09/26 11:50:36
+
+        text
+        text
+
+Affected files ...
+
+... //path/to/file#rev edit
+... //path/to/file#rev add
+... //path/to/file#rev delete
+... //path/to/file#rev integrate
+*/
+class cmCTestP4::DescribeParser: public cmCTestVC::LineParser
+{
+public:
+  DescribeParser(cmCTestP4* p4, const char* prefix):
+      LineParser('\n', false), P4(p4), Section(SectionHeader)
+    {
+    this->SetLog(&P4->Log, prefix);
+    this->RegexHeader.compile("^Change ([0-9]+) by (.+)@(.+) on (.*)$");
+    this->RegexDiff.compile("^\\.\\.\\. (.*)#[0-9]+ ([^ ]+)$");
+    }
+private:
+  cmsys::RegularExpression RegexHeader;
+  cmsys::RegularExpression RegexDiff;
+  cmCTestP4* P4;
+
+  typedef cmCTestP4::Revision Revision;
+  typedef cmCTestP4::Change Change;
+  std::vector<Change> Changes;
+  enum SectionType { SectionHeader, SectionBody, SectionDiffHeader,
+                     SectionDiff, SectionCount };
+  SectionType Section;
+  Revision Rev;
+
+  virtual bool ProcessLine()
+    {
+    if(this->Line.empty())
+      {
+      this->NextSection();
+      }
+    else
+      {
+      switch(this->Section)
+        {
+        case SectionHeader:     this->DoHeaderLine(); break;
+        case SectionBody:       this->DoBodyLine(); break;
+        case SectionDiffHeader: break; // nothing to do
+        case SectionDiff:       this->DoDiffLine(); break;
+        case SectionCount:      break; // never happens
+        }
+      }
+      return true;
+      }
+
+  void NextSection()
+    {
+    if(this->Section == SectionDiff)
+    {
+      this->P4->DoRevision(this->Rev, this->Changes);
+      this->Rev = Revision();
+    }
+
+    this->Section = SectionType((this->Section+1) % SectionCount);
+    }
+
+  void DoHeaderLine()
+    {
+    if(this->RegexHeader.find(this->Line))
+      {
+      this->Rev.Rev = this->RegexHeader.match(1);
+      this->Rev.Date = this->RegexHeader.match(4);
+
+      cmCTestP4::User user = P4->GetUserData(this->RegexHeader.match(2));
+      this->Rev.Author = user.Name;
+      this->Rev.EMail = user.EMail;
+
+      this->Rev.Committer = this->Rev.Author;
+      this->Rev.CommitterEMail = this->Rev.EMail;
+      this->Rev.CommitDate = this->Rev.Date;
+      }
+    }
+
+  void DoBodyLine()
+    {
+    if(this->Line[0] == '\t')
+      {
+      this->Rev.Log += this->Line.substr(1);
+      }
+    this->Rev.Log += "\n";
+    }
+
+  void DoDiffLine()
+    {
+    if(this->RegexDiff.find(this->Line))
+    {
+    Change change;
+    change.Path = this->RegexDiff.match(1);
+    std::string action = this->RegexDiff.match(2);
+
+    if(action == "add")
+      {
+      change.Action = 'A';
+      }
+    else if(action == "delete")
+      {
+      change.Action = 'D';
+      }
+    else if(action == "edit" || action == "integrate")
+      {
+      change.Action = 'M';
+      }
+
+    Changes.push_back(change);
+    }
+  }
+};
+
+//----------------------------------------------------------------------------
+void cmCTestP4::SetP4Options(std::vector<char const*> &options)
+{
+  if(Options.size() == 0)
+    {
+    const char* p4 = this->CommandLineTool.c_str();
+    Options.push_back(p4);
+
+    //The P4_CLIENT variable sets the P4 client used when issuing Perforce
+    //commands, if it's different from the default one.
+    std::string client = this->CTest->GetCTestConfiguration("P4Client");
+    if(!client.empty())
+      {
+      Options.push_back("-c");
+      Options.push_back(client);
+      }
+
+    //The P4_OPTIONS variable adds additional Perforce command line options
+    //before the main command
+    std::string opts = this->CTest->GetCTestConfiguration("P4Options");
+    std::vector<cmStdString> args =
+            cmSystemTools::ParseArguments(opts.c_str());
+
+    for(std::vector<cmStdString>::const_iterator ai = args.begin();
+        ai != args.end(); ++ai)
+      {
+      Options.push_back(ai->c_str());
+      }
+    }
+
+  options.clear();
+  std::vector<std::string>::size_type i;
+
+  for(i=0; i<Options.size(); i++)
+    {
+    options.push_back(Options[i].c_str());
+    }
+}
+
+//----------------------------------------------------------------------------
+std::string cmCTestP4::GetWorkingRevision()
+{
+  std::vector<char const*> p4_identify;
+  SetP4Options(p4_identify);
+
+  p4_identify.push_back("changes");
+  p4_identify.push_back("-m");
+  p4_identify.push_back("1");
+  p4_identify.push_back("-t");
+
+  std::string source = this->SourceDirectory + "/...#have";
+  p4_identify.push_back(source.c_str());
+  p4_identify.push_back(0);
+
+  std::string rev;
+  IdentifyParser out(this, "rev-out> ", rev);
+  OutputLogger err(this->Log, "rev-err> ");
+
+  RunChild(&p4_identify[0], &out, &err);
+
+  return rev;
+}
+
+//----------------------------------------------------------------------------
+void cmCTestP4::NoteOldRevision()
+{
+  this->OldRevision = this->GetWorkingRevision();
+  cmCTestLog(this->CTest, HANDLER_OUTPUT, "   Old revision of repository is: "
+             << this->OldRevision << "\n");
+  this->PriorRev.Rev = this->OldRevision;
+}
+
+//----------------------------------------------------------------------------
+void cmCTestP4::NoteNewRevision()
+{
+  this->NewRevision = this->GetWorkingRevision();
+  cmCTestLog(this->CTest, HANDLER_OUTPUT, "   New revision of repository is: "
+             << this->NewRevision << "\n");
+}
+
+//----------------------------------------------------------------------------
+void cmCTestP4::LoadRevisions()
+{
+  std::vector<char const*> p4_changes;
+  SetP4Options(p4_changes);
+
+  // Use 'p4 changes ...@old,new' to get a list of changelists
+  std::string range = this->SourceDirectory + "/...@" + this->OldRevision
+                      + "," + this->NewRevision;
+  p4_changes.push_back("changes");
+  p4_changes.push_back(range.c_str());
+  p4_changes.push_back(0);
+
+  ChangesParser out(this, "changes-out> ");
+  OutputLogger err(this->Log, "changes-err> ");
+
+  ChangeLists.clear();
+  this->RunChild(&p4_changes[0], &out, &err);
+
+  if(ChangeLists.size() == 0)
+      return;
+
+  //p4 describe -s ...@1111111,2222222
+  std::vector<char const*> p4_describe;
+  for(int i=ChangeLists.size()-1; i >= 0; i--)
+    {
+    SetP4Options(p4_describe);
+    p4_describe.push_back("describe");
+    p4_describe.push_back("-s");
+    p4_describe.push_back(ChangeLists[i].c_str());
+    p4_describe.push_back(0);
+
+    DescribeParser outDescribe(this, "describe-out> ");
+    OutputLogger errDescribe(this->Log, "describe-err> ");
+    this->RunChild(&p4_describe[0], &outDescribe, &errDescribe);
+    }
+}
+
+//----------------------------------------------------------------------------
+void cmCTestP4::LoadModifications()
+{
+  std::vector<char const*> p4_diff;
+  SetP4Options(p4_diff);
+
+  p4_diff.push_back("diff");
+
+  p4_diff.push_back("-sa");
+  std::string source = this->SourceDirectory + "/...";
+  p4_diff.push_back(source.c_str());
+  p4_diff.push_back(0);
+
+  DiffParser out(this, "diff-out> ");
+  OutputLogger err(this->Log, "diff-err> ");
+  this->RunChild(&p4_diff[0], &out, &err);
+}
+
+//----------------------------------------------------------------------------
+bool cmCTestP4::UpdateCustom(const std::string& custom)
+{
+  std::vector<std::string> p4_custom_command;
+  cmSystemTools::ExpandListArgument(custom, p4_custom_command, true);
+
+  std::vector<char const*> p4_custom;
+  for(std::vector<std::string>::const_iterator
+        i = p4_custom_command.begin(); i != p4_custom_command.end(); ++i)
+    {
+    p4_custom.push_back(i->c_str());
+    }
+  p4_custom.push_back(0);
+
+  OutputLogger custom_out(this->Log, "custom-out> ");
+  OutputLogger custom_err(this->Log, "custom-err> ");
+
+  return this->RunUpdateCommand(&p4_custom[0], &custom_out, &custom_err);
+}
+
+//----------------------------------------------------------------------------
+bool cmCTestP4::UpdateImpl()
+{
+  std::string custom = this->CTest->GetCTestConfiguration("P4UpdateCustom");
+  if(!custom.empty())
+    {
+    return this->UpdateCustom(custom);
+    }
+
+  std::vector<char const*> p4_sync;
+  SetP4Options(p4_sync);
+
+  p4_sync.push_back("sync");
+
+  // Get user-specified update options.
+  std::string opts = this->CTest->GetCTestConfiguration("UpdateOptions");
+  if(opts.empty())
+    {
+    opts = this->CTest->GetCTestConfiguration("P4UpdateOptions");
+    }
+  std::vector<cmStdString> args = cmSystemTools::ParseArguments(opts.c_str());
+  for(std::vector<cmStdString>::const_iterator ai = args.begin();
+      ai != args.end(); ++ai)
+    {
+    p4_sync.push_back(ai->c_str());
+    }
+
+  std::string source = this->SourceDirectory + "/...";
+
+  // Specify the start time for nightly testing.
+  if(this->CTest->GetTestModel() == cmCTest::NIGHTLY)
+    {
+    std::string date = this->GetNightlyTime();
+    //CTest reports the date as YYYY-MM-DD, Perforce needs it as YYYY/MM/DD
+    std::replace(date.begin(), date.end(), '-', '/');
+
+    //Revision specification: /...@"YYYY/MM/DD HH:MM:SS"
+    source.append("@\"").append(date).append("\"");
+    }
+
+  p4_sync.push_back(source.c_str());
+  p4_sync.push_back(0);
+
+  OutputLogger out(this->Log, "sync-out> ");
+  OutputLogger err(this->Log, "sync-err> ");
+
+  return this->RunChild(&p4_sync[0], &out, &err);
+}
diff --git a/Source/CTest/cmCTestP4.h b/Source/CTest/cmCTestP4.h
new file mode 100644
index 0000000..2e69c2e
--- /dev/null
+++ b/Source/CTest/cmCTestP4.h
@@ -0,0 +1,71 @@
+/*============================================================================
+  CMake - Cross Platform Makefile Generator
+  Copyright 2000-2013 Kitware, Inc.
+
+  Distributed under the OSI-approved BSD License (the "License");
+  see accompanying file Copyright.txt for details.
+
+  This software is distributed WITHOUT ANY WARRANTY; without even the
+  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+  See the License for more information.
+============================================================================*/
+#ifndef cmCTestP4_h
+#define cmCTestP4_h
+
+#include "cmCTestGlobalVC.h"
+#include <vector>
+#include <map>
+
+/** \class cmCTestP4
+ * \brief Interaction with the Perforce command-line tool
+ *
+ */
+class cmCTestP4: public cmCTestGlobalVC
+{
+public:
+  /** Construct with a CTest instance and update log stream.  */
+  cmCTestP4(cmCTest* ctest, std::ostream& log);
+
+  virtual ~cmCTestP4();
+
+private:
+  std::vector<std::string> ChangeLists;
+
+  struct User
+    {
+    std::string UserName;
+    std::string Name;
+    std::string EMail;
+    std::string AccessTime;
+
+    User(): UserName(), Name(), EMail(), AccessTime() {}
+    };
+  std::map<std::string, User> Users;
+  std::vector<std::string> Options;
+
+  User GetUserData(const std::string& username);
+  void SetP4Options(std::vector<char const*> &options);
+
+  std::string GetWorkingRevision();
+  virtual void NoteOldRevision();
+  virtual void NoteNewRevision();
+  virtual bool UpdateImpl();
+  bool UpdateCustom(const std::string& custom);
+
+  void LoadRevisions();
+  void LoadModifications();
+
+  // Parsing helper classes.
+  class IdentifyParser;
+  class ChangesParser;
+  class UserParser;
+  class DescribeParser;
+  class DiffParser;
+  friend class IdentifyParser;
+  friend class ChangesParser;
+  friend class UserParser;
+  friend class DescribeParser;
+  friend class DiffParser;
+};
+
+#endif
diff --git a/Source/CTest/cmCTestUpdateCommand.cxx b/Source/CTest/cmCTestUpdateCommand.cxx
index 2ca9f6c..fa59da0 100644
--- a/Source/CTest/cmCTestUpdateCommand.cxx
+++ b/Source/CTest/cmCTestUpdateCommand.cxx
@@ -59,6 +59,10 @@ cmCTestGenericHandler* cmCTestUpdateCommand::InitializeHandler()
     "HGCommand", "CTEST_HG_COMMAND");
   this->CTest->SetCTestConfigurationFromCMakeVariable(this->Makefile,
     "HGUpdateOptions", "CTEST_HG_UPDATE_OPTIONS");
+  this->CTest->SetCTestConfigurationFromCMakeVariable(this->Makefile,
+    "P4Command", "CTEST_P4_COMMAND");
+  this->CTest->SetCTestConfigurationFromCMakeVariable(this->Makefile,
+    "P4UpdateOptions", "CTEST_P4_UPDATE_OPTIONS");
 
   cmCTestGenericHandler* handler
     = this->CTest->GetInitializedHandler("update");
diff --git a/Source/CTest/cmCTestUpdateHandler.cxx b/Source/CTest/cmCTestUpdateHandler.cxx
index 9eae3f3..11474ec 100644
--- a/Source/CTest/cmCTestUpdateHandler.cxx
+++ b/Source/CTest/cmCTestUpdateHandler.cxx
@@ -28,6 +28,7 @@
 #include "cmCTestBZR.h"
 #include "cmCTestGIT.h"
 #include "cmCTestHG.h"
+#include "cmCTestP4.h"
 
 #include <cmsys/auto_ptr.hxx>
 
@@ -51,7 +52,8 @@ static const char* cmCTestUpdateHandlerUpdateStrings[] =
   "SVN",
   "BZR",
   "GIT",
-  "HG"
+  "HG",
+  "P4"
 };
 
 static const char* cmCTestUpdateHandlerUpdateToString(int type)
@@ -146,6 +148,10 @@ int cmCTestUpdateHandler::DetermineType(const char* cmd, const char* type)
       {
       return cmCTestUpdateHandler::e_HG;
       }
+    if ( stype.find("p4") != std::string::npos )
+      {
+      return cmCTestUpdateHandler::e_P4;
+      }
     }
   else
     {
@@ -172,6 +178,10 @@ int cmCTestUpdateHandler::DetermineType(const char* cmd, const char* type)
       {
       return cmCTestUpdateHandler::e_HG;
       }
+    if ( stype.find("p4") != std::string::npos )
+      {
+      return cmCTestUpdateHandler::e_P4;
+      }
     }
   return cmCTestUpdateHandler::e_UNKNOWN;
 }
@@ -223,6 +233,7 @@ int cmCTestUpdateHandler::ProcessHandler()
     case e_BZR: vc.reset(new cmCTestBZR(this->CTest, ofs)); break;
     case e_GIT: vc.reset(new cmCTestGIT(this->CTest, ofs)); break;
     case e_HG:  vc.reset(new cmCTestHG(this->CTest, ofs)); break;
+    case e_P4:  vc.reset(new cmCTestP4(this->CTest, ofs)); break;
     default:    vc.reset(new cmCTestVC(this->CTest, ofs));  break;
     }
   vc->SetCommandLineTool(this->UpdateCommand);
@@ -350,6 +361,18 @@ int cmCTestUpdateHandler::DetectVCS(const char* dir)
     {
     return cmCTestUpdateHandler::e_HG;
     }
+  sourceDirectory = dir;
+  sourceDirectory += "/.p4";
+  if ( cmSystemTools::FileExists(sourceDirectory.c_str()) )
+    {
+    return cmCTestUpdateHandler::e_P4;
+    }
+  sourceDirectory = dir;
+  sourceDirectory += "/.p4config";
+  if ( cmSystemTools::FileExists(sourceDirectory.c_str()) )
+    {
+    return cmCTestUpdateHandler::e_P4;
+    }
   return cmCTestUpdateHandler::e_UNKNOWN;
 }
 
@@ -380,6 +403,7 @@ bool cmCTestUpdateHandler::SelectVCS()
       case e_BZR: key = "BZRCommand"; break;
       case e_GIT: key = "GITCommand"; break;
       case e_HG:  key = "HGCommand";  break;
+      case e_P4:  key = "P4Command";  break;
       default: break;
       }
     if (key)
diff --git a/Source/CTest/cmCTestUpdateHandler.h b/Source/CTest/cmCTestUpdateHandler.h
index 55ec974..954c024 100644
--- a/Source/CTest/cmCTestUpdateHandler.h
+++ b/Source/CTest/cmCTestUpdateHandler.h
@@ -44,6 +44,7 @@ public:
     e_BZR,
     e_GIT,
     e_HG,
+    e_P4,
     e_LAST
   };
 
-- 
1.7.10.4

