summaryrefslogtreecommitdiffstats
path: root/Source/CTest/cmCTestUpdateHandler.cxx
blob: 85624e04688b796af96b473101b23b607c8aefaa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/*============================================================================
  CMake - Cross Platform Makefile Generator
  Copyright 2000-2009 Kitware, Inc., Insight Software Consortium

  Distributed under the OSI-approved BSD License (the "License");
  see accompanying file Copyright.txt for details.

  This software is distributed WITHOUT ANY WARRANTY; without even the
  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  See the License for more information.
============================================================================*/

#include "cmCTestUpdateHandler.h"

#include "cmCLocaleEnvironmentScope.h"
#include "cmCTest.h"
#include "cmGeneratedFileStream.h"
#include "cmGlobalGenerator.h"
#include "cmMakefile.h"
#include "cmVersion.h"
#include "cmXMLParser.h"
#include "cmXMLWriter.h"
#include "cmake.h"

#include "cmCTestBZR.h"
#include "cmCTestCVS.h"
#include "cmCTestGIT.h"
#include "cmCTestHG.h"
#include "cmCTestP4.h"
#include "cmCTestSVN.h"
#include "cmCTestVC.h"

#include <cmsys/auto_ptr.hxx>

//#include <cmsys/RegularExpression.hxx>
#include <cmsys/Process.h>

// used for sleep
#ifdef _WIN32
#include "windows.h"
#endif

#include <float.h>
#include <math.h>
#include <stdlib.h>

static const char* cmCTestUpdateHandlerUpdateStrings[] =
{
  "Unknown",
  "CVS",
  "SVN",
  "BZR",
  "GIT",
  "HG",
  "P4"
};

static const char* cmCTestUpdateHandlerUpdateToString(int type)
{
  if ( type < cmCTestUpdateHandler::e_UNKNOWN ||
    type >= cmCTestUpdateHandler::e_LAST )
    {
    return cmCTestUpdateHandlerUpdateStrings[cmCTestUpdateHandler::e_UNKNOWN];
    }
  return cmCTestUpdateHandlerUpdateStrings[type];
}

cmCTestUpdateHandler::cmCTestUpdateHandler()
{
}

void cmCTestUpdateHandler::Initialize()
{
  this->Superclass::Initialize();
  this->UpdateCommand = "";
  this->UpdateType = e_CVS;
}

int cmCTestUpdateHandler::DetermineType(const char* cmd, const char* type)
{
  cmCTestOptionalLog(this->CTest, DEBUG,
    "Determine update type from command: " << cmd << " and type: " << type <<
    std::endl, this->Quiet);
  if ( type && *type )
    {
    cmCTestOptionalLog(this->CTest, DEBUG, "Type specified: " << type <<
      std::endl, this->Quiet);
    std::string stype = cmSystemTools::LowerCase(type);
    if ( stype.find("cvs") != std::string::npos )
      {
      return cmCTestUpdateHandler::e_CVS;
      }
    if ( stype.find("svn") != std::string::npos )
      {
      return cmCTestUpdateHandler::e_SVN;
      }
    if ( stype.find("bzr") != std::string::npos )
      {
      return cmCTestUpdateHandler::e_BZR;
      }
    if ( stype.find("git") != std::string::npos )
      {
      return cmCTestUpdateHandler::e_GIT;
      }
    if ( stype.find("hg") != std::string::npos )
      {
      return cmCTestUpdateHandler::e_HG;
      }
    if ( stype.find("p4") != std::string::npos )
      {
      return cmCTestUpdateHandler::e_P4;
      }
    }
  else
    {
    cmCTestOptionalLog(this->CTest, DEBUG,
      "Type not specified, check command: " << cmd << std::endl, this->Quiet);
    std::string stype = cmSystemTools::LowerCase(cmd);
    if ( stype.find("cvs") != std::string::npos )
      {
      return cmCTestUpdateHandler::e_CVS;
      }
    if ( stype.find("svn") != std::string::npos )
      {
      return cmCTestUpdateHandler::e_SVN;
      }
    if ( stype.find("bzr") != std::string::npos )
      {
      return cmCTestUpdateHandler::e_BZR;
      }
    if ( stype.find("git") != std::string::npos )
      {
      return cmCTestUpdateHandler::e_GIT;
      }
    if ( stype.find("hg") != std::string::npos )
      {
      return cmCTestUpdateHandler::e_HG;
      }
    if ( stype.find("p4") != std::string::npos )
      {
      return cmCTestUpdateHandler::e_P4;
      }
    }
  return cmCTestUpdateHandler::e_UNKNOWN;
}

//clearly it would be nice if this were broken up into a few smaller
//functions and commented...
int cmCTestUpdateHandler::ProcessHandler()
{
  // Make sure VCS tool messages are in English so we can parse them.
  cmCLocaleEnvironmentScope fixLocale;
  static_cast<void>(fixLocale);

  // Get source dir
  const char* sourceDirectory = this->GetOption("SourceDirectory");
  if ( !sourceDirectory )
    {
    cmCTestLog(this->CTest, ERROR_MESSAGE,
      "Cannot find SourceDirectory  key in the DartConfiguration.tcl"
      << std::endl);
    return -1;
    }

  cmGeneratedFileStream ofs;
  if ( !this->CTest->GetShowOnly() )
    {
    this->StartLogFile("Update", ofs);
    }

  cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT,
    "   Updating the repository: " << sourceDirectory << std::endl,
    this->Quiet);

  if(!this->SelectVCS())
    {
    return -1;
    }

  cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT, "   Use "
    << cmCTestUpdateHandlerUpdateToString(this->UpdateType)
    << " repository type"
    << std::endl;, this->Quiet);

  // Create an object to interact with the VCS tool.
  cmsys::auto_ptr<cmCTestVC> vc;
  switch (this->UpdateType)
    {
    case e_CVS: vc.reset(new cmCTestCVS(this->CTest, ofs)); break;
    case e_SVN: vc.reset(new cmCTestSVN(this->CTest, ofs)); break;
    case e_BZR: vc.reset(new cmCTestBZR(this->CTest, ofs)); break;
    case e_GIT: vc.reset(new cmCTestGIT(this->CTest, ofs)); break;
    case e_HG:  vc.reset(new cmCTestHG(this->CTest, ofs)); break;
    case e_P4:  vc.reset(new cmCTestP4(this->CTest, ofs)); break;
    default:    vc.reset(new cmCTestVC(this->CTest, ofs));  break;
    }
  vc->SetCommandLineTool(this->UpdateCommand);
  vc->SetSourceDirectory(sourceDirectory);

  // Cleanup the working tree.
  vc->Cleanup();

  //
  // Now update repository and remember what files were updated
  //
  cmGeneratedFileStream os;
  if(!this->StartResultingXML(cmCTest::PartUpdate, "Update", os))
    {
    cmCTestLog(this->CTest, ERROR_MESSAGE, "Cannot open log file"
      << std::endl);
    return -1;
    }
  std::string start_time = this->CTest->CurrentTime();
  unsigned int start_time_time =
    static_cast<unsigned int>(cmSystemTools::GetTime());
  double elapsed_time_start = cmSystemTools::GetTime();

  bool updated = vc->Update();
  std::string buildname = cmCTest::SafeBuildIdField(
    this->CTest->GetCTestConfiguration("BuildName"));

  cmXMLWriter xml(os);
  xml.StartDocument();
  xml.StartElement("Update");
  xml.Attribute("mode", "Client");
  xml.Attribute("Generator",
    std::string("ctest-") + cmVersion::GetCMakeVersion());
  xml.Element("Site", this->CTest->GetCTestConfiguration("Site"));
  xml.Element("BuildName", buildname);
  xml.Element("BuildStamp", this->CTest->GetCurrentTag() + "-" +
    this->CTest->GetTestModelString());
  xml.Element("StartDateTime", start_time);
  xml.Element("StartTime", start_time_time);
  xml.Element("UpdateCommand", vc->GetUpdateCommandLine());
  xml.Element("UpdateType",
    cmCTestUpdateHandlerUpdateToString(this->UpdateType));

  vc->WriteXML(xml);

  int localModifications = 0;
  int numUpdated = vc->GetPathCount(cmCTestVC::PathUpdated);
  if(numUpdated)
    {
    cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT,
      "   Found " << numUpdated << " updated files\n", this->Quiet);
    }
  if(int numModified = vc->GetPathCount(cmCTestVC::PathModified))
    {
    cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT,
      "   Found " << numModified << " locally modified files\n", this->Quiet);
    localModifications += numModified;
    }
  if(int numConflicting = vc->GetPathCount(cmCTestVC::PathConflicting))
    {
    cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT,
    "   Found " << numConflicting << " conflicting files\n", this->Quiet);
    localModifications += numConflicting;
    }

  cmCTestOptionalLog(this->CTest, DEBUG, "End" << std::endl, this->Quiet);
  std::string end_time = this->CTest->CurrentTime();
  xml.Element("EndDateTime", end_time);
  xml.Element("EndTime", static_cast<unsigned int>(cmSystemTools::GetTime()));
  xml.Element("ElapsedMinutes",
    static_cast<int>((cmSystemTools::GetTime() - elapsed_time_start)/6)/10.0);

  xml.StartElement("UpdateReturnStatus");
  if(localModifications)
    {
    xml.Content("Update error: "
      "There are modified or conflicting files in the repository");
    cmCTestLog(this->CTest, ERROR_MESSAGE,
      "   There are modified or conflicting files in the repository"
      << std::endl);
    }
  if(!updated)
    {
    xml.Content("Update command failed:\n");
    xml.Content(vc->GetUpdateCommandLine());
    cmCTestLog(this->CTest, HANDLER_OUTPUT, "   Update command failed: "
               << vc->GetUpdateCommandLine() << "\n");
    }
  xml.EndElement(); // UpdateReturnStatus
  xml.EndElement(); // Update
  xml.EndDocument();
  return updated? numUpdated : -1;
}

int cmCTestUpdateHandler::DetectVCS(const char* dir)
{
  std::string sourceDirectory = dir;
  cmCTestOptionalLog(this->CTest, DEBUG, "Check directory: "
    << sourceDirectory << std::endl, this->Quiet);
  sourceDirectory += "/.svn";
  if ( cmSystemTools::FileExists(sourceDirectory.c_str()) )
    {
    return cmCTestUpdateHandler::e_SVN;
    }
  sourceDirectory = dir;
  sourceDirectory += "/CVS";
  if ( cmSystemTools::FileExists(sourceDirectory.c_str()) )
    {
    return cmCTestUpdateHandler::e_CVS;
    }
  sourceDirectory = dir;
  sourceDirectory += "/.bzr";
  if ( cmSystemTools::FileExists(sourceDirectory.c_str()) )
    {
    return cmCTestUpdateHandler::e_BZR;
    }
  sourceDirectory = dir;
  sourceDirectory += "/.git";
  if ( cmSystemTools::FileExists(sourceDirectory.c_str()) )
    {
    return cmCTestUpdateHandler::e_GIT;
    }
  sourceDirectory = dir;
  sourceDirectory += "/.hg";
  if ( cmSystemTools::FileExists(sourceDirectory.c_str()) )
    {
    return cmCTestUpdateHandler::e_HG;
    }
  sourceDirectory = dir;
  sourceDirectory += "/.p4";
  if ( cmSystemTools::FileExists(sourceDirectory.c_str()) )
    {
    return cmCTestUpdateHandler::e_P4;
    }
  sourceDirectory = dir;
  sourceDirectory += "/.p4config";
  if ( cmSystemTools::FileExists(sourceDirectory.c_str()) )
    {
    return cmCTestUpdateHandler::e_P4;
    }
  return cmCTestUpdateHandler::e_UNKNOWN;
}

bool cmCTestUpdateHandler::SelectVCS()
{
  // Get update command
  this->UpdateCommand = this->CTest->GetCTestConfiguration("UpdateCommand");

  // Detect the VCS managing the source tree.
  this->UpdateType = this->DetectVCS(this->GetOption("SourceDirectory"));
  if (this->UpdateType == e_UNKNOWN)
    {
    // The source tree does not have a recognized VCS.  Check the
    // configuration value or command name.
    this->UpdateType = this->DetermineType(this->UpdateCommand.c_str(),
      this->CTest->GetCTestConfiguration("UpdateType").c_str());
    }

  // If no update command was specified, lookup one for this VCS tool.
  if (this->UpdateCommand.empty())
    {
    const char* key = 0;
    switch (this->UpdateType)
      {
      case e_CVS: key = "CVSCommand"; break;
      case e_SVN: key = "SVNCommand"; break;
      case e_BZR: key = "BZRCommand"; break;
      case e_GIT: key = "GITCommand"; break;
      case e_HG:  key = "HGCommand";  break;
      case e_P4:  key = "P4Command";  break;
      default: break;
      }
    if (key)
      {
      this->UpdateCommand = this->CTest->GetCTestConfiguration(key);
      }
    if (this->UpdateCommand.empty())
      {
      std::ostringstream e;
      e << "Cannot find UpdateCommand ";
      if (key)
        {
        e << "or " << key;
        }
      e << " configuration key.";
      cmCTestLog(this->CTest, ERROR_MESSAGE, e.str() << std::endl);
      return false;
      }
    }

  return true;
}