cmCTestHG.cxx 9.1 KB

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