cmCTestGIT.cxx 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  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 "cmCTestGIT.h"
  11. #include "cmCTest.h"
  12. #include "cmSystemTools.h"
  13. #include "cmXMLSafe.h"
  14. #include <cmsys/RegularExpression.hxx>
  15. #include <cmsys/ios/sstream>
  16. #include <cmsys/Process.h>
  17. #include <sys/types.h>
  18. #include <time.h>
  19. #include <ctype.h>
  20. //----------------------------------------------------------------------------
  21. cmCTestGIT::cmCTestGIT(cmCTest* ct, std::ostream& log):
  22. cmCTestGlobalVC(ct, log)
  23. {
  24. this->PriorRev = this->Unknown;
  25. }
  26. //----------------------------------------------------------------------------
  27. cmCTestGIT::~cmCTestGIT()
  28. {
  29. }
  30. //----------------------------------------------------------------------------
  31. class cmCTestGIT::OneLineParser: public cmCTestVC::LineParser
  32. {
  33. public:
  34. OneLineParser(cmCTestGIT* git, const char* prefix,
  35. std::string& l): Line1(l)
  36. {
  37. this->SetLog(&git->Log, prefix);
  38. }
  39. private:
  40. std::string& Line1;
  41. virtual bool ProcessLine()
  42. {
  43. // Only the first line is of interest.
  44. this->Line1 = this->Line;
  45. return false;
  46. }
  47. };
  48. //----------------------------------------------------------------------------
  49. std::string cmCTestGIT::GetWorkingRevision()
  50. {
  51. // Run plumbing "git rev-list" to get work tree revision.
  52. const char* git = this->CommandLineTool.c_str();
  53. const char* git_rev_list[] = {git, "rev-list", "-n", "1", "HEAD", 0};
  54. std::string rev;
  55. OneLineParser out(this, "rl-out> ", rev);
  56. OutputLogger err(this->Log, "rl-err> ");
  57. this->RunChild(git_rev_list, &out, &err);
  58. return rev;
  59. }
  60. //----------------------------------------------------------------------------
  61. void cmCTestGIT::NoteOldRevision()
  62. {
  63. this->OldRevision = this->GetWorkingRevision();
  64. cmCTestLog(this->CTest, HANDLER_OUTPUT, " Old revision of repository is: "
  65. << this->OldRevision << "\n");
  66. this->PriorRev.Rev = this->OldRevision;
  67. }
  68. //----------------------------------------------------------------------------
  69. void cmCTestGIT::NoteNewRevision()
  70. {
  71. this->NewRevision = this->GetWorkingRevision();
  72. cmCTestLog(this->CTest, HANDLER_OUTPUT, " New revision of repository is: "
  73. << this->NewRevision << "\n");
  74. }
  75. //----------------------------------------------------------------------------
  76. std::string cmCTestGIT::FindGitDir()
  77. {
  78. std::string git_dir;
  79. // Run "git rev-parse --git-dir" to locate the real .git directory.
  80. const char* git = this->CommandLineTool.c_str();
  81. char const* git_rev_parse[] = {git, "rev-parse", "--git-dir", 0};
  82. std::string git_dir_line;
  83. OneLineParser rev_parse_out(this, "rev-parse-out> ", git_dir_line);
  84. OutputLogger rev_parse_err(this->Log, "rev-parse-err> ");
  85. if(this->RunChild(git_rev_parse, &rev_parse_out, &rev_parse_err))
  86. {
  87. git_dir = git_dir_line;
  88. }
  89. if(git_dir.empty())
  90. {
  91. git_dir = ".git";
  92. }
  93. // Git reports a relative path only when the .git directory is in
  94. // the current directory.
  95. if(git_dir[0] == '.')
  96. {
  97. git_dir = this->SourceDirectory + "/" + git_dir;
  98. }
  99. #if defined(_WIN32) && !defined(__CYGWIN__)
  100. else if(git_dir[0] == '/')
  101. {
  102. // Cygwin Git reports a full path that Cygwin understands, but we
  103. // are a Windows application. Run "cygpath" to get Windows path.
  104. std::string cygpath_exe = cmSystemTools::GetFilenamePath(git);
  105. cygpath_exe += "/cygpath.exe";
  106. if(cmSystemTools::FileExists(cygpath_exe.c_str()))
  107. {
  108. char const* cygpath[] = {cygpath_exe.c_str(), "-w", git_dir.c_str(), 0};
  109. OneLineParser cygpath_out(this, "cygpath-out> ", git_dir_line);
  110. OutputLogger cygpath_err(this->Log, "cygpath-err> ");
  111. if(this->RunChild(cygpath, &cygpath_out, &cygpath_err))
  112. {
  113. git_dir = git_dir_line;
  114. }
  115. }
  116. }
  117. #endif
  118. return git_dir;
  119. }
  120. //----------------------------------------------------------------------------
  121. std::string cmCTestGIT::FindTopDir()
  122. {
  123. std::string top_dir = this->SourceDirectory;
  124. // Run "git rev-parse --show-cdup" to locate the top of the tree.
  125. const char* git = this->CommandLineTool.c_str();
  126. char const* git_rev_parse[] = {git, "rev-parse", "--show-cdup", 0};
  127. std::string cdup;
  128. OneLineParser rev_parse_out(this, "rev-parse-out> ", cdup);
  129. OutputLogger rev_parse_err(this->Log, "rev-parse-err> ");
  130. if(this->RunChild(git_rev_parse, &rev_parse_out, &rev_parse_err) &&
  131. !cdup.empty())
  132. {
  133. top_dir += "/";
  134. top_dir += cdup;
  135. top_dir = cmSystemTools::CollapseFullPath(top_dir.c_str());
  136. }
  137. return top_dir;
  138. }
  139. //----------------------------------------------------------------------------
  140. bool cmCTestGIT::UpdateByFetchAndReset()
  141. {
  142. const char* git = this->CommandLineTool.c_str();
  143. // Use "git fetch" to get remote commits.
  144. std::vector<char const*> git_fetch;
  145. git_fetch.push_back(git);
  146. git_fetch.push_back("fetch");
  147. // Add user-specified update options.
  148. std::string opts = this->CTest->GetCTestConfiguration("UpdateOptions");
  149. if(opts.empty())
  150. {
  151. opts = this->CTest->GetCTestConfiguration("GITUpdateOptions");
  152. }
  153. std::vector<cmStdString> args = cmSystemTools::ParseArguments(opts.c_str());
  154. for(std::vector<cmStdString>::const_iterator ai = args.begin();
  155. ai != args.end(); ++ai)
  156. {
  157. git_fetch.push_back(ai->c_str());
  158. }
  159. // Sentinel argument.
  160. git_fetch.push_back(0);
  161. // Fetch upstream refs.
  162. OutputLogger fetch_out(this->Log, "fetch-out> ");
  163. OutputLogger fetch_err(this->Log, "fetch-err> ");
  164. if(!this->RunUpdateCommand(&git_fetch[0], &fetch_out, &fetch_err))
  165. {
  166. return false;
  167. }
  168. // Identify the merge head that would be used by "git pull".
  169. std::string sha1;
  170. {
  171. std::string fetch_head = this->FindGitDir() + "/FETCH_HEAD";
  172. std::ifstream fin(fetch_head.c_str(), std::ios::in | std::ios::binary);
  173. if(!fin)
  174. {
  175. this->Log << "Unable to open " << fetch_head << "\n";
  176. return false;
  177. }
  178. std::string line;
  179. while(sha1.empty() && cmSystemTools::GetLineFromStream(fin, line))
  180. {
  181. this->Log << "FETCH_HEAD> " << line << "\n";
  182. if(line.find("\tnot-for-merge\t") == line.npos)
  183. {
  184. std::string::size_type pos = line.find('\t');
  185. if(pos != line.npos)
  186. {
  187. sha1 = line.substr(0, pos);
  188. }
  189. }
  190. }
  191. if(sha1.empty())
  192. {
  193. this->Log << "FETCH_HEAD has no upstream branch candidate!\n";
  194. return false;
  195. }
  196. }
  197. // Reset the local branch to point at that tracked from upstream.
  198. char const* git_reset[] = {git, "reset", "--hard", sha1.c_str(), 0};
  199. OutputLogger reset_out(this->Log, "reset-out> ");
  200. OutputLogger reset_err(this->Log, "reset-err> ");
  201. return this->RunChild(&git_reset[0], &reset_out, &reset_err);
  202. }
  203. //----------------------------------------------------------------------------
  204. bool cmCTestGIT::UpdateByCustom(std::string const& custom)
  205. {
  206. std::vector<std::string> git_custom_command;
  207. cmSystemTools::ExpandListArgument(custom, git_custom_command, true);
  208. std::vector<char const*> git_custom;
  209. for(std::vector<std::string>::const_iterator
  210. i = git_custom_command.begin(); i != git_custom_command.end(); ++i)
  211. {
  212. git_custom.push_back(i->c_str());
  213. }
  214. git_custom.push_back(0);
  215. OutputLogger custom_out(this->Log, "custom-out> ");
  216. OutputLogger custom_err(this->Log, "custom-err> ");
  217. return this->RunUpdateCommand(&git_custom[0], &custom_out, &custom_err);
  218. }
  219. //----------------------------------------------------------------------------
  220. bool cmCTestGIT::UpdateInternal()
  221. {
  222. std::string custom = this->CTest->GetCTestConfiguration("GITUpdateCustom");
  223. if(!custom.empty())
  224. {
  225. return this->UpdateByCustom(custom);
  226. }
  227. return this->UpdateByFetchAndReset();
  228. }
  229. //----------------------------------------------------------------------------
  230. bool cmCTestGIT::UpdateImpl()
  231. {
  232. if(!this->UpdateInternal())
  233. {
  234. return false;
  235. }
  236. std::string top_dir = this->FindTopDir();
  237. const char* git = this->CommandLineTool.c_str();
  238. char const* git_submodule[] = {git, "submodule", "update", 0};
  239. OutputLogger submodule_out(this->Log, "submodule-out> ");
  240. OutputLogger submodule_err(this->Log, "submodule-err> ");
  241. return this->RunChild(git_submodule, &submodule_out, &submodule_err,
  242. top_dir.c_str());
  243. }
  244. //----------------------------------------------------------------------------
  245. /* Diff format:
  246. :src-mode dst-mode src-sha1 dst-sha1 status\0
  247. src-path\0
  248. [dst-path\0]
  249. The format is repeated for every file changed. The [dst-path\0]
  250. line appears only for lines with status 'C' or 'R'. See 'git help
  251. diff-tree' for details.
  252. */
  253. class cmCTestGIT::DiffParser: public cmCTestVC::LineParser
  254. {
  255. public:
  256. DiffParser(cmCTestGIT* git, const char* prefix):
  257. LineParser('\0', false), GIT(git), DiffField(DiffFieldNone)
  258. {
  259. this->SetLog(&git->Log, prefix);
  260. }
  261. typedef cmCTestGIT::Change Change;
  262. std::vector<Change> Changes;
  263. protected:
  264. cmCTestGIT* GIT;
  265. enum DiffFieldType { DiffFieldNone, DiffFieldChange,
  266. DiffFieldSrc, DiffFieldDst };
  267. DiffFieldType DiffField;
  268. Change CurChange;
  269. void DiffReset()
  270. {
  271. this->DiffField = DiffFieldNone;
  272. this->Changes.clear();
  273. }
  274. virtual bool ProcessLine()
  275. {
  276. if(this->Line[0] == ':')
  277. {
  278. this->DiffField = DiffFieldChange;
  279. this->CurChange = Change();
  280. }
  281. if(this->DiffField == DiffFieldChange)
  282. {
  283. // :src-mode dst-mode src-sha1 dst-sha1 status
  284. if(this->Line[0] != ':')
  285. {
  286. this->DiffField = DiffFieldNone;
  287. return true;
  288. }
  289. const char* src_mode_first = this->Line.c_str()+1;
  290. const char* src_mode_last = this->ConsumeField(src_mode_first);
  291. const char* dst_mode_first = this->ConsumeSpace(src_mode_last);
  292. const char* dst_mode_last = this->ConsumeField(dst_mode_first);
  293. const char* src_sha1_first = this->ConsumeSpace(dst_mode_last);
  294. const char* src_sha1_last = this->ConsumeField(src_sha1_first);
  295. const char* dst_sha1_first = this->ConsumeSpace(src_sha1_last);
  296. const char* dst_sha1_last = this->ConsumeField(dst_sha1_first);
  297. const char* status_first = this->ConsumeSpace(dst_sha1_last);
  298. const char* status_last = this->ConsumeField(status_first);
  299. if(status_first != status_last)
  300. {
  301. this->CurChange.Action = *status_first;
  302. this->DiffField = DiffFieldSrc;
  303. }
  304. else
  305. {
  306. this->DiffField = DiffFieldNone;
  307. }
  308. }
  309. else if(this->DiffField == DiffFieldSrc)
  310. {
  311. // src-path
  312. if(this->CurChange.Action == 'C')
  313. {
  314. // Convert copy to addition of destination.
  315. this->CurChange.Action = 'A';
  316. this->DiffField = DiffFieldDst;
  317. }
  318. else if(this->CurChange.Action == 'R')
  319. {
  320. // Convert rename to deletion of source and addition of destination.
  321. this->CurChange.Action = 'D';
  322. this->CurChange.Path = this->Line;
  323. this->Changes.push_back(this->CurChange);
  324. this->CurChange = Change('A');
  325. this->DiffField = DiffFieldDst;
  326. }
  327. else
  328. {
  329. this->CurChange.Path = this->Line;
  330. this->Changes.push_back(this->CurChange);
  331. this->DiffField = this->DiffFieldNone;
  332. }
  333. }
  334. else if(this->DiffField == DiffFieldDst)
  335. {
  336. // dst-path
  337. this->CurChange.Path = this->Line;
  338. this->Changes.push_back(this->CurChange);
  339. this->DiffField = this->DiffFieldNone;
  340. }
  341. return true;
  342. }
  343. const char* ConsumeSpace(const char* c)
  344. {
  345. while(*c && isspace(*c)) { ++c; }
  346. return c;
  347. }
  348. const char* ConsumeField(const char* c)
  349. {
  350. while(*c && !isspace(*c)) { ++c; }
  351. return c;
  352. }
  353. };
  354. //----------------------------------------------------------------------------
  355. /* Commit format:
  356. commit ...\n
  357. tree ...\n
  358. parent ...\n
  359. author ...\n
  360. committer ...\n
  361. \n
  362. Log message indented by (4) spaces\n
  363. (even blank lines have the spaces)\n
  364. [[
  365. \n
  366. [Diff format]
  367. OR
  368. \0
  369. ]]
  370. The header may have more fields. See 'git help diff-tree'.
  371. */
  372. class cmCTestGIT::CommitParser: public cmCTestGIT::DiffParser
  373. {
  374. public:
  375. CommitParser(cmCTestGIT* git, const char* prefix):
  376. DiffParser(git, prefix), Section(SectionHeader)
  377. {
  378. this->Separator = SectionSep[this->Section];
  379. }
  380. private:
  381. typedef cmCTestGIT::Revision Revision;
  382. enum SectionType { SectionHeader, SectionBody, SectionDiff, SectionCount };
  383. static char const SectionSep[SectionCount];
  384. SectionType Section;
  385. Revision Rev;
  386. struct Person
  387. {
  388. std::string Name;
  389. std::string EMail;
  390. unsigned long Time;
  391. long TimeZone;
  392. Person(): Name(), EMail(), Time(0), TimeZone(0) {}
  393. };
  394. void ParsePerson(const char* str, Person& person)
  395. {
  396. // Person Name <[email protected]> 1234567890 +0000
  397. const char* c = str;
  398. while(*c && isspace(*c)) { ++c; }
  399. const char* name_first = c;
  400. while(*c && *c != '<') { ++c; }
  401. const char* name_last = c;
  402. while(name_last != name_first && isspace(*(name_last-1))) { --name_last; }
  403. person.Name.assign(name_first, name_last-name_first);
  404. const char* email_first = *c? ++c : c;
  405. while(*c && *c != '>') { ++c; }
  406. const char* email_last = *c? c++ : c;
  407. person.EMail.assign(email_first, email_last-email_first);
  408. person.Time = strtoul(c, (char**)&c, 10);
  409. person.TimeZone = strtol(c, (char**)&c, 10);
  410. }
  411. virtual bool ProcessLine()
  412. {
  413. if(this->Line.empty())
  414. {
  415. if(this->Section == SectionBody && this->LineEnd == '\0')
  416. {
  417. // Skip SectionDiff
  418. this->NextSection();
  419. }
  420. this->NextSection();
  421. }
  422. else
  423. {
  424. switch(this->Section)
  425. {
  426. case SectionHeader: this->DoHeaderLine(); break;
  427. case SectionBody: this->DoBodyLine(); break;
  428. case SectionDiff: this->DiffParser::ProcessLine(); break;
  429. case SectionCount: break; // never happens
  430. }
  431. }
  432. return true;
  433. }
  434. void NextSection()
  435. {
  436. this->Section = SectionType((this->Section+1) % SectionCount);
  437. this->Separator = SectionSep[this->Section];
  438. if(this->Section == SectionHeader)
  439. {
  440. this->GIT->DoRevision(this->Rev, this->Changes);
  441. this->Rev = Revision();
  442. this->DiffReset();
  443. }
  444. }
  445. void DoHeaderLine()
  446. {
  447. // Look for header fields that we need.
  448. if(strncmp(this->Line.c_str(), "commit ", 7) == 0)
  449. {
  450. this->Rev.Rev = this->Line.c_str()+7;
  451. }
  452. else if(strncmp(this->Line.c_str(), "author ", 7) == 0)
  453. {
  454. Person author;
  455. this->ParsePerson(this->Line.c_str()+7, author);
  456. this->Rev.Author = author.Name;
  457. this->Rev.EMail = author.EMail;
  458. // Convert the time to a human-readable format that is also easy
  459. // to machine-parse: "CCYY-MM-DD hh:mm:ss".
  460. time_t seconds = static_cast<time_t>(author.Time);
  461. struct tm* t = gmtime(&seconds);
  462. char dt[1024];
  463. sprintf(dt, "%04d-%02d-%02d %02d:%02d:%02d",
  464. t->tm_year+1900, t->tm_mon+1, t->tm_mday,
  465. t->tm_hour, t->tm_min, t->tm_sec);
  466. this->Rev.Date = dt;
  467. // Add the time-zone field "+zone" or "-zone".
  468. char tz[32];
  469. if(author.TimeZone >= 0)
  470. {
  471. sprintf(tz, " +%04ld", author.TimeZone);
  472. }
  473. else
  474. {
  475. sprintf(tz, " -%04ld", -author.TimeZone);
  476. }
  477. this->Rev.Date += tz;
  478. }
  479. }
  480. void DoBodyLine()
  481. {
  482. // Commit log lines are indented by 4 spaces.
  483. if(this->Line.size() >= 4)
  484. {
  485. this->Rev.Log += this->Line.substr(4);
  486. }
  487. this->Rev.Log += "\n";
  488. }
  489. };
  490. char const cmCTestGIT::CommitParser::SectionSep[SectionCount] =
  491. {'\n', '\n', '\0'};
  492. //----------------------------------------------------------------------------
  493. void cmCTestGIT::LoadRevisions()
  494. {
  495. // Use 'git rev-list ... | git diff-tree ...' to get revisions.
  496. std::string range = this->OldRevision + ".." + this->NewRevision;
  497. const char* git = this->CommandLineTool.c_str();
  498. const char* git_rev_list[] =
  499. {git, "rev-list", "--reverse", range.c_str(), "--", 0};
  500. const char* git_diff_tree[] =
  501. {git, "diff-tree", "--stdin", "--always", "-z", "-r", "--pretty=raw",
  502. "--encoding=utf-8", 0};
  503. this->Log << this->ComputeCommandLine(git_rev_list) << " | "
  504. << this->ComputeCommandLine(git_diff_tree) << "\n";
  505. cmsysProcess* cp = cmsysProcess_New();
  506. cmsysProcess_AddCommand(cp, git_rev_list);
  507. cmsysProcess_AddCommand(cp, git_diff_tree);
  508. cmsysProcess_SetWorkingDirectory(cp, this->SourceDirectory.c_str());
  509. CommitParser out(this, "dt-out> ");
  510. OutputLogger err(this->Log, "dt-err> ");
  511. this->RunProcess(cp, &out, &err);
  512. // Send one extra zero-byte to terminate the last record.
  513. out.Process("", 1);
  514. cmsysProcess_Delete(cp);
  515. }
  516. //----------------------------------------------------------------------------
  517. void cmCTestGIT::LoadModifications()
  518. {
  519. const char* git = this->CommandLineTool.c_str();
  520. // Use 'git update-index' to refresh the index w.r.t. the work tree.
  521. const char* git_update_index[] = {git, "update-index", "--refresh", 0};
  522. OutputLogger ui_out(this->Log, "ui-out> ");
  523. OutputLogger ui_err(this->Log, "ui-err> ");
  524. this->RunChild(git_update_index, &ui_out, &ui_err);
  525. // Use 'git diff-index' to get modified files.
  526. const char* git_diff_index[] = {git, "diff-index", "-z", "HEAD", 0};
  527. DiffParser out(this, "di-out> ");
  528. OutputLogger err(this->Log, "di-err> ");
  529. this->RunChild(git_diff_index, &out, &err);
  530. for(std::vector<Change>::const_iterator ci = out.Changes.begin();
  531. ci != out.Changes.end(); ++ci)
  532. {
  533. this->DoModification(PathModified, ci->Path);
  534. }
  535. }