cmCTestHG.cxx 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc.
  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 "cmCTestHG.h"
  11. #include "cmCTest.h"
  12. #include "cmSystemTools.h"
  13. #include "cmXMLParser.h"
  14. #include <cmsys/RegularExpression.hxx>
  15. cmCTestHG::cmCTestHG(cmCTest* ct, std::ostream& log)
  16. : cmCTestGlobalVC(ct, log)
  17. {
  18. this->PriorRev = this->Unknown;
  19. }
  20. cmCTestHG::~cmCTestHG()
  21. {
  22. }
  23. class cmCTestHG::IdentifyParser : public cmCTestVC::LineParser
  24. {
  25. public:
  26. IdentifyParser(cmCTestHG* hg, const char* prefix, std::string& rev)
  27. : Rev(rev)
  28. {
  29. this->SetLog(&hg->Log, prefix);
  30. this->RegexIdentify.compile("^([0-9a-f]+)");
  31. }
  32. private:
  33. std::string& Rev;
  34. cmsys::RegularExpression RegexIdentify;
  35. bool ProcessLine() CM_OVERRIDE
  36. {
  37. if (this->RegexIdentify.find(this->Line)) {
  38. this->Rev = this->RegexIdentify.match(1);
  39. return false;
  40. }
  41. return true;
  42. }
  43. };
  44. class cmCTestHG::StatusParser : public cmCTestVC::LineParser
  45. {
  46. public:
  47. StatusParser(cmCTestHG* hg, const char* prefix)
  48. : HG(hg)
  49. {
  50. this->SetLog(&hg->Log, prefix);
  51. this->RegexStatus.compile("([MARC!?I]) (.*)");
  52. }
  53. private:
  54. cmCTestHG* HG;
  55. cmsys::RegularExpression RegexStatus;
  56. bool ProcessLine() CM_OVERRIDE
  57. {
  58. if (this->RegexStatus.find(this->Line)) {
  59. this->DoPath(this->RegexStatus.match(1)[0], this->RegexStatus.match(2));
  60. }
  61. return true;
  62. }
  63. void DoPath(char status, std::string const& path)
  64. {
  65. if (path.empty()) {
  66. return;
  67. }
  68. // See "hg help status". Note that there is no 'conflict' status.
  69. switch (status) {
  70. case 'M':
  71. case 'A':
  72. case '!':
  73. case 'R':
  74. this->HG->DoModification(PathModified, path);
  75. break;
  76. case 'I':
  77. case '?':
  78. case 'C':
  79. case ' ':
  80. default:
  81. break;
  82. }
  83. }
  84. };
  85. std::string cmCTestHG::GetWorkingRevision()
  86. {
  87. // Run plumbing "hg identify" to get work tree revision.
  88. const char* hg = this->CommandLineTool.c_str();
  89. const char* hg_identify[] = { hg, "identify", "-i", 0 };
  90. std::string rev;
  91. IdentifyParser out(this, "rev-out> ", rev);
  92. OutputLogger err(this->Log, "rev-err> ");
  93. this->RunChild(hg_identify, &out, &err);
  94. return rev;
  95. }
  96. void cmCTestHG::NoteOldRevision()
  97. {
  98. this->OldRevision = this->GetWorkingRevision();
  99. cmCTestLog(this->CTest, HANDLER_OUTPUT, " Old revision of repository is: "
  100. << this->OldRevision << "\n");
  101. this->PriorRev.Rev = this->OldRevision;
  102. }
  103. void cmCTestHG::NoteNewRevision()
  104. {
  105. this->NewRevision = this->GetWorkingRevision();
  106. cmCTestLog(this->CTest, HANDLER_OUTPUT, " New revision of repository is: "
  107. << this->NewRevision << "\n");
  108. }
  109. bool cmCTestHG::UpdateImpl()
  110. {
  111. // Use "hg pull" followed by "hg update" to update the working tree.
  112. {
  113. const char* hg = this->CommandLineTool.c_str();
  114. const char* hg_pull[] = { hg, "pull", "-v", 0 };
  115. OutputLogger out(this->Log, "pull-out> ");
  116. OutputLogger err(this->Log, "pull-err> ");
  117. this->RunChild(&hg_pull[0], &out, &err);
  118. }
  119. // TODO: if(this->CTest->GetTestModel() == cmCTest::NIGHTLY)
  120. std::vector<char const*> hg_update;
  121. hg_update.push_back(this->CommandLineTool.c_str());
  122. hg_update.push_back("update");
  123. hg_update.push_back("-v");
  124. // Add user-specified update options.
  125. std::string opts = this->CTest->GetCTestConfiguration("UpdateOptions");
  126. if (opts.empty()) {
  127. opts = this->CTest->GetCTestConfiguration("HGUpdateOptions");
  128. }
  129. std::vector<std::string> args = cmSystemTools::ParseArguments(opts.c_str());
  130. for (std::vector<std::string>::const_iterator ai = args.begin();
  131. ai != args.end(); ++ai) {
  132. hg_update.push_back(ai->c_str());
  133. }
  134. // Sentinel argument.
  135. hg_update.push_back(0);
  136. OutputLogger out(this->Log, "update-out> ");
  137. OutputLogger err(this->Log, "update-err> ");
  138. return this->RunUpdateCommand(&hg_update[0], &out, &err);
  139. }
  140. class cmCTestHG::LogParser : public cmCTestVC::OutputLogger,
  141. private cmXMLParser
  142. {
  143. public:
  144. LogParser(cmCTestHG* hg, const char* prefix)
  145. : OutputLogger(hg->Log, prefix)
  146. , HG(hg)
  147. {
  148. this->InitializeParser();
  149. }
  150. ~LogParser() { this->CleanupParser(); }
  151. private:
  152. cmCTestHG* HG;
  153. typedef cmCTestHG::Revision Revision;
  154. typedef cmCTestHG::Change Change;
  155. Revision Rev;
  156. std::vector<Change> Changes;
  157. Change CurChange;
  158. std::vector<char> CData;
  159. bool ProcessChunk(const char* data, int length) CM_OVERRIDE
  160. {
  161. this->OutputLogger::ProcessChunk(data, length);
  162. this->ParseChunk(data, length);
  163. return true;
  164. }
  165. void StartElement(const std::string& name, const char** atts) CM_OVERRIDE
  166. {
  167. this->CData.clear();
  168. if (name == "logentry") {
  169. this->Rev = Revision();
  170. if (const char* rev = this->FindAttribute(atts, "revision")) {
  171. this->Rev.Rev = rev;
  172. }
  173. this->Changes.clear();
  174. }
  175. }
  176. void CharacterDataHandler(const char* data, int length) CM_OVERRIDE
  177. {
  178. this->CData.insert(this->CData.end(), data, data + length);
  179. }
  180. void EndElement(const std::string& name) CM_OVERRIDE
  181. {
  182. if (name == "logentry") {
  183. this->HG->DoRevision(this->Rev, this->Changes);
  184. } else if (!this->CData.empty() && name == "author") {
  185. this->Rev.Author.assign(&this->CData[0], this->CData.size());
  186. } else if (!this->CData.empty() && name == "email") {
  187. this->Rev.EMail.assign(&this->CData[0], this->CData.size());
  188. } else if (!this->CData.empty() && name == "date") {
  189. this->Rev.Date.assign(&this->CData[0], this->CData.size());
  190. } else if (!this->CData.empty() && name == "msg") {
  191. this->Rev.Log.assign(&this->CData[0], this->CData.size());
  192. } else if (!this->CData.empty() && name == "files") {
  193. std::vector<std::string> paths = this->SplitCData();
  194. for (unsigned int i = 0; i < paths.size(); ++i) {
  195. // Updated by default, will be modified using file_adds and
  196. // file_dels.
  197. this->CurChange = Change('U');
  198. this->CurChange.Path = paths[i];
  199. this->Changes.push_back(this->CurChange);
  200. }
  201. } else if (!this->CData.empty() && name == "file_adds") {
  202. std::string added_paths(this->CData.begin(), this->CData.end());
  203. for (unsigned int i = 0; i < this->Changes.size(); ++i) {
  204. if (added_paths.find(this->Changes[i].Path) != std::string::npos) {
  205. this->Changes[i].Action = 'A';
  206. }
  207. }
  208. } else if (!this->CData.empty() && name == "file_dels") {
  209. std::string added_paths(this->CData.begin(), this->CData.end());
  210. for (unsigned int i = 0; i < this->Changes.size(); ++i) {
  211. if (added_paths.find(this->Changes[i].Path) != std::string::npos) {
  212. this->Changes[i].Action = 'D';
  213. }
  214. }
  215. }
  216. this->CData.clear();
  217. }
  218. std::vector<std::string> SplitCData()
  219. {
  220. std::vector<std::string> output;
  221. std::string currPath;
  222. for (unsigned int i = 0; i < this->CData.size(); ++i) {
  223. if (this->CData[i] != ' ') {
  224. currPath += this->CData[i];
  225. } else {
  226. output.push_back(currPath);
  227. currPath = "";
  228. }
  229. }
  230. output.push_back(currPath);
  231. return output;
  232. }
  233. void ReportError(int, int, const char* msg) CM_OVERRIDE
  234. {
  235. this->HG->Log << "Error parsing hg log xml: " << msg << "\n";
  236. }
  237. };
  238. void cmCTestHG::LoadRevisions()
  239. {
  240. // Use 'hg log' to get revisions in a xml format.
  241. //
  242. // TODO: This should use plumbing or python code to be more precise.
  243. // The "list of strings" templates like {files} will not work when
  244. // the project has spaces in the path. Also, they may not have
  245. // proper XML escapes.
  246. std::string range = this->OldRevision + ":" + this->NewRevision;
  247. const char* hg = this->CommandLineTool.c_str();
  248. const char* hgXMLTemplate = "<logentry\n"
  249. " revision=\"{node|short}\">\n"
  250. " <author>{author|person}</author>\n"
  251. " <email>{author|email}</email>\n"
  252. " <date>{date|isodate}</date>\n"
  253. " <msg>{desc}</msg>\n"
  254. " <files>{files}</files>\n"
  255. " <file_adds>{file_adds}</file_adds>\n"
  256. " <file_dels>{file_dels}</file_dels>\n"
  257. "</logentry>\n";
  258. const char* hg_log[] = {
  259. hg, "log", "--removed", "-r", range.c_str(), "--template", hgXMLTemplate, 0
  260. };
  261. LogParser out(this, "log-out> ");
  262. out.Process("<?xml version=\"1.0\"?>\n"
  263. "<log>\n");
  264. OutputLogger err(this->Log, "log-err> ");
  265. this->RunChild(hg_log, &out, &err);
  266. out.Process("</log>\n");
  267. }
  268. void cmCTestHG::LoadModifications()
  269. {
  270. // Use 'hg status' to get modified files.
  271. const char* hg = this->CommandLineTool.c_str();
  272. const char* hg_status[] = { hg, "status", 0 };
  273. StatusParser out(this, "status-out> ");
  274. OutputLogger err(this->Log, "status-err> ");
  275. this->RunChild(hg_status, &out, &err);
  276. }