cmCTestVC.cxx 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 "cmCTestVC.h"
  4. #include <cstdio>
  5. #include <ctime>
  6. #include <sstream>
  7. #include <vector>
  8. #include "cmCTest.h"
  9. #include "cmMakefile.h"
  10. #include "cmSystemTools.h"
  11. #include "cmUVProcessChain.h"
  12. #include "cmXMLWriter.h"
  13. cmCTestVC::cmCTestVC(cmCTest* ct, cmMakefile* mf, std::ostream& log)
  14. : CTest(ct)
  15. , Makefile(mf)
  16. , Log(log)
  17. {
  18. this->PathCount[PathUpdated] = 0;
  19. this->PathCount[PathModified] = 0;
  20. this->PathCount[PathConflicting] = 0;
  21. this->Unknown.Date = "Unknown";
  22. this->Unknown.Author = "Unknown";
  23. this->Unknown.Rev = "Unknown";
  24. }
  25. cmCTestVC::~cmCTestVC() = default;
  26. void cmCTestVC::SetCommandLineTool(std::string const& tool)
  27. {
  28. this->CommandLineTool = tool;
  29. }
  30. void cmCTestVC::SetSourceDirectory(std::string const& dir)
  31. {
  32. this->SourceDirectory = dir;
  33. }
  34. bool cmCTestVC::InitialCheckout(const std::string& command)
  35. {
  36. cmCTestLog(this->CTest, HANDLER_OUTPUT,
  37. " First perform the initial checkout: " << command << "\n");
  38. // Make the parent directory in which to perform the checkout.
  39. std::string parent = cmSystemTools::GetFilenamePath(this->SourceDirectory);
  40. cmCTestLog(this->CTest, HANDLER_OUTPUT,
  41. " Perform checkout in directory: " << parent << "\n");
  42. if (!cmSystemTools::MakeDirectory(parent)) {
  43. cmCTestLog(this->CTest, ERROR_MESSAGE,
  44. "Cannot create directory: " << parent << std::endl);
  45. return false;
  46. }
  47. // Construct the initial checkout command line.
  48. std::vector<std::string> args = cmSystemTools::ParseArguments(command);
  49. // Run the initial checkout command and log its output.
  50. this->Log << "--- Begin Initial Checkout ---\n";
  51. OutputLogger out(this->Log, "co-out> ");
  52. OutputLogger err(this->Log, "co-err> ");
  53. bool result = this->RunChild(args, &out, &err, parent);
  54. this->Log << "--- End Initial Checkout ---\n";
  55. if (!result) {
  56. cmCTestLog(this->CTest, ERROR_MESSAGE,
  57. "Initial checkout failed!" << std::endl);
  58. }
  59. return result;
  60. }
  61. bool cmCTestVC::RunChild(const std::vector<std::string>& cmd,
  62. OutputParser* out, OutputParser* err,
  63. std::string workDir, Encoding encoding)
  64. {
  65. this->Log << cmCTestVC::ComputeCommandLine(cmd) << "\n";
  66. cmUVProcessChainBuilder builder;
  67. if (workDir.empty()) {
  68. workDir = this->SourceDirectory;
  69. }
  70. builder.AddCommand(cmd).SetWorkingDirectory(workDir);
  71. auto status = cmCTestVC::RunProcess(builder, out, err, encoding);
  72. return status.front().SpawnResult == 0 && status.front().ExitStatus == 0;
  73. }
  74. std::string cmCTestVC::ComputeCommandLine(const std::vector<std::string>& cmd)
  75. {
  76. std::ostringstream line;
  77. const char* sep = "";
  78. for (auto const& arg : cmd) {
  79. line << sep << "\"" << arg << "\"";
  80. sep = " ";
  81. }
  82. return line.str();
  83. }
  84. bool cmCTestVC::RunUpdateCommand(const std::vector<std::string>& cmd,
  85. OutputParser* out, OutputParser* err,
  86. Encoding encoding)
  87. {
  88. // Report the command line.
  89. this->UpdateCommandLine = this->ComputeCommandLine(cmd);
  90. if (this->CTest->GetShowOnly()) {
  91. this->Log << this->UpdateCommandLine << "\n";
  92. return true;
  93. }
  94. // Run the command.
  95. return this->RunChild(cmd, out, err, "", encoding);
  96. }
  97. std::string cmCTestVC::GetNightlyTime()
  98. {
  99. // Get the nightly start time corresponding to the current dau.
  100. struct tm* t = this->CTest->GetNightlyTime(
  101. this->Makefile->GetSafeDefinition("CTEST_NIGHTLY_START_TIME"),
  102. this->CTest->GetTomorrowTag());
  103. char current_time[1024];
  104. snprintf(current_time, sizeof(current_time), "%04d-%02d-%02d %02d:%02d:%02d",
  105. t->tm_year + 1900, t->tm_mon + 1, t->tm_mday, t->tm_hour, t->tm_min,
  106. t->tm_sec);
  107. return { current_time };
  108. }
  109. void cmCTestVC::Cleanup()
  110. {
  111. this->Log << "--- Begin Cleanup ---\n";
  112. this->CleanupImpl();
  113. this->Log << "--- End Cleanup ---\n";
  114. }
  115. void cmCTestVC::CleanupImpl()
  116. {
  117. // We do no cleanup by default.
  118. }
  119. bool cmCTestVC::Update()
  120. {
  121. bool result = true;
  122. // Use the explicitly specified version.
  123. std::string versionOverride =
  124. this->Makefile->GetSafeDefinition("CTEST_UPDATE_VERSION_OVERRIDE");
  125. if (!versionOverride.empty()) {
  126. this->SetNewRevision(versionOverride);
  127. return true;
  128. }
  129. // if update version only is on then do not actually update,
  130. // just note the current version and finish
  131. if (!this->Makefile->IsOn("CTEST_UPDATE_VERSION_ONLY")) {
  132. result = this->NoteOldRevision() && result;
  133. this->Log << "--- Begin Update ---\n";
  134. result = this->UpdateImpl() && result;
  135. this->Log << "--- End Update ---\n";
  136. }
  137. result = this->NoteNewRevision() && result;
  138. return result;
  139. }
  140. bool cmCTestVC::NoteOldRevision()
  141. {
  142. // We do nothing by default.
  143. return true;
  144. }
  145. bool cmCTestVC::NoteNewRevision()
  146. {
  147. // We do nothing by default.
  148. return true;
  149. }
  150. void cmCTestVC::SetNewRevision(std::string const& /*unused*/)
  151. {
  152. // We do nothing by default.
  153. }
  154. bool cmCTestVC::UpdateImpl()
  155. {
  156. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  157. "* Unknown VCS tool, not updating!" << std::endl);
  158. return true;
  159. }
  160. bool cmCTestVC::WriteXML(cmXMLWriter& xml)
  161. {
  162. this->Log << "--- Begin Revisions ---\n";
  163. bool result = this->WriteXMLUpdates(xml);
  164. this->Log << "--- End Revisions ---\n";
  165. return result;
  166. }
  167. bool cmCTestVC::WriteXMLUpdates(cmXMLWriter& /*unused*/)
  168. {
  169. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  170. "* CTest cannot extract updates for this VCS tool.\n");
  171. return true;
  172. }
  173. void cmCTestVC::WriteXMLEntry(cmXMLWriter& xml, std::string const& path,
  174. std::string const& name, std::string const& full,
  175. File const& f)
  176. {
  177. static const char* desc[3] = { "Updated", "Modified", "Conflicting" };
  178. Revision const& rev = f.Rev ? *f.Rev : this->Unknown;
  179. std::string prior = f.PriorRev ? f.PriorRev->Rev : std::string("Unknown");
  180. xml.StartElement(desc[f.Status]);
  181. xml.Element("File", name);
  182. xml.Element("Directory", path);
  183. xml.Element("FullName", full);
  184. xml.Element("CheckinDate", rev.Date);
  185. xml.Element("Author", rev.Author);
  186. xml.Element("Email", rev.EMail);
  187. xml.Element("Committer", rev.Committer);
  188. xml.Element("CommitterEmail", rev.CommitterEMail);
  189. xml.Element("CommitDate", rev.CommitDate);
  190. xml.Element("Log", rev.Log);
  191. xml.Element("Revision", rev.Rev);
  192. xml.Element("PriorRevision", prior);
  193. xml.EndElement();
  194. ++this->PathCount[f.Status];
  195. }