cmCTestUpdateHandler.cxx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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. std::string changeId = this->CTest->GetCTestConfiguration("ChangeId");
  185. if (!changeId.empty()) {
  186. xml.Element("ChangeId", changeId);
  187. }
  188. bool loadedMods = vc->WriteXML(xml);
  189. int localModifications = 0;
  190. int numUpdated = vc->GetPathCount(cmCTestVC::PathUpdated);
  191. if (numUpdated) {
  192. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT,
  193. " Found " << numUpdated << " updated files\n",
  194. this->Quiet);
  195. }
  196. if (int numModified = vc->GetPathCount(cmCTestVC::PathModified)) {
  197. cmCTestOptionalLog(
  198. this->CTest, HANDLER_OUTPUT,
  199. " Found " << numModified << " locally modified files\n", this->Quiet);
  200. localModifications += numModified;
  201. }
  202. if (int numConflicting = vc->GetPathCount(cmCTestVC::PathConflicting)) {
  203. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT,
  204. " Found " << numConflicting << " conflicting files\n",
  205. this->Quiet);
  206. localModifications += numConflicting;
  207. }
  208. cmCTestOptionalLog(this->CTest, DEBUG, "End" << std::endl, this->Quiet);
  209. std::string end_time = this->CTest->CurrentTime();
  210. xml.Element("EndDateTime", end_time);
  211. xml.Element("EndTime", std::chrono::system_clock::now());
  212. xml.Element("ElapsedMinutes",
  213. std::chrono::duration_cast<std::chrono::minutes>(
  214. std::chrono::steady_clock::now() - elapsed_time_start)
  215. .count());
  216. xml.StartElement("UpdateReturnStatus");
  217. if (localModifications) {
  218. xml.Content("Update error: "
  219. "There are modified or conflicting files in the repository");
  220. cmCTestLog(this->CTest, ERROR_MESSAGE,
  221. " There are modified or conflicting files in the repository"
  222. << std::endl);
  223. }
  224. if (!updated) {
  225. xml.Content("Update command failed:\n");
  226. xml.Content(vc->GetUpdateCommandLine());
  227. cmCTestLog(this->CTest, HANDLER_OUTPUT,
  228. " Update command failed: " << vc->GetUpdateCommandLine()
  229. << "\n");
  230. }
  231. xml.EndElement(); // UpdateReturnStatus
  232. xml.EndElement(); // Update
  233. xml.EndDocument();
  234. return updated && loadedMods ? numUpdated : -1;
  235. }
  236. int cmCTestUpdateHandler::DetectVCS(const char* dir)
  237. {
  238. std::string sourceDirectory = dir;
  239. cmCTestOptionalLog(this->CTest, DEBUG,
  240. "Check directory: " << sourceDirectory << std::endl,
  241. this->Quiet);
  242. sourceDirectory += "/.svn";
  243. if (cmSystemTools::FileExists(sourceDirectory)) {
  244. return cmCTestUpdateHandler::e_SVN;
  245. }
  246. sourceDirectory = dir;
  247. sourceDirectory += "/CVS";
  248. if (cmSystemTools::FileExists(sourceDirectory)) {
  249. return cmCTestUpdateHandler::e_CVS;
  250. }
  251. sourceDirectory = dir;
  252. sourceDirectory += "/.bzr";
  253. if (cmSystemTools::FileExists(sourceDirectory)) {
  254. return cmCTestUpdateHandler::e_BZR;
  255. }
  256. sourceDirectory = dir;
  257. sourceDirectory += "/.git";
  258. if (cmSystemTools::FileExists(sourceDirectory)) {
  259. return cmCTestUpdateHandler::e_GIT;
  260. }
  261. sourceDirectory = dir;
  262. sourceDirectory += "/.hg";
  263. if (cmSystemTools::FileExists(sourceDirectory)) {
  264. return cmCTestUpdateHandler::e_HG;
  265. }
  266. sourceDirectory = dir;
  267. sourceDirectory += "/.p4";
  268. if (cmSystemTools::FileExists(sourceDirectory)) {
  269. return cmCTestUpdateHandler::e_P4;
  270. }
  271. sourceDirectory = dir;
  272. sourceDirectory += "/.p4config";
  273. if (cmSystemTools::FileExists(sourceDirectory)) {
  274. return cmCTestUpdateHandler::e_P4;
  275. }
  276. return cmCTestUpdateHandler::e_UNKNOWN;
  277. }
  278. bool cmCTestUpdateHandler::SelectVCS()
  279. {
  280. // Get update command
  281. this->UpdateCommand = this->CTest->GetCTestConfiguration("UpdateCommand");
  282. // Detect the VCS managing the source tree.
  283. this->UpdateType = this->DetectVCS(this->GetOption("SourceDirectory"));
  284. if (this->UpdateType == e_UNKNOWN) {
  285. // The source tree does not have a recognized VCS. Check the
  286. // configuration value or command name.
  287. this->UpdateType = this->DetermineType(
  288. this->UpdateCommand.c_str(),
  289. this->CTest->GetCTestConfiguration("UpdateType").c_str());
  290. }
  291. // If no update command was specified, lookup one for this VCS tool.
  292. if (this->UpdateCommand.empty()) {
  293. const char* key = nullptr;
  294. switch (this->UpdateType) {
  295. case e_CVS:
  296. key = "CVSCommand";
  297. break;
  298. case e_SVN:
  299. key = "SVNCommand";
  300. break;
  301. case e_BZR:
  302. key = "BZRCommand";
  303. break;
  304. case e_GIT:
  305. key = "GITCommand";
  306. break;
  307. case e_HG:
  308. key = "HGCommand";
  309. break;
  310. case e_P4:
  311. key = "P4Command";
  312. break;
  313. default:
  314. break;
  315. }
  316. if (key) {
  317. this->UpdateCommand = this->CTest->GetCTestConfiguration(key);
  318. }
  319. if (this->UpdateCommand.empty()) {
  320. std::ostringstream e;
  321. e << "Cannot find UpdateCommand ";
  322. if (key) {
  323. e << "or " << key;
  324. }
  325. e << " configuration key.";
  326. cmCTestLog(this->CTest, ERROR_MESSAGE, e.str() << std::endl);
  327. return false;
  328. }
  329. }
  330. return true;
  331. }