cmCTestUpdateHandler.cxx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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, "Determine update type from command: "
  44. << cmd << " and type: " << type << std::endl,
  45. this->Quiet);
  46. if (type && *type) {
  47. cmCTestOptionalLog(this->CTest, DEBUG,
  48. "Type specified: " << type << std::endl, this->Quiet);
  49. std::string stype = cmSystemTools::LowerCase(type);
  50. if (stype.find("cvs") != std::string::npos) {
  51. return cmCTestUpdateHandler::e_CVS;
  52. }
  53. if (stype.find("svn") != std::string::npos) {
  54. return cmCTestUpdateHandler::e_SVN;
  55. }
  56. if (stype.find("bzr") != std::string::npos) {
  57. return cmCTestUpdateHandler::e_BZR;
  58. }
  59. if (stype.find("git") != std::string::npos) {
  60. return cmCTestUpdateHandler::e_GIT;
  61. }
  62. if (stype.find("hg") != std::string::npos) {
  63. return cmCTestUpdateHandler::e_HG;
  64. }
  65. if (stype.find("p4") != std::string::npos) {
  66. return cmCTestUpdateHandler::e_P4;
  67. }
  68. } else {
  69. cmCTestOptionalLog(
  70. this->CTest, DEBUG,
  71. "Type not specified, check command: " << cmd << std::endl, this->Quiet);
  72. std::string stype = cmSystemTools::LowerCase(cmd);
  73. if (stype.find("cvs") != std::string::npos) {
  74. return cmCTestUpdateHandler::e_CVS;
  75. }
  76. if (stype.find("svn") != std::string::npos) {
  77. return cmCTestUpdateHandler::e_SVN;
  78. }
  79. if (stype.find("bzr") != std::string::npos) {
  80. return cmCTestUpdateHandler::e_BZR;
  81. }
  82. if (stype.find("git") != std::string::npos) {
  83. return cmCTestUpdateHandler::e_GIT;
  84. }
  85. if (stype.find("hg") != std::string::npos) {
  86. return cmCTestUpdateHandler::e_HG;
  87. }
  88. if (stype.find("p4") != std::string::npos) {
  89. return cmCTestUpdateHandler::e_P4;
  90. }
  91. }
  92. return cmCTestUpdateHandler::e_UNKNOWN;
  93. }
  94. // clearly it would be nice if this were broken up into a few smaller
  95. // functions and commented...
  96. int cmCTestUpdateHandler::ProcessHandler()
  97. {
  98. // Make sure VCS tool messages are in English so we can parse them.
  99. cmCLocaleEnvironmentScope fixLocale;
  100. static_cast<void>(fixLocale);
  101. // Get source dir
  102. const char* sourceDirectory = this->GetOption("SourceDirectory");
  103. if (!sourceDirectory) {
  104. cmCTestLog(this->CTest, ERROR_MESSAGE,
  105. "Cannot find SourceDirectory key in the DartConfiguration.tcl"
  106. << std::endl);
  107. return -1;
  108. }
  109. cmGeneratedFileStream ofs;
  110. if (!this->CTest->GetShowOnly()) {
  111. this->StartLogFile("Update", ofs);
  112. }
  113. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT,
  114. " Updating the repository: " << sourceDirectory
  115. << std::endl,
  116. this->Quiet);
  117. if (!this->SelectVCS()) {
  118. return -1;
  119. }
  120. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT, " 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, "Cannot open log file"
  159. << 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", this->CTest->GetCurrentTag() + "-" +
  177. this->CTest->GetTestModelString());
  178. xml.Element("StartDateTime", start_time);
  179. xml.Element("StartTime", start_time_time);
  180. xml.Element("UpdateCommand", vc->GetUpdateCommandLine());
  181. xml.Element("UpdateType",
  182. cmCTestUpdateHandlerUpdateToString(this->UpdateType));
  183. bool loadedMods = vc->WriteXML(xml);
  184. int localModifications = 0;
  185. int numUpdated = vc->GetPathCount(cmCTestVC::PathUpdated);
  186. if (numUpdated) {
  187. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT,
  188. " Found " << numUpdated << " updated files\n",
  189. this->Quiet);
  190. }
  191. if (int numModified = vc->GetPathCount(cmCTestVC::PathModified)) {
  192. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT, " Found "
  193. << numModified << " locally modified files\n",
  194. this->Quiet);
  195. localModifications += numModified;
  196. }
  197. if (int numConflicting = vc->GetPathCount(cmCTestVC::PathConflicting)) {
  198. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT,
  199. " Found " << numConflicting << " conflicting files\n",
  200. this->Quiet);
  201. localModifications += numConflicting;
  202. }
  203. cmCTestOptionalLog(this->CTest, DEBUG, "End" << std::endl, this->Quiet);
  204. std::string end_time = this->CTest->CurrentTime();
  205. xml.Element("EndDateTime", end_time);
  206. xml.Element("EndTime", std::chrono::system_clock::now());
  207. xml.Element("ElapsedMinutes",
  208. std::chrono::duration_cast<std::chrono::minutes>(
  209. std::chrono::steady_clock::now() - elapsed_time_start)
  210. .count());
  211. xml.StartElement("UpdateReturnStatus");
  212. if (localModifications) {
  213. xml.Content("Update error: "
  214. "There are modified or conflicting files in the repository");
  215. cmCTestLog(this->CTest, ERROR_MESSAGE,
  216. " There are modified or conflicting files in the repository"
  217. << std::endl);
  218. }
  219. if (!updated) {
  220. xml.Content("Update command failed:\n");
  221. xml.Content(vc->GetUpdateCommandLine());
  222. cmCTestLog(this->CTest, HANDLER_OUTPUT, " Update command failed: "
  223. << vc->GetUpdateCommandLine() << "\n");
  224. }
  225. xml.EndElement(); // UpdateReturnStatus
  226. xml.EndElement(); // Update
  227. xml.EndDocument();
  228. return updated && loadedMods ? numUpdated : -1;
  229. }
  230. int cmCTestUpdateHandler::DetectVCS(const char* dir)
  231. {
  232. std::string sourceDirectory = dir;
  233. cmCTestOptionalLog(this->CTest, DEBUG,
  234. "Check directory: " << sourceDirectory << std::endl,
  235. this->Quiet);
  236. sourceDirectory += "/.svn";
  237. if (cmSystemTools::FileExists(sourceDirectory)) {
  238. return cmCTestUpdateHandler::e_SVN;
  239. }
  240. sourceDirectory = dir;
  241. sourceDirectory += "/CVS";
  242. if (cmSystemTools::FileExists(sourceDirectory)) {
  243. return cmCTestUpdateHandler::e_CVS;
  244. }
  245. sourceDirectory = dir;
  246. sourceDirectory += "/.bzr";
  247. if (cmSystemTools::FileExists(sourceDirectory)) {
  248. return cmCTestUpdateHandler::e_BZR;
  249. }
  250. sourceDirectory = dir;
  251. sourceDirectory += "/.git";
  252. if (cmSystemTools::FileExists(sourceDirectory)) {
  253. return cmCTestUpdateHandler::e_GIT;
  254. }
  255. sourceDirectory = dir;
  256. sourceDirectory += "/.hg";
  257. if (cmSystemTools::FileExists(sourceDirectory)) {
  258. return cmCTestUpdateHandler::e_HG;
  259. }
  260. sourceDirectory = dir;
  261. sourceDirectory += "/.p4";
  262. if (cmSystemTools::FileExists(sourceDirectory)) {
  263. return cmCTestUpdateHandler::e_P4;
  264. }
  265. sourceDirectory = dir;
  266. sourceDirectory += "/.p4config";
  267. if (cmSystemTools::FileExists(sourceDirectory)) {
  268. return cmCTestUpdateHandler::e_P4;
  269. }
  270. return cmCTestUpdateHandler::e_UNKNOWN;
  271. }
  272. bool cmCTestUpdateHandler::SelectVCS()
  273. {
  274. // Get update command
  275. this->UpdateCommand = this->CTest->GetCTestConfiguration("UpdateCommand");
  276. // Detect the VCS managing the source tree.
  277. this->UpdateType = this->DetectVCS(this->GetOption("SourceDirectory"));
  278. if (this->UpdateType == e_UNKNOWN) {
  279. // The source tree does not have a recognized VCS. Check the
  280. // configuration value or command name.
  281. this->UpdateType = this->DetermineType(
  282. this->UpdateCommand.c_str(),
  283. this->CTest->GetCTestConfiguration("UpdateType").c_str());
  284. }
  285. // If no update command was specified, lookup one for this VCS tool.
  286. if (this->UpdateCommand.empty()) {
  287. const char* key = nullptr;
  288. switch (this->UpdateType) {
  289. case e_CVS:
  290. key = "CVSCommand";
  291. break;
  292. case e_SVN:
  293. key = "SVNCommand";
  294. break;
  295. case e_BZR:
  296. key = "BZRCommand";
  297. break;
  298. case e_GIT:
  299. key = "GITCommand";
  300. break;
  301. case e_HG:
  302. key = "HGCommand";
  303. break;
  304. case e_P4:
  305. key = "P4Command";
  306. break;
  307. default:
  308. break;
  309. }
  310. if (key) {
  311. this->UpdateCommand = this->CTest->GetCTestConfiguration(key);
  312. }
  313. if (this->UpdateCommand.empty()) {
  314. std::ostringstream e;
  315. e << "Cannot find UpdateCommand ";
  316. if (key) {
  317. e << "or " << key;
  318. }
  319. e << " configuration key.";
  320. cmCTestLog(this->CTest, ERROR_MESSAGE, e.str() << std::endl);
  321. return false;
  322. }
  323. }
  324. return true;
  325. }