cmCTestVC.cxx 6.5 KB

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