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 <memory> // IWYU pragma: keep
  19. #include <sstream>
  20. static const char* cmCTestUpdateHandlerUpdateStrings[] = {
  21. "Unknown", "CVS", "SVN", "BZR", "GIT", "HG", "P4"
  22. };
  23. static const char* cmCTestUpdateHandlerUpdateToString(int type)
  24. {
  25. if (type < cmCTestUpdateHandler::e_UNKNOWN ||
  26. type >= cmCTestUpdateHandler::e_LAST) {
  27. return cmCTestUpdateHandlerUpdateStrings[cmCTestUpdateHandler::e_UNKNOWN];
  28. }
  29. return cmCTestUpdateHandlerUpdateStrings[type];
  30. }
  31. cmCTestUpdateHandler::cmCTestUpdateHandler()
  32. {
  33. }
  34. void cmCTestUpdateHandler::Initialize()
  35. {
  36. this->Superclass::Initialize();
  37. this->UpdateCommand.clear();
  38. this->UpdateType = e_CVS;
  39. }
  40. int cmCTestUpdateHandler::DetermineType(const char* cmd, const char* type)
  41. {
  42. cmCTestOptionalLog(this->CTest, DEBUG, "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, " Use "
  120. << cmCTestUpdateHandlerUpdateToString(this->UpdateType)
  121. << " repository type" << std::endl;
  122. , this->Quiet);
  123. // Create an object to interact with the VCS tool.
  124. std::unique_ptr<cmCTestVC> vc;
  125. switch (this->UpdateType) {
  126. case e_CVS:
  127. vc = cm::make_unique<cmCTestCVS>(this->CTest, ofs);
  128. break;
  129. case e_SVN:
  130. vc = cm::make_unique<cmCTestSVN>(this->CTest, ofs);
  131. break;
  132. case e_BZR:
  133. vc = cm::make_unique<cmCTestBZR>(this->CTest, ofs);
  134. break;
  135. case e_GIT:
  136. vc = cm::make_unique<cmCTestGIT>(this->CTest, ofs);
  137. break;
  138. case e_HG:
  139. vc = cm::make_unique<cmCTestHG>(this->CTest, ofs);
  140. break;
  141. case e_P4:
  142. vc = cm::make_unique<cmCTestP4>(this->CTest, ofs);
  143. break;
  144. default:
  145. vc = cm::make_unique<cmCTestVC>(this->CTest, ofs);
  146. break;
  147. }
  148. vc->SetCommandLineTool(this->UpdateCommand);
  149. vc->SetSourceDirectory(sourceDirectory);
  150. // Cleanup the working tree.
  151. vc->Cleanup();
  152. //
  153. // Now update repository and remember what files were updated
  154. //
  155. cmGeneratedFileStream os;
  156. if (!this->StartResultingXML(cmCTest::PartUpdate, "Update", os)) {
  157. cmCTestLog(this->CTest, ERROR_MESSAGE, "Cannot open log file"
  158. << std::endl);
  159. return -1;
  160. }
  161. std::string start_time = this->CTest->CurrentTime();
  162. unsigned int start_time_time =
  163. static_cast<unsigned int>(cmSystemTools::GetTime());
  164. double elapsed_time_start = cmSystemTools::GetTime();
  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", static_cast<unsigned int>(cmSystemTools::GetTime()));
  207. xml.Element(
  208. "ElapsedMinutes",
  209. static_cast<int>((cmSystemTools::GetTime() - elapsed_time_start) / 6) /
  210. 10.0);
  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.c_str())) {
  238. return cmCTestUpdateHandler::e_SVN;
  239. }
  240. sourceDirectory = dir;
  241. sourceDirectory += "/CVS";
  242. if (cmSystemTools::FileExists(sourceDirectory.c_str())) {
  243. return cmCTestUpdateHandler::e_CVS;
  244. }
  245. sourceDirectory = dir;
  246. sourceDirectory += "/.bzr";
  247. if (cmSystemTools::FileExists(sourceDirectory.c_str())) {
  248. return cmCTestUpdateHandler::e_BZR;
  249. }
  250. sourceDirectory = dir;
  251. sourceDirectory += "/.git";
  252. if (cmSystemTools::FileExists(sourceDirectory.c_str())) {
  253. return cmCTestUpdateHandler::e_GIT;
  254. }
  255. sourceDirectory = dir;
  256. sourceDirectory += "/.hg";
  257. if (cmSystemTools::FileExists(sourceDirectory.c_str())) {
  258. return cmCTestUpdateHandler::e_HG;
  259. }
  260. sourceDirectory = dir;
  261. sourceDirectory += "/.p4";
  262. if (cmSystemTools::FileExists(sourceDirectory.c_str())) {
  263. return cmCTestUpdateHandler::e_P4;
  264. }
  265. sourceDirectory = dir;
  266. sourceDirectory += "/.p4config";
  267. if (cmSystemTools::FileExists(sourceDirectory.c_str())) {
  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. }