summaryrefslogtreecommitdiff
path: root/utils/svn2cvs/svn2cvs.pp
blob: 6c7586e12e5416d8852b721f1d320cb3afbe543e (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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
{$mode objfpc}
{$H+}
program svn2cvs;

uses Classes,sysutils,process,DOM,xmlread,custapp,IniFiles;

Const
  SGlobal       = 'Global';
  KeyCVSBin     = 'CVSBinary';
  KeySVNBin     = 'SVNBinary';
  KeySVNURL     = 'SVNURL';
  KeyCVSROOT    = 'CVSROOT';
  KeyRepository = 'CVSRepository';
  KeyRevision   = 'Revision';
  KeyWorkDir    = 'WorkingDir';

Resourcestring
  SErrFailedToCheckOut     = 'Failed to check out SVN repository';
  SErrFailedToInitCVS      = 'Failed to initialize CVS: ';
  SErrNoRepository         = 'Cannot initialize CVS: no CVS Repository specified';
  SErrDirectoryFailed      = 'Failed to create directory : %s';
  SErrFailedToGetVersions  = 'Failed to retrieve SVN versions';
  SErrInValidSVNLog        = 'Invalid SVN log.';
  SErrUpdateFailed         = 'Update to revision %d failed.';
  SErrFailedToCommit       = 'Failed to commit to CVS.';
  SErrFailedToRemove       = 'Failed to remove file: %s';
  SErrFailedToAddDirectory = 'Failed to add directory to CVS: %s';
  SErrFailedToAddFile      = 'Failed to add file to CVS: %s';
  SErrDirectoryNotInCVS    = 'Directory not in CVS: %s';
  SLogRevision             = 'Revision %s by %s :';
  SConvertingRevision      = 'Converting revision : %d';
  SWarnUnknownAction       = 'Warning: Unknown action: "%s" for filename : "%s"';
  SWarnErrorInLine         = 'Warning: Erroneous file line : %s';
  SExecuting               = 'Executing: %s';
  
Type

 { TSVN2CVSApp }
 TVersion = Class(TCollectionItem)
 private
   FAuthor: String;
   FDate: string;
   FLogMessage: String;
   FRevision: Integer;
 Public
   Property Revision : Integer read FRevision;
   Property LogMessage : String Read FLogMessage;
   Property Date : string Read FDate;
   Property Author : String Read FAuthor;
 end;
 
 { TVersions }

 TVersions = Class(TCollection)
 private
   function GetVersion(Index : INteger): TVersion;
   procedure SetVersion(Index : INteger; const AValue: TVersion);
 Protected
   procedure ConvertLogEntry(E : TDomElement);
 public
   Procedure LoadFromXML(Doc : TXMlDocument);
   property Versions[Index : INteger] : TVersion Read GetVersion Write SetVersion; Default;
 end;

 { TSVN2CVSApp }

 TSVN2CVSApp = Class(TCustomApplication)
 Public
   SVNBin : String;
   CVSBin : String;
   versions : TVersions;
   WorkingDir : String;
   StartRevision : Integer;
   SVNURL : String;
   CVSROOT : String;
   CVSRepository : String;
   Function RunCmd(Cmd: String; CmdOutput: TStream): Boolean;
   Function RunSVN(Cmd : String; CmdOutput : TStream) : Boolean;
   Function RunCVS(Cmd : String; CmdOutput : TStream) : Boolean;
   Function UpdateSVN(Version : TVersion; Files : TStrings) : Boolean;
   Procedure WriteLogMessage(Version : TVersion);
   Procedure UpdateEntry(AFileName : String);
   Procedure DeleteEntry(AFileName : String);
   Procedure DoCVSEntries(Version : TVersion;Files : TStrings);
   procedure CheckInCVS;
   procedure CheckOutSVN(Files : TStrings);
   Procedure ConvertVersion(Version : TVersion);
   Procedure ConvertRepository;
   procedure GetVersions;
   procedure ProcessConfigFile;
   Function  ProcessArguments : Boolean;
   Procedure DoRun; override;
 end;

 AppError = Class(Exception);

{ TVersions }

function TVersions.GetVersion(Index : INteger): TVersion;
begin
  Result:=Items[Index] as Tversion;
end;

procedure TVersions.SetVersion(Index : INteger; const AValue: TVersion);
begin
  Items[Index]:=AValue;
end;

procedure TVersions.ConvertLogEntry(E : TDomElement);

  Function GetNodeText(N : TDomNode) : String;
  
  begin
    N:=N.FirstChild;
    If N<>Nil then
      Result:=N.NodeValue;
  end;

Var
  N : TDomNode;
  V : TVersion;

begin
  V:=Add as TVersion;
  V.FRevision:=StrToIntDef(E['revision'],-1);
  N:=E.FirstChild;
  While (N<>Nil) do
    begin
    If (N.NodeType=ELEMENT_NODE) then
      begin
      if (N.NodeName='author') then
        V.FAuthor:=GetNodeText(N)
      else If (N.NodeName='date') then
        V.FDate:=GetNodeText(N)
      else If (N.NodeName='msg') then
        V.FLogMessage:=GetNodeText(N);
      end;
    N:=N.NextSibling;
    end;
end;

procedure TVersions.LoadFromXML(Doc: TXMlDocument);

var
  L : TDomNode;
  E : TDomElement;

begin
  L:=Doc.FirstChild;
  While (L<>Nil) and not ((L.NodeType=ELEMENT_NODE) and (L.NodeName='log')) do
    L:=L.NextSibling;
  if (L=Nil) then
    Raise AppError.Create(SErrInValidSVNLog);
  L:=L.FirstChild;
  While (L<>Nil) do
    begin
    If (L.NodeType=ELEMENT_NODE) and (L.NodeName='logentry') then
      E:=TDomElement(L);
    ConvertLogEntry(E);
    L:=L.NextSibling;
    end;
end;
 

{ TSVN2CVSApp }

function TSVN2CVSApp.RunCmd(Cmd: String; CmdOutput: TStream): Boolean;

Var
  Buf : Array[1..4096] of Byte;
  Count : Integer;

begin
  With TProcess.Create(Self) do
    Try
      CommandLine:=cmd;
      Writeln(Format(SExecuting,[CommandLine]));
      if (CmdOutput<>Nil) then
        Options:=[poUsePipes];
      Execute;
      If (CmdOutPut=Nil) then
        WaitOnExit
      else
        Repeat
          Count:=Output.Read(Buf,SizeOf(Buf));
          If (Count>0) then
            cmdOutput.Write(Buf,Count);
        Until (Count=0);
      Result:=(ExitStatus=0);
    finally
      Free;
    end;
end;

function TSVN2CVSApp.RunSVN(Cmd: String; CmdOutput: TStream): Boolean;


begin
  Result:=RunCmd(SVNbin+' '+Cmd,CmdOutput);
end;

function TSVN2CVSApp.RunCVS(Cmd: String; CmdOutput: TStream): Boolean;
begin
  Result:=RunCmd(CVSbin+' '+Cmd,CmdOutput);
end;

procedure TSVN2CVSApp.CheckOutSVN(Files : TStrings);

Var
  S : TStringStream;

begin
  S:=TStringStream.Create('');
  Try
    if not RunSVN(Format('co -r %d %s .',[StartRevision,SVNURL]),S) then
      Raise AppError.Create(SErrFailedToCheckOut);
    Files.Text:=S.DataString;
  Finally
    FreeAndNil(S);
  end;
end;

procedure TSVN2CVSApp.CheckInCVS;

Var
  F : Text;

begin
  If not ForceDirectories(WorkingDir+'CVS') then
  Try
    AssignFile(F,WorkingDir+'CVS/Root');
    Rewrite(F);
    Try
      Writeln(F,CVSRoot);
    Finally
      CloseFile(F);
    end;
    AssignFile(F,WorkingDir+'CVS/Repository');
    Rewrite(F);
    Try
      Writeln(F,CVSRepository);
    Finally
      Close(F);
    end;
    AssignFile(F,WorkingDir+'CVS/Entries');
    Rewrite(F);
    Try
      // Do nothing.
    Finally
      Close(F);
    end;
  except
    On E : Exception do
      begin
      E.Message:=SErrFailedToInitCVS+E.Message;
      Raise;
      end;
  end;
end;

procedure TSVN2CVSApp.Convertrepository;

Var
  InitCVS,INITSVN : Boolean;
  I : Integer;
  Files : TStringList;

begin
  If Not DirectoryExists(WorkingDir) then
    begin
    if Not ForceDirectories(WorkingDir) then
      Raise AppError.CreateFmt(SErrDirectoryFailed,[WorkingDir]);
    InitSVN:=True;
    InitCVS:=true;
    end
  else
    begin
    if Not DirectoryExists(WorkingDir+'.svn') then
      InitSVN:=True;
    if Not DirectoryExists(WorkingDir+'CVS') then
      InitCVS:=True;
    end;
  ChDir(WorkingDir);
  if InitCVS and (CVSRepository='') then
    Raise AppError.Create(SErrNoRepository);
  if InitSVN then
    begin
    Files:=TStringList.Create;
    Try
      CheckoutSVN(Files);
      if InitCVS then
        begin
        CheckinCVS;
        DoCVSEntries(Nil,Files);
        end
      else
        DoCVSEntries(Nil,Files);
    finally
      FreeAndNil(Files);
    end;
    end;
  GetVersions;
  For I:=0 to Versions.Count-1 do
    ConvertVersion(Versions[i]);
end;

procedure TSVN2CVSApp.GetVersions;

Var
  S : TStringStream;
  Doc : TXMLDocument;

begin
  Versions:=TVersions.Create(TVersion);
  S:=TStringStream.Create('');
  Try
    if not RunSVN(Format('log --xml -r %d:HEAD',[StartRevision]),S) then
      Raise AppError(SErrFailedToGetVersions);
    S.Position:=0;
    ReadXMLFile(Doc,S);
    Try
      Versions.LoadFromXML(Doc);
    finally
      Doc.Free;
    end;
  Finally
    S.Free;
  end;
end;


procedure TSVN2CVSApp.ConvertVersion(Version: TVersion);

Var
  Files : TStringList;

begin
  Writeln(Format(SConvertingRevision,[Version.revision]));
  Files:=TStringList.Create;
  Try
    If Not UpdateSVN(Version,Files) then
      Raise AppError.CreateFmt(SErrUpdateFailed,[Version.Revision]);
    DoCVSEntries(Version,Files);
  Finally
    Files.Free;
  end;
end;

Function TSVN2CVSApp.UpdateSVN(Version : TVersion; Files : TStrings) : Boolean;

Var
  S : TStringStream;

begin
  S:=TStringStream.Create('');
  Try
    Result:=RunSVN(Format('up -r %d',[version.revision]),S);
    if Result then
      Files.Text:=S.DataString;
  Finally
    S.Free;
  end;
end;

Procedure TSVN2CVSApp.WriteLogMessage(Version : TVersion);

Var
  F : Text;
  
begin
  AssignFile(F,'logmsg.txt');
  Rewrite(F);
  Try
    Writeln(F,Format(SLogRevision,[Version.Revision,Version.Author]));
    Writeln(F, Version.LogMessage);
  Finally
    CloseFile(F);
  end;
end;

Procedure TSVN2CVSApp.DoCVSEntries(Version : TVersion;Files : TStrings);

Var
  I,P        : Integer;
  Action   : Char;
  FileName : String;

begin
  For I:=0 to Files.Count-1 do
   begin
   FileName:=trim(Files[i]);
   P:=Pos(' ',FileName);
   if (P=0) then
     Writeln(StdErr,Format(SWarnErrorInLine,[FileName]))
   else
     begin
     Action:=FileName[1];
     system.Delete(FileName,1,P);
     FileName:=Trim(FileName);
     end;
   Case UpCase(action) of
     'U' : UpdateEntry(FileName);
     'D' : DeleteEntry(FileName);
   else
      Writeln(stdErr,Format(SWarnUnknownAction,[Action,FileName]));
   end;
   end;
 WriteLogMessage(version);
 Try
   If not RunCVS('commit -m -F logmsg.txt .',Nil) then
     Raise AppError.Create(SErrFailedToCommit);
 Finally
   if not DeleteFile('logmsg.txt') then
     Writeln(StdErr,'Warning: failed to remove log message file.');
 end;
end;

Procedure TSVN2CVSApp.UpdateEntry(AFileName : String);

Var
  FD : String;
  L : TStringList;
  I : Integer;
  Found : Boolean;
  
begin
  If ((FileGetAttr(AFileName) and faDirectory)<>0) then
    begin
    if Not RunCVS('add '+AFileName,Nil) then
      Raise AppError.CreateFmt(SErrFailedToAddDirectory,[AFileName]);
    end
  else // Check if file is under CVS control by checking the Entries file.
    begin
    FD:=ExtractFilePath(AFileName);
    If not DirectoryExists(FD+'Entries') then
      Raise AppError.CreateFmt(SErrDirectoryNotInCVS,[FD]);
    Found:=False;
    L:=TStringList.Create;
    Try
      L.LoadFromFile(FD+'Entries');
      Found:=False;
      I:=0;
      While (not found) and (I<L.Count) do
        begin
        Inc(I);
        end;
      if not found then
       if Not RunCVS('add '+AFileName,Nil) then
         Raise AppError.CreateFmt(SErrFailedToAddFile,[AFileName]);
    finally
      L.Free;
    end;
    end;
end;

Procedure TSVN2CVSApp.DeleteEntry(AFileName : String);

begin
  If ((FileGetAttr(AFileName) and faDirectory)=0) then
    if Not RunCVS('rm '+AFileName,Nil) then
      Raise AppError.CreateFmt(SErrFailedToRemove,[AFileName]);
end;

procedure TSVN2CVSApp.DoRun;

begin
  If Not ProcessArguments then
    exit;
  ConvertRepository;
end;

procedure TSVN2CVSApp.ProcessConfigFile;

begin
  With TMemIniFile.Create(GetAppConfigFile(False)) do
    try
      SVNURL:=ReadString(SGlobal,KeySVNURL,'');
      CVSROOT:=ReadString(SGlobal,KeyCVSROOT,'');
      CVSRepository:=ReadString(SGlobal,KeyRepository,'');
      WorkingDir:=ReadString(SGLobal,KeyWorkDir,'');
      StartRevision:=ReadInteger(SGlobal,KeyRevision,-1)+1;
      SVNBin:=ReadString(SGlobal,KeySVNBin,'svn');
      CVSBin:=ReadString(SGlobal,KeyCVSBin,'cvs');
    finally
      Free;
    end;
end;


function TSVN2CVSApp.ProcessArguments: Boolean;

begin
  ProcessConfigFile;
  if HasOption('s','svn-repository') then
    SVNURL:=GetOptionValue('s','svn-repository');
  if HasOption('c','cvsroot') then
    CVSROOT:=GetOptionValue('c','cvsroot');
  if HasOption('c','cvsrepository') then
    CVSROOT:=GetOptionValue('p','cvsrepository');
  if HasOption('r','revision') then
    StartRevision:=StrToIntDef(GetOptionValue('c'),0);
  if HasOption('d','directory') then
    WorkingDir:=GetOptionValue('d','directory');
  Result:=(SVNUrl<>'') and (CVSROOT<>'');
  If Result then
    begin
    If (WorkingDir='') then
      WorkingDir:=GetCurrentDir;
    WorkingDir:=IncludeTrailingPathDelimiter(WorkingDir);
    end;
end;

begin
  With TSVN2CVSApp.Create(Nil) do
    try
      Initialize;
      Run;
    Finally
      free;
    end;  
end.