cmCTestSVN.cxx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  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 "cmCTestSVN.h"
  4. #include "cmCTest.h"
  5. #include "cmCTestVC.h"
  6. #include "cmProcessTools.h"
  7. #include "cmSystemTools.h"
  8. #include "cmXMLParser.h"
  9. #include "cmXMLWriter.h"
  10. #include <cmsys/RegularExpression.hxx>
  11. #include <map>
  12. #include <ostream>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. struct cmCTestSVN::Revision : public cmCTestVC::Revision
  16. {
  17. cmCTestSVN::SVNInfo* SVNInfo;
  18. };
  19. cmCTestSVN::cmCTestSVN(cmCTest* ct, std::ostream& log)
  20. : cmCTestGlobalVC(ct, log)
  21. {
  22. this->PriorRev = this->Unknown;
  23. }
  24. cmCTestSVN::~cmCTestSVN()
  25. {
  26. }
  27. void cmCTestSVN::CleanupImpl()
  28. {
  29. std::vector<const char*> svn_cleanup;
  30. svn_cleanup.push_back("cleanup");
  31. OutputLogger out(this->Log, "cleanup-out> ");
  32. OutputLogger err(this->Log, "cleanup-err> ");
  33. this->RunSVNCommand(svn_cleanup, &out, &err);
  34. }
  35. class cmCTestSVN::InfoParser : public cmCTestVC::LineParser
  36. {
  37. public:
  38. InfoParser(cmCTestSVN* svn, const char* prefix, std::string& rev,
  39. SVNInfo& svninfo)
  40. : Rev(rev)
  41. , SVNRepo(svninfo)
  42. {
  43. this->SetLog(&svn->Log, prefix);
  44. this->RegexRev.compile("^Revision: ([0-9]+)");
  45. this->RegexURL.compile("^URL: +([^ ]+) *$");
  46. this->RegexRoot.compile("^Repository Root: +([^ ]+) *$");
  47. }
  48. private:
  49. std::string& Rev;
  50. cmCTestSVN::SVNInfo& SVNRepo;
  51. cmsys::RegularExpression RegexRev;
  52. cmsys::RegularExpression RegexURL;
  53. cmsys::RegularExpression RegexRoot;
  54. bool ProcessLine() CM_OVERRIDE
  55. {
  56. if (this->RegexRev.find(this->Line)) {
  57. this->Rev = this->RegexRev.match(1);
  58. } else if (this->RegexURL.find(this->Line)) {
  59. this->SVNRepo.URL = this->RegexURL.match(1);
  60. } else if (this->RegexRoot.find(this->Line)) {
  61. this->SVNRepo.Root = this->RegexRoot.match(1);
  62. }
  63. return true;
  64. }
  65. };
  66. static bool cmCTestSVNPathStarts(std::string const& p1, std::string const& p2)
  67. {
  68. // Does path p1 start with path p2?
  69. if (p1.size() == p2.size()) {
  70. return p1 == p2;
  71. }
  72. if (p1.size() > p2.size() && p1[p2.size()] == '/') {
  73. return strncmp(p1.c_str(), p2.c_str(), p2.size()) == 0;
  74. }
  75. return false;
  76. }
  77. std::string cmCTestSVN::LoadInfo(SVNInfo& svninfo)
  78. {
  79. // Run "svn info" to get the repository info from the work tree.
  80. std::vector<const char*> svn_info;
  81. svn_info.push_back("info");
  82. svn_info.push_back(svninfo.LocalPath.c_str());
  83. std::string rev;
  84. InfoParser out(this, "info-out> ", rev, svninfo);
  85. OutputLogger err(this->Log, "info-err> ");
  86. this->RunSVNCommand(svn_info, &out, &err);
  87. return rev;
  88. }
  89. void cmCTestSVN::NoteOldRevision()
  90. {
  91. this->LoadRepositories();
  92. std::list<SVNInfo>::iterator itbeg = this->Repositories.begin();
  93. std::list<SVNInfo>::iterator itend = this->Repositories.end();
  94. for (; itbeg != itend; itbeg++) {
  95. SVNInfo& svninfo = *itbeg;
  96. svninfo.OldRevision = this->LoadInfo(svninfo);
  97. this->Log << "Revision for repository '" << svninfo.LocalPath
  98. << "' before update: " << svninfo.OldRevision << "\n";
  99. cmCTestLog(
  100. this->CTest, HANDLER_OUTPUT, " Old revision of external repository '"
  101. << svninfo.LocalPath << "' is: " << svninfo.OldRevision << "\n");
  102. }
  103. // Set the global old revision to the one of the root
  104. this->OldRevision = this->RootInfo->OldRevision;
  105. this->PriorRev.Rev = this->OldRevision;
  106. }
  107. void cmCTestSVN::NoteNewRevision()
  108. {
  109. this->LoadRepositories();
  110. std::list<SVNInfo>::iterator itbeg = this->Repositories.begin();
  111. std::list<SVNInfo>::iterator itend = this->Repositories.end();
  112. for (; itbeg != itend; itbeg++) {
  113. SVNInfo& svninfo = *itbeg;
  114. svninfo.NewRevision = this->LoadInfo(svninfo);
  115. this->Log << "Revision for repository '" << svninfo.LocalPath
  116. << "' after update: " << svninfo.NewRevision << "\n";
  117. cmCTestLog(
  118. this->CTest, HANDLER_OUTPUT, " New revision of external repository '"
  119. << svninfo.LocalPath << "' is: " << svninfo.NewRevision << "\n");
  120. // svninfo.Root = ""; // uncomment to test GuessBase
  121. this->Log << "Repository '" << svninfo.LocalPath
  122. << "' URL = " << svninfo.URL << "\n";
  123. this->Log << "Repository '" << svninfo.LocalPath
  124. << "' Root = " << svninfo.Root << "\n";
  125. // Compute the base path the working tree has checked out under
  126. // the repository root.
  127. if (!svninfo.Root.empty() &&
  128. cmCTestSVNPathStarts(svninfo.URL, svninfo.Root)) {
  129. svninfo.Base =
  130. cmCTest::DecodeURL(svninfo.URL.substr(svninfo.Root.size()));
  131. svninfo.Base += "/";
  132. }
  133. this->Log << "Repository '" << svninfo.LocalPath
  134. << "' Base = " << svninfo.Base << "\n";
  135. }
  136. // Set the global new revision to the one of the root
  137. this->NewRevision = this->RootInfo->NewRevision;
  138. }
  139. void cmCTestSVN::GuessBase(SVNInfo& svninfo,
  140. std::vector<Change> const& changes)
  141. {
  142. // Subversion did not give us a good repository root so we need to
  143. // guess the base path from the URL and the paths in a revision with
  144. // changes under it.
  145. // Consider each possible URL suffix from longest to shortest.
  146. for (std::string::size_type slash = svninfo.URL.find('/');
  147. svninfo.Base.empty() && slash != std::string::npos;
  148. slash = svninfo.URL.find('/', slash + 1)) {
  149. // If the URL suffix is a prefix of at least one path then it is the base.
  150. std::string base = cmCTest::DecodeURL(svninfo.URL.substr(slash));
  151. for (std::vector<Change>::const_iterator ci = changes.begin();
  152. svninfo.Base.empty() && ci != changes.end(); ++ci) {
  153. if (cmCTestSVNPathStarts(ci->Path, base)) {
  154. svninfo.Base = base;
  155. }
  156. }
  157. }
  158. // We always append a slash so that we know paths beginning in the
  159. // base lie under its path. If no base was found then the working
  160. // tree must be a checkout of the entire repo and this will match
  161. // the leading slash in all paths.
  162. svninfo.Base += "/";
  163. this->Log << "Guessed Base = " << svninfo.Base << "\n";
  164. }
  165. class cmCTestSVN::UpdateParser : public cmCTestVC::LineParser
  166. {
  167. public:
  168. UpdateParser(cmCTestSVN* svn, const char* prefix)
  169. : SVN(svn)
  170. {
  171. this->SetLog(&svn->Log, prefix);
  172. this->RegexUpdate.compile("^([ADUCGE ])([ADUCGE ])[B ] +(.+)$");
  173. }
  174. private:
  175. cmCTestSVN* SVN;
  176. cmsys::RegularExpression RegexUpdate;
  177. bool ProcessLine() CM_OVERRIDE
  178. {
  179. if (this->RegexUpdate.find(this->Line)) {
  180. this->DoPath(this->RegexUpdate.match(1)[0],
  181. this->RegexUpdate.match(2)[0], this->RegexUpdate.match(3));
  182. }
  183. return true;
  184. }
  185. void DoPath(char path_status, char prop_status, std::string const& path)
  186. {
  187. char status = (path_status != ' ') ? path_status : prop_status;
  188. std::string dir = cmSystemTools::GetFilenamePath(path);
  189. std::string name = cmSystemTools::GetFilenameName(path);
  190. // See "svn help update".
  191. switch (status) {
  192. case 'G':
  193. this->SVN->Dirs[dir][name].Status = PathModified;
  194. break;
  195. case 'C':
  196. this->SVN->Dirs[dir][name].Status = PathConflicting;
  197. break;
  198. case 'A':
  199. case 'D':
  200. case 'U':
  201. this->SVN->Dirs[dir][name].Status = PathUpdated;
  202. break;
  203. case 'E': // TODO?
  204. case '?':
  205. case ' ':
  206. default:
  207. break;
  208. }
  209. }
  210. };
  211. bool cmCTestSVN::UpdateImpl()
  212. {
  213. // Get user-specified update options.
  214. std::string opts = this->CTest->GetCTestConfiguration("UpdateOptions");
  215. if (opts.empty()) {
  216. opts = this->CTest->GetCTestConfiguration("SVNUpdateOptions");
  217. }
  218. std::vector<std::string> args = cmSystemTools::ParseArguments(opts.c_str());
  219. // Specify the start time for nightly testing.
  220. if (this->CTest->GetTestModel() == cmCTest::NIGHTLY) {
  221. args.push_back("-r{" + this->GetNightlyTime() + " +0000}");
  222. }
  223. std::vector<char const*> svn_update;
  224. svn_update.push_back("update");
  225. for (std::vector<std::string>::const_iterator ai = args.begin();
  226. ai != args.end(); ++ai) {
  227. svn_update.push_back(ai->c_str());
  228. }
  229. UpdateParser out(this, "up-out> ");
  230. OutputLogger err(this->Log, "up-err> ");
  231. return this->RunSVNCommand(svn_update, &out, &err);
  232. }
  233. bool cmCTestSVN::RunSVNCommand(std::vector<char const*> const& parameters,
  234. OutputParser* out, OutputParser* err)
  235. {
  236. if (parameters.empty()) {
  237. return false;
  238. }
  239. std::vector<char const*> args;
  240. args.push_back(this->CommandLineTool.c_str());
  241. args.insert(args.end(), parameters.begin(), parameters.end());
  242. args.push_back("--non-interactive");
  243. std::string userOptions = this->CTest->GetCTestConfiguration("SVNOptions");
  244. std::vector<std::string> parsedUserOptions =
  245. cmSystemTools::ParseArguments(userOptions.c_str());
  246. for (std::vector<std::string>::iterator i = parsedUserOptions.begin();
  247. i != parsedUserOptions.end(); ++i) {
  248. args.push_back(i->c_str());
  249. }
  250. args.push_back(CM_NULLPTR);
  251. if (strcmp(parameters[0], "update") == 0) {
  252. return RunUpdateCommand(&args[0], out, err);
  253. }
  254. return RunChild(&args[0], out, err);
  255. }
  256. class cmCTestSVN::LogParser : public cmCTestVC::OutputLogger,
  257. private cmXMLParser
  258. {
  259. public:
  260. LogParser(cmCTestSVN* svn, const char* prefix, SVNInfo& svninfo)
  261. : OutputLogger(svn->Log, prefix)
  262. , SVN(svn)
  263. , SVNRepo(svninfo)
  264. {
  265. this->InitializeParser();
  266. }
  267. ~LogParser() CM_OVERRIDE { this->CleanupParser(); }
  268. private:
  269. cmCTestSVN* SVN;
  270. cmCTestSVN::SVNInfo& SVNRepo;
  271. typedef cmCTestSVN::Revision Revision;
  272. typedef cmCTestSVN::Change Change;
  273. Revision Rev;
  274. std::vector<Change> Changes;
  275. Change CurChange;
  276. std::vector<char> CData;
  277. bool ProcessChunk(const char* data, int length) CM_OVERRIDE
  278. {
  279. this->OutputLogger::ProcessChunk(data, length);
  280. this->ParseChunk(data, length);
  281. return true;
  282. }
  283. void StartElement(const std::string& name, const char** atts) CM_OVERRIDE
  284. {
  285. this->CData.clear();
  286. if (name == "logentry") {
  287. this->Rev = Revision();
  288. this->Rev.SVNInfo = &SVNRepo;
  289. if (const char* rev = this->FindAttribute(atts, "revision")) {
  290. this->Rev.Rev = rev;
  291. }
  292. this->Changes.clear();
  293. } else if (name == "path") {
  294. this->CurChange = Change();
  295. if (const char* action = this->FindAttribute(atts, "action")) {
  296. this->CurChange.Action = action[0];
  297. }
  298. }
  299. }
  300. void CharacterDataHandler(const char* data, int length) CM_OVERRIDE
  301. {
  302. this->CData.insert(this->CData.end(), data, data + length);
  303. }
  304. void EndElement(const std::string& name) CM_OVERRIDE
  305. {
  306. if (name == "logentry") {
  307. this->SVN->DoRevisionSVN(this->Rev, this->Changes);
  308. } else if (!this->CData.empty() && name == "path") {
  309. std::string orig_path(&this->CData[0], this->CData.size());
  310. std::string new_path = SVNRepo.BuildLocalPath(orig_path);
  311. this->CurChange.Path.assign(new_path);
  312. this->Changes.push_back(this->CurChange);
  313. } else if (!this->CData.empty() && name == "author") {
  314. this->Rev.Author.assign(&this->CData[0], this->CData.size());
  315. } else if (!this->CData.empty() && name == "date") {
  316. this->Rev.Date.assign(&this->CData[0], this->CData.size());
  317. } else if (!this->CData.empty() && name == "msg") {
  318. this->Rev.Log.assign(&this->CData[0], this->CData.size());
  319. }
  320. this->CData.clear();
  321. }
  322. void ReportError(int /*line*/, int /*column*/, const char* msg) CM_OVERRIDE
  323. {
  324. this->SVN->Log << "Error parsing svn log xml: " << msg << "\n";
  325. }
  326. };
  327. void cmCTestSVN::LoadRevisions()
  328. {
  329. // Get revisions for all the external repositories
  330. std::list<SVNInfo>::iterator itbeg = this->Repositories.begin();
  331. std::list<SVNInfo>::iterator itend = this->Repositories.end();
  332. for (; itbeg != itend; itbeg++) {
  333. SVNInfo& svninfo = *itbeg;
  334. LoadRevisions(svninfo);
  335. }
  336. }
  337. void cmCTestSVN::LoadRevisions(SVNInfo& svninfo)
  338. {
  339. // We are interested in every revision included in the update.
  340. std::string revs;
  341. if (atoi(svninfo.OldRevision.c_str()) < atoi(svninfo.NewRevision.c_str())) {
  342. revs = "-r" + svninfo.OldRevision + ":" + svninfo.NewRevision;
  343. } else {
  344. revs = "-r" + svninfo.NewRevision;
  345. }
  346. // Run "svn log" to get all global revisions of interest.
  347. std::vector<const char*> svn_log;
  348. svn_log.push_back("log");
  349. svn_log.push_back("--xml");
  350. svn_log.push_back("-v");
  351. svn_log.push_back(revs.c_str());
  352. svn_log.push_back(svninfo.LocalPath.c_str());
  353. LogParser out(this, "log-out> ", svninfo);
  354. OutputLogger err(this->Log, "log-err> ");
  355. this->RunSVNCommand(svn_log, &out, &err);
  356. }
  357. void cmCTestSVN::DoRevisionSVN(Revision const& revision,
  358. std::vector<Change> const& changes)
  359. {
  360. // Guess the base checkout path from the changes if necessary.
  361. if (this->RootInfo->Base.empty() && !changes.empty()) {
  362. this->GuessBase(*this->RootInfo, changes);
  363. }
  364. // Ignore changes in the old revision for external repositories
  365. if (revision.Rev == revision.SVNInfo->OldRevision &&
  366. revision.SVNInfo->LocalPath != "") {
  367. return;
  368. }
  369. this->cmCTestGlobalVC::DoRevision(revision, changes);
  370. }
  371. class cmCTestSVN::StatusParser : public cmCTestVC::LineParser
  372. {
  373. public:
  374. StatusParser(cmCTestSVN* svn, const char* prefix)
  375. : SVN(svn)
  376. {
  377. this->SetLog(&svn->Log, prefix);
  378. this->RegexStatus.compile("^([ACDIMRX?!~ ])([CM ])[ L]... +(.+)$");
  379. }
  380. private:
  381. cmCTestSVN* SVN;
  382. cmsys::RegularExpression RegexStatus;
  383. bool ProcessLine() CM_OVERRIDE
  384. {
  385. if (this->RegexStatus.find(this->Line)) {
  386. this->DoPath(this->RegexStatus.match(1)[0],
  387. this->RegexStatus.match(2)[0], this->RegexStatus.match(3));
  388. }
  389. return true;
  390. }
  391. void DoPath(char path_status, char prop_status, std::string const& path)
  392. {
  393. char status = (path_status != ' ') ? path_status : prop_status;
  394. // See "svn help status".
  395. switch (status) {
  396. case 'M':
  397. case '!':
  398. case 'A':
  399. case 'D':
  400. case 'R':
  401. this->SVN->DoModification(PathModified, path);
  402. break;
  403. case 'C':
  404. case '~':
  405. this->SVN->DoModification(PathConflicting, path);
  406. break;
  407. case 'X':
  408. case 'I':
  409. case '?':
  410. case ' ':
  411. default:
  412. break;
  413. }
  414. }
  415. };
  416. void cmCTestSVN::LoadModifications()
  417. {
  418. // Run "svn status" which reports local modifications.
  419. std::vector<const char*> svn_status;
  420. svn_status.push_back("status");
  421. StatusParser out(this, "status-out> ");
  422. OutputLogger err(this->Log, "status-err> ");
  423. this->RunSVNCommand(svn_status, &out, &err);
  424. }
  425. void cmCTestSVN::WriteXMLGlobal(cmXMLWriter& xml)
  426. {
  427. this->cmCTestGlobalVC::WriteXMLGlobal(xml);
  428. xml.Element("SVNPath", this->RootInfo->Base);
  429. }
  430. class cmCTestSVN::ExternalParser : public cmCTestVC::LineParser
  431. {
  432. public:
  433. ExternalParser(cmCTestSVN* svn, const char* prefix)
  434. : SVN(svn)
  435. {
  436. this->SetLog(&svn->Log, prefix);
  437. this->RegexExternal.compile("^X..... +(.+)$");
  438. }
  439. private:
  440. cmCTestSVN* SVN;
  441. cmsys::RegularExpression RegexExternal;
  442. bool ProcessLine() CM_OVERRIDE
  443. {
  444. if (this->RegexExternal.find(this->Line)) {
  445. this->DoPath(this->RegexExternal.match(1));
  446. }
  447. return true;
  448. }
  449. void DoPath(std::string const& path)
  450. {
  451. // Get local path relative to the source directory
  452. std::string local_path;
  453. if (path.size() > this->SVN->SourceDirectory.size() &&
  454. strncmp(path.c_str(), this->SVN->SourceDirectory.c_str(),
  455. this->SVN->SourceDirectory.size()) == 0) {
  456. local_path = path.c_str() + this->SVN->SourceDirectory.size() + 1;
  457. } else {
  458. local_path = path;
  459. }
  460. this->SVN->Repositories.push_back(SVNInfo(local_path.c_str()));
  461. }
  462. };
  463. void cmCTestSVN::LoadRepositories()
  464. {
  465. // Info for root repository
  466. this->Repositories.clear();
  467. this->Repositories.push_back(SVNInfo(""));
  468. this->RootInfo = &(this->Repositories.back());
  469. // Run "svn status" to get the list of external repositories
  470. std::vector<const char*> svn_status;
  471. svn_status.push_back("status");
  472. ExternalParser out(this, "external-out> ");
  473. OutputLogger err(this->Log, "external-err> ");
  474. this->RunSVNCommand(svn_status, &out, &err);
  475. }
  476. std::string cmCTestSVN::SVNInfo::BuildLocalPath(std::string const& path) const
  477. {
  478. std::string local_path;
  479. // Add local path prefix if not empty
  480. if (!this->LocalPath.empty()) {
  481. local_path += this->LocalPath;
  482. local_path += "/";
  483. }
  484. // Add path with base prefix removed
  485. if (path.size() > this->Base.size() &&
  486. strncmp(path.c_str(), this->Base.c_str(), this->Base.size()) == 0) {
  487. local_path += (path.c_str() + this->Base.size());
  488. } else {
  489. local_path += path;
  490. }
  491. return local_path;
  492. }