cmCTestCVS.cxx 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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 "cmCTest.h"
  5. #include "cmProcessTools.h"
  6. #include "cmSystemTools.h"
  7. #include "cmXMLWriter.h"
  8. #include "cmsys/FStream.hxx"
  9. #include "cmsys/RegularExpression.hxx"
  10. #include <utility>
  11. cmCTestCVS::cmCTestCVS(cmCTest* ct, std::ostream& log)
  12. : cmCTestVC(ct, log)
  13. {
  14. }
  15. cmCTestCVS::~cmCTestCVS()
  16. {
  17. }
  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.c_str());
  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<char const*> cvs_update;
  81. cvs_update.push_back(this->CommandLineTool.c_str());
  82. cvs_update.push_back("-z3");
  83. cvs_update.push_back("update");
  84. for (std::string const& arg : args) {
  85. cvs_update.push_back(arg.c_str());
  86. }
  87. cvs_update.push_back(nullptr);
  88. UpdateParser out(this, "up-out> ");
  89. UpdateParser err(this, "up-err> ");
  90. return this->RunUpdateCommand(&cvs_update[0], &out, &err);
  91. }
  92. class cmCTestCVS::LogParser : public cmCTestVC::LineParser
  93. {
  94. public:
  95. typedef cmCTestCVS::Revision Revision;
  96. LogParser(cmCTestCVS* cvs, const char* prefix, std::vector<Revision>& revs)
  97. : CVS(cvs)
  98. , Revisions(revs)
  99. , Section(SectionHeader)
  100. {
  101. this->SetLog(&cvs->Log, prefix),
  102. this->RegexRevision.compile("^revision +([^ ]*) *$");
  103. this->RegexBranches.compile("^branches: .*$");
  104. this->RegexPerson.compile("^date: +([^;]+); +author: +([^;]+);");
  105. }
  106. private:
  107. cmCTestCVS* CVS;
  108. std::vector<Revision>& Revisions;
  109. cmsys::RegularExpression RegexRevision;
  110. cmsys::RegularExpression RegexBranches;
  111. cmsys::RegularExpression RegexPerson;
  112. enum SectionType
  113. {
  114. SectionHeader,
  115. SectionRevisions,
  116. SectionEnd
  117. };
  118. SectionType Section;
  119. Revision Rev;
  120. bool ProcessLine() override
  121. {
  122. if (this->Line == ("======================================="
  123. "======================================")) {
  124. // This line ends the revision list.
  125. if (this->Section == SectionRevisions) {
  126. this->FinishRevision();
  127. }
  128. this->Section = SectionEnd;
  129. } else if (this->Line == "----------------------------") {
  130. // This line divides revisions from the header and each other.
  131. if (this->Section == SectionHeader) {
  132. this->Section = SectionRevisions;
  133. } else if (this->Section == SectionRevisions) {
  134. this->FinishRevision();
  135. }
  136. } else if (this->Section == SectionRevisions) {
  137. if (!this->Rev.Log.empty()) {
  138. // Continue the existing log.
  139. this->Rev.Log += this->Line;
  140. this->Rev.Log += "\n";
  141. } else if (this->Rev.Rev.empty() &&
  142. this->RegexRevision.find(this->Line)) {
  143. this->Rev.Rev = this->RegexRevision.match(1);
  144. } else if (this->Rev.Date.empty() &&
  145. this->RegexPerson.find(this->Line)) {
  146. this->Rev.Date = this->RegexPerson.match(1);
  147. this->Rev.Author = this->RegexPerson.match(2);
  148. } else if (!this->RegexBranches.find(this->Line)) {
  149. // Start the log.
  150. this->Rev.Log += this->Line;
  151. this->Rev.Log += "\n";
  152. }
  153. }
  154. return this->Section != SectionEnd;
  155. }
  156. void FinishRevision()
  157. {
  158. if (!this->Rev.Rev.empty()) {
  159. // Record this revision.
  160. /* clang-format off */
  161. this->CVS->Log << "Found revision " << this->Rev.Rev << "\n"
  162. << " author = " << this->Rev.Author << "\n"
  163. << " date = " << this->Rev.Date << "\n";
  164. /* clang-format on */
  165. this->Revisions.push_back(this->Rev);
  166. // We only need two revisions.
  167. if (this->Revisions.size() >= 2) {
  168. this->Section = SectionEnd;
  169. }
  170. }
  171. this->Rev = Revision();
  172. }
  173. };
  174. std::string cmCTestCVS::ComputeBranchFlag(std::string const& dir)
  175. {
  176. // Compute the tag file location for this directory.
  177. std::string tagFile = this->SourceDirectory;
  178. if (!dir.empty()) {
  179. tagFile += "/";
  180. tagFile += dir;
  181. }
  182. tagFile += "/CVS/Tag";
  183. // Lookup the branch in the tag file, if any.
  184. std::string tagLine;
  185. cmsys::ifstream tagStream(tagFile.c_str());
  186. if (tagStream && cmSystemTools::GetLineFromStream(tagStream, tagLine) &&
  187. tagLine.size() > 1 && tagLine[0] == 'T') {
  188. // Use the branch specified in the tag file.
  189. std::string flag = "-r";
  190. flag += tagLine.substr(1);
  191. return flag;
  192. }
  193. // Use the default branch.
  194. return "-b";
  195. }
  196. void cmCTestCVS::LoadRevisions(std::string const& file, const char* branchFlag,
  197. std::vector<Revision>& revisions)
  198. {
  199. cmCTestLog(this->CTest, HANDLER_OUTPUT, "." << std::flush);
  200. // Run "cvs log" to get revisions of this file on this branch.
  201. const char* cvs = this->CommandLineTool.c_str();
  202. const char* cvs_log[] = {
  203. cvs, "log", "-N", branchFlag, file.c_str(), nullptr
  204. };
  205. LogParser out(this, "log-out> ", revisions);
  206. OutputLogger err(this->Log, "log-err> ");
  207. this->RunChild(cvs_log, &out, &err);
  208. }
  209. void cmCTestCVS::WriteXMLDirectory(cmXMLWriter& xml, std::string const& path,
  210. Directory const& dir)
  211. {
  212. const char* slash = path.empty() ? "" : "/";
  213. xml.StartElement("Directory");
  214. xml.Element("Name", path);
  215. // Lookup the branch checked out in the working tree.
  216. std::string branchFlag = this->ComputeBranchFlag(path);
  217. // Load revisions and write an entry for each file in this directory.
  218. std::vector<Revision> revisions;
  219. for (auto const& fi : dir) {
  220. std::string full = path + slash + fi.first;
  221. // Load two real or unknown revisions.
  222. revisions.clear();
  223. if (fi.second != PathUpdated) {
  224. // For local modifications the current rev is unknown and the
  225. // prior rev is the latest from cvs.
  226. revisions.push_back(this->Unknown);
  227. }
  228. this->LoadRevisions(full, branchFlag.c_str(), revisions);
  229. revisions.resize(2, this->Unknown);
  230. // Write the entry for this file with these revisions.
  231. File f(fi.second, &revisions[0], &revisions[1]);
  232. this->WriteXMLEntry(xml, path, fi.first, full, f);
  233. }
  234. xml.EndElement(); // Directory
  235. }
  236. bool cmCTestCVS::WriteXMLUpdates(cmXMLWriter& xml)
  237. {
  238. cmCTestLog(this->CTest, HANDLER_OUTPUT,
  239. " Gathering version information (one . per updated file):\n"
  240. " "
  241. << std::flush);
  242. for (auto const& d : this->Dirs) {
  243. this->WriteXMLDirectory(xml, d.first, d.second);
  244. }
  245. cmCTestLog(this->CTest, HANDLER_OUTPUT, std::endl);
  246. return true;
  247. }