cmCTestUpdateHandler.cxx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmCTestUpdateHandler.h"
  11. #include "cmCLocaleEnvironmentScope.h"
  12. #include "cmCTest.h"
  13. #include "cmCTestBZR.h"
  14. #include "cmCTestCVS.h"
  15. #include "cmCTestGIT.h"
  16. #include "cmCTestHG.h"
  17. #include "cmCTestP4.h"
  18. #include "cmCTestSVN.h"
  19. #include "cmCTestVC.h"
  20. #include "cmGeneratedFileStream.h"
  21. #include "cmSystemTools.h"
  22. #include "cmVersion.h"
  23. #include "cmXMLWriter.h"
  24. #include <cm_auto_ptr.hxx>
  25. #include <sstream>
  26. static const char* cmCTestUpdateHandlerUpdateStrings[] = {
  27. "Unknown", "CVS", "SVN", "BZR", "GIT", "HG", "P4"
  28. };
  29. static const char* cmCTestUpdateHandlerUpdateToString(int type)
  30. {
  31. if (type < cmCTestUpdateHandler::e_UNKNOWN ||
  32. type >= cmCTestUpdateHandler::e_LAST) {
  33. return cmCTestUpdateHandlerUpdateStrings[cmCTestUpdateHandler::e_UNKNOWN];
  34. }
  35. return cmCTestUpdateHandlerUpdateStrings[type];
  36. }
  37. cmCTestUpdateHandler::cmCTestUpdateHandler()
  38. {
  39. }
  40. void cmCTestUpdateHandler::Initialize()
  41. {
  42. this->Superclass::Initialize();
  43. this->UpdateCommand = "";
  44. this->UpdateType = e_CVS;
  45. }
  46. int cmCTestUpdateHandler::DetermineType(const char* cmd, const char* type)
  47. {
  48. cmCTestOptionalLog(this->CTest, DEBUG, "Determine update type from command: "
  49. << cmd << " and type: " << type << std::endl,
  50. this->Quiet);
  51. if (type && *type) {
  52. cmCTestOptionalLog(this->CTest, DEBUG,
  53. "Type specified: " << type << std::endl, this->Quiet);
  54. std::string stype = cmSystemTools::LowerCase(type);
  55. if (stype.find("cvs") != std::string::npos) {
  56. return cmCTestUpdateHandler::e_CVS;
  57. }
  58. if (stype.find("svn") != std::string::npos) {
  59. return cmCTestUpdateHandler::e_SVN;
  60. }
  61. if (stype.find("bzr") != std::string::npos) {
  62. return cmCTestUpdateHandler::e_BZR;
  63. }
  64. if (stype.find("git") != std::string::npos) {
  65. return cmCTestUpdateHandler::e_GIT;
  66. }
  67. if (stype.find("hg") != std::string::npos) {
  68. return cmCTestUpdateHandler::e_HG;
  69. }
  70. if (stype.find("p4") != std::string::npos) {
  71. return cmCTestUpdateHandler::e_P4;
  72. }
  73. } else {
  74. cmCTestOptionalLog(
  75. this->CTest, DEBUG,
  76. "Type not specified, check command: " << cmd << std::endl, this->Quiet);
  77. std::string stype = cmSystemTools::LowerCase(cmd);
  78. if (stype.find("cvs") != std::string::npos) {
  79. return cmCTestUpdateHandler::e_CVS;
  80. }
  81. if (stype.find("svn") != std::string::npos) {
  82. return cmCTestUpdateHandler::e_SVN;
  83. }
  84. if (stype.find("bzr") != std::string::npos) {
  85. return cmCTestUpdateHandler::e_BZR;
  86. }
  87. if (stype.find("git") != std::string::npos) {
  88. return cmCTestUpdateHandler::e_GIT;
  89. }
  90. if (stype.find("hg") != std::string::npos) {
  91. return cmCTestUpdateHandler::e_HG;
  92. }
  93. if (stype.find("p4") != std::string::npos) {
  94. return cmCTestUpdateHandler::e_P4;
  95. }
  96. }
  97. return cmCTestUpdateHandler::e_UNKNOWN;
  98. }
  99. // clearly it would be nice if this were broken up into a few smaller
  100. // functions and commented...
  101. int cmCTestUpdateHandler::ProcessHandler()
  102. {
  103. // Make sure VCS tool messages are in English so we can parse them.
  104. cmCLocaleEnvironmentScope fixLocale;
  105. static_cast<void>(fixLocale);
  106. // Get source dir
  107. const char* sourceDirectory = this->GetOption("SourceDirectory");
  108. if (!sourceDirectory) {
  109. cmCTestLog(this->CTest, ERROR_MESSAGE,
  110. "Cannot find SourceDirectory key in the DartConfiguration.tcl"
  111. << std::endl);
  112. return -1;
  113. }
  114. cmGeneratedFileStream ofs;
  115. if (!this->CTest->GetShowOnly()) {
  116. this->StartLogFile("Update", ofs);
  117. }
  118. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT,
  119. " Updating the repository: " << sourceDirectory
  120. << std::endl,
  121. this->Quiet);
  122. if (!this->SelectVCS()) {
  123. return -1;
  124. }
  125. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT, " Use "
  126. << cmCTestUpdateHandlerUpdateToString(this->UpdateType)
  127. << " repository type" << std::endl;
  128. , this->Quiet);
  129. // Create an object to interact with the VCS tool.
  130. CM_AUTO_PTR<cmCTestVC> vc;
  131. switch (this->UpdateType) {
  132. case e_CVS:
  133. vc.reset(new cmCTestCVS(this->CTest, ofs));
  134. break;
  135. case e_SVN:
  136. vc.reset(new cmCTestSVN(this->CTest, ofs));
  137. break;
  138. case e_BZR:
  139. vc.reset(new cmCTestBZR(this->CTest, ofs));
  140. break;
  141. case e_GIT:
  142. vc.reset(new cmCTestGIT(this->CTest, ofs));
  143. break;
  144. case e_HG:
  145. vc.reset(new cmCTestHG(this->CTest, ofs));
  146. break;
  147. case e_P4:
  148. vc.reset(new cmCTestP4(this->CTest, ofs));
  149. break;
  150. default:
  151. vc.reset(new cmCTestVC(this->CTest, ofs));
  152. break;
  153. }
  154. vc->SetCommandLineTool(this->UpdateCommand);
  155. vc->SetSourceDirectory(sourceDirectory);
  156. // Cleanup the working tree.
  157. vc->Cleanup();
  158. //
  159. // Now update repository and remember what files were updated
  160. //
  161. cmGeneratedFileStream os;
  162. if (!this->StartResultingXML(cmCTest::PartUpdate, "Update", os)) {
  163. cmCTestLog(this->CTest, ERROR_MESSAGE, "Cannot open log file"
  164. << std::endl);
  165. return -1;
  166. }
  167. std::string start_time = this->CTest->CurrentTime();
  168. unsigned int start_time_time =
  169. static_cast<unsigned int>(cmSystemTools::GetTime());
  170. double elapsed_time_start = cmSystemTools::GetTime();
  171. bool updated = vc->Update();
  172. std::string buildname =
  173. cmCTest::SafeBuildIdField(this->CTest->GetCTestConfiguration("BuildName"));
  174. cmXMLWriter xml(os);
  175. xml.StartDocument();
  176. xml.StartElement("Update");
  177. xml.Attribute("mode", "Client");
  178. xml.Attribute("Generator",
  179. std::string("ctest-") + cmVersion::GetCMakeVersion());
  180. xml.Element("Site", this->CTest->GetCTestConfiguration("Site"));
  181. xml.Element("BuildName", buildname);
  182. xml.Element("BuildStamp", this->CTest->GetCurrentTag() + "-" +
  183. this->CTest->GetTestModelString());
  184. xml.Element("StartDateTime", start_time);
  185. xml.Element("StartTime", start_time_time);
  186. xml.Element("UpdateCommand", vc->GetUpdateCommandLine());
  187. xml.Element("UpdateType",
  188. cmCTestUpdateHandlerUpdateToString(this->UpdateType));
  189. vc->WriteXML(xml);
  190. int localModifications = 0;
  191. int numUpdated = vc->GetPathCount(cmCTestVC::PathUpdated);
  192. if (numUpdated) {
  193. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT,
  194. " Found " << numUpdated << " updated files\n",
  195. this->Quiet);
  196. }
  197. if (int numModified = vc->GetPathCount(cmCTestVC::PathModified)) {
  198. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT, " Found "
  199. << numModified << " locally modified files\n",
  200. this->Quiet);
  201. localModifications += numModified;
  202. }
  203. if (int numConflicting = vc->GetPathCount(cmCTestVC::PathConflicting)) {
  204. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT,
  205. " Found " << numConflicting << " conflicting files\n",
  206. this->Quiet);
  207. localModifications += numConflicting;
  208. }
  209. cmCTestOptionalLog(this->CTest, DEBUG, "End" << std::endl, this->Quiet);
  210. std::string end_time = this->CTest->CurrentTime();
  211. xml.Element("EndDateTime", end_time);
  212. xml.Element("EndTime", static_cast<unsigned int>(cmSystemTools::GetTime()));
  213. xml.Element(
  214. "ElapsedMinutes",
  215. static_cast<int>((cmSystemTools::GetTime() - elapsed_time_start) / 6) /
  216. 10.0);
  217. xml.StartElement("UpdateReturnStatus");
  218. if (localModifications) {
  219. xml.Content("Update error: "
  220. "There are modified or conflicting files in the repository");
  221. cmCTestLog(this->CTest, ERROR_MESSAGE,
  222. " There are modified or conflicting files in the repository"
  223. << std::endl);
  224. }
  225. if (!updated) {
  226. xml.Content("Update command failed:\n");
  227. xml.Content(vc->GetUpdateCommandLine());
  228. cmCTestLog(this->CTest, HANDLER_OUTPUT, " Update command failed: "
  229. << vc->GetUpdateCommandLine() << "\n");
  230. }
  231. xml.EndElement(); // UpdateReturnStatus
  232. xml.EndElement(); // Update
  233. xml.EndDocument();
  234. return updated ? 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.c_str())) {
  244. return cmCTestUpdateHandler::e_SVN;
  245. }
  246. sourceDirectory = dir;
  247. sourceDirectory += "/CVS";
  248. if (cmSystemTools::FileExists(sourceDirectory.c_str())) {
  249. return cmCTestUpdateHandler::e_CVS;
  250. }
  251. sourceDirectory = dir;
  252. sourceDirectory += "/.bzr";
  253. if (cmSystemTools::FileExists(sourceDirectory.c_str())) {
  254. return cmCTestUpdateHandler::e_BZR;
  255. }
  256. sourceDirectory = dir;
  257. sourceDirectory += "/.git";
  258. if (cmSystemTools::FileExists(sourceDirectory.c_str())) {
  259. return cmCTestUpdateHandler::e_GIT;
  260. }
  261. sourceDirectory = dir;
  262. sourceDirectory += "/.hg";
  263. if (cmSystemTools::FileExists(sourceDirectory.c_str())) {
  264. return cmCTestUpdateHandler::e_HG;
  265. }
  266. sourceDirectory = dir;
  267. sourceDirectory += "/.p4";
  268. if (cmSystemTools::FileExists(sourceDirectory.c_str())) {
  269. return cmCTestUpdateHandler::e_P4;
  270. }
  271. sourceDirectory = dir;
  272. sourceDirectory += "/.p4config";
  273. if (cmSystemTools::FileExists(sourceDirectory.c_str())) {
  274. return cmCTestUpdateHandler::e_P4;
  275. }
  276. return cmCTestUpdateHandler::e_UNKNOWN;
  277. }
  278. bool cmCTestUpdateHandler::SelectVCS()
  279. {
  280. // Get update command
  281. this->UpdateCommand = this->CTest->GetCTestConfiguration("UpdateCommand");
  282. // Detect the VCS managing the source tree.
  283. this->UpdateType = this->DetectVCS(this->GetOption("SourceDirectory"));
  284. if (this->UpdateType == e_UNKNOWN) {
  285. // The source tree does not have a recognized VCS. Check the
  286. // configuration value or command name.
  287. this->UpdateType = this->DetermineType(
  288. this->UpdateCommand.c_str(),
  289. this->CTest->GetCTestConfiguration("UpdateType").c_str());
  290. }
  291. // If no update command was specified, lookup one for this VCS tool.
  292. if (this->UpdateCommand.empty()) {
  293. const char* key = CM_NULLPTR;
  294. switch (this->UpdateType) {
  295. case e_CVS:
  296. key = "CVSCommand";
  297. break;
  298. case e_SVN:
  299. key = "SVNCommand";
  300. break;
  301. case e_BZR:
  302. key = "BZRCommand";
  303. break;
  304. case e_GIT:
  305. key = "GITCommand";
  306. break;
  307. case e_HG:
  308. key = "HGCommand";
  309. break;
  310. case e_P4:
  311. key = "P4Command";
  312. break;
  313. default:
  314. break;
  315. }
  316. if (key) {
  317. this->UpdateCommand = this->CTest->GetCTestConfiguration(key);
  318. }
  319. if (this->UpdateCommand.empty()) {
  320. std::ostringstream e;
  321. e << "Cannot find UpdateCommand ";
  322. if (key) {
  323. e << "or " << key;
  324. }
  325. e << " configuration key.";
  326. cmCTestLog(this->CTest, ERROR_MESSAGE, e.str() << std::endl);
  327. return false;
  328. }
  329. }
  330. return true;
  331. }