1
0

cmCTestCVS.cxx 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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 "cmCTestCVS.h"
  4. #include <utility>
  5. #include <cm/string_view>
  6. #include <cmext/algorithm>
  7. #include "cmsys/FStream.hxx"
  8. #include "cmsys/RegularExpression.hxx"
  9. #include "cmCTest.h"
  10. #include "cmStringAlgorithms.h"
  11. #include "cmSystemTools.h"
  12. #include "cmXMLWriter.h"
  13. cmCTestCVS::cmCTestCVS(cmCTest* ct, std::ostream& log)
  14. : cmCTestVC(ct, log)
  15. {
  16. }
  17. cmCTestCVS::~cmCTestCVS() = default;
  18. class cmCTestCVS::UpdateParser : public cmCTestVC::LineParser
  19. {
  20. public:
  21. UpdateParser(cmCTestCVS* cvs, const char* prefix)
  22. : CVS(cvs)
  23. {
  24. this->SetLog(&cvs->Log, prefix);
  25. // See "man cvs", section "update output".
  26. this->RegexFileUpdated.compile("^([UP]) *(.*)");
  27. this->RegexFileModified.compile("^([MRA]) *(.*)");
  28. this->RegexFileConflicting.compile("^([C]) *(.*)");
  29. this->RegexFileRemoved1.compile(
  30. "cvs[^ ]* update: `?([^']*)'? is no longer in the repository");
  31. this->RegexFileRemoved2.compile(
  32. "cvs[^ ]* update: "
  33. "warning: `?([^']*)'? is not \\(any longer\\) pertinent");
  34. }
  35. private:
  36. cmCTestCVS* CVS;
  37. cmsys::RegularExpression RegexFileUpdated;
  38. cmsys::RegularExpression RegexFileModified;
  39. cmsys::RegularExpression RegexFileConflicting;
  40. cmsys::RegularExpression RegexFileRemoved1;
  41. cmsys::RegularExpression RegexFileRemoved2;
  42. bool ProcessLine() override
  43. {
  44. if (this->RegexFileUpdated.find(this->Line)) {
  45. this->DoFile(PathUpdated, this->RegexFileUpdated.match(2));
  46. } else if (this->RegexFileModified.find(this->Line)) {
  47. this->DoFile(PathModified, this->RegexFileModified.match(2));
  48. } else if (this->RegexFileConflicting.find(this->Line)) {
  49. this->DoFile(PathConflicting, this->RegexFileConflicting.match(2));
  50. } else if (this->RegexFileRemoved1.find(this->Line)) {
  51. this->DoFile(PathUpdated, this->RegexFileRemoved1.match(1));
  52. } else if (this->RegexFileRemoved2.find(this->Line)) {
  53. this->DoFile(PathUpdated, this->RegexFileRemoved2.match(1));
  54. }
  55. return true;
  56. }
  57. void DoFile(PathStatus status, std::string const& file)
  58. {
  59. std::string dir = cmSystemTools::GetFilenamePath(file);
  60. std::string name = cmSystemTools::GetFilenameName(file);
  61. this->CVS->Dirs[dir][name] = status;
  62. }
  63. };
  64. bool cmCTestCVS::UpdateImpl()
  65. {
  66. // Get user-specified update options.
  67. std::string opts = this->CTest->GetCTestConfiguration("UpdateOptions");
  68. if (opts.empty()) {
  69. opts = this->CTest->GetCTestConfiguration("CVSUpdateOptions");
  70. if (opts.empty()) {
  71. opts = "-dP";
  72. }
  73. }
  74. std::vector<std::string> args = cmSystemTools::ParseArguments(opts);
  75. // Specify the start time for nightly testing.
  76. if (this->CTest->GetTestModel() == cmCTest::NIGHTLY) {
  77. args.push_back("-D" + this->GetNightlyTime() + " UTC");
  78. }
  79. // Run "cvs update" to update the work tree.
  80. std::vector<std::string> cvs_update;
  81. cvs_update.push_back(this->CommandLineTool);
  82. cvs_update.emplace_back("-z3");
  83. cvs_update.emplace_back("update");
  84. cm::append(cvs_update, args);
  85. UpdateParser out(this, "up-out> ");
  86. UpdateParser err(this, "up-err> ");
  87. return this->RunUpdateCommand(cvs_update, &out, &err);
  88. }
  89. class cmCTestCVS::LogParser : public cmCTestVC::LineParser
  90. {
  91. public:
  92. using Revision = cmCTestCVS::Revision;
  93. LogParser(cmCTestCVS* cvs, const char* prefix, std::vector<Revision>& revs)
  94. : CVS(cvs)
  95. , Revisions(revs)
  96. {
  97. this->SetLog(&cvs->Log, prefix);
  98. this->RegexRevision.compile("^revision +([^ ]*) *$");
  99. this->RegexBranches.compile("^branches: .*$");
  100. this->RegexPerson.compile("^date: +([^;]+); +author: +([^;]+);");
  101. }
  102. private:
  103. cmCTestCVS* CVS;
  104. std::vector<Revision>& Revisions;
  105. cmsys::RegularExpression RegexRevision;
  106. cmsys::RegularExpression RegexBranches;
  107. cmsys::RegularExpression RegexPerson;
  108. enum SectionType
  109. {
  110. SectionHeader,
  111. SectionRevisions,
  112. SectionEnd
  113. };
  114. SectionType Section = SectionHeader;
  115. Revision Rev;
  116. bool ProcessLine() override
  117. {
  118. if (this->Line ==
  119. ("======================================="
  120. "======================================")) {
  121. // This line ends the revision list.
  122. if (this->Section == SectionRevisions) {
  123. this->FinishRevision();
  124. }
  125. this->Section = SectionEnd;
  126. } else if (this->Line == "----------------------------") {
  127. // This line divides revisions from the header and each other.
  128. if (this->Section == SectionHeader) {
  129. this->Section = SectionRevisions;
  130. } else if (this->Section == SectionRevisions) {
  131. this->FinishRevision();
  132. }
  133. } else if (this->Section == SectionRevisions) {
  134. // XXX(clang-tidy): https://bugs.llvm.org/show_bug.cgi?id=44165
  135. // NOLINTNEXTLINE(bugprone-branch-clone)
  136. if (!this->Rev.Log.empty()) {
  137. // Continue the existing log.
  138. this->Rev.Log += this->Line;
  139. this->Rev.Log += '\n';
  140. } else if (this->Rev.Rev.empty() &&
  141. this->RegexRevision.find(this->Line)) {
  142. this->Rev.Rev = this->RegexRevision.match(1);
  143. } else if (this->Rev.Date.empty() &&
  144. this->RegexPerson.find(this->Line)) {
  145. this->Rev.Date = this->RegexPerson.match(1);
  146. this->Rev.Author = this->RegexPerson.match(2);
  147. } else if (!this->RegexBranches.find(this->Line)) {
  148. // Start the log.
  149. this->Rev.Log += this->Line;
  150. this->Rev.Log += '\n';
  151. }
  152. }
  153. return this->Section != SectionEnd;
  154. }
  155. void FinishRevision()
  156. {
  157. if (!this->Rev.Rev.empty()) {
  158. // Record this revision.
  159. /* clang-format off */
  160. this->CVS->Log << "Found revision " << this->Rev.Rev << "\n"
  161. << " author = " << this->Rev.Author << "\n"
  162. << " date = " << this->Rev.Date << "\n";
  163. /* clang-format on */
  164. this->Revisions.push_back(this->Rev);
  165. // We only need two revisions.
  166. if (this->Revisions.size() >= 2) {
  167. this->Section = SectionEnd;
  168. }
  169. }
  170. this->Rev = Revision();
  171. }
  172. };
  173. std::string cmCTestCVS::ComputeBranchFlag(std::string const& dir)
  174. {
  175. // Compute the tag file location for this directory.
  176. std::string tagFile = this->SourceDirectory;
  177. if (!dir.empty()) {
  178. tagFile += "/";
  179. tagFile += dir;
  180. }
  181. tagFile += "/CVS/Tag";
  182. // Lookup the branch in the tag file, if any.
  183. std::string tagLine;
  184. cmsys::ifstream tagStream(tagFile.c_str());
  185. if (tagStream && cmSystemTools::GetLineFromStream(tagStream, tagLine) &&
  186. tagLine.size() > 1 && tagLine[0] == 'T') {
  187. // Use the branch specified in the tag file.
  188. std::string flag = cmStrCat("-r", cm::string_view(tagLine).substr(1));
  189. return flag;
  190. }
  191. // Use the default branch.
  192. return "-b";
  193. }
  194. void cmCTestCVS::LoadRevisions(std::string const& file, const char* branchFlag,
  195. std::vector<Revision>& revisions)
  196. {
  197. cmCTestLog(this->CTest, HANDLER_OUTPUT, "." << std::flush);
  198. // Run "cvs log" to get revisions of this file on this branch.
  199. std::string cvs = this->CommandLineTool;
  200. std::vector<std::string> cvs_log = { cvs, "log", "-N", branchFlag, file };
  201. LogParser out(this, "log-out> ", revisions);
  202. OutputLogger err(this->Log, "log-err> ");
  203. this->RunChild(cvs_log, &out, &err);
  204. }
  205. void cmCTestCVS::WriteXMLDirectory(cmXMLWriter& xml, std::string const& path,
  206. Directory const& dir)
  207. {
  208. const char* slash = path.empty() ? "" : "/";
  209. xml.StartElement("Directory");
  210. xml.Element("Name", path);
  211. // Lookup the branch checked out in the working tree.
  212. std::string branchFlag = this->ComputeBranchFlag(path);
  213. // Load revisions and write an entry for each file in this directory.
  214. std::vector<Revision> revisions;
  215. for (auto const& fi : dir) {
  216. std::string full = path + slash + fi.first;
  217. // Load two real or unknown revisions.
  218. revisions.clear();
  219. if (fi.second != PathUpdated) {
  220. // For local modifications the current rev is unknown and the
  221. // prior rev is the latest from cvs.
  222. revisions.push_back(this->Unknown);
  223. }
  224. this->LoadRevisions(full, branchFlag.c_str(), revisions);
  225. revisions.resize(2, this->Unknown);
  226. // Write the entry for this file with these revisions.
  227. File f(fi.second, revisions.data(), revisions.data() + 1);
  228. this->WriteXMLEntry(xml, path, fi.first, full, f);
  229. }
  230. xml.EndElement(); // Directory
  231. }
  232. bool cmCTestCVS::WriteXMLUpdates(cmXMLWriter& xml)
  233. {
  234. cmCTestLog(this->CTest, HANDLER_OUTPUT,
  235. " Gathering version information (one . per updated file):\n"
  236. " "
  237. << std::flush);
  238. for (auto const& d : this->Dirs) {
  239. this->WriteXMLDirectory(xml, d.first, d.second);
  240. }
  241. cmCTestLog(this->CTest, HANDLER_OUTPUT, std::endl);
  242. return true;
  243. }