cmCTestGIT.cxx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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. bool cmCTestGIT::UpdateImpl()
  77. {
  78. const char* git = this->CommandLineTool.c_str();
  79. // Use "git pull" to update the working tree.
  80. std::vector<char const*> git_pull;
  81. git_pull.push_back(git);
  82. git_pull.push_back("pull");
  83. // TODO: if(this->CTest->GetTestModel() == cmCTest::NIGHTLY)
  84. // Add user-specified update options.
  85. std::string opts = this->CTest->GetCTestConfiguration("UpdateOptions");
  86. if(opts.empty())
  87. {
  88. opts = this->CTest->GetCTestConfiguration("GITUpdateOptions");
  89. }
  90. std::vector<cmStdString> args = cmSystemTools::ParseArguments(opts.c_str());
  91. for(std::vector<cmStdString>::const_iterator ai = args.begin();
  92. ai != args.end(); ++ai)
  93. {
  94. git_pull.push_back(ai->c_str());
  95. }
  96. // Sentinel argument.
  97. git_pull.push_back(0);
  98. OutputLogger out(this->Log, "pull-out> ");
  99. OutputLogger err(this->Log, "pull-err> ");
  100. if(this->RunUpdateCommand(&git_pull[0], &out, &err))
  101. {
  102. char const* git_submodule[] = {git, "submodule", "update", 0};
  103. OutputLogger out2(this->Log, "submodule-out> ");
  104. OutputLogger err2(this->Log, "submodule-err> ");
  105. return this->RunChild(git_submodule, &out, &err);
  106. }
  107. return false;
  108. }
  109. //----------------------------------------------------------------------------
  110. /* Diff format:
  111. :src-mode dst-mode src-sha1 dst-sha1 status\0
  112. src-path\0
  113. [dst-path\0]
  114. The format is repeated for every file changed. The [dst-path\0]
  115. line appears only for lines with status 'C' or 'R'. See 'git help
  116. diff-tree' for details.
  117. */
  118. class cmCTestGIT::DiffParser: public cmCTestVC::LineParser
  119. {
  120. public:
  121. DiffParser(cmCTestGIT* git, const char* prefix):
  122. LineParser('\0', false), GIT(git), DiffField(DiffFieldNone)
  123. {
  124. this->SetLog(&git->Log, prefix);
  125. }
  126. typedef cmCTestGIT::Change Change;
  127. std::vector<Change> Changes;
  128. protected:
  129. cmCTestGIT* GIT;
  130. enum DiffFieldType { DiffFieldNone, DiffFieldChange,
  131. DiffFieldSrc, DiffFieldDst };
  132. DiffFieldType DiffField;
  133. Change CurChange;
  134. void DiffReset()
  135. {
  136. this->DiffField = DiffFieldNone;
  137. this->Changes.clear();
  138. }
  139. virtual bool ProcessLine()
  140. {
  141. if(this->Line[0] == ':')
  142. {
  143. this->DiffField = DiffFieldChange;
  144. this->CurChange = Change();
  145. }
  146. if(this->DiffField == DiffFieldChange)
  147. {
  148. // :src-mode dst-mode src-sha1 dst-sha1 status
  149. if(this->Line[0] != ':')
  150. {
  151. this->DiffField = DiffFieldNone;
  152. return true;
  153. }
  154. const char* src_mode_first = this->Line.c_str()+1;
  155. const char* src_mode_last = this->ConsumeField(src_mode_first);
  156. const char* dst_mode_first = this->ConsumeSpace(src_mode_last);
  157. const char* dst_mode_last = this->ConsumeField(dst_mode_first);
  158. const char* src_sha1_first = this->ConsumeSpace(dst_mode_last);
  159. const char* src_sha1_last = this->ConsumeField(src_sha1_first);
  160. const char* dst_sha1_first = this->ConsumeSpace(src_sha1_last);
  161. const char* dst_sha1_last = this->ConsumeField(dst_sha1_first);
  162. const char* status_first = this->ConsumeSpace(dst_sha1_last);
  163. const char* status_last = this->ConsumeField(status_first);
  164. if(status_first != status_last)
  165. {
  166. this->CurChange.Action = *status_first;
  167. this->DiffField = DiffFieldSrc;
  168. }
  169. else
  170. {
  171. this->DiffField = DiffFieldNone;
  172. }
  173. }
  174. else if(this->DiffField == DiffFieldSrc)
  175. {
  176. // src-path
  177. if(this->CurChange.Action == 'C')
  178. {
  179. // Convert copy to addition of destination.
  180. this->CurChange.Action = 'A';
  181. this->DiffField = DiffFieldDst;
  182. }
  183. else if(this->CurChange.Action == 'R')
  184. {
  185. // Convert rename to deletion of source and addition of destination.
  186. this->CurChange.Action = 'D';
  187. this->CurChange.Path = this->Line;
  188. this->Changes.push_back(this->CurChange);
  189. this->CurChange = Change('A');
  190. this->DiffField = DiffFieldDst;
  191. }
  192. else
  193. {
  194. this->CurChange.Path = this->Line;
  195. this->Changes.push_back(this->CurChange);
  196. this->DiffField = this->DiffFieldNone;
  197. }
  198. }
  199. else if(this->DiffField == DiffFieldDst)
  200. {
  201. // dst-path
  202. this->CurChange.Path = this->Line;
  203. this->Changes.push_back(this->CurChange);
  204. this->DiffField = this->DiffFieldNone;
  205. }
  206. return true;
  207. }
  208. const char* ConsumeSpace(const char* c)
  209. {
  210. while(*c && isspace(*c)) { ++c; }
  211. return c;
  212. }
  213. const char* ConsumeField(const char* c)
  214. {
  215. while(*c && !isspace(*c)) { ++c; }
  216. return c;
  217. }
  218. };
  219. //----------------------------------------------------------------------------
  220. /* Commit format:
  221. commit ...\n
  222. tree ...\n
  223. parent ...\n
  224. author ...\n
  225. committer ...\n
  226. \n
  227. Log message indented by (4) spaces\n
  228. (even blank lines have the spaces)\n
  229. \n
  230. [Diff format]
  231. The header may have more fields. See 'git help diff-tree'.
  232. */
  233. class cmCTestGIT::CommitParser: public cmCTestGIT::DiffParser
  234. {
  235. public:
  236. CommitParser(cmCTestGIT* git, const char* prefix):
  237. DiffParser(git, prefix), Section(SectionHeader)
  238. {
  239. this->Separator = SectionSep[this->Section];
  240. }
  241. private:
  242. typedef cmCTestGIT::Revision Revision;
  243. enum SectionType { SectionHeader, SectionBody, SectionDiff, SectionCount };
  244. static char const SectionSep[SectionCount];
  245. SectionType Section;
  246. Revision Rev;
  247. struct Person
  248. {
  249. std::string Name;
  250. std::string EMail;
  251. unsigned long Time;
  252. long TimeZone;
  253. Person(): Name(), EMail(), Time(0), TimeZone(0) {}
  254. };
  255. void ParsePerson(const char* str, Person& person)
  256. {
  257. // Person Name <[email protected]> 1234567890 +0000
  258. const char* c = str;
  259. while(*c && isspace(*c)) { ++c; }
  260. const char* name_first = c;
  261. while(*c && *c != '<') { ++c; }
  262. const char* name_last = c;
  263. while(name_last != name_first && isspace(*(name_last-1))) { --name_last; }
  264. person.Name.assign(name_first, name_last-name_first);
  265. const char* email_first = *c? ++c : c;
  266. while(*c && *c != '>') { ++c; }
  267. const char* email_last = *c? c++ : c;
  268. person.EMail.assign(email_first, email_last-email_first);
  269. person.Time = strtoul(c, (char**)&c, 10);
  270. person.TimeZone = strtol(c, (char**)&c, 10);
  271. }
  272. virtual bool ProcessLine()
  273. {
  274. if(this->Line.empty())
  275. {
  276. this->NextSection();
  277. }
  278. else
  279. {
  280. switch(this->Section)
  281. {
  282. case SectionHeader: this->DoHeaderLine(); break;
  283. case SectionBody: this->DoBodyLine(); break;
  284. case SectionDiff: this->DiffParser::ProcessLine(); break;
  285. case SectionCount: break; // never happens
  286. }
  287. }
  288. return true;
  289. }
  290. void NextSection()
  291. {
  292. this->Section = SectionType((this->Section+1) % SectionCount);
  293. this->Separator = SectionSep[this->Section];
  294. if(this->Section == SectionHeader)
  295. {
  296. this->GIT->DoRevision(this->Rev, this->Changes);
  297. this->Rev = Revision();
  298. this->DiffReset();
  299. }
  300. }
  301. void DoHeaderLine()
  302. {
  303. // Look for header fields that we need.
  304. if(strncmp(this->Line.c_str(), "commit ", 7) == 0)
  305. {
  306. this->Rev.Rev = this->Line.c_str()+7;
  307. }
  308. else if(strncmp(this->Line.c_str(), "author ", 7) == 0)
  309. {
  310. Person author;
  311. this->ParsePerson(this->Line.c_str()+7, author);
  312. this->Rev.Author = author.Name;
  313. this->Rev.EMail = author.EMail;
  314. // Convert the time to a human-readable format that is also easy
  315. // to machine-parse: "CCYY-MM-DD hh:mm:ss".
  316. time_t seconds = static_cast<time_t>(author.Time);
  317. struct tm* t = gmtime(&seconds);
  318. char dt[1024];
  319. sprintf(dt, "%04d-%02d-%02d %02d:%02d:%02d",
  320. t->tm_year+1900, t->tm_mon+1, t->tm_mday,
  321. t->tm_hour, t->tm_min, t->tm_sec);
  322. this->Rev.Date = dt;
  323. // Add the time-zone field "+zone" or "-zone".
  324. char tz[32];
  325. if(author.TimeZone >= 0)
  326. {
  327. sprintf(tz, " +%04ld", author.TimeZone);
  328. }
  329. else
  330. {
  331. sprintf(tz, " -%04ld", -author.TimeZone);
  332. }
  333. this->Rev.Date += tz;
  334. }
  335. }
  336. void DoBodyLine()
  337. {
  338. // Commit log lines are indented by 4 spaces.
  339. if(this->Line.size() >= 4)
  340. {
  341. this->Rev.Log += this->Line.substr(4);
  342. }
  343. this->Rev.Log += "\n";
  344. }
  345. };
  346. char const cmCTestGIT::CommitParser::SectionSep[SectionCount] =
  347. {'\n', '\n', '\0'};
  348. //----------------------------------------------------------------------------
  349. void cmCTestGIT::LoadRevisions()
  350. {
  351. // Use 'git rev-list ... | git diff-tree ...' to get revisions.
  352. std::string range = this->OldRevision + ".." + this->NewRevision;
  353. const char* git = this->CommandLineTool.c_str();
  354. const char* git_rev_list[] =
  355. {git, "rev-list", "--reverse", range.c_str(), "--", 0};
  356. const char* git_diff_tree[] =
  357. {git, "diff-tree", "--stdin", "--always", "-z", "-r", "--pretty=raw",
  358. "--encoding=utf-8", 0};
  359. this->Log << this->ComputeCommandLine(git_rev_list) << " | "
  360. << this->ComputeCommandLine(git_diff_tree) << "\n";
  361. cmsysProcess* cp = cmsysProcess_New();
  362. cmsysProcess_AddCommand(cp, git_rev_list);
  363. cmsysProcess_AddCommand(cp, git_diff_tree);
  364. cmsysProcess_SetWorkingDirectory(cp, this->SourceDirectory.c_str());
  365. CommitParser out(this, "dt-out> ");
  366. OutputLogger err(this->Log, "dt-err> ");
  367. this->RunProcess(cp, &out, &err);
  368. // Send one extra zero-byte to terminate the last record.
  369. out.Process("", 1);
  370. cmsysProcess_Delete(cp);
  371. }
  372. //----------------------------------------------------------------------------
  373. void cmCTestGIT::LoadModifications()
  374. {
  375. const char* git = this->CommandLineTool.c_str();
  376. // Use 'git update-index' to refresh the index w.r.t. the work tree.
  377. const char* git_update_index[] = {git, "update-index", "--refresh", 0};
  378. OutputLogger ui_out(this->Log, "ui-out> ");
  379. OutputLogger ui_err(this->Log, "ui-err> ");
  380. this->RunChild(git_update_index, &ui_out, &ui_err);
  381. // Use 'git diff-index' to get modified files.
  382. const char* git_diff_index[] = {git, "diff-index", "-z", "HEAD", 0};
  383. DiffParser out(this, "di-out> ");
  384. OutputLogger err(this->Log, "di-err> ");
  385. this->RunChild(git_diff_index, &out, &err);
  386. for(std::vector<Change>::const_iterator ci = out.Changes.begin();
  387. ci != out.Changes.end(); ++ci)
  388. {
  389. this->DoModification(PathModified, ci->Path);
  390. }
  391. }