cmCTestUpdateHandler.cxx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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 "cmCTestUpdateHandler.h"
  4. #include "cmAlgorithms.h"
  5. #include "cmCLocaleEnvironmentScope.h"
  6. #include "cmCTest.h"
  7. #include "cmCTestBZR.h"
  8. #include "cmCTestCVS.h"
  9. #include "cmCTestGIT.h"
  10. #include "cmCTestHG.h"
  11. #include "cmCTestP4.h"
  12. #include "cmCTestSVN.h"
  13. #include "cmCTestVC.h"
  14. #include "cmGeneratedFileStream.h"
  15. #include "cmSystemTools.h"
  16. #include "cmVersion.h"
  17. #include "cmXMLWriter.h"
  18. #include <chrono>
  19. #include <memory> // IWYU pragma: keep
  20. #include <sstream>
  21. static const char* cmCTestUpdateHandlerUpdateStrings[] = {
  22. "Unknown", "CVS", "SVN", "BZR", "GIT", "HG", "P4"
  23. };
  24. static const char* cmCTestUpdateHandlerUpdateToString(int type)
  25. {
  26. if (type < cmCTestUpdateHandler::e_UNKNOWN ||
  27. type >= cmCTestUpdateHandler::e_LAST) {
  28. return cmCTestUpdateHandlerUpdateStrings[cmCTestUpdateHandler::e_UNKNOWN];
  29. }
  30. return cmCTestUpdateHandlerUpdateStrings[type];
  31. }
  32. cmCTestUpdateHandler::cmCTestUpdateHandler() = default;
  33. void cmCTestUpdateHandler::Initialize()
  34. {
  35. this->Superclass::Initialize();
  36. this->UpdateCommand.clear();
  37. this->UpdateType = e_CVS;
  38. }
  39. int cmCTestUpdateHandler::DetermineType(const char* cmd, const char* type)
  40. {
  41. cmCTestOptionalLog(this->CTest, DEBUG,
  42. "Determine update type from command: "
  43. << cmd << " and type: " << type << std::endl,
  44. this->Quiet);
  45. if (type && *type) {
  46. cmCTestOptionalLog(this->CTest, DEBUG,
  47. "Type specified: " << type << std::endl, this->Quiet);
  48. std::string stype = cmSystemTools::LowerCase(type);
  49. if (stype.find("cvs") != std::string::npos) {
  50. return cmCTestUpdateHandler::e_CVS;
  51. }
  52. if (stype.find("svn") != std::string::npos) {
  53. return cmCTestUpdateHandler::e_SVN;
  54. }
  55. if (stype.find("bzr") != std::string::npos) {
  56. return cmCTestUpdateHandler::e_BZR;
  57. }
  58. if (stype.find("git") != std::string::npos) {
  59. return cmCTestUpdateHandler::e_GIT;
  60. }
  61. if (stype.find("hg") != std::string::npos) {
  62. return cmCTestUpdateHandler::e_HG;
  63. }
  64. if (stype.find("p4") != std::string::npos) {
  65. return cmCTestUpdateHandler::e_P4;
  66. }
  67. } else {
  68. cmCTestOptionalLog(
  69. this->CTest, DEBUG,
  70. "Type not specified, check command: " << cmd << std::endl, this->Quiet);
  71. std::string stype = cmSystemTools::LowerCase(cmd);
  72. if (stype.find("cvs") != std::string::npos) {
  73. return cmCTestUpdateHandler::e_CVS;
  74. }
  75. if (stype.find("svn") != std::string::npos) {
  76. return cmCTestUpdateHandler::e_SVN;
  77. }
  78. if (stype.find("bzr") != std::string::npos) {
  79. return cmCTestUpdateHandler::e_BZR;
  80. }
  81. if (stype.find("git") != std::string::npos) {
  82. return cmCTestUpdateHandler::e_GIT;
  83. }
  84. if (stype.find("hg") != std::string::npos) {
  85. return cmCTestUpdateHandler::e_HG;
  86. }
  87. if (stype.find("p4") != std::string::npos) {
  88. return cmCTestUpdateHandler::e_P4;
  89. }
  90. }
  91. return cmCTestUpdateHandler::e_UNKNOWN;
  92. }
  93. // clearly it would be nice if this were broken up into a few smaller
  94. // functions and commented...
  95. int cmCTestUpdateHandler::ProcessHandler()
  96. {
  97. // Make sure VCS tool messages are in English so we can parse them.
  98. cmCLocaleEnvironmentScope fixLocale;
  99. static_cast<void>(fixLocale);
  100. // Get source dir
  101. const char* sourceDirectory = this->GetOption("SourceDirectory");
  102. if (!sourceDirectory) {
  103. cmCTestLog(this->CTest, ERROR_MESSAGE,
  104. "Cannot find SourceDirectory key in the DartConfiguration.tcl"
  105. << std::endl);
  106. return -1;
  107. }
  108. cmGeneratedFileStream ofs;
  109. if (!this->CTest->GetShowOnly()) {
  110. this->StartLogFile("Update", ofs);
  111. }
  112. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT,
  113. " Updating the repository: " << sourceDirectory
  114. << std::endl,
  115. this->Quiet);
  116. if (!this->SelectVCS()) {
  117. return -1;
  118. }
  119. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT,
  120. " Use "
  121. << cmCTestUpdateHandlerUpdateToString(this->UpdateType)
  122. << " repository type" << std::endl;
  123. , this->Quiet);
  124. // Create an object to interact with the VCS tool.
  125. std::unique_ptr<cmCTestVC> vc;
  126. switch (this->UpdateType) {
  127. case e_CVS:
  128. vc = cm::make_unique<cmCTestCVS>(this->CTest, ofs);
  129. break;
  130. case e_SVN:
  131. vc = cm::make_unique<cmCTestSVN>(this->CTest, ofs);
  132. break;
  133. case e_BZR:
  134. vc = cm::make_unique<cmCTestBZR>(this->CTest, ofs);
  135. break;
  136. case e_GIT:
  137. vc = cm::make_unique<cmCTestGIT>(this->CTest, ofs);
  138. break;
  139. case e_HG:
  140. vc = cm::make_unique<cmCTestHG>(this->CTest, ofs);
  141. break;
  142. case e_P4:
  143. vc = cm::make_unique<cmCTestP4>(this->CTest, ofs);
  144. break;
  145. default:
  146. vc = cm::make_unique<cmCTestVC>(this->CTest, ofs);
  147. break;
  148. }
  149. vc->SetCommandLineTool(this->UpdateCommand);
  150. vc->SetSourceDirectory(sourceDirectory);
  151. // Cleanup the working tree.
  152. vc->Cleanup();
  153. //
  154. // Now update repository and remember what files were updated
  155. //
  156. cmGeneratedFileStream os;
  157. if (!this->StartResultingXML(cmCTest::PartUpdate, "Update", os)) {
  158. cmCTestLog(this->CTest, ERROR_MESSAGE,
  159. "Cannot open log file" << std::endl);
  160. return -1;
  161. }
  162. std::string start_time = this->CTest->CurrentTime();
  163. auto start_time_time = std::chrono::system_clock::now();
  164. auto elapsed_time_start = std::chrono::steady_clock::now();
  165. bool updated = vc->Update();
  166. std::string buildname =
  167. cmCTest::SafeBuildIdField(this->CTest->GetCTestConfiguration("BuildName"));
  168. cmXMLWriter xml(os);
  169. xml.StartDocument();
  170. xml.StartElement("Update");
  171. xml.Attribute("mode", "Client");
  172. xml.Attribute("Generator",
  173. std::string("ctest-") + cmVersion::GetCMakeVersion());
  174. xml.Element("Site", this->CTest->GetCTestConfiguration("Site"));
  175. xml.Element("BuildName", buildname);
  176. xml.Element("BuildStamp",
  177. this->CTest->GetCurrentTag() + "-" +
  178. this->CTest->GetTestModelString());
  179. xml.Element("StartDateTime", start_time);
  180. xml.Element("StartTime", start_time_time);
  181. xml.Element("UpdateCommand", vc->GetUpdateCommandLine());
  182. xml.Element("UpdateType",
  183. cmCTestUpdateHandlerUpdateToString(this->UpdateType));
  184. bool loadedMods = vc->WriteXML(xml);
  185. int localModifications = 0;
  186. int numUpdated = vc->GetPathCount(cmCTestVC::PathUpdated);
  187. if (numUpdated) {
  188. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT,
  189. " Found " << numUpdated << " updated files\n",
  190. this->Quiet);
  191. }
  192. if (int numModified = vc->GetPathCount(cmCTestVC::PathModified)) {
  193. cmCTestOptionalLog(
  194. this->CTest, HANDLER_OUTPUT,
  195. " Found " << numModified << " locally modified files\n", this->Quiet);
  196. localModifications += numModified;
  197. }
  198. if (int numConflicting = vc->GetPathCount(cmCTestVC::PathConflicting)) {
  199. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT,
  200. " Found " << numConflicting << " conflicting files\n",
  201. this->Quiet);
  202. localModifications += numConflicting;
  203. }
  204. cmCTestOptionalLog(this->CTest, DEBUG, "End" << std::endl, this->Quiet);
  205. std::string end_time = this->CTest->CurrentTime();
  206. xml.Element("EndDateTime", end_time);
  207. xml.Element("EndTime", std::chrono::system_clock::now());
  208. xml.Element("ElapsedMinutes",
  209. std::chrono::duration_cast<std::chrono::minutes>(
  210. std::chrono::steady_clock::now() - elapsed_time_start)
  211. .count());
  212. xml.StartElement("UpdateReturnStatus");
  213. if (localModifications) {
  214. xml.Content("Update error: "
  215. "There are modified or conflicting files in the repository");
  216. cmCTestLog(this->CTest, ERROR_MESSAGE,
  217. " There are modified or conflicting files in the repository"
  218. << std::endl);
  219. }
  220. if (!updated) {
  221. xml.Content("Update command failed:\n");
  222. xml.Content(vc->GetUpdateCommandLine());
  223. cmCTestLog(this->CTest, HANDLER_OUTPUT,
  224. " Update command failed: " << vc->GetUpdateCommandLine()
  225. << "\n");
  226. }
  227. xml.EndElement(); // UpdateReturnStatus
  228. xml.EndElement(); // Update
  229. xml.EndDocument();
  230. return updated && loadedMods ? numUpdated : -1;
  231. }
  232. int cmCTestUpdateHandler::DetectVCS(const char* dir)
  233. {
  234. std::string sourceDirectory = dir;
  235. cmCTestOptionalLog(this->CTest, DEBUG,
  236. "Check directory: " << sourceDirectory << std::endl,
  237. this->Quiet);
  238. sourceDirectory += "/.svn";
  239. if (cmSystemTools::FileExists(sourceDirectory)) {
  240. return cmCTestUpdateHandler::e_SVN;
  241. }
  242. sourceDirectory = dir;
  243. sourceDirectory += "/CVS";
  244. if (cmSystemTools::FileExists(sourceDirectory)) {
  245. return cmCTestUpdateHandler::e_CVS;
  246. }
  247. sourceDirectory = dir;
  248. sourceDirectory += "/.bzr";
  249. if (cmSystemTools::FileExists(sourceDirectory)) {
  250. return cmCTestUpdateHandler::e_BZR;
  251. }
  252. sourceDirectory = dir;
  253. sourceDirectory += "/.git";
  254. if (cmSystemTools::FileExists(sourceDirectory)) {
  255. return cmCTestUpdateHandler::e_GIT;
  256. }
  257. sourceDirectory = dir;
  258. sourceDirectory += "/.hg";
  259. if (cmSystemTools::FileExists(sourceDirectory)) {
  260. return cmCTestUpdateHandler::e_HG;
  261. }
  262. sourceDirectory = dir;
  263. sourceDirectory += "/.p4";
  264. if (cmSystemTools::FileExists(sourceDirectory)) {
  265. return cmCTestUpdateHandler::e_P4;
  266. }
  267. sourceDirectory = dir;
  268. sourceDirectory += "/.p4config";
  269. if (cmSystemTools::FileExists(sourceDirectory)) {
  270. return cmCTestUpdateHandler::e_P4;
  271. }
  272. return cmCTestUpdateHandler::e_UNKNOWN;
  273. }
  274. bool cmCTestUpdateHandler::SelectVCS()
  275. {
  276. // Get update command
  277. this->UpdateCommand = this->CTest->GetCTestConfiguration("UpdateCommand");
  278. // Detect the VCS managing the source tree.
  279. this->UpdateType = this->DetectVCS(this->GetOption("SourceDirectory"));
  280. if (this->UpdateType == e_UNKNOWN) {
  281. // The source tree does not have a recognized VCS. Check the
  282. // configuration value or command name.
  283. this->UpdateType = this->DetermineType(
  284. this->UpdateCommand.c_str(),
  285. this->CTest->GetCTestConfiguration("UpdateType").c_str());
  286. }
  287. // If no update command was specified, lookup one for this VCS tool.
  288. if (this->UpdateCommand.empty()) {
  289. const char* key = nullptr;
  290. switch (this->UpdateType) {
  291. case e_CVS:
  292. key = "CVSCommand";
  293. break;
  294. case e_SVN:
  295. key = "SVNCommand";
  296. break;
  297. case e_BZR:
  298. key = "BZRCommand";
  299. break;
  300. case e_GIT:
  301. key = "GITCommand";
  302. break;
  303. case e_HG:
  304. key = "HGCommand";
  305. break;
  306. case e_P4:
  307. key = "P4Command";
  308. break;
  309. default:
  310. break;
  311. }
  312. if (key) {
  313. this->UpdateCommand = this->CTest->GetCTestConfiguration(key);
  314. }
  315. if (this->UpdateCommand.empty()) {
  316. std::ostringstream e;
  317. e << "Cannot find UpdateCommand ";
  318. if (key) {
  319. e << "or " << key;
  320. }
  321. e << " configuration key.";
  322. cmCTestLog(this->CTest, ERROR_MESSAGE, e.str() << std::endl);
  323. return false;
  324. }
  325. }
  326. return true;
  327. }