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 "cmSystemTools.h"
  6. #include "cmXMLWriter.h"
  7. #include "cmsys/Process.h"
  8. #include <sstream>
  9. #include <stdio.h>
  10. #include <time.h>
  11. #include <vector>
  12. cmCTestVC::cmCTestVC(cmCTest* ct, std::ostream& log)
  13. : CTest(ct)
  14. , Log(log)
  15. {
  16. this->PathCount[PathUpdated] = 0;
  17. this->PathCount[PathModified] = 0;
  18. this->PathCount[PathConflicting] = 0;
  19. this->Unknown.Date = "Unknown";
  20. this->Unknown.Author = "Unknown";
  21. this->Unknown.Rev = "Unknown";
  22. }
  23. cmCTestVC::~cmCTestVC() = default;
  24. void cmCTestVC::SetCommandLineTool(std::string const& tool)
  25. {
  26. this->CommandLineTool = tool;
  27. }
  28. void cmCTestVC::SetSourceDirectory(std::string const& dir)
  29. {
  30. this->SourceDirectory = dir;
  31. }
  32. bool cmCTestVC::InitialCheckout(const char* command)
  33. {
  34. cmCTestLog(this->CTest, HANDLER_OUTPUT,
  35. " First perform the initial checkout: " << command << "\n");
  36. // Make the parent directory in which to perform the checkout.
  37. std::string parent = cmSystemTools::GetFilenamePath(this->SourceDirectory);
  38. cmCTestLog(this->CTest, HANDLER_OUTPUT,
  39. " Perform checkout in directory: " << parent << "\n");
  40. if (!cmSystemTools::MakeDirectory(parent)) {
  41. cmCTestLog(this->CTest, ERROR_MESSAGE,
  42. "Cannot create directory: " << parent << std::endl);
  43. return false;
  44. }
  45. // Construct the initial checkout command line.
  46. std::vector<std::string> args = cmSystemTools::ParseArguments(command);
  47. std::vector<char const*> vc_co;
  48. vc_co.reserve(args.size() + 1);
  49. for (std::string const& arg : args) {
  50. vc_co.push_back(arg.c_str());
  51. }
  52. vc_co.push_back(nullptr);
  53. // Run the initial checkout command and log its output.
  54. this->Log << "--- Begin Initial Checkout ---\n";
  55. OutputLogger out(this->Log, "co-out> ");
  56. OutputLogger err(this->Log, "co-err> ");
  57. bool result = this->RunChild(&vc_co[0], &out, &err, parent.c_str());
  58. this->Log << "--- End Initial Checkout ---\n";
  59. if (!result) {
  60. cmCTestLog(this->CTest, ERROR_MESSAGE,
  61. "Initial checkout failed!" << std::endl);
  62. }
  63. return result;
  64. }
  65. bool cmCTestVC::RunChild(char const* const* cmd, OutputParser* out,
  66. OutputParser* err, const char* workDir,
  67. Encoding encoding)
  68. {
  69. this->Log << cmCTestVC::ComputeCommandLine(cmd) << "\n";
  70. cmsysProcess* cp = cmsysProcess_New();
  71. cmsysProcess_SetCommand(cp, cmd);
  72. workDir = workDir ? workDir : this->SourceDirectory.c_str();
  73. cmsysProcess_SetWorkingDirectory(cp, workDir);
  74. cmCTestVC::RunProcess(cp, out, err, encoding);
  75. int result = cmsysProcess_GetExitValue(cp);
  76. cmsysProcess_Delete(cp);
  77. return result == 0;
  78. }
  79. std::string cmCTestVC::ComputeCommandLine(char const* const* cmd)
  80. {
  81. std::ostringstream line;
  82. const char* sep = "";
  83. for (const char* const* arg = cmd; *arg; ++arg) {
  84. line << sep << "\"" << *arg << "\"";
  85. sep = " ";
  86. }
  87. return line.str();
  88. }
  89. bool cmCTestVC::RunUpdateCommand(char const* const* cmd, OutputParser* out,
  90. OutputParser* err, Encoding encoding)
  91. {
  92. // Report the command line.
  93. this->UpdateCommandLine = this->ComputeCommandLine(cmd);
  94. if (this->CTest->GetShowOnly()) {
  95. this->Log << this->UpdateCommandLine << "\n";
  96. return true;
  97. }
  98. // Run the command.
  99. return this->RunChild(cmd, out, err, nullptr, encoding);
  100. }
  101. std::string cmCTestVC::GetNightlyTime()
  102. {
  103. // Get the nightly start time corresponding to the current dau.
  104. struct tm* t = this->CTest->GetNightlyTime(
  105. this->CTest->GetCTestConfiguration("NightlyStartTime"),
  106. this->CTest->GetTomorrowTag());
  107. char current_time[1024];
  108. sprintf(current_time, "%04d-%02d-%02d %02d:%02d:%02d", t->tm_year + 1900,
  109. t->tm_mon + 1, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec);
  110. return std::string(current_time);
  111. }
  112. void cmCTestVC::Cleanup()
  113. {
  114. this->Log << "--- Begin Cleanup ---\n";
  115. this->CleanupImpl();
  116. this->Log << "--- End Cleanup ---\n";
  117. }
  118. void cmCTestVC::CleanupImpl()
  119. {
  120. // We do no cleanup by default.
  121. }
  122. bool cmCTestVC::Update()
  123. {
  124. bool result = true;
  125. // Use the explicitly specified version.
  126. std::string versionOverride =
  127. this->CTest->GetCTestConfiguration("UpdateVersionOverride");
  128. if (!versionOverride.empty()) {
  129. this->SetNewRevision(versionOverride);
  130. return true;
  131. }
  132. // if update version only is on then do not actually update,
  133. // just note the current version and finish
  134. if (!cmSystemTools::IsOn(
  135. 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. }