cmCTestCVS.cxx 9.1 KB

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