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 "cmCLocaleEnvironmentScope.h"
  5. #include "cmCTest.h"
  6. #include "cmCTestBZR.h"
  7. #include "cmCTestCVS.h"
  8. #include "cmCTestGIT.h"
  9. #include "cmCTestHG.h"
  10. #include "cmCTestP4.h"
  11. #include "cmCTestSVN.h"
  12. #include "cmCTestVC.h"
  13. #include "cmGeneratedFileStream.h"
  14. #include "cmStringAlgorithms.h"
  15. #include "cmSystemTools.h"
  16. #include "cmVersion.h"
  17. #include "cmXMLWriter.h"
  18. #include <chrono>
  19. #include <sstream>
  20. #include "cm_memory.hxx"
  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 = cmStrCat(dir, "/CVS");
  247. if (cmSystemTools::FileExists(sourceDirectory)) {
  248. return cmCTestUpdateHandler::e_CVS;
  249. }
  250. sourceDirectory = cmStrCat(dir, "/.bzr");
  251. if (cmSystemTools::FileExists(sourceDirectory)) {
  252. return cmCTestUpdateHandler::e_BZR;
  253. }
  254. sourceDirectory = cmStrCat(dir, "/.git");
  255. if (cmSystemTools::FileExists(sourceDirectory)) {
  256. return cmCTestUpdateHandler::e_GIT;
  257. }
  258. sourceDirectory = cmStrCat(dir, "/.hg");
  259. if (cmSystemTools::FileExists(sourceDirectory)) {
  260. return cmCTestUpdateHandler::e_HG;
  261. }
  262. sourceDirectory = cmStrCat(dir, "/.p4");
  263. if (cmSystemTools::FileExists(sourceDirectory)) {
  264. return cmCTestUpdateHandler::e_P4;
  265. }
  266. sourceDirectory = cmStrCat(dir, "/.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. }