1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
|
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file LICENSE.rst or https://cmake.org/licensing for details. */
#include "cmCTestUpdateCommand.h"
#include <chrono>
#include <sstream>
#include <string>
#include <cm/memory>
#include <cmext/string_view>
#include "cmArgumentParser.h"
#include "cmCLocaleEnvironmentScope.h"
#include "cmCTest.h"
#include "cmCTestBZR.h"
#include "cmCTestCVS.h"
#include "cmCTestGIT.h"
#include "cmCTestHG.h"
#include "cmCTestP4.h"
#include "cmCTestSVN.h"
#include "cmCTestVC.h"
#include "cmExecutionStatus.h"
#include "cmGeneratedFileStream.h"
#include "cmMakefile.h"
#include "cmStringAlgorithms.h"
#include "cmSystemTools.h"
#include "cmVersion.h"
#include "cmXMLWriter.h"
namespace {
enum
{
e_UNKNOWN = 0,
e_CVS,
e_SVN,
e_BZR,
e_GIT,
e_HG,
e_P4,
e_LAST
};
char const* TypeToString(int type)
{
// clang-format off
switch (type) {
case e_CVS: return "CVS";
case e_SVN: return "SVN";
case e_BZR: return "BZR";
case e_GIT: return "GIT";
case e_HG: return "HG";
case e_P4: return "P4";
default: return "Unknown";
}
// clang-format on
}
char const* TypeToCommandKey(int type)
{
// clang-format off
switch (type) {
case e_CVS: return "CTEST_CVS_COMMAND";
case e_SVN: return "CTEST_SVN_COMMAND";
case e_BZR: return "CTEST_BZR_COMMAND";
case e_GIT: return "CTEST_GIT_COMMAND";
case e_HG: return "CTEST_HG_COMMAND";
case e_P4: return "CTEST_P4_COMMAND";
default: return nullptr;
}
// clang-format on
}
int DetermineType(std::string const& cmd)
{
std::string stype = cmSystemTools::LowerCase(cmd);
if (stype.find("cvs") != std::string::npos) {
return e_CVS;
}
if (stype.find("svn") != std::string::npos) {
return e_SVN;
}
if (stype.find("bzr") != std::string::npos) {
return e_BZR;
}
if (stype.find("git") != std::string::npos) {
return e_GIT;
}
if (stype.find("hg") != std::string::npos) {
return e_HG;
}
if (stype.find("p4") != std::string::npos) {
return e_P4;
}
return e_UNKNOWN;
}
int DetectVCS(std::string const& dir)
{
if (cmSystemTools::FileExists(cmStrCat(dir, "/CVS"))) {
return e_CVS;
}
if (cmSystemTools::FileExists(cmStrCat(dir, "/.svn"))) {
return e_SVN;
}
if (cmSystemTools::FileExists(cmStrCat(dir, "/.bzr"))) {
return e_BZR;
}
if (cmSystemTools::FileExists(cmStrCat(dir, "/.git"))) {
return e_GIT;
}
if (cmSystemTools::FileExists(cmStrCat(dir, "/.hg"))) {
return e_HG;
}
if (cmSystemTools::FileExists(cmStrCat(dir, "/.p4"))) {
return e_P4;
}
if (cmSystemTools::FileExists(cmStrCat(dir, "/.p4config"))) {
return e_P4;
}
return e_UNKNOWN;
}
std::unique_ptr<cmCTestVC> MakeVC(int type, cmCTest* ctest, cmMakefile* mf,
std::ostream& os)
{
// clang-format off
switch (type) {
case e_CVS: return cm::make_unique<cmCTestCVS>(ctest, mf, os);
case e_SVN: return cm::make_unique<cmCTestSVN>(ctest, mf, os);
case e_BZR: return cm::make_unique<cmCTestBZR>(ctest, mf, os);
case e_GIT: return cm::make_unique<cmCTestGIT>(ctest, mf, os);
case e_HG: return cm::make_unique<cmCTestHG> (ctest, mf, os);
case e_P4: return cm::make_unique<cmCTestP4> (ctest, mf, os);
default: return cm::make_unique<cmCTestVC> (ctest, mf, os);
}
// clang-format on
}
} // namespace
bool cmCTestUpdateCommand::ExecuteUpdate(UpdateArguments& args,
cmExecutionStatus& status) const
{
cmMakefile& mf = status.GetMakefile();
std::string const& source_dir = !args.Source.empty()
? args.Source
: mf.GetSafeDefinition("CTEST_SOURCE_DIRECTORY");
if (source_dir.empty()) {
status.SetError("called with no source directory specified. "
"Use SOURCE argument or set CTEST_SOURCE_DIRECTORY.");
return false;
}
std::string const currentTag = this->CTest->GetCurrentTag();
if (currentTag.empty()) {
status.SetError("called with no current tag.");
return false;
}
// Detect the VCS managing the source tree.
int updateType = DetectVCS(source_dir);
// Get update command
std::string updateCommand = mf.GetSafeDefinition("CTEST_UPDATE_COMMAND");
if (updateType == e_UNKNOWN && !updateCommand.empty()) {
updateType = DetermineType(updateCommand);
}
if (updateType == e_UNKNOWN) {
updateType = DetermineType(mf.GetSafeDefinition("CTEST_UPDATE_TYPE"));
}
// If no update command was specified, lookup one for this VCS tool.
if (updateCommand.empty()) {
char const* key = TypeToCommandKey(updateType);
if (key) {
updateCommand = mf.GetSafeDefinition(key);
}
if (updateCommand.empty()) {
std::ostringstream e;
e << "called with no update command specified. "
"Please set CTEST_UPDATE_COMMAND";
if (key) {
e << " or " << key;
}
e << '.';
status.SetError(e.str());
return false;
}
}
cmGeneratedFileStream ofs;
if (!this->CTest->GetShowOnly()) {
std::string logFile = cmStrCat("LastUpdate_", currentTag, ".log");
if (!this->CTest->OpenOutputFile("Temporary", logFile, ofs)) {
status.SetError(cmStrCat("cannot create log file: ", logFile));
return false;
}
}
cmGeneratedFileStream os;
if (!this->CTest->OpenOutputFile(currentTag, "Update.xml", os, true)) {
status.SetError("cannot create resulting XML file: Update.xml");
return false;
}
this->CTest->AddSubmitFile(cmCTest::PartUpdate, "Update.xml");
cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT,
" Updating " << TypeToString(updateType)
<< " repository: " << source_dir << '\n',
args.Quiet);
// Make sure VCS tool messages are in English so we can parse them.
cmCLocaleEnvironmentScope fixLocale;
static_cast<void>(fixLocale);
// Create an object to interact with the VCS tool.
std::unique_ptr<cmCTestVC> vc = MakeVC(updateType, this->CTest, &mf, ofs);
vc->SetCommandLineTool(updateCommand);
vc->SetSourceDirectory(source_dir);
// Cleanup the working tree.
vc->Cleanup();
std::string start_time = this->CTest->CurrentTime();
auto start_time_time = std::chrono::system_clock::now();
auto elapsed_time_start = std::chrono::steady_clock::now();
bool updated = vc->Update();
std::string buildname =
cmCTest::SafeBuildIdField(mf.GetSafeDefinition("CTEST_BUILD_NAME"));
cmXMLWriter xml(os);
xml.StartDocument();
xml.StartElement("Update");
xml.Attribute("mode", "Client");
xml.Attribute("Generator",
std::string("ctest-") + cmVersion::GetCMakeVersion());
xml.Element("Site", mf.GetSafeDefinition("CTEST_SITE"));
xml.Element("BuildName", buildname);
xml.Element("BuildStamp",
this->CTest->GetCurrentTag() + "-" +
this->CTest->GetTestGroupString());
xml.Element("StartDateTime", start_time);
xml.Element("StartTime", start_time_time);
xml.Element("UpdateCommand", vc->GetUpdateCommandLine());
xml.Element("UpdateType", TypeToString(updateType));
std::string changeId = mf.GetSafeDefinition("CTEST_CHANGE_ID");
if (!changeId.empty()) {
xml.Element("ChangeId", changeId);
}
bool loadedMods = vc->WriteXML(xml);
int localModifications = 0;
int numUpdated = vc->GetPathCount(cmCTestVC::PathUpdated);
if (numUpdated) {
cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT,
" Found " << numUpdated << " updated files\n",
args.Quiet);
}
if (int numModified = vc->GetPathCount(cmCTestVC::PathModified)) {
cmCTestOptionalLog(
this->CTest, HANDLER_OUTPUT,
" Found " << numModified << " locally modified files\n", args.Quiet);
localModifications += numModified;
}
if (int numConflicting = vc->GetPathCount(cmCTestVC::PathConflicting)) {
cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT,
" Found " << numConflicting << " conflicting files\n",
args.Quiet);
localModifications += numConflicting;
}
cmCTestOptionalLog(this->CTest, DEBUG, "End" << std::endl, args.Quiet);
std::string end_time = this->CTest->CurrentTime();
xml.Element("EndDateTime", end_time);
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());
xml.StartElement("UpdateReturnStatus");
if (localModifications) {
xml.Content("Update error: "
"There are modified or conflicting files in the repository");
cmCTestLog(this->CTest, WARNING,
" There are modified or conflicting files in the repository"
<< std::endl);
}
if (!updated) {
xml.Content("Update command failed:\n");
xml.Content(vc->GetUpdateCommandLine());
cmCTestLog(this->CTest, HANDLER_OUTPUT,
" Update command failed: " << vc->GetUpdateCommandLine()
<< "\n");
}
xml.EndElement(); // UpdateReturnStatus
xml.EndElement(); // Update
xml.EndDocument();
if (!args.ReturnValue.empty()) {
mf.AddDefinition(args.ReturnValue,
std::to_string(updated && loadedMods ? numUpdated : -1));
}
return true;
}
bool cmCTestUpdateCommand::InitialPass(std::vector<std::string> const& args,
cmExecutionStatus& status) const
{
static auto const parser =
cmArgumentParser<UpdateArguments>{ MakeBasicParser<UpdateArguments>() }
.Bind("SOURCE"_s, &UpdateArguments::Source)
.Bind("RETURN_VALUE"_s, &UpdateArguments::ReturnValue)
.Bind("QUIET"_s, &UpdateArguments::Quiet);
return this->Invoke(parser, args, status, [&](UpdateArguments& a) {
return this->ExecuteUpdate(a, status);
});
}
|