cmCTestUpdateHandler.cxx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. #include <type_traits>
  22. static const char* cmCTestUpdateHandlerUpdateStrings[] = {
  23. "Unknown", "CVS", "SVN", "BZR", "GIT", "HG", "P4"
  24. };
  25. static const char* cmCTestUpdateHandlerUpdateToString(int type)
  26. {
  27. if (type < cmCTestUpdateHandler::e_UNKNOWN ||
  28. type >= cmCTestUpdateHandler::e_LAST) {
  29. return cmCTestUpdateHandlerUpdateStrings[cmCTestUpdateHandler::e_UNKNOWN];
  30. }
  31. return cmCTestUpdateHandlerUpdateStrings[type];
  32. }
  33. cmCTestUpdateHandler::cmCTestUpdateHandler()
  34. {
  35. }
  36. void cmCTestUpdateHandler::Initialize()
  37. {
  38. this->Superclass::Initialize();
  39. this->UpdateCommand.clear();
  40. this->UpdateType = e_CVS;
  41. }
  42. int cmCTestUpdateHandler::DetermineType(const char* cmd, const char* type)
  43. {
  44. cmCTestOptionalLog(this->CTest, DEBUG, "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, " Use "
  122. << cmCTestUpdateHandlerUpdateToString(this->UpdateType)
  123. << " repository type" << std::endl;
  124. , this->Quiet);
  125. // Create an object to interact with the VCS tool.
  126. std::unique_ptr<cmCTestVC> vc;
  127. switch (this->UpdateType) {
  128. case e_CVS:
  129. vc = cm::make_unique<cmCTestCVS>(this->CTest, ofs);
  130. break;
  131. case e_SVN:
  132. vc = cm::make_unique<cmCTestSVN>(this->CTest, ofs);
  133. break;
  134. case e_BZR:
  135. vc = cm::make_unique<cmCTestBZR>(this->CTest, ofs);
  136. break;
  137. case e_GIT:
  138. vc = cm::make_unique<cmCTestGIT>(this->CTest, ofs);
  139. break;
  140. case e_HG:
  141. vc = cm::make_unique<cmCTestHG>(this->CTest, ofs);
  142. break;
  143. case e_P4:
  144. vc = cm::make_unique<cmCTestP4>(this->CTest, ofs);
  145. break;
  146. default:
  147. vc = cm::make_unique<cmCTestVC>(this->CTest, ofs);
  148. break;
  149. }
  150. vc->SetCommandLineTool(this->UpdateCommand);
  151. vc->SetSourceDirectory(sourceDirectory);
  152. // Cleanup the working tree.
  153. vc->Cleanup();
  154. //
  155. // Now update repository and remember what files were updated
  156. //
  157. cmGeneratedFileStream os;
  158. if (!this->StartResultingXML(cmCTest::PartUpdate, "Update", os)) {
  159. cmCTestLog(this->CTest, ERROR_MESSAGE, "Cannot open log file"
  160. << std::endl);
  161. return -1;
  162. }
  163. std::string start_time = this->CTest->CurrentTime();
  164. auto start_time_time = std::chrono::system_clock::now();
  165. auto elapsed_time_start = std::chrono::steady_clock::now();
  166. bool updated = vc->Update();
  167. std::string buildname =
  168. cmCTest::SafeBuildIdField(this->CTest->GetCTestConfiguration("BuildName"));
  169. cmXMLWriter xml(os);
  170. xml.StartDocument();
  171. xml.StartElement("Update");
  172. xml.Attribute("mode", "Client");
  173. xml.Attribute("Generator",
  174. std::string("ctest-") + cmVersion::GetCMakeVersion());
  175. xml.Element("Site", this->CTest->GetCTestConfiguration("Site"));
  176. xml.Element("BuildName", buildname);
  177. xml.Element("BuildStamp", 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(this->CTest, HANDLER_OUTPUT, " Found "
  194. << numModified << " locally modified files\n",
  195. 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, " Update command failed: "
  224. << vc->GetUpdateCommandLine() << "\n");
  225. }
  226. xml.EndElement(); // UpdateReturnStatus
  227. xml.EndElement(); // Update
  228. xml.EndDocument();
  229. return updated && loadedMods ? numUpdated : -1;
  230. }
  231. int cmCTestUpdateHandler::DetectVCS(const char* dir)
  232. {
  233. std::string sourceDirectory = dir;
  234. cmCTestOptionalLog(this->CTest, DEBUG,
  235. "Check directory: " << sourceDirectory << std::endl,
  236. this->Quiet);
  237. sourceDirectory += "/.svn";
  238. if (cmSystemTools::FileExists(sourceDirectory.c_str())) {
  239. return cmCTestUpdateHandler::e_SVN;
  240. }
  241. sourceDirectory = dir;
  242. sourceDirectory += "/CVS";
  243. if (cmSystemTools::FileExists(sourceDirectory.c_str())) {
  244. return cmCTestUpdateHandler::e_CVS;
  245. }
  246. sourceDirectory = dir;
  247. sourceDirectory += "/.bzr";
  248. if (cmSystemTools::FileExists(sourceDirectory.c_str())) {
  249. return cmCTestUpdateHandler::e_BZR;
  250. }
  251. sourceDirectory = dir;
  252. sourceDirectory += "/.git";
  253. if (cmSystemTools::FileExists(sourceDirectory.c_str())) {
  254. return cmCTestUpdateHandler::e_GIT;
  255. }
  256. sourceDirectory = dir;
  257. sourceDirectory += "/.hg";
  258. if (cmSystemTools::FileExists(sourceDirectory.c_str())) {
  259. return cmCTestUpdateHandler::e_HG;
  260. }
  261. sourceDirectory = dir;
  262. sourceDirectory += "/.p4";
  263. if (cmSystemTools::FileExists(sourceDirectory.c_str())) {
  264. return cmCTestUpdateHandler::e_P4;
  265. }
  266. sourceDirectory = dir;
  267. sourceDirectory += "/.p4config";
  268. if (cmSystemTools::FileExists(sourceDirectory.c_str())) {
  269. return cmCTestUpdateHandler::e_P4;
  270. }
  271. return cmCTestUpdateHandler::e_UNKNOWN;
  272. }
  273. bool cmCTestUpdateHandler::SelectVCS()
  274. {
  275. // Get update command
  276. this->UpdateCommand = this->CTest->GetCTestConfiguration("UpdateCommand");
  277. // Detect the VCS managing the source tree.
  278. this->UpdateType = this->DetectVCS(this->GetOption("SourceDirectory"));
  279. if (this->UpdateType == e_UNKNOWN) {
  280. // The source tree does not have a recognized VCS. Check the
  281. // configuration value or command name.
  282. this->UpdateType = this->DetermineType(
  283. this->UpdateCommand.c_str(),
  284. this->CTest->GetCTestConfiguration("UpdateType").c_str());
  285. }
  286. // If no update command was specified, lookup one for this VCS tool.
  287. if (this->UpdateCommand.empty()) {
  288. const char* key = nullptr;
  289. switch (this->UpdateType) {
  290. case e_CVS:
  291. key = "CVSCommand";
  292. break;
  293. case e_SVN:
  294. key = "SVNCommand";
  295. break;
  296. case e_BZR:
  297. key = "BZRCommand";
  298. break;
  299. case e_GIT:
  300. key = "GITCommand";
  301. break;
  302. case e_HG:
  303. key = "HGCommand";
  304. break;
  305. case e_P4:
  306. key = "P4Command";
  307. break;
  308. default:
  309. break;
  310. }
  311. if (key) {
  312. this->UpdateCommand = this->CTest->GetCTestConfiguration(key);
  313. }
  314. if (this->UpdateCommand.empty()) {
  315. std::ostringstream e;
  316. e << "Cannot find UpdateCommand ";
  317. if (key) {
  318. e << "or " << key;
  319. }
  320. e << " configuration key.";
  321. cmCTestLog(this->CTest, ERROR_MESSAGE, e.str() << std::endl);
  322. return false;
  323. }
  324. }
  325. return true;
  326. }