cmCTestP4.cxx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  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 "cmCTestP4.h"
  4. #include "cmAlgorithms.h"
  5. #include "cmCTest.h"
  6. #include "cmCTestVC.h"
  7. #include "cmProcessTools.h"
  8. #include "cmRange.h"
  9. #include "cmStringAlgorithms.h"
  10. #include "cmSystemTools.h"
  11. #include "cmsys/RegularExpression.hxx"
  12. #include <algorithm>
  13. #include <ostream>
  14. #include <time.h>
  15. #include <utility>
  16. cmCTestP4::cmCTestP4(cmCTest* ct, std::ostream& log)
  17. : cmCTestGlobalVC(ct, log)
  18. {
  19. this->PriorRev = this->Unknown;
  20. }
  21. cmCTestP4::~cmCTestP4() = default;
  22. class cmCTestP4::IdentifyParser : public cmCTestVC::LineParser
  23. {
  24. public:
  25. IdentifyParser(cmCTestP4* p4, const char* prefix, std::string& rev)
  26. : Rev(rev)
  27. {
  28. this->SetLog(&p4->Log, prefix);
  29. this->RegexIdentify.compile("^Change ([0-9]+) on");
  30. }
  31. private:
  32. std::string& Rev;
  33. cmsys::RegularExpression RegexIdentify;
  34. bool ProcessLine() override
  35. {
  36. if (this->RegexIdentify.find(this->Line)) {
  37. this->Rev = this->RegexIdentify.match(1);
  38. return false;
  39. }
  40. return true;
  41. }
  42. };
  43. class cmCTestP4::ChangesParser : public cmCTestVC::LineParser
  44. {
  45. public:
  46. ChangesParser(cmCTestP4* p4, const char* prefix)
  47. : P4(p4)
  48. {
  49. this->SetLog(&P4->Log, prefix);
  50. this->RegexIdentify.compile("^Change ([0-9]+) on");
  51. }
  52. private:
  53. cmsys::RegularExpression RegexIdentify;
  54. cmCTestP4* P4;
  55. bool ProcessLine() override
  56. {
  57. if (this->RegexIdentify.find(this->Line)) {
  58. P4->ChangeLists.push_back(this->RegexIdentify.match(1));
  59. }
  60. return true;
  61. }
  62. };
  63. class cmCTestP4::UserParser : public cmCTestVC::LineParser
  64. {
  65. public:
  66. UserParser(cmCTestP4* p4, const char* prefix)
  67. : P4(p4)
  68. {
  69. this->SetLog(&P4->Log, prefix);
  70. this->RegexUser.compile("^(.+) <(.*)> \\((.*)\\) accessed (.*)$");
  71. }
  72. private:
  73. cmsys::RegularExpression RegexUser;
  74. cmCTestP4* P4;
  75. bool ProcessLine() override
  76. {
  77. if (this->RegexUser.find(this->Line)) {
  78. User NewUser;
  79. NewUser.UserName = this->RegexUser.match(1);
  80. NewUser.EMail = this->RegexUser.match(2);
  81. NewUser.Name = this->RegexUser.match(3);
  82. NewUser.AccessTime = this->RegexUser.match(4);
  83. P4->Users[this->RegexUser.match(1)] = NewUser;
  84. return false;
  85. }
  86. return true;
  87. }
  88. };
  89. /* Diff format:
  90. ==== //depot/file#rev - /absolute/path/to/file ====
  91. (diff data)
  92. ==== //depot/file2#rev - /absolute/path/to/file2 ====
  93. (diff data)
  94. ==== //depot/file3#rev - /absolute/path/to/file3 ====
  95. ==== //depot/file4#rev - /absolute/path/to/file4 ====
  96. (diff data)
  97. */
  98. class cmCTestP4::DiffParser : public cmCTestVC::LineParser
  99. {
  100. public:
  101. DiffParser(cmCTestP4* p4, const char* prefix)
  102. : P4(p4)
  103. , AlreadyNotified(false)
  104. {
  105. this->SetLog(&P4->Log, prefix);
  106. this->RegexDiff.compile("^==== (.*)#[0-9]+ - (.*)");
  107. }
  108. private:
  109. cmCTestP4* P4;
  110. bool AlreadyNotified;
  111. std::string CurrentPath;
  112. cmsys::RegularExpression RegexDiff;
  113. bool ProcessLine() override
  114. {
  115. if (!this->Line.empty() && this->Line[0] == '=' &&
  116. this->RegexDiff.find(this->Line)) {
  117. CurrentPath = this->RegexDiff.match(1);
  118. AlreadyNotified = false;
  119. } else {
  120. if (!AlreadyNotified) {
  121. P4->DoModification(PathModified, CurrentPath);
  122. AlreadyNotified = true;
  123. }
  124. }
  125. return true;
  126. }
  127. };
  128. cmCTestP4::User cmCTestP4::GetUserData(const std::string& username)
  129. {
  130. std::map<std::string, cmCTestP4::User>::const_iterator it =
  131. Users.find(username);
  132. if (it == Users.end()) {
  133. std::vector<char const*> p4_users;
  134. SetP4Options(p4_users);
  135. p4_users.push_back("users");
  136. p4_users.push_back("-m");
  137. p4_users.push_back("1");
  138. p4_users.push_back(username.c_str());
  139. p4_users.push_back(nullptr);
  140. UserParser out(this, "users-out> ");
  141. OutputLogger err(this->Log, "users-err> ");
  142. RunChild(&p4_users[0], &out, &err);
  143. // The user should now be added to the map. Search again.
  144. it = Users.find(username);
  145. if (it == Users.end()) {
  146. return cmCTestP4::User();
  147. }
  148. }
  149. return it->second;
  150. }
  151. /* Commit format:
  152. Change 1111111 by user@client on 2013/09/26 11:50:36
  153. text
  154. text
  155. Affected files ...
  156. ... //path/to/file#rev edit
  157. ... //path/to/file#rev add
  158. ... //path/to/file#rev delete
  159. ... //path/to/file#rev integrate
  160. */
  161. class cmCTestP4::DescribeParser : public cmCTestVC::LineParser
  162. {
  163. public:
  164. DescribeParser(cmCTestP4* p4, const char* prefix)
  165. : LineParser('\n', false)
  166. , P4(p4)
  167. , Section(SectionHeader)
  168. {
  169. this->SetLog(&P4->Log, prefix);
  170. this->RegexHeader.compile("^Change ([0-9]+) by (.+)@(.+) on (.*)$");
  171. this->RegexDiff.compile(R"(^\.\.\. (.*)#[0-9]+ ([^ ]+)$)");
  172. }
  173. private:
  174. cmsys::RegularExpression RegexHeader;
  175. cmsys::RegularExpression RegexDiff;
  176. cmCTestP4* P4;
  177. typedef cmCTestP4::Revision Revision;
  178. typedef cmCTestP4::Change Change;
  179. std::vector<Change> Changes;
  180. enum SectionType
  181. {
  182. SectionHeader,
  183. SectionBody,
  184. SectionDiffHeader,
  185. SectionDiff,
  186. SectionCount
  187. };
  188. SectionType Section;
  189. Revision Rev;
  190. bool ProcessLine() override
  191. {
  192. if (this->Line.empty()) {
  193. this->NextSection();
  194. } else {
  195. switch (this->Section) {
  196. case SectionHeader:
  197. this->DoHeaderLine();
  198. break;
  199. case SectionBody:
  200. this->DoBodyLine();
  201. break;
  202. case SectionDiffHeader:
  203. break; // nothing to do
  204. case SectionDiff:
  205. this->DoDiffLine();
  206. break;
  207. case SectionCount:
  208. break; // never happens
  209. }
  210. }
  211. return true;
  212. }
  213. void NextSection()
  214. {
  215. if (this->Section == SectionDiff) {
  216. this->P4->DoRevision(this->Rev, this->Changes);
  217. this->Rev = Revision();
  218. }
  219. this->Section = SectionType((this->Section + 1) % SectionCount);
  220. }
  221. void DoHeaderLine()
  222. {
  223. if (this->RegexHeader.find(this->Line)) {
  224. this->Rev.Rev = this->RegexHeader.match(1);
  225. this->Rev.Date = this->RegexHeader.match(4);
  226. cmCTestP4::User user = P4->GetUserData(this->RegexHeader.match(2));
  227. this->Rev.Author = user.Name;
  228. this->Rev.EMail = user.EMail;
  229. this->Rev.Committer = this->Rev.Author;
  230. this->Rev.CommitterEMail = this->Rev.EMail;
  231. this->Rev.CommitDate = this->Rev.Date;
  232. }
  233. }
  234. void DoBodyLine()
  235. {
  236. if (this->Line[0] == '\t') {
  237. this->Rev.Log += this->Line.substr(1);
  238. }
  239. this->Rev.Log += "\n";
  240. }
  241. void DoDiffLine()
  242. {
  243. if (this->RegexDiff.find(this->Line)) {
  244. Change change;
  245. std::string Path = this->RegexDiff.match(1);
  246. if (Path.length() > 2 && Path[0] == '/' && Path[1] == '/') {
  247. size_t found = Path.find('/', 2);
  248. if (found != std::string::npos) {
  249. Path = Path.substr(found + 1);
  250. }
  251. }
  252. change.Path = Path;
  253. std::string action = this->RegexDiff.match(2);
  254. if (action == "add") {
  255. change.Action = 'A';
  256. } else if (action == "delete") {
  257. change.Action = 'D';
  258. } else if (action == "edit" || action == "integrate") {
  259. change.Action = 'M';
  260. }
  261. Changes.push_back(change);
  262. }
  263. }
  264. };
  265. void cmCTestP4::SetP4Options(std::vector<char const*>& CommandOptions)
  266. {
  267. if (P4Options.empty()) {
  268. const char* p4 = this->CommandLineTool.c_str();
  269. P4Options.emplace_back(p4);
  270. // The CTEST_P4_CLIENT variable sets the P4 client used when issuing
  271. // Perforce commands, if it's different from the default one.
  272. std::string client = this->CTest->GetCTestConfiguration("P4Client");
  273. if (!client.empty()) {
  274. P4Options.emplace_back("-c");
  275. P4Options.push_back(client);
  276. }
  277. // Set the message language to be English, in case the P4 admin
  278. // has localized them
  279. P4Options.emplace_back("-L");
  280. P4Options.emplace_back("en");
  281. // The CTEST_P4_OPTIONS variable adds additional Perforce command line
  282. // options before the main command
  283. std::string opts = this->CTest->GetCTestConfiguration("P4Options");
  284. cmAppend(P4Options, cmSystemTools::ParseArguments(opts));
  285. }
  286. CommandOptions.clear();
  287. for (std::string const& o : P4Options) {
  288. CommandOptions.push_back(o.c_str());
  289. }
  290. }
  291. std::string cmCTestP4::GetWorkingRevision()
  292. {
  293. std::vector<char const*> p4_identify;
  294. SetP4Options(p4_identify);
  295. p4_identify.push_back("changes");
  296. p4_identify.push_back("-m");
  297. p4_identify.push_back("1");
  298. p4_identify.push_back("-t");
  299. std::string source = this->SourceDirectory + "/...#have";
  300. p4_identify.push_back(source.c_str());
  301. p4_identify.push_back(nullptr);
  302. std::string rev;
  303. IdentifyParser out(this, "p4_changes-out> ", rev);
  304. OutputLogger err(this->Log, "p4_changes-err> ");
  305. bool result = RunChild(&p4_identify[0], &out, &err);
  306. // If there was a problem contacting the server return "<unknown>"
  307. if (!result) {
  308. return "<unknown>";
  309. }
  310. if (rev.empty()) {
  311. return "0";
  312. }
  313. return rev;
  314. }
  315. bool cmCTestP4::NoteOldRevision()
  316. {
  317. this->OldRevision = this->GetWorkingRevision();
  318. cmCTestLog(this->CTest, HANDLER_OUTPUT,
  319. " Old revision of repository is: " << this->OldRevision
  320. << "\n");
  321. this->PriorRev.Rev = this->OldRevision;
  322. return true;
  323. }
  324. bool cmCTestP4::NoteNewRevision()
  325. {
  326. this->NewRevision = this->GetWorkingRevision();
  327. cmCTestLog(this->CTest, HANDLER_OUTPUT,
  328. " New revision of repository is: " << this->NewRevision
  329. << "\n");
  330. return true;
  331. }
  332. bool cmCTestP4::LoadRevisions()
  333. {
  334. std::vector<char const*> p4_changes;
  335. SetP4Options(p4_changes);
  336. // Use 'p4 changes ...@old,new' to get a list of changelists
  337. std::string range = this->SourceDirectory + "/...";
  338. // If any revision is unknown it means we couldn't contact the server.
  339. // Do not process updates
  340. if (this->OldRevision == "<unknown>" || this->NewRevision == "<unknown>") {
  341. cmCTestLog(this->CTest, HANDLER_OUTPUT,
  342. " At least one of the revisions "
  343. << "is unknown. No repository changes will be reported.\n");
  344. return false;
  345. }
  346. range.append("@")
  347. .append(this->OldRevision)
  348. .append(",")
  349. .append(this->NewRevision);
  350. p4_changes.push_back("changes");
  351. p4_changes.push_back(range.c_str());
  352. p4_changes.push_back(nullptr);
  353. ChangesParser out(this, "p4_changes-out> ");
  354. OutputLogger err(this->Log, "p4_changes-err> ");
  355. ChangeLists.clear();
  356. this->RunChild(&p4_changes[0], &out, &err);
  357. if (ChangeLists.empty()) {
  358. return true;
  359. }
  360. // p4 describe -s ...@1111111,2222222
  361. std::vector<char const*> p4_describe;
  362. for (std::string const& i : cmReverseRange(ChangeLists)) {
  363. SetP4Options(p4_describe);
  364. p4_describe.push_back("describe");
  365. p4_describe.push_back("-s");
  366. p4_describe.push_back(i.c_str());
  367. p4_describe.push_back(nullptr);
  368. DescribeParser outDescribe(this, "p4_describe-out> ");
  369. OutputLogger errDescribe(this->Log, "p4_describe-err> ");
  370. this->RunChild(&p4_describe[0], &outDescribe, &errDescribe);
  371. }
  372. return true;
  373. }
  374. bool cmCTestP4::LoadModifications()
  375. {
  376. std::vector<char const*> p4_diff;
  377. SetP4Options(p4_diff);
  378. p4_diff.push_back("diff");
  379. // Ideally we would use -Od but not all clients support it
  380. p4_diff.push_back("-dn");
  381. std::string source = this->SourceDirectory + "/...";
  382. p4_diff.push_back(source.c_str());
  383. p4_diff.push_back(nullptr);
  384. DiffParser out(this, "p4_diff-out> ");
  385. OutputLogger err(this->Log, "p4_diff-err> ");
  386. this->RunChild(&p4_diff[0], &out, &err);
  387. return true;
  388. }
  389. bool cmCTestP4::UpdateCustom(const std::string& custom)
  390. {
  391. std::vector<std::string> p4_custom_command;
  392. cmExpandList(custom, p4_custom_command, true);
  393. std::vector<char const*> p4_custom;
  394. p4_custom.reserve(p4_custom_command.size() + 1);
  395. for (std::string const& i : p4_custom_command) {
  396. p4_custom.push_back(i.c_str());
  397. }
  398. p4_custom.push_back(nullptr);
  399. OutputLogger custom_out(this->Log, "p4_customsync-out> ");
  400. OutputLogger custom_err(this->Log, "p4_customsync-err> ");
  401. return this->RunUpdateCommand(&p4_custom[0], &custom_out, &custom_err);
  402. }
  403. bool cmCTestP4::UpdateImpl()
  404. {
  405. std::string custom = this->CTest->GetCTestConfiguration("P4UpdateCustom");
  406. if (!custom.empty()) {
  407. return this->UpdateCustom(custom);
  408. }
  409. // If we couldn't get a revision number before updating, abort.
  410. if (this->OldRevision == "<unknown>") {
  411. this->UpdateCommandLine = "Unknown current revision";
  412. cmCTestLog(this->CTest, ERROR_MESSAGE, " Unknown current revision\n");
  413. return false;
  414. }
  415. std::vector<char const*> p4_sync;
  416. SetP4Options(p4_sync);
  417. p4_sync.push_back("sync");
  418. // Get user-specified update options.
  419. std::string opts = this->CTest->GetCTestConfiguration("UpdateOptions");
  420. if (opts.empty()) {
  421. opts = this->CTest->GetCTestConfiguration("P4UpdateOptions");
  422. }
  423. std::vector<std::string> args = cmSystemTools::ParseArguments(opts);
  424. for (std::string const& arg : args) {
  425. p4_sync.push_back(arg.c_str());
  426. }
  427. std::string source = this->SourceDirectory + "/...";
  428. // Specify the start time for nightly testing.
  429. if (this->CTest->GetTestModel() == cmCTest::NIGHTLY) {
  430. std::string date = this->GetNightlyTime();
  431. // CTest reports the date as YYYY-MM-DD, Perforce needs it as YYYY/MM/DD
  432. std::replace(date.begin(), date.end(), '-', '/');
  433. // Revision specification: /...@"YYYY/MM/DD HH:MM:SS"
  434. source.append("@\"").append(date).append("\"");
  435. }
  436. p4_sync.push_back(source.c_str());
  437. p4_sync.push_back(nullptr);
  438. OutputLogger out(this->Log, "p4_sync-out> ");
  439. OutputLogger err(this->Log, "p4_sync-err> ");
  440. return this->RunUpdateCommand(&p4_sync[0], &out, &err);
  441. }