cmake/Source/CTest/cmCTestVC.cxx

223 lines
6.4 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. */
2009-10-04 10:30:41 +03:00
#include "cmCTestVC.h"
2020-02-01 23:06:01 +01:00
#include <cstdio>
#include <ctime>
#include <sstream>
#include <vector>
2009-10-04 10:30:41 +03:00
#include "cmCTest.h"
#include "cmSystemTools.h"
2024-04-14 22:45:38 +02:00
#include "cmUVProcessChain.h"
2021-11-20 13:41:27 +01:00
#include "cmValue.h"
2015-08-17 11:37:30 +02:00
#include "cmXMLWriter.h"
2009-10-04 10:30:41 +03:00
2016-07-09 11:21:54 +02:00
cmCTestVC::cmCTestVC(cmCTest* ct, std::ostream& log)
: CTest(ct)
, Log(log)
2009-10-04 10:30:41 +03:00
{
this->PathCount[PathUpdated] = 0;
this->PathCount[PathModified] = 0;
this->PathCount[PathConflicting] = 0;
this->Unknown.Date = "Unknown";
this->Unknown.Author = "Unknown";
this->Unknown.Rev = "Unknown";
}
2019-11-11 23:01:05 +01:00
cmCTestVC::~cmCTestVC() = default;
2009-10-04 10:30:41 +03:00
void cmCTestVC::SetCommandLineTool(std::string const& tool)
{
this->CommandLineTool = tool;
}
void cmCTestVC::SetSourceDirectory(std::string const& dir)
{
this->SourceDirectory = dir;
}
2020-08-30 11:54:41 +02:00
bool cmCTestVC::InitialCheckout(const std::string& command)
2009-10-04 10:30:41 +03:00
{
cmCTestLog(this->CTest, HANDLER_OUTPUT,
" First perform the initial checkout: " << command << "\n");
// Make the parent directory in which to perform the checkout.
std::string parent = cmSystemTools::GetFilenamePath(this->SourceDirectory);
cmCTestLog(this->CTest, HANDLER_OUTPUT,
" Perform checkout in directory: " << parent << "\n");
2018-04-23 21:13:27 +02:00
if (!cmSystemTools::MakeDirectory(parent)) {
2009-10-04 10:30:41 +03:00
cmCTestLog(this->CTest, ERROR_MESSAGE,
"Cannot create directory: " << parent << std::endl);
return false;
2016-07-09 11:21:54 +02:00
}
2009-10-04 10:30:41 +03:00
// Construct the initial checkout command line.
2015-04-27 22:25:09 +02:00
std::vector<std::string> args = cmSystemTools::ParseArguments(command);
2009-10-04 10:30:41 +03:00
// Run the initial checkout command and log its output.
this->Log << "--- Begin Initial Checkout ---\n";
OutputLogger out(this->Log, "co-out> ");
OutputLogger err(this->Log, "co-err> ");
2024-04-14 22:45:38 +02:00
bool result = this->RunChild(args, &out, &err, parent);
2009-10-04 10:30:41 +03:00
this->Log << "--- End Initial Checkout ---\n";
2016-07-09 11:21:54 +02:00
if (!result) {
2018-08-09 18:06:22 +02:00
cmCTestLog(this->CTest, ERROR_MESSAGE,
"Initial checkout failed!" << std::endl);
2016-07-09 11:21:54 +02:00
}
2009-10-04 10:30:41 +03:00
return result;
}
2024-04-14 22:45:38 +02:00
bool cmCTestVC::RunChild(const std::vector<std::string>& cmd,
OutputParser* out, OutputParser* err,
std::string workDir, Encoding encoding)
2009-10-04 10:30:41 +03:00
{
2019-11-11 23:01:05 +01:00
this->Log << cmCTestVC::ComputeCommandLine(cmd) << "\n";
2009-10-04 10:30:41 +03:00
2024-04-14 22:45:38 +02:00
cmUVProcessChainBuilder builder;
if (workDir.empty()) {
workDir = this->SourceDirectory;
}
builder.AddCommand(cmd).SetWorkingDirectory(workDir);
auto status = cmCTestVC::RunProcess(builder, out, err, encoding);
return status.front().SpawnResult == 0 && status.front().ExitStatus == 0;
2009-10-04 10:30:41 +03:00
}
2024-04-14 22:45:38 +02:00
std::string cmCTestVC::ComputeCommandLine(const std::vector<std::string>& cmd)
2009-10-04 10:30:41 +03:00
{
2015-04-27 22:25:09 +02:00
std::ostringstream line;
2009-10-04 10:30:41 +03:00
const char* sep = "";
2024-04-14 22:45:38 +02:00
for (auto const& arg : cmd) {
line << sep << "\"" << arg << "\"";
2009-10-04 10:30:41 +03:00
sep = " ";
2016-07-09 11:21:54 +02:00
}
2009-10-04 10:30:41 +03:00
return line.str();
}
2024-04-14 22:45:38 +02:00
bool cmCTestVC::RunUpdateCommand(const std::vector<std::string>& cmd,
OutputParser* out, OutputParser* err,
Encoding encoding)
2009-10-04 10:30:41 +03:00
{
// Report the command line.
this->UpdateCommandLine = this->ComputeCommandLine(cmd);
2016-07-09 11:21:54 +02:00
if (this->CTest->GetShowOnly()) {
2009-10-04 10:30:41 +03:00
this->Log << this->UpdateCommandLine << "\n";
return true;
2016-07-09 11:21:54 +02:00
}
2009-10-04 10:30:41 +03:00
// Run the command.
2024-04-14 22:45:38 +02:00
return this->RunChild(cmd, out, err, "", encoding);
2009-10-04 10:30:41 +03:00
}
std::string cmCTestVC::GetNightlyTime()
{
// Get the nightly start time corresponding to the current dau.
struct tm* t = this->CTest->GetNightlyTime(
this->CTest->GetCTestConfiguration("NightlyStartTime"),
this->CTest->GetTomorrowTag());
char current_time[1024];
2022-03-29 21:10:50 +02:00
snprintf(current_time, sizeof(current_time), "%04d-%02d-%02d %02d:%02d:%02d",
t->tm_year + 1900, t->tm_mon + 1, t->tm_mday, t->tm_hour, t->tm_min,
t->tm_sec);
return { current_time };
2009-10-04 10:30:41 +03:00
}
void cmCTestVC::Cleanup()
{
this->Log << "--- Begin Cleanup ---\n";
this->CleanupImpl();
this->Log << "--- End Cleanup ---\n";
}
void cmCTestVC::CleanupImpl()
{
// We do no cleanup by default.
}
bool cmCTestVC::Update()
{
2015-04-27 22:25:09 +02:00
bool result = true;
2019-11-11 23:01:05 +01:00
// Use the explicitly specified version.
std::string versionOverride =
this->CTest->GetCTestConfiguration("UpdateVersionOverride");
if (!versionOverride.empty()) {
this->SetNewRevision(versionOverride);
return true;
}
2015-04-27 22:25:09 +02:00
// if update version only is on then do not actually update,
// just note the current version and finish
2020-02-01 23:06:01 +01:00
if (!cmIsOn(this->CTest->GetCTestConfiguration("UpdateVersionOnly"))) {
2017-07-20 19:35:53 +02:00
result = this->NoteOldRevision() && result;
2015-04-27 22:25:09 +02:00
this->Log << "--- Begin Update ---\n";
2017-07-20 19:35:53 +02:00
result = this->UpdateImpl() && result;
2015-04-27 22:25:09 +02:00
this->Log << "--- End Update ---\n";
2016-07-09 11:21:54 +02:00
}
2017-07-20 19:35:53 +02:00
result = this->NoteNewRevision() && result;
2009-10-04 10:30:41 +03:00
return result;
}
2017-07-20 19:35:53 +02:00
bool cmCTestVC::NoteOldRevision()
2009-10-04 10:30:41 +03:00
{
// We do nothing by default.
2017-07-20 19:35:53 +02:00
return true;
2009-10-04 10:30:41 +03:00
}
2017-07-20 19:35:53 +02:00
bool cmCTestVC::NoteNewRevision()
2009-10-04 10:30:41 +03:00
{
// We do nothing by default.
2017-07-20 19:35:53 +02:00
return true;
2009-10-04 10:30:41 +03:00
}
2019-11-11 23:01:05 +01:00
void cmCTestVC::SetNewRevision(std::string const& /*unused*/)
{
// We do nothing by default.
}
2009-10-04 10:30:41 +03:00
bool cmCTestVC::UpdateImpl()
{
cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
"* Unknown VCS tool, not updating!" << std::endl);
return true;
}
2015-08-17 11:37:30 +02:00
bool cmCTestVC::WriteXML(cmXMLWriter& xml)
2009-10-04 10:30:41 +03:00
{
this->Log << "--- Begin Revisions ---\n";
bool result = this->WriteXMLUpdates(xml);
this->Log << "--- End Revisions ---\n";
return result;
}
2016-10-30 18:24:19 +01:00
bool cmCTestVC::WriteXMLUpdates(cmXMLWriter& /*unused*/)
2009-10-04 10:30:41 +03:00
{
cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
"* CTest cannot extract updates for this VCS tool.\n");
return true;
}
2016-07-09 11:21:54 +02:00
void cmCTestVC::WriteXMLEntry(cmXMLWriter& xml, std::string const& path,
std::string const& name, std::string const& full,
2009-10-04 10:30:41 +03:00
File const& f)
{
2016-07-09 11:21:54 +02:00
static const char* desc[3] = { "Updated", "Modified", "Conflicting" };
Revision const& rev = f.Rev ? *f.Rev : this->Unknown;
std::string prior = f.PriorRev ? f.PriorRev->Rev : std::string("Unknown");
2015-08-17 11:37:30 +02:00
xml.StartElement(desc[f.Status]);
xml.Element("File", name);
xml.Element("Directory", path);
xml.Element("FullName", full);
xml.Element("CheckinDate", rev.Date);
xml.Element("Author", rev.Author);
xml.Element("Email", rev.EMail);
xml.Element("Committer", rev.Committer);
xml.Element("CommitterEmail", rev.CommitterEMail);
xml.Element("CommitDate", rev.CommitDate);
xml.Element("Log", rev.Log);
xml.Element("Revision", rev.Rev);
xml.Element("PriorRevision", prior);
xml.EndElement();
2009-10-04 10:30:41 +03:00
++this->PathCount[f.Status];
}