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