-
Notifications
You must be signed in to change notification settings - Fork 11
/
repoclient.pas
375 lines (325 loc) · 14 KB
/
repoclient.pas
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
unit repoclient;
{ Generic repository client class. Implementations for hg, svn,... are availalbe
Copyright (C) 2012-2013 Reinier Olislagers, Ludo Brands
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.
}
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils;
const
// Custom return codes; note: keep separate from ProcessEx return codes (processutils.PROC_INTERNALERROR=-1)
FRET_LOCAL_REMOTE_URL_NOMATCH = -10; //Return code that indicates remote and local repository URLs don't match
FRET_WORKING_COPY_TOO_OLD = -11; //Return code for SVN problem with old client version used
FRET_NONEXISTING_REPO = -12; // Repo client could not detect a local repository/local copy of the remote repository
FRET_UNKNOWN_REVISION = 'FRET_UNKNOWN_REVISION';
ERRORMAXRETRIES = 3;
CONNECTIONMAXRETRIES = 10;
type
ERepoClientError = class(Exception);
{ TRepoClient }
TRepoClient = class(TObject)
private
FParent:TObject;
FRepositoryURL: string;
FLocalRepository: string;
FDesiredRevision: string;
FDesiredBranch: string;
FDesiredTag: string;
FHTTPProxyHost: string;
FHTTPProxyPassword: string;
FHTTPProxyPort: integer;
FHTTPProxyUser: string;
FModuleName: string;
FVerbose: boolean;
FExportOnly: boolean;
FForceLocal: boolean;
protected
FLocalRevision: string;
FRepoExecutable: string;
FRepoExecutableName: string;
FReturnCode: integer;
FReturnOutput: string;
function IncludeTrailingSlash(AValue: string): string;
function ExcludeTrailingSlash(AValue: string): string;
//Performs a checkout/initial download
//Note: it's often easier to call CheckOutOrUpdate
procedure CheckOut(UseForce:boolean=false); virtual;
function GetLocalRevision: string; virtual;
procedure SetDesiredRevision(AValue: string); virtual;
procedure SetDesiredBranch(AValue: string); virtual;
procedure SetDesiredTag(AValue: string); virtual;
procedure SetLocalRepository(AValue: string); virtual;
function GetRepositoryURL:string; virtual;
procedure SetRepositoryURL(AValue: string); virtual;
procedure SetRepoExecutable(AValue: string); virtual;
procedure SetVerbose(AValue: boolean); virtual;
procedure SetExportOnly(AValue: boolean); virtual;
function GetValidClient:boolean;
// Search for installed version control client executable (might return just a filename if in the OS path)
function GetRepoExecutable:string;virtual;
function GetRepoExecutableName:string;virtual;
function FindRepoExecutable: string; virtual;
//Performs an update (pull)
//Note: it's often easier to call CheckOutOrUpdate; that also has some more network error recovery built in
procedure Update; virtual;
public
property Parent:TObject read FParent;
// Downloads from remote repo: runs checkout if local repository doesn't exist, else does an update
procedure CheckOutOrUpdate; virtual;
// Downloads only the whole tree from remote repo ... do not include .svn or .git
procedure ExportRepo; virtual;
// Commits local changes to local and remote repository
function Commit(Message: string): boolean; virtual;
// Creates diff of all changes in the local directory versus the remote version
function GetDiffAll: string; virtual;
// Shows commit log for local directory
procedure Log(var Log: TStringList); virtual;
// change (switch) the remote URL
procedure SwitchURL; virtual;
// Parses file lists generated by version control client, optionally limited to characters in FilterCodes
procedure ParseFileList(const CommandOutput: string; var FileList: TStringList; const FilterCodes: array of string); virtual;
// Reverts/removes local changes so we get a clean copy again. Note: will remove modifications to files!
procedure Revert; virtual;
// Get/set desired revision to checkout/pull to (if none given, use HEAD/tip/newest)
property DesiredRevision: string read FDesiredRevision write SetDesiredRevision;
// Get/set desired branch to checkout/pull
property DesiredBranch: string read FDesiredBranch write SetDesiredBranch;
// Get/set desired tag to checkout/pull
property DesiredTag: string read FDesiredTag write SetDesiredTag;
// If using http transport, an http proxy can be used. Proxy hostname/ip address
property HTTPProxyHost: string read FHTTPProxyHost write FHTTPProxyHost;
// If using http transport, an http proxy can be used. Proxy port
property HTTPProxyPort: integer read FHTTPProxyPort write FHTTPProxyPort;
// If using http transport, an http proxy can be used. Proxy username (optional)
property HTTPProxyUser: string read FHTTPProxyUser write FHTTPProxyUser;
// If using http transport, an http proxy can be used. Proxy password (optional)
property HTTPProxyPassword: string read FHTTPProxyPassword write FHTTPProxyPassword;
// Shows list of files that have been modified locally (and not committed)
procedure LocalModifications({%H-}var FileList: TStringList); virtual;
// Checks to see if local directory is a valid repository for the repository URL given (if any)
function LocalRepositoryExists: boolean; virtual;
// Local directory that has a repository/checkout.
// When setting, relative paths will be expanded; trailing path delimiters will be removed
property LocalRepository: string read FLocalRepository write SetLocalRepository;
// Revision number of local repository: branch revision number if we're in a branch.
property LocalRevision: string read GetLocalRevision;
// URL where central (remote) repository is placed
property Repository: string read GetRepositoryURL write SetRepositoryURL;
// Exit code returned by last client command; 0 for success. Useful for troubleshooting
property ReturnCode: integer read FReturnCode;
// Output returned by last client command. Useful for troubleshooting
property ReturnOutput: string read FReturnOutput;
// Version control client executable. Can be set to explicitly determine which executable to use.
property RepoExecutable: string read GetRepoExecutable write SetRepoExecutable;
// Show additional console/log output?
property Verbose: boolean read FVerbose write SetVerbose;
property ModuleName: string read FModuleName write FModuleName;
property ExportOnly: boolean read FExportOnly write SetExportOnly;
property ForceLocal: boolean read FForceLocal write FForceLocal;
property ValidClient: boolean read GetValidClient;
property RepoExecutableName: string read GetRepoExecutableName;
constructor Create(aParent:TObject);
destructor Destroy; override;
end;
implementation
uses
fpcuputil;
{ TRepoClient }
function TRepoClient.GetLocalRevision: string;
begin
// Inherited classes, please implement
FLocalRevision := FRET_UNKNOWN_REVISION;
raise Exception.Create('TRepoClient descendants must implement this themselves.');
end;
function TRepoClient.GetRepoExecutable: string;
begin
{ Inherited classes, please implement getting the client executable
for your version control system, e.g. svn.exe, git, hg, bzr... or nothing}
raise Exception.Create('TRepoClient descendants must implement this themselves.');
Result := '';
end;
function TRepoClient.IncludeTrailingSlash(AValue: string): string;
begin
// Default: either empty string or / already there
Result := AValue;
if (Result <> '') and (Result[Length(Result)] <> '/') then
Result := Result + '/';
end;
function TRepoClient.ExcludeTrailingSlash(AValue: string): string;
begin
Result := AValue;
if (Result <> '') and (Result[Length(Result)] = '/') then
SetLength(Result,Length(Result)-1);
end;
procedure TRepoClient.SetDesiredRevision(AValue: string);
begin
if FDesiredRevision = AValue then
Exit;
FDesiredRevision := AValue;
end;
procedure TRepoClient.SetDesiredBranch(AValue: string);
begin
if FDesiredBranch = AValue then
Exit;
FDesiredBranch := AValue;
end;
procedure TRepoClient.SetDesiredTag(AValue: string);
begin
if FDesiredTag = AValue then
Exit;
FDesiredTag := AValue;
end;
procedure TRepoClient.SetLocalRepository(AValue: string);
// Sets local repository, converting relative path to absolute path
// and adding a trailing / or \
begin
if FLocalRepository = AValue then
Exit;
// Avoid ExpandFilename expanding to current dir
if AValue = '' then
FLocalRepository := AValue
else
FLocalRepository := ExcludeTrailingPathDelimiter(AValue);
end;
function TRepoClient.GetRepositoryURL:string;
begin
result:=FRepositoryURL;
end;
procedure TRepoClient.SetRepositoryURL(AValue: string);
// Make sure there's a trailing / in the URL.
// This normalization helps matching remote and local URLs
begin
if FRepositoryURL = AValue then
Exit;
FRepositoryURL := IncludeTrailingSlash(AValue);
end;
procedure TRepoClient.SetRepoExecutable(AValue: string);
begin
if FRepoExecutable <> AValue then
begin
FRepoExecutable := AValue;
// If it exists, assume it's the correct client; if not...
if not (FileExists(FRepoExecutable)) then
FindRepoExecutable; //... use fallbacks to get a working client
end;
end;
procedure TRepoClient.SetVerbose(AValue: boolean);
begin
if FVerbose = AValue then
Exit;
FVerbose := AValue;
end;
procedure TRepoClient.SetExportOnly(AValue: boolean);
begin
if FExportOnly = AValue then
Exit;
FExportOnly := AValue;
end;
function TRepoClient.GetValidClient:boolean;
begin
result:=( (Length(FRepoExecutable)<>0) AND (FileExists(FRepoExecutable)) );
if (NOT result) then
begin
FindRepoExecutable;
result:=( (Length(FRepoExecutable)<>0) AND (FileExists(FRepoExecutable)) );
end;
end;
procedure TRepoClient.CheckOut(UseForce:boolean=false);
begin
raise Exception.Create('TRepoClient descendants must implement CheckOut by themselves.');
end;
procedure TRepoClient.CheckOutOrUpdate;
begin
raise Exception.Create('TRepoClient descendants must implement CheckOutOrUpdate by themselves.');
end;
procedure TRepoClient.ExportRepo;
begin
raise Exception.Create('TRepoClient descendants must implement ExportRepo by themselves.');
end;
function TRepoClient.Commit(Message: string): boolean;
begin
raise Exception.Create('TRepoClient descendants must implement Commit by themselves.');
end;
function TRepoClient.GetRepoExecutableName:string;
begin
raise Exception.Create('TRepoClient descendants must implement GetRepoExecutableName by themselves.');
end;
function TRepoClient.GetDiffAll: string;
begin
raise Exception.Create('TRepoClient descendants must implement GetDiffAll by themselves.');
end;
function TRepoClient.FindRepoExecutable: string;
begin
raise Exception.Create('TRepoClient descendants must implement FindRepoExecutable by themselves.');
end;
procedure TRepoClient.Log(var Log: TStringList);
begin
raise Exception.Create('TRepoClient descendants must implement Log by themselves.');
end;
procedure TRepoClient.SwitchURL;
begin
raise Exception.Create('TRepoClient descendants must implement SwitchURL by themselves.');
end;
procedure TRepoClient.ParseFileList(const CommandOutput: string; var FileList: TStringList; const FilterCodes: array of string);
begin
raise Exception.Create('TRepoClient descendants must implement ParseFileList by themselves.');
end;
procedure TRepoClient.Revert;
begin
raise Exception.Create('TRepoClient descendants must implement Revert by themselves.');
end;
procedure TRepoClient.Update;
begin
raise Exception.Create('TRepoClient descendants must implement Update by themselves.');
end;
procedure TRepoClient.LocalModifications(var FileList: TStringList);
begin
raise Exception.Create('TRepoClient descendants must implement LocalModifications by themselves.');
end;
function TRepoClient.LocalRepositoryExists: boolean;
begin
result:=False;
raise Exception.Create('TRepoClient descendants must implement LocalRepositoryExists by themselves.');
end;
constructor TRepoClient.Create(aParent:TObject);
begin
inherited Create;
FParent:=aParent;
FLocalRepository := '';
FRepositoryURL := '';
FDesiredRevision := '';
FLocalRevision := FRET_UNKNOWN_REVISION;
FReturnCode := 0;
FReturnOutput := '';
FRepoExecutable := '';
FForceLocal := False;
FVerbose := True;
//FForceLocal := TInstaller(FParent).ForceLocal;
//FindRepoExecutable;
end;
destructor TRepoClient.Destroy;
begin
inherited Destroy;
end;
end.