cmCTestSVN.cxx 17 KB

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