cmCTestUpdateCommand.cxx 9.8 KB

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