cmCTestUpdateHandler.cxx 11 KB

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