cmake/Source/CTest/cmCTestUpdateHandler.cxx

355 lines
11 KiB
C++
Raw Normal View History

2016-10-30 18:24:19 +01:00
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
#include "cmCTestUpdateHandler.h"
2020-02-01 23:06:01 +01:00
#include <chrono>
#include <sstream>
#include <cm/memory>
2016-07-09 11:21:54 +02:00
#include "cmCLocaleEnvironmentScope.h"
#include "cmCTest.h"
2009-10-04 10:30:41 +03:00
#include "cmCTestBZR.h"
2016-07-09 11:21:54 +02:00
#include "cmCTestCVS.h"
2009-10-04 10:30:41 +03:00
#include "cmCTestGIT.h"
#include "cmCTestHG.h"
2014-08-03 19:52:23 +02:00
#include "cmCTestP4.h"
2016-07-09 11:21:54 +02:00
#include "cmCTestSVN.h"
#include "cmCTestVC.h"
2016-10-30 18:24:19 +01:00
#include "cmGeneratedFileStream.h"
2020-02-01 23:06:01 +01:00
#include "cmStringAlgorithms.h"
2016-10-30 18:24:19 +01:00
#include "cmSystemTools.h"
2021-11-20 13:41:27 +01:00
#include "cmValue.h"
2016-10-30 18:24:19 +01:00
#include "cmVersion.h"
#include "cmXMLWriter.h"
2009-10-04 10:30:41 +03:00
2016-07-09 11:21:54 +02:00
static const char* cmCTestUpdateHandlerUpdateStrings[] = {
"Unknown", "CVS", "SVN", "BZR", "GIT", "HG", "P4"
};
static const char* cmCTestUpdateHandlerUpdateToString(int type)
{
2016-07-09 11:21:54 +02:00
if (type < cmCTestUpdateHandler::e_UNKNOWN ||
type >= cmCTestUpdateHandler::e_LAST) {
return cmCTestUpdateHandlerUpdateStrings[cmCTestUpdateHandler::e_UNKNOWN];
2016-07-09 11:21:54 +02:00
}
return cmCTestUpdateHandlerUpdateStrings[type];
}
2019-11-11 23:01:05 +01:00
cmCTestUpdateHandler::cmCTestUpdateHandler() = default;
void cmCTestUpdateHandler::Initialize()
{
this->Superclass::Initialize();
2018-01-26 17:06:56 +01:00
this->UpdateCommand.clear();
2009-10-04 10:30:41 +03:00
this->UpdateType = e_CVS;
}
int cmCTestUpdateHandler::DetermineType(const char* cmd, const char* type)
{
2018-08-09 18:06:22 +02:00
cmCTestOptionalLog(this->CTest, DEBUG,
"Determine update type from command: "
2016-07-09 11:21:54 +02:00
<< cmd << " and type: " << type << std::endl,
this->Quiet);
if (type && *type) {
cmCTestOptionalLog(this->CTest, DEBUG,
"Type specified: " << type << std::endl, this->Quiet);
std::string stype = cmSystemTools::LowerCase(type);
2016-07-09 11:21:54 +02:00
if (stype.find("cvs") != std::string::npos) {
return cmCTestUpdateHandler::e_CVS;
2016-07-09 11:21:54 +02:00
}
if (stype.find("svn") != std::string::npos) {
return cmCTestUpdateHandler::e_SVN;
2016-07-09 11:21:54 +02:00
}
if (stype.find("bzr") != std::string::npos) {
2009-10-04 10:30:41 +03:00
return cmCTestUpdateHandler::e_BZR;
2016-07-09 11:21:54 +02:00
}
if (stype.find("git") != std::string::npos) {
2009-10-04 10:30:41 +03:00
return cmCTestUpdateHandler::e_GIT;
2016-07-09 11:21:54 +02:00
}
if (stype.find("hg") != std::string::npos) {
2009-10-04 10:30:41 +03:00
return cmCTestUpdateHandler::e_HG;
2016-07-09 11:21:54 +02:00
}
if (stype.find("p4") != std::string::npos) {
2014-08-03 19:52:23 +02:00
return cmCTestUpdateHandler::e_P4;
}
2016-07-09 11:21:54 +02:00
} else {
cmCTestOptionalLog(
this->CTest, DEBUG,
2015-08-17 11:37:30 +02:00
"Type not specified, check command: " << cmd << std::endl, this->Quiet);
std::string stype = cmSystemTools::LowerCase(cmd);
2016-07-09 11:21:54 +02:00
if (stype.find("cvs") != std::string::npos) {
return cmCTestUpdateHandler::e_CVS;
2016-07-09 11:21:54 +02:00
}
if (stype.find("svn") != std::string::npos) {
return cmCTestUpdateHandler::e_SVN;
2016-07-09 11:21:54 +02:00
}
if (stype.find("bzr") != std::string::npos) {
2009-10-04 10:30:41 +03:00
return cmCTestUpdateHandler::e_BZR;
2016-07-09 11:21:54 +02:00
}
if (stype.find("git") != std::string::npos) {
2009-10-04 10:30:41 +03:00
return cmCTestUpdateHandler::e_GIT;
2016-07-09 11:21:54 +02:00
}
if (stype.find("hg") != std::string::npos) {
2009-10-04 10:30:41 +03:00
return cmCTestUpdateHandler::e_HG;
2016-07-09 11:21:54 +02:00
}
if (stype.find("p4") != std::string::npos) {
2014-08-03 19:52:23 +02:00
return cmCTestUpdateHandler::e_P4;
}
2016-07-09 11:21:54 +02:00
}
return cmCTestUpdateHandler::e_UNKNOWN;
}
2016-07-09 11:21:54 +02:00
// clearly it would be nice if this were broken up into a few smaller
// functions and commented...
int cmCTestUpdateHandler::ProcessHandler()
{
// Make sure VCS tool messages are in English so we can parse them.
2015-08-17 11:37:30 +02:00
cmCLocaleEnvironmentScope fixLocale;
static_cast<void>(fixLocale);
// Get source dir
2021-11-20 13:41:27 +01:00
cmValue sourceDirectory = this->GetOption("SourceDirectory");
2016-07-09 11:21:54 +02:00
if (!sourceDirectory) {
cmCTestLog(this->CTest, ERROR_MESSAGE,
2016-07-09 11:21:54 +02:00
"Cannot find SourceDirectory key in the DartConfiguration.tcl"
<< std::endl);
return -1;
2016-07-09 11:21:54 +02:00
}
cmGeneratedFileStream ofs;
2016-07-09 11:21:54 +02:00
if (!this->CTest->GetShowOnly()) {
this->StartLogFile("Update", ofs);
2016-07-09 11:21:54 +02:00
}
2015-08-17 11:37:30 +02:00
cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT,
2016-07-09 11:21:54 +02:00
" Updating the repository: " << sourceDirectory
<< std::endl,
this->Quiet);
2016-07-09 11:21:54 +02:00
if (!this->SelectVCS()) {
2009-10-04 10:30:41 +03:00
return -1;
2016-07-09 11:21:54 +02:00
}
2018-08-09 18:06:22 +02:00
cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT,
" Use "
2016-07-09 11:21:54 +02:00
<< cmCTestUpdateHandlerUpdateToString(this->UpdateType)
<< " repository type" << std::endl;
, this->Quiet);
2009-10-04 10:30:41 +03:00
// Create an object to interact with the VCS tool.
2018-01-26 17:06:56 +01:00
std::unique_ptr<cmCTestVC> vc;
2016-07-09 11:21:54 +02:00
switch (this->UpdateType) {
case e_CVS:
2018-01-26 17:06:56 +01:00
vc = cm::make_unique<cmCTestCVS>(this->CTest, ofs);
2016-07-09 11:21:54 +02:00
break;
case e_SVN:
2018-01-26 17:06:56 +01:00
vc = cm::make_unique<cmCTestSVN>(this->CTest, ofs);
2016-07-09 11:21:54 +02:00
break;
case e_BZR:
2018-01-26 17:06:56 +01:00
vc = cm::make_unique<cmCTestBZR>(this->CTest, ofs);
2016-07-09 11:21:54 +02:00
break;
case e_GIT:
2018-01-26 17:06:56 +01:00
vc = cm::make_unique<cmCTestGIT>(this->CTest, ofs);
2016-07-09 11:21:54 +02:00
break;
case e_HG:
2018-01-26 17:06:56 +01:00
vc = cm::make_unique<cmCTestHG>(this->CTest, ofs);
2016-07-09 11:21:54 +02:00
break;
case e_P4:
2018-01-26 17:06:56 +01:00
vc = cm::make_unique<cmCTestP4>(this->CTest, ofs);
2016-07-09 11:21:54 +02:00
break;
default:
2018-01-26 17:06:56 +01:00
vc = cm::make_unique<cmCTestVC>(this->CTest, ofs);
2016-07-09 11:21:54 +02:00
break;
}
2009-10-04 10:30:41 +03:00
vc->SetCommandLineTool(this->UpdateCommand);
vc->SetSourceDirectory(sourceDirectory);
2009-10-04 10:30:41 +03:00
// Cleanup the working tree.
vc->Cleanup();
//
// Now update repository and remember what files were updated
//
cmGeneratedFileStream os;
2016-07-09 11:21:54 +02:00
if (!this->StartResultingXML(cmCTest::PartUpdate, "Update", os)) {
2018-08-09 18:06:22 +02:00
cmCTestLog(this->CTest, ERROR_MESSAGE,
"Cannot open log file" << std::endl);
return -1;
2016-07-09 11:21:54 +02:00
}
std::string start_time = this->CTest->CurrentTime();
2018-04-23 21:13:27 +02:00
auto start_time_time = std::chrono::system_clock::now();
auto elapsed_time_start = std::chrono::steady_clock::now();
2009-10-04 10:30:41 +03:00
bool updated = vc->Update();
2016-07-09 11:21:54 +02:00
std::string buildname =
cmCTest::SafeBuildIdField(this->CTest->GetCTestConfiguration("BuildName"));
2015-08-17 11:37:30 +02:00
cmXMLWriter xml(os);
xml.StartDocument();
xml.StartElement("Update");
xml.Attribute("mode", "Client");
xml.Attribute("Generator",
2016-07-09 11:21:54 +02:00
std::string("ctest-") + cmVersion::GetCMakeVersion());
2015-08-17 11:37:30 +02:00
xml.Element("Site", this->CTest->GetCTestConfiguration("Site"));
xml.Element("BuildName", buildname);
2018-08-09 18:06:22 +02:00
xml.Element("BuildStamp",
this->CTest->GetCurrentTag() + "-" +
2016-07-09 11:21:54 +02:00
this->CTest->GetTestModelString());
2015-08-17 11:37:30 +02:00
xml.Element("StartDateTime", start_time);
xml.Element("StartTime", start_time_time);
xml.Element("UpdateCommand", vc->GetUpdateCommandLine());
xml.Element("UpdateType",
2016-07-09 11:21:54 +02:00
cmCTestUpdateHandlerUpdateToString(this->UpdateType));
2019-11-11 23:01:05 +01:00
std::string changeId = this->CTest->GetCTestConfiguration("ChangeId");
if (!changeId.empty()) {
xml.Element("ChangeId", changeId);
}
2015-08-17 11:37:30 +02:00
2017-07-20 19:35:53 +02:00
bool loadedMods = vc->WriteXML(xml);
2009-10-04 10:30:41 +03:00
int localModifications = 0;
int numUpdated = vc->GetPathCount(cmCTestVC::PathUpdated);
2016-07-09 11:21:54 +02:00
if (numUpdated) {
2015-08-17 11:37:30 +02:00
cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT,
2016-07-09 11:21:54 +02:00
" Found " << numUpdated << " updated files\n",
this->Quiet);
}
if (int numModified = vc->GetPathCount(cmCTestVC::PathModified)) {
2018-08-09 18:06:22 +02:00
cmCTestOptionalLog(
this->CTest, HANDLER_OUTPUT,
" Found " << numModified << " locally modified files\n", this->Quiet);
2009-10-04 10:30:41 +03:00
localModifications += numModified;
2016-07-09 11:21:54 +02:00
}
if (int numConflicting = vc->GetPathCount(cmCTestVC::PathConflicting)) {
2015-08-17 11:37:30 +02:00
cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT,
2016-07-09 11:21:54 +02:00
" Found " << numConflicting << " conflicting files\n",
this->Quiet);
2009-10-04 10:30:41 +03:00
localModifications += numConflicting;
2016-07-09 11:21:54 +02:00
}
2015-08-17 11:37:30 +02:00
cmCTestOptionalLog(this->CTest, DEBUG, "End" << std::endl, this->Quiet);
std::string end_time = this->CTest->CurrentTime();
2015-08-17 11:37:30 +02:00
xml.Element("EndDateTime", end_time);
2018-04-23 21:13:27 +02:00
xml.Element("EndTime", std::chrono::system_clock::now());
xml.Element("ElapsedMinutes",
std::chrono::duration_cast<std::chrono::minutes>(
std::chrono::steady_clock::now() - elapsed_time_start)
.count());
2015-08-17 11:37:30 +02:00
xml.StartElement("UpdateReturnStatus");
2016-07-09 11:21:54 +02:00
if (localModifications) {
2015-08-17 11:37:30 +02:00
xml.Content("Update error: "
2016-07-09 11:21:54 +02:00
"There are modified or conflicting files in the repository");
2021-09-14 00:13:48 +02:00
cmCTestLog(this->CTest, WARNING,
2016-07-09 11:21:54 +02:00
" There are modified or conflicting files in the repository"
<< std::endl);
}
if (!updated) {
2015-08-17 11:37:30 +02:00
xml.Content("Update command failed:\n");
xml.Content(vc->GetUpdateCommandLine());
2018-08-09 18:06:22 +02:00
cmCTestLog(this->CTest, HANDLER_OUTPUT,
" Update command failed: " << vc->GetUpdateCommandLine()
<< "\n");
2016-07-09 11:21:54 +02:00
}
2015-08-17 11:37:30 +02:00
xml.EndElement(); // UpdateReturnStatus
xml.EndElement(); // Update
xml.EndDocument();
2017-07-20 19:35:53 +02:00
return updated && loadedMods ? numUpdated : -1;
2009-10-04 10:30:41 +03:00
}
2021-11-20 13:41:27 +01:00
int cmCTestUpdateHandler::DetectVCS(const std::string& dir)
2009-10-04 10:30:41 +03:00
{
std::string sourceDirectory = dir;
2016-07-09 11:21:54 +02:00
cmCTestOptionalLog(this->CTest, DEBUG,
"Check directory: " << sourceDirectory << std::endl,
this->Quiet);
2009-10-04 10:30:41 +03:00
sourceDirectory += "/.svn";
2018-04-23 21:13:27 +02:00
if (cmSystemTools::FileExists(sourceDirectory)) {
2009-10-04 10:30:41 +03:00
return cmCTestUpdateHandler::e_SVN;
2016-07-09 11:21:54 +02:00
}
2020-02-01 23:06:01 +01:00
sourceDirectory = cmStrCat(dir, "/CVS");
2018-04-23 21:13:27 +02:00
if (cmSystemTools::FileExists(sourceDirectory)) {
2009-10-04 10:30:41 +03:00
return cmCTestUpdateHandler::e_CVS;
2016-07-09 11:21:54 +02:00
}
2020-02-01 23:06:01 +01:00
sourceDirectory = cmStrCat(dir, "/.bzr");
2018-04-23 21:13:27 +02:00
if (cmSystemTools::FileExists(sourceDirectory)) {
2009-10-04 10:30:41 +03:00
return cmCTestUpdateHandler::e_BZR;
2016-07-09 11:21:54 +02:00
}
2020-02-01 23:06:01 +01:00
sourceDirectory = cmStrCat(dir, "/.git");
2018-04-23 21:13:27 +02:00
if (cmSystemTools::FileExists(sourceDirectory)) {
2009-10-04 10:30:41 +03:00
return cmCTestUpdateHandler::e_GIT;
2016-07-09 11:21:54 +02:00
}
2020-02-01 23:06:01 +01:00
sourceDirectory = cmStrCat(dir, "/.hg");
2018-04-23 21:13:27 +02:00
if (cmSystemTools::FileExists(sourceDirectory)) {
2009-10-04 10:30:41 +03:00
return cmCTestUpdateHandler::e_HG;
2016-07-09 11:21:54 +02:00
}
2020-02-01 23:06:01 +01:00
sourceDirectory = cmStrCat(dir, "/.p4");
2018-04-23 21:13:27 +02:00
if (cmSystemTools::FileExists(sourceDirectory)) {
2014-08-03 19:52:23 +02:00
return cmCTestUpdateHandler::e_P4;
2016-07-09 11:21:54 +02:00
}
2020-02-01 23:06:01 +01:00
sourceDirectory = cmStrCat(dir, "/.p4config");
2018-04-23 21:13:27 +02:00
if (cmSystemTools::FileExists(sourceDirectory)) {
2014-08-03 19:52:23 +02:00
return cmCTestUpdateHandler::e_P4;
2016-07-09 11:21:54 +02:00
}
2009-10-04 10:30:41 +03:00
return cmCTestUpdateHandler::e_UNKNOWN;
}
bool cmCTestUpdateHandler::SelectVCS()
{
// Get update command
this->UpdateCommand = this->CTest->GetCTestConfiguration("UpdateCommand");
// Detect the VCS managing the source tree.
this->UpdateType = this->DetectVCS(this->GetOption("SourceDirectory"));
2016-07-09 11:21:54 +02:00
if (this->UpdateType == e_UNKNOWN) {
2009-10-04 10:30:41 +03:00
// The source tree does not have a recognized VCS. Check the
// configuration value or command name.
2016-07-09 11:21:54 +02:00
this->UpdateType = this->DetermineType(
this->UpdateCommand.c_str(),
2009-10-04 10:30:41 +03:00
this->CTest->GetCTestConfiguration("UpdateType").c_str());
2016-07-09 11:21:54 +02:00
}
2009-10-04 10:30:41 +03:00
// If no update command was specified, lookup one for this VCS tool.
2016-07-09 11:21:54 +02:00
if (this->UpdateCommand.empty()) {
2018-01-26 17:06:56 +01:00
const char* key = nullptr;
2016-07-09 11:21:54 +02:00
switch (this->UpdateType) {
case e_CVS:
key = "CVSCommand";
break;
case e_SVN:
key = "SVNCommand";
break;
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) {
2009-10-04 10:30:41 +03:00
this->UpdateCommand = this->CTest->GetCTestConfiguration(key);
2016-07-09 11:21:54 +02:00
}
if (this->UpdateCommand.empty()) {
2015-04-27 22:25:09 +02:00
std::ostringstream e;
2009-10-04 10:30:41 +03:00
e << "Cannot find UpdateCommand ";
2016-07-09 11:21:54 +02:00
if (key) {
2009-10-04 10:30:41 +03:00
e << "or " << key;
2016-07-09 11:21:54 +02:00
}
2009-10-04 10:30:41 +03:00
e << " configuration key.";
cmCTestLog(this->CTest, ERROR_MESSAGE, e.str() << std::endl);
return false;
}
2016-07-09 11:21:54 +02:00
}
2009-10-04 10:30:41 +03:00
return true;
}