summaryrefslogtreecommitdiffstats
path: root/Source/CTest/cmCTestCVS.cxx
blob: 87ab7625c600f782b9779f27fb2564e70460b789 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
   file Copyright.txt or https://cmake.org/licensing for details.  */
#include "cmCTestCVS.h"

#include <utility>

#include <cm/string_view>

#include "cmsys/FStream.hxx"
#include "cmsys/RegularExpression.hxx"

#include "cmCTest.h"
#include "cmProcessTools.h"
#include "cmStringAlgorithms.h"
#include "cmSystemTools.h"
#include "cmXMLWriter.h"

cmCTestCVS::cmCTestCVS(cmCTest* ct, std::ostream& log)
  : cmCTestVC(ct, log)
{
}

cmCTestCVS::~cmCTestCVS() = default;

class cmCTestCVS::UpdateParser : public cmCTestVC::LineParser
{
public:
  UpdateParser(cmCTestCVS* cvs, const char* prefix)
    : CVS(cvs)
  {
    this->SetLog(&cvs->Log, prefix);
    // See "man cvs", section "update output".
    this->RegexFileUpdated.compile("^([UP])  *(.*)");
    this->RegexFileModified.compile("^([MRA])  *(.*)");
    this->RegexFileConflicting.compile("^([C])  *(.*)");
    this->RegexFileRemoved1.compile(
      "cvs[^ ]* update: `?([^']*)'? is no longer in the repository");
    this->RegexFileRemoved2.compile(
      "cvs[^ ]* update: "
      "warning: `?([^']*)'? is not \\(any longer\\) pertinent");
  }

private:
  cmCTestCVS* CVS;
  cmsys::RegularExpression RegexFileUpdated;
  cmsys::RegularExpression RegexFileModified;
  cmsys::RegularExpression RegexFileConflicting;
  cmsys::RegularExpression RegexFileRemoved1;
  cmsys::RegularExpression RegexFileRemoved2;

  bool ProcessLine() override
  {
    if (this->RegexFileUpdated.find(this->Line)) {
      this->DoFile(PathUpdated, this->RegexFileUpdated.match(2));
    } else if (this->RegexFileModified.find(this->Line)) {
      this->DoFile(PathModified, this->RegexFileModified.match(2));
    } else if (this->RegexFileConflicting.find(this->Line)) {
      this->DoFile(PathConflicting, this->RegexFileConflicting.match(2));
    } else if (this->RegexFileRemoved1.find(this->Line)) {
      this->DoFile(PathUpdated, this->RegexFileRemoved1.match(1));
    } else if (this->RegexFileRemoved2.find(this->Line)) {
      this->DoFile(PathUpdated, this->RegexFileRemoved2.match(1));
    }
    return true;
  }

  void DoFile(PathStatus status, std::string const& file)
  {
    std::string dir = cmSystemTools::GetFilenamePath(file);
    std::string name = cmSystemTools::GetFilenameName(file);
    this->CVS->Dirs[dir][name] = status;
  }
};

bool cmCTestCVS::UpdateImpl()
{
  // Get user-specified update options.
  std::string opts = this->CTest->GetCTestConfiguration("UpdateOptions");
  if (opts.empty()) {
    opts = this->CTest->GetCTestConfiguration("CVSUpdateOptions");
    if (opts.empty()) {
      opts = "-dP";
    }
  }
  std::vector<std::string> args = cmSystemTools::ParseArguments(opts);

  // Specify the start time for nightly testing.
  if (this->CTest->GetTestModel() == cmCTest::NIGHTLY) {
    args.push_back("-D" + this->GetNightlyTime() + " UTC");
  }

  // Run "cvs update" to update the work tree.
  std::vector<char const*> cvs_update;
  cvs_update.push_back(this->CommandLineTool.c_str());
  cvs_update.push_back("-z3");
  cvs_update.push_back("update");
  for (std::string const& arg : args) {
    cvs_update.push_back(arg.c_str());
  }
  cvs_update.push_back(nullptr);

  UpdateParser out(this, "up-out> ");
  UpdateParser err(this, "up-err> ");
  return this->RunUpdateCommand(cvs_update.data(), &out, &err);
}

class cmCTestCVS::LogParser : public cmCTestVC::LineParser
{
public:
  using Revision = cmCTestCVS::Revision;
  LogParser(cmCTestCVS* cvs, const char* prefix, std::vector<Revision>& revs)
    : CVS(cvs)
    , Revisions(revs)
  {
    this->SetLog(&cvs->Log, prefix);
    this->RegexRevision.compile("^revision +([^ ]*) *$");
    this->RegexBranches.compile("^branches: .*$");
    this->RegexPerson.compile("^date: +([^;]+); +author: +([^;]+);");
  }

private:
  cmCTestCVS* CVS;
  std::vector<Revision>& Revisions;
  cmsys::RegularExpression RegexRevision;
  cmsys::RegularExpression RegexBranches;
  cmsys::RegularExpression RegexPerson;
  enum SectionType
  {
    SectionHeader,
    SectionRevisions,
    SectionEnd
  };
  SectionType Section = SectionHeader;
  Revision Rev;

  bool ProcessLine() override
  {
    if (this->Line ==
        ("======================================="
         "======================================")) {
      // This line ends the revision list.
      if (this->Section == SectionRevisions) {
        this->FinishRevision();
      }
      this->Section = SectionEnd;
    } else if (this->Line == "----------------------------") {
      // This line divides revisions from the header and each other.
      if (this->Section == SectionHeader) {
        this->Section = SectionRevisions;
      } else if (this->Section == SectionRevisions) {
        this->FinishRevision();
      }
    } else if (this->Section == SectionRevisions) {
      // XXX(clang-tidy): https://bugs.llvm.org/show_bug.cgi?id=44165
      // NOLINTNEXTLINE(bugprone-branch-clone)
      if (!this->Rev.Log.empty()) {
        // Continue the existing log.
        this->Rev.Log += this->Line;
        this->Rev.Log += '\n';
      } else if (this->Rev.Rev.empty() &&
                 this->RegexRevision.find(this->Line)) {
        this->Rev.Rev = this->RegexRevision.match(1);
      } else if (this->Rev.Date.empty() &&
                 this->RegexPerson.find(this->Line)) {
        this->Rev.Date = this->RegexPerson.match(1);
        this->Rev.Author = this->RegexPerson.match(2);
      } else if (!this->RegexBranches.find(this->Line)) {
        // Start the log.
        this->Rev.Log += this->Line;
        this->Rev.Log += '\n';
      }
    }
    return this->Section != SectionEnd;
  }

  void FinishRevision()
  {
    if (!this->Rev.Rev.empty()) {
      // Record this revision.
      /* clang-format off */
      this->CVS->Log << "Found revision " << this->Rev.Rev << "\n"
                     << "  author = " << this->Rev.Author << "\n"
                     << "  date = " << this->Rev.Date << "\n";
      /* clang-format on */
      this->Revisions.push_back(this->Rev);

      // We only need two revisions.
      if (this->Revisions.size() >= 2) {
        this->Section = SectionEnd;
      }
    }
    this->Rev = Revision();
  }
};

std::string cmCTestCVS::ComputeBranchFlag(std::string const& dir)
{
  // Compute the tag file location for this directory.
  std::string tagFile = this->SourceDirectory;
  if (!dir.empty()) {
    tagFile += "/";
    tagFile += dir;
  }
  tagFile += "/CVS/Tag";

  // Lookup the branch in the tag file, if any.
  std::string tagLine;
  cmsys::ifstream tagStream(tagFile.c_str());
  if (tagStream && cmSystemTools::GetLineFromStream(tagStream, tagLine) &&
      tagLine.size() > 1 && tagLine[0] == 'T') {
    // Use the branch specified in the tag file.
    std::string flag = cmStrCat("-r", cm::string_view(tagLine).substr(1));
    return flag;
  }
  // Use the default branch.
  return "-b";
}

void cmCTestCVS::LoadRevisions(std::string const& file, const char* branchFlag,
                               std::vector<Revision>& revisions)
{
  cmCTestLog(this->CTest, HANDLER_OUTPUT, "." << std::flush);

  // Run "cvs log" to get revisions of this file on this branch.
  const char* cvs = this->CommandLineTool.c_str();
  const char* cvs_log[] = {
    cvs, "log", "-N", branchFlag, file.c_str(), nullptr
  };

  LogParser out(this, "log-out> ", revisions);
  OutputLogger err(this->Log, "log-err> ");
  this->RunChild(cvs_log, &out, &err);
}

void cmCTestCVS::WriteXMLDirectory(cmXMLWriter& xml, std::string const& path,
                                   Directory const& dir)
{
  const char* slash = path.empty() ? "" : "/";
  xml.StartElement("Directory");
  xml.Element("Name", path);

  // Lookup the branch checked out in the working tree.
  std::string branchFlag = this->ComputeBranchFlag(path);

  // Load revisions and write an entry for each file in this directory.
  std::vector<Revision> revisions;
  for (auto const& fi : dir) {
    std::string full = path + slash + fi.first;

    // Load two real or unknown revisions.
    revisions.clear();
    if (fi.second != PathUpdated) {
      // For local modifications the current rev is unknown and the
      // prior rev is the latest from cvs.
      revisions.push_back(this->Unknown);
    }
    this->LoadRevisions(full, branchFlag.c_str(), revisions);
    revisions.resize(2, this->Unknown);

    // Write the entry for this file with these revisions.
    File f(fi.second, revisions.data(), revisions.data() + 1);
    this->WriteXMLEntry(xml, path, fi.first, full, f);
  }
  xml.EndElement(); // Directory
}

bool cmCTestCVS::WriteXMLUpdates(cmXMLWriter& xml)
{
  cmCTestLog(this->CTest, HANDLER_OUTPUT,
             "   Gathering version information (one . per updated file):\n"
             "    "
               << std::flush);

  for (auto const& d : this->Dirs) {
    this->WriteXMLDirectory(xml, d.first, d.second);
  }

  cmCTestLog(this->CTest, HANDLER_OUTPUT, std::endl);

  return true;
}