1
0

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