File: find_missing_wiki_svnrevisions.pas

package info (click to toggle)
lazarus 2.0.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 214,460 kB
  • sloc: pascal: 1,862,622; xml: 265,709; cpp: 56,595; sh: 3,008; java: 609; makefile: 535; perl: 297; sql: 222; ansic: 137
file content (248 lines) | stat: -rw-r--r-- 7,187 bytes parent folder | download | duplicates (6)
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
{ Finds missing svn revisions in wiki list format,
  used for updating the wiki pages fixes branch.

  Usage:
    ./findmissingsvnrevisionwiki -h
    ./findmissingsvnrevisionwiki -s <svnlogfile> -w <wiki-list-file>

  Copyright (C) 2017 Mattias Gaertner mattias@freepascal.org

  This library is free software; you can redistribute it and/or modify it
  under the terms of the GNU Library General Public License as published by
  the Free Software Foundation; either version 2 of the License, or (at your
  option) any later version with the following modification:

  As a special exception, the copyright holders of this library give you
  permission to link this library with independent modules to produce an
  executable, regardless of the license terms of these independent modules,and
  to copy and distribute the resulting executable under terms of your choice,
  provided that you also meet, for each linked independent module, the terms
  and conditions of the license of that module. An independent module is a
  module which is not derived from or based on this library. If you modify
  this library, you may extend this exception to your version of the library,
  but you are not obligated to do so. If you do not wish to do so, delete this
  exception statement from your version.

  This program is distributed in the hope that it will be useful, but WITHOUT
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
  for more details.

  You should have received a copy of the GNU Library General Public License
  along with this library; if not, write to the Free Software Foundation,
  Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
}
program find_missing_wiki_svnrevisions;

{$mode objfpc}{$H+}

uses
  Classes, SysUtils, CustApp, LazFileUtils;

type

  { TMissingWikiSVNRevisions }

  TMissingWikiSVNRevisions = class(TCustomApplication)
  protected
    procedure DoRun; override;
    procedure ParamError(Msg: string);
    procedure ExtractSVN(Lines: TStrings; UserFilter: String);
    procedure ExtractWikiRevs(Lines: TStrings);
  public
    constructor Create(TheOwner: TComponent); override;
    destructor Destroy; override;
    procedure WriteHelp; virtual;
  end;

{ TMissingWikiSVNRevisions }

procedure TMissingWikiSVNRevisions.DoRun;
var
  ErrorMsg, SVNFile, WikiFile, Line, Rev, UserFilter: String;
  SVNLines, WIKILines: TStringList;
  i: Integer;
  p: SizeInt;
begin
  // quick check parameters
  ErrorMsg:=CheckOptions('hs:w:u:', 'help svnlog wikilist user');
  if ErrorMsg<>'' then
    ParamError(ErrorMsg);

  // parse parameters
  if HasOption('h', 'help') then begin
    WriteHelp;
    Halt;
  end;

  if not HasOption('s','svnlog') then begin
    WriteHelp;
    Halt;
  end;
  if not HasOption('w','wikilist') then begin
    WriteHelp;
    Halt;
  end;

  SVNFile:=ExpandFileNameUTF8(GetOptionValue('s','svnlog'));
  if not FileExists(SVNFile) then
    ParamError('svn file not found "'+SVNFile+'"');

  WikiFile:=ExpandFileNameUTF8(GetOptionValue('w','wikilist'));
  if not FileExists(WikiFile) then
    ParamError('wiki list file not found "'+WikiFile+'"');

  UserFilter:=GetOptionValue('u','user');

  SVNLines:=TStringList.Create;
  SVNLines.LoadFromFile(SVNFile);
  WIKILines:=TStringList.Create;
  WIKILines.LoadFromFile(WikiFile);

  ExtractSVN(SVNLines,UserFilter);
  ExtractWikiRevs(WIKILines);
  //writeln('SVN: ',SVNLines.Text);
  //writeln('WIKI: ',WIKILines.Text);

  // write missing svn revisions
  writeln('Missing svn revisions:');
  for i:=0 to SVNLines.Count-1 do begin
    Line:=SVNLines[i];
    p:=Pos(' ',Line);
    Rev:=LeftStr(Line,p-1);
    if WIKILines.IndexOf(Rev)>=0 then continue;
    writeln('*',Line);
  end;

  // stop program loop
  Terminate;
end;

procedure TMissingWikiSVNRevisions.ParamError(Msg: string);
begin
  writeln('Invalid param: ',Msg);
  writeln;
  writeln('Usage: ',ExeName,' -h');
  Halt;
end;

procedure TMissingWikiSVNRevisions.ExtractSVN(Lines: TStrings;
  UserFilter: String);
{ Remove empty and separator lines.
  Combine each log entry into a single line "*r12345 message"

------------------------------------------------------------------------
r54919 | martin | 2017-05-14 12:59:15 +0200 (So, 14 Mai 2017) | 1 line

Fixed compile ifdef

->

*r54919 Fixed compile ifdef
}
const MaxCol=80;
var
  i: Integer;
  Line, Revision, UserName: String;
  p, StartP: PChar;
begin
  i:=0;
  UserName:='';
  while i<Lines.Count do begin
    Line:=Lines[i];
    p:=PChar(Line);
    if (LeftStr(Line,10)='----------') or (Trim(Line)='') then begin
      Lines.Delete(i);
    end else if (p^='r') and (p[1] in ['0'..'9']) then begin
      // revision
      inc(p,1);
      while p^ in ['0'..'9'] do inc(p);
      Revision:=LeftStr(Line,p-PChar(Line)); // 'r12345'
      while p^ in [' ','|'] do inc(p);
      StartP:=p;
      while not (p^ in [' ','|',#0]) do inc(p);
      UserName:=copy(Line,StartP-PChar(Line)+1,p-StartP);
      if (UserFilter<>'') and (UserName<>UserFilter) then
        Lines.Delete(i)
      else begin
        Lines[i]:=Revision;
        inc(i);
      end;
    end else begin
      // comment
      Line:=Trim(Line);
      if (UserFilter='') or (UserName=UserFilter) then begin
        if (i>0) and (length(Lines[i-1])<MaxCol) then begin
          Line:=Lines[i-1]+' '+Line;
          if length(Line)>MaxCol then
            Line:=LeftStr(Line,MaxCol)+'...';
          Lines[i-1]:=Line;
        end;
      end;
      Lines.Delete(i);
    end;
  end;
end;

procedure TMissingWikiSVNRevisions.ExtractWikiRevs(Lines: TStrings);
var
  i: Integer;
  Line: String;
  p: PChar;
begin
  i:=0;
  while i<Lines.Count do begin
    Line:=Lines[i];
    if Trim(Line)='' then begin
      Lines.Delete(i);
    end else begin
      p:=PChar(Line);
      if (p[0]='*') and (p[1]='r') and (p[2] in ['0'..'9']) then begin
        inc(p,2);
        while p^ in ['0'..'9'] do inc(p);
        Lines[i]:=copy(Line,2,p-PChar(Line)-1); // 'r12345'
        inc(i);
      end else begin
        writeln('WARNING: invalid wiki line: "',Line,'"');
        Lines.Delete(i);
      end;
    end;
  end;
end;

constructor TMissingWikiSVNRevisions.Create(TheOwner: TComponent);
begin
  inherited Create(TheOwner);
  StopOnException:=True;
end;

destructor TMissingWikiSVNRevisions.Destroy;
begin
  inherited Destroy;
end;

procedure TMissingWikiSVNRevisions.WriteHelp;
begin
  writeln('Usage: ', ExeName, ' -s svn.log -w wikilist.txt [-u username]');
  writeln;
  writeln('Shows svn log entries not listed in the wikilist.');
  writeln;
  writeln('-h, --help');
  writeln('        show this help');
  writeln('-s <file>, --svnlog=<file>');
  writeln('        SVN log, e.g. "svn log --limit=1000 > log.txt"');
  writeln('-w <file>, --wikilist=<file>');
  writeln('        List in wiki format: Every line "*r12345 text"');
  writeln('-u <user>, --user=<user>');
  writeln('        only user ');
end;

var
  Application: TMissingWikiSVNRevisions;
begin
  Application:=TMissingWikiSVNRevisions.Create(nil);
  Application.Title:='Find missing svn revisions in wiki';
  Application.Run;
  Application.Free;
end.