cmCTestUpdateHandler.cxx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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 "cm_auto_ptr.hxx"
  18. #include <sstream>
  19. static const char* cmCTestUpdateHandlerUpdateStrings[] = {
  20. "Unknown", "CVS", "SVN", "BZR", "GIT", "HG", "P4"
  21. };
  22. static const char* cmCTestUpdateHandlerUpdateToString(int type)
  23. {
  24. if (type < cmCTestUpdateHandler::e_UNKNOWN ||
  25. type >= cmCTestUpdateHandler::e_LAST) {
  26. return cmCTestUpdateHandlerUpdateStrings[cmCTestUpdateHandler::e_UNKNOWN];
  27. }
  28. return cmCTestUpdateHandlerUpdateStrings[type];
  29. }
  30. cmCTestUpdateHandler::cmCTestUpdateHandler()
  31. {
  32. }
  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, "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, " Use "
  119. << cmCTestUpdateHandlerUpdateToString(this->UpdateType)
  120. << " repository type" << std::endl;
  121. , this->Quiet);
  122. // Create an object to interact with the VCS tool.
  123. CM_AUTO_PTR<cmCTestVC> vc;
  124. switch (this->UpdateType) {
  125. case e_CVS:
  126. vc.reset(new cmCTestCVS(this->CTest, ofs));
  127. break;
  128. case e_SVN:
  129. vc.reset(new cmCTestSVN(this->CTest, ofs));
  130. break;
  131. case e_BZR:
  132. vc.reset(new cmCTestBZR(this->CTest, ofs));
  133. break;
  134. case e_GIT:
  135. vc.reset(new cmCTestGIT(this->CTest, ofs));
  136. break;
  137. case e_HG:
  138. vc.reset(new cmCTestHG(this->CTest, ofs));
  139. break;
  140. case e_P4:
  141. vc.reset(new cmCTestP4(this->CTest, ofs));
  142. break;
  143. default:
  144. vc.reset(new cmCTestVC(this->CTest, ofs));
  145. break;
  146. }
  147. vc->SetCommandLineTool(this->UpdateCommand);
  148. vc->SetSourceDirectory(sourceDirectory);
  149. // Cleanup the working tree.
  150. vc->Cleanup();
  151. //
  152. // Now update repository and remember what files were updated
  153. //
  154. cmGeneratedFileStream os;
  155. if (!this->StartResultingXML(cmCTest::PartUpdate, "Update", os)) {
  156. cmCTestLog(this->CTest, ERROR_MESSAGE, "Cannot open log file"
  157. << std::endl);
  158. return -1;
  159. }
  160. std::string start_time = this->CTest->CurrentTime();
  161. unsigned int start_time_time =
  162. static_cast<unsigned int>(cmSystemTools::GetTime());
  163. double elapsed_time_start = cmSystemTools::GetTime();
  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", this->CTest->GetCurrentTag() + "-" +
  176. this->CTest->GetTestModelString());
  177. xml.Element("StartDateTime", start_time);
  178. xml.Element("StartTime", start_time_time);
  179. xml.Element("UpdateCommand", vc->GetUpdateCommandLine());
  180. xml.Element("UpdateType",
  181. cmCTestUpdateHandlerUpdateToString(this->UpdateType));
  182. bool loadedMods = vc->WriteXML(xml);
  183. int localModifications = 0;
  184. int numUpdated = vc->GetPathCount(cmCTestVC::PathUpdated);
  185. if (numUpdated) {
  186. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT,
  187. " Found " << numUpdated << " updated files\n",
  188. this->Quiet);
  189. }
  190. if (int numModified = vc->GetPathCount(cmCTestVC::PathModified)) {
  191. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT, " Found "
  192. << numModified << " locally modified files\n",
  193. this->Quiet);
  194. localModifications += numModified;
  195. }
  196. if (int numConflicting = vc->GetPathCount(cmCTestVC::PathConflicting)) {
  197. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT,
  198. " Found " << numConflicting << " conflicting files\n",
  199. this->Quiet);
  200. localModifications += numConflicting;
  201. }
  202. cmCTestOptionalLog(this->CTest, DEBUG, "End" << std::endl, this->Quiet);
  203. std::string end_time = this->CTest->CurrentTime();
  204. xml.Element("EndDateTime", end_time);
  205. xml.Element("EndTime", static_cast<unsigned int>(cmSystemTools::GetTime()));
  206. xml.Element(
  207. "ElapsedMinutes",
  208. static_cast<int>((cmSystemTools::GetTime() - elapsed_time_start) / 6) /
  209. 10.0);
  210. xml.StartElement("UpdateReturnStatus");
  211. if (localModifications) {
  212. xml.Content("Update error: "
  213. "There are modified or conflicting files in the repository");
  214. cmCTestLog(this->CTest, ERROR_MESSAGE,
  215. " There are modified or conflicting files in the repository"
  216. << std::endl);
  217. }
  218. if (!updated) {
  219. xml.Content("Update command failed:\n");
  220. xml.Content(vc->GetUpdateCommandLine());
  221. cmCTestLog(this->CTest, HANDLER_OUTPUT, " Update command failed: "
  222. << vc->GetUpdateCommandLine() << "\n");
  223. }
  224. xml.EndElement(); // UpdateReturnStatus
  225. xml.EndElement(); // Update
  226. xml.EndDocument();
  227. return updated && loadedMods ? numUpdated : -1;
  228. }
  229. int cmCTestUpdateHandler::DetectVCS(const char* dir)
  230. {
  231. std::string sourceDirectory = dir;
  232. cmCTestOptionalLog(this->CTest, DEBUG,
  233. "Check directory: " << sourceDirectory << std::endl,
  234. this->Quiet);
  235. sourceDirectory += "/.svn";
  236. if (cmSystemTools::FileExists(sourceDirectory.c_str())) {
  237. return cmCTestUpdateHandler::e_SVN;
  238. }
  239. sourceDirectory = dir;
  240. sourceDirectory += "/CVS";
  241. if (cmSystemTools::FileExists(sourceDirectory.c_str())) {
  242. return cmCTestUpdateHandler::e_CVS;
  243. }
  244. sourceDirectory = dir;
  245. sourceDirectory += "/.bzr";
  246. if (cmSystemTools::FileExists(sourceDirectory.c_str())) {
  247. return cmCTestUpdateHandler::e_BZR;
  248. }
  249. sourceDirectory = dir;
  250. sourceDirectory += "/.git";
  251. if (cmSystemTools::FileExists(sourceDirectory.c_str())) {
  252. return cmCTestUpdateHandler::e_GIT;
  253. }
  254. sourceDirectory = dir;
  255. sourceDirectory += "/.hg";
  256. if (cmSystemTools::FileExists(sourceDirectory.c_str())) {
  257. return cmCTestUpdateHandler::e_HG;
  258. }
  259. sourceDirectory = dir;
  260. sourceDirectory += "/.p4";
  261. if (cmSystemTools::FileExists(sourceDirectory.c_str())) {
  262. return cmCTestUpdateHandler::e_P4;
  263. }
  264. sourceDirectory = dir;
  265. sourceDirectory += "/.p4config";
  266. if (cmSystemTools::FileExists(sourceDirectory.c_str())) {
  267. return cmCTestUpdateHandler::e_P4;
  268. }
  269. return cmCTestUpdateHandler::e_UNKNOWN;
  270. }
  271. bool cmCTestUpdateHandler::SelectVCS()
  272. {
  273. // Get update command
  274. this->UpdateCommand = this->CTest->GetCTestConfiguration("UpdateCommand");
  275. // Detect the VCS managing the source tree.
  276. this->UpdateType = this->DetectVCS(this->GetOption("SourceDirectory"));
  277. if (this->UpdateType == e_UNKNOWN) {
  278. // The source tree does not have a recognized VCS. Check the
  279. // configuration value or command name.
  280. this->UpdateType = this->DetermineType(
  281. this->UpdateCommand.c_str(),
  282. this->CTest->GetCTestConfiguration("UpdateType").c_str());
  283. }
  284. // If no update command was specified, lookup one for this VCS tool.
  285. if (this->UpdateCommand.empty()) {
  286. const char* key = nullptr;
  287. switch (this->UpdateType) {
  288. case e_CVS:
  289. key = "CVSCommand";
  290. break;
  291. case e_SVN:
  292. key = "SVNCommand";
  293. break;
  294. case e_BZR:
  295. key = "BZRCommand";
  296. break;
  297. case e_GIT:
  298. key = "GITCommand";
  299. break;
  300. case e_HG:
  301. key = "HGCommand";
  302. break;
  303. case e_P4:
  304. key = "P4Command";
  305. break;
  306. default:
  307. break;
  308. }
  309. if (key) {
  310. this->UpdateCommand = this->CTest->GetCTestConfiguration(key);
  311. }
  312. if (this->UpdateCommand.empty()) {
  313. std::ostringstream e;
  314. e << "Cannot find UpdateCommand ";
  315. if (key) {
  316. e << "or " << key;
  317. }
  318. e << " configuration key.";
  319. cmCTestLog(this->CTest, ERROR_MESSAGE, e.str() << std::endl);
  320. return false;
  321. }
  322. }
  323. return true;
  324. }