cmCTestSVN.cxx 18 KB

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