cmCTestHG.cxx 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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 "cmCTestHG.h"
  4. #include "cmCTest.h"
  5. #include "cmCTestVC.h"
  6. #include "cmProcessTools.h"
  7. #include "cmSystemTools.h"
  8. #include "cmXMLParser.h"
  9. #include "cmsys/RegularExpression.hxx"
  10. #include <ostream>
  11. #include <vector>
  12. cmCTestHG::cmCTestHG(cmCTest* ct, std::ostream& log)
  13. : cmCTestGlobalVC(ct, log)
  14. {
  15. this->PriorRev = this->Unknown;
  16. }
  17. cmCTestHG::~cmCTestHG() = default;
  18. class cmCTestHG::IdentifyParser : public cmCTestVC::LineParser
  19. {
  20. public:
  21. IdentifyParser(cmCTestHG* hg, const char* prefix, std::string& rev)
  22. : Rev(rev)
  23. {
  24. this->SetLog(&hg->Log, prefix);
  25. this->RegexIdentify.compile("^([0-9a-f]+)");
  26. }
  27. private:
  28. std::string& Rev;
  29. cmsys::RegularExpression RegexIdentify;
  30. bool ProcessLine() override
  31. {
  32. if (this->RegexIdentify.find(this->Line)) {
  33. this->Rev = this->RegexIdentify.match(1);
  34. return false;
  35. }
  36. return true;
  37. }
  38. };
  39. class cmCTestHG::StatusParser : public cmCTestVC::LineParser
  40. {
  41. public:
  42. StatusParser(cmCTestHG* hg, const char* prefix)
  43. : HG(hg)
  44. {
  45. this->SetLog(&hg->Log, prefix);
  46. this->RegexStatus.compile("([MARC!?I]) (.*)");
  47. }
  48. private:
  49. cmCTestHG* HG;
  50. cmsys::RegularExpression RegexStatus;
  51. bool ProcessLine() override
  52. {
  53. if (this->RegexStatus.find(this->Line)) {
  54. this->DoPath(this->RegexStatus.match(1)[0], this->RegexStatus.match(2));
  55. }
  56. return true;
  57. }
  58. void DoPath(char status, std::string const& path)
  59. {
  60. if (path.empty()) {
  61. return;
  62. }
  63. // See "hg help status". Note that there is no 'conflict' status.
  64. switch (status) {
  65. case 'M':
  66. case 'A':
  67. case '!':
  68. case 'R':
  69. this->HG->DoModification(PathModified, path);
  70. break;
  71. case 'I':
  72. case '?':
  73. case 'C':
  74. case ' ':
  75. default:
  76. break;
  77. }
  78. }
  79. };
  80. std::string cmCTestHG::GetWorkingRevision()
  81. {
  82. // Run plumbing "hg identify" to get work tree revision.
  83. const char* hg = this->CommandLineTool.c_str();
  84. const char* hg_identify[] = { hg, "identify", "-i", nullptr };
  85. std::string rev;
  86. IdentifyParser out(this, "rev-out> ", rev);
  87. OutputLogger err(this->Log, "rev-err> ");
  88. this->RunChild(hg_identify, &out, &err);
  89. return rev;
  90. }
  91. bool cmCTestHG::NoteOldRevision()
  92. {
  93. this->OldRevision = this->GetWorkingRevision();
  94. cmCTestLog(this->CTest, HANDLER_OUTPUT,
  95. " Old revision of repository is: " << this->OldRevision
  96. << "\n");
  97. this->PriorRev.Rev = this->OldRevision;
  98. return true;
  99. }
  100. bool cmCTestHG::NoteNewRevision()
  101. {
  102. this->NewRevision = this->GetWorkingRevision();
  103. cmCTestLog(this->CTest, HANDLER_OUTPUT,
  104. " New revision of repository is: " << this->NewRevision
  105. << "\n");
  106. return true;
  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", nullptr };
  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::string const& arg : args) {
  130. hg_update.push_back(arg.c_str());
  131. }
  132. // Sentinel argument.
  133. hg_update.push_back(nullptr);
  134. OutputLogger out(this->Log, "update-out> ");
  135. OutputLogger err(this->Log, "update-err> ");
  136. return this->RunUpdateCommand(&hg_update[0], &out, &err);
  137. }
  138. class cmCTestHG::LogParser
  139. : 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() override { 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. bool ProcessChunk(const char* data, int length) override
  159. {
  160. this->OutputLogger::ProcessChunk(data, length);
  161. this->ParseChunk(data, length);
  162. return true;
  163. }
  164. void StartElement(const std::string& name, const char** atts) override
  165. {
  166. this->CData.clear();
  167. if (name == "logentry") {
  168. this->Rev = Revision();
  169. if (const char* rev =
  170. cmCTestHG::LogParser::FindAttribute(atts, "revision")) {
  171. this->Rev.Rev = rev;
  172. }
  173. this->Changes.clear();
  174. }
  175. }
  176. void CharacterDataHandler(const char* data, int length) override
  177. {
  178. this->CData.insert(this->CData.end(), data, data + length);
  179. }
  180. void EndElement(const std::string& name) 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 (std::string const& path : paths) {
  195. // Updated by default, will be modified using file_adds and
  196. // file_dels.
  197. this->CurChange = Change('U');
  198. this->CurChange.Path = path;
  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 (Change& change : this->Changes) {
  204. if (added_paths.find(change.Path) != std::string::npos) {
  205. change.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 (Change& change : this->Changes) {
  211. if (added_paths.find(change.Path) != std::string::npos) {
  212. change.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 (char i : this->CData) {
  223. if (i != ' ') {
  224. currPath += i;
  225. } else {
  226. output.push_back(currPath);
  227. currPath.clear();
  228. }
  229. }
  230. output.push_back(currPath);
  231. return output;
  232. }
  233. void ReportError(int /*line*/, int /*column*/, const char* msg) override
  234. {
  235. this->HG->Log << "Error parsing hg log xml: " << msg << "\n";
  236. }
  237. };
  238. bool 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(),
  260. "--template", hgXMLTemplate, nullptr
  261. };
  262. LogParser out(this, "log-out> ");
  263. out.Process("<?xml version=\"1.0\"?>\n"
  264. "<log>\n");
  265. OutputLogger err(this->Log, "log-err> ");
  266. this->RunChild(hg_log, &out, &err);
  267. out.Process("</log>\n");
  268. return true;
  269. }
  270. bool cmCTestHG::LoadModifications()
  271. {
  272. // Use 'hg status' to get modified files.
  273. const char* hg = this->CommandLineTool.c_str();
  274. const char* hg_status[] = { hg, "status", nullptr };
  275. StatusParser out(this, "status-out> ");
  276. OutputLogger err(this->Log, "status-err> ");
  277. this->RunChild(hg_status, &out, &err);
  278. return true;
  279. }