cmCTestHG.cxx 8.9 KB

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