cmCTestHG.cxx 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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()
  18. {
  19. }
  20. class cmCTestHG::IdentifyParser : public cmCTestVC::LineParser
  21. {
  22. public:
  23. IdentifyParser(cmCTestHG* hg, const char* prefix, std::string& rev)
  24. : Rev(rev)
  25. {
  26. this->SetLog(&hg->Log, prefix);
  27. this->RegexIdentify.compile("^([0-9a-f]+)");
  28. }
  29. private:
  30. std::string& Rev;
  31. cmsys::RegularExpression RegexIdentify;
  32. bool ProcessLine() CM_OVERRIDE
  33. {
  34. if (this->RegexIdentify.find(this->Line)) {
  35. this->Rev = this->RegexIdentify.match(1);
  36. return false;
  37. }
  38. return true;
  39. }
  40. };
  41. class cmCTestHG::StatusParser : public cmCTestVC::LineParser
  42. {
  43. public:
  44. StatusParser(cmCTestHG* hg, const char* prefix)
  45. : HG(hg)
  46. {
  47. this->SetLog(&hg->Log, prefix);
  48. this->RegexStatus.compile("([MARC!?I]) (.*)");
  49. }
  50. private:
  51. cmCTestHG* HG;
  52. cmsys::RegularExpression RegexStatus;
  53. bool ProcessLine() CM_OVERRIDE
  54. {
  55. if (this->RegexStatus.find(this->Line)) {
  56. this->DoPath(this->RegexStatus.match(1)[0], this->RegexStatus.match(2));
  57. }
  58. return true;
  59. }
  60. void DoPath(char status, std::string const& path)
  61. {
  62. if (path.empty()) {
  63. return;
  64. }
  65. // See "hg help status". Note that there is no 'conflict' status.
  66. switch (status) {
  67. case 'M':
  68. case 'A':
  69. case '!':
  70. case 'R':
  71. this->HG->DoModification(PathModified, path);
  72. break;
  73. case 'I':
  74. case '?':
  75. case 'C':
  76. case ' ':
  77. default:
  78. break;
  79. }
  80. }
  81. };
  82. std::string cmCTestHG::GetWorkingRevision()
  83. {
  84. // Run plumbing "hg identify" to get work tree revision.
  85. const char* hg = this->CommandLineTool.c_str();
  86. const char* hg_identify[] = { hg, "identify", "-i", nullptr };
  87. std::string rev;
  88. IdentifyParser out(this, "rev-out> ", rev);
  89. OutputLogger err(this->Log, "rev-err> ");
  90. this->RunChild(hg_identify, &out, &err);
  91. return rev;
  92. }
  93. bool cmCTestHG::NoteOldRevision()
  94. {
  95. this->OldRevision = this->GetWorkingRevision();
  96. cmCTestLog(this->CTest, HANDLER_OUTPUT, " Old revision of repository is: "
  97. << this->OldRevision << "\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, " New revision of repository is: "
  105. << this->NewRevision << "\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::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(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 : 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() CM_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) CM_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) CM_OVERRIDE
  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. void CharacterDataHandler(const char* data, int length) CM_OVERRIDE
  176. {
  177. this->CData.insert(this->CData.end(), data, data + length);
  178. }
  179. void EndElement(const std::string& name) CM_OVERRIDE
  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. void ReportError(int /*line*/, int /*column*/, const char* msg) CM_OVERRIDE
  233. {
  234. this->HG->Log << "Error parsing hg log xml: " << msg << "\n";
  235. }
  236. };
  237. bool 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(),
  259. "--template", hgXMLTemplate, nullptr
  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. return true;
  268. }
  269. bool cmCTestHG::LoadModifications()
  270. {
  271. // Use 'hg status' to get modified files.
  272. const char* hg = this->CommandLineTool.c_str();
  273. const char* hg_status[] = { hg, "status", nullptr };
  274. StatusParser out(this, "status-out> ");
  275. OutputLogger err(this->Log, "status-err> ");
  276. this->RunChild(hg_status, &out, &err);
  277. return true;
  278. }