Check-in [20d7dd8edd]

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Added console running code to TVCSClient.
Timelines: family | ancestors | descendants | both | feature/rewrite-multi-async
Files: files | file ages | folders
SHA1:20d7dd8eddae669743f3ddf47bfedf035d9058fc
User & Date: tinus 2016-02-22 18:34:25
Context
2016-02-22
18:50
Run commands in the repo root by default. Implemented Fossil.GetBranches. check-in: c92164b9f9 user: tinus tags: feature/rewrite-multi-async
18:34
Added console running code to TVCSClient. check-in: 20d7dd8edd user: tinus tags: feature/rewrite-multi-async
2016-02-21
15:22
Added scaffolding for VCS client interface. check-in: 083aa4a165 user: tinus tags: feature/rewrite-multi-async
Changes

Changes to src/vcsinfo.VCSClient.pas.

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
..
52
53
54
55
56
57
58



59
60
61
62
63
64
65
..
82
83
84
85
86
87
88



























































































































































































































































89
90
91
92
93
94
95
interface
uses
  System.SysUtils, System.Generics.Collections;

type
  EVCSException = class(Exception);





type
  TVCSClient = class;
  TVCSClientClass = class of TVCSClient;

  TVCSClient = class
  public
    class function CreateForPath(const APath: string): TVCSClient;
    class function IsRepo(const APath: string; out ARootPath: string): boolean; virtual; abstract;
    class procedure RegisterVCSClass(const VCSClass: TVCSClientClass);
  strict private
    class var FRegisteredVCSes: TList<TVCSClientClass>;





  strict protected
    FRoot: string;












    function  GetExecutable: string; virtual; abstract;
    function  GetUIExecutable: string; virtual; abstract;
    function  GetTitle: string; virtual;
    function  GetCurrentBranch: string; virtual;
  public
    constructor Create(const APath: string);
    destructor  Destroy; override;
................................................................................

    property CurrentBranch: string  read GetCurrentBranch;
  end;


implementation




{ TVCSClient }

constructor TVCSClient.Create(const APath: string);
begin

end;

................................................................................
    Result := '';
end;

function TVCSClient.GetTitle: string;
begin
  Result := ExtractFileName(ExcludeTrailingPathDelimiter(FRoot));
end;




























































































































































































































































class function TVCSClient.CreateForPath(const APath: string): TVCSClient;
var
  VCSClass: TVCSClientClass;
  RootPath: string;
begin
  for VCSClass in FRegisteredVCSes do begin







>
>
>
>











>
>
>
>
>



>
>
>
>
>
>
>
>
>
>
>







 







>
>
>







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







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
..
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
...
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
interface
uses
  System.SysUtils, System.Generics.Collections;

type
  EVCSException = class(Exception);

type
  // e.g. TStrings.Append
  TTextHandler = procedure(const Text: string) of object;

type
  TVCSClient = class;
  TVCSClientClass = class of TVCSClient;

  TVCSClient = class
  public
    class function CreateForPath(const APath: string): TVCSClient;
    class function IsRepo(const APath: string; out ARootPath: string): boolean; virtual; abstract;
    class procedure RegisterVCSClass(const VCSClass: TVCSClientClass);
  strict private
    class var FRegisteredVCSes: TList<TVCSClientClass>;
  strict private
    function InternalExecute(CommandLine: string; var Output: string;
      OutputLineCallback: TTextHandler; RawOutput: Boolean; AbortPtr: PBoolean;
      const CurrentDir: string): Cardinal;
    function MuteCRTerminatedLines(const RawOutput: string): string;
  strict protected
    FRoot: string;

    function ExecuteCmd(const CommandLine: string;
                        OutputLineCallback: TTextHandler;
                        RawOutput: Boolean = False;
                        AbortPtr: PBoolean = nil;
                        const CurrentDir: string = ''): Cardinal; overload;
    function ExecuteCmd(const CommandLine: string;
                        var Output: string;
                        RawOutput: Boolean = False;
                        AbortPtr: PBoolean = nil;
                        const CurrentDir: string = ''): Cardinal; overload;

    function  GetExecutable: string; virtual; abstract;
    function  GetUIExecutable: string; virtual; abstract;
    function  GetTitle: string; virtual;
    function  GetCurrentBranch: string; virtual;
  public
    constructor Create(const APath: string);
    destructor  Destroy; override;
................................................................................

    property CurrentBranch: string  read GetCurrentBranch;
  end;


implementation

uses
  Winapi.Windows;

{ TVCSClient }

constructor TVCSClient.Create(const APath: string);
begin

end;

................................................................................
    Result := '';
end;

function TVCSClient.GetTitle: string;
begin
  Result := ExtractFileName(ExcludeTrailingPathDelimiter(FRoot));
end;

{$REGION 'Functions to execute command-line and capture output'}
//--- JclBase and JclSysUtils --------------------------------------------------
const
  // line delimiters for a version of Delphi/C++Builder
  NativeLineFeed       = Char(#10);
  NativeCarriageReturn = Char(#13);

function CharIsReturn(const C: Char): Boolean;
begin
  Result := (C = NativeLineFeed) or (C = NativeCarriageReturn);
end;

// memory initialization
procedure ResetMemory(out P; Size: Longint);
begin
  if Size > 0 then
  begin
    Byte(P) := 0;
    FillChar(P, Size, 0);
  end;
end;

const
  ABORT_EXIT_CODE = {$IFDEF MSWINDOWS} ERROR_CANCELLED {$ELSE} 1223 {$ENDIF};


function TVCSClient.MuteCRTerminatedLines(const RawOutput: string): string;
const
  Delta = 1024;
var
  BufPos, OutPos, LfPos, EndPos: Integer;
  C: Char;
begin
  SetLength(Result, Length(RawOutput));
  OutPos := 1;
  LfPos := OutPos;
  EndPos := OutPos;
  for BufPos := 1 to Length(RawOutput) do
  begin
    if OutPos >= Length(Result)-2 then
      SetLength(Result, Length(Result) + Delta);
    C := RawOutput[BufPos];
    case C of
      NativeCarriageReturn:
        OutPos := LfPos;
      NativeLineFeed:
        begin
          OutPos := EndPos;
          Result[OutPos] := NativeCarriageReturn;
          Inc(OutPos);
          Result[OutPos] := C;
          Inc(OutPos);
          EndPos := OutPos;
          LfPos := OutPos;
        end;
    else
      Result[OutPos] := C;
      Inc(OutPos);
      EndPos := OutPos;
    end;
  end;
  SetLength(Result, OutPos - 1);
end;

function TVCSClient.InternalExecute(CommandLine: string; var Output: string; OutputLineCallback: TTextHandler;
  RawOutput: Boolean; AbortPtr: PBoolean; const CurrentDir: string): Cardinal;

const
  BufferSize = 255;
type
  TBuffer = array [0..BufferSize] of AnsiChar;

  procedure ProcessLine(const Line: string; LineEnd: Integer);
  begin
    if RawOutput or (Line[LineEnd] <> NativeCarriageReturn) then
    begin
      while (LineEnd > 0) and CharIsReturn(Line[LineEnd]) do
        Dec(LineEnd);
      OutputLineCallback(Copy(Line, 1, LineEnd));
    end;
  end;

  procedure ProcessBuffer(var Buffer: TBuffer; var Line: string; PipeBytesRead: Cardinal);
  var
    CR, LF: Integer;
  begin
    Buffer[PipeBytesRead] := #0;
    Line := Line + string(Buffer);
    if Assigned(OutputLineCallback) then
    repeat
      CR := Pos(NativeCarriageReturn, Line);
      if CR = Length(Line) then
        CR := 0;        // line feed at CR + 1 might be missing
      LF := Pos(NativeLineFeed, Line);
      if (CR > 0) and ((LF > CR + 1) or (LF = 0)) then
        LF := CR;       // accept CR as line end
      if LF > 0 then
      begin
        ProcessLine(Line, LF);
        Delete(Line, 1, LF);
      end;
    until LF = 0;
  end;

var
  Buffer: TBuffer;
  Line: string;
  PipeBytesRead: Cardinal;
{$IFDEF MSWINDOWS}
var
  StartupInfo: TStartupInfo;
  ProcessInfo: TProcessInformation;
  SecurityAttr: TSecurityAttributes;
  PipeRead, PipeWrite: THandle;
  PCurrentDir: PChar;
begin
  Result := $FFFFFFFF;
  SecurityAttr.nLength := SizeOf(SecurityAttr);
  SecurityAttr.lpSecurityDescriptor := nil;
  SecurityAttr.bInheritHandle := True;
  PipeWrite := 0;
  PipeRead := 0;
  Line := '';
  ResetMemory(Buffer, SizeOf(Buffer));
  if not CreatePipe(PipeRead, PipeWrite, @SecurityAttr, 0) then
  begin
    Result := GetLastError;
    Exit;
  end;
  ResetMemory(StartupInfo, SizeOf(TStartupInfo));
  StartupInfo.cb := SizeOf(TStartupInfo);
  StartupInfo.dwFlags := STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES;
  StartupInfo.wShowWindow := SW_HIDE;
  StartupInfo.hStdInput := GetStdHandle(STD_INPUT_HANDLE);
  StartupInfo.hStdOutput := PipeWrite;
  StartupInfo.hStdError := PipeWrite;
  UniqueString(CommandLine); // CommandLine must be in a writable memory block
  ProcessInfo.dwProcessId := 0;
  try
    if CurrentDir <> '' then
      PCurrentDir := PChar(CurrentDir)
    else
      PCurrentDir := nil;
    if CreateProcess(nil, PChar(CommandLine), nil, nil, True, NORMAL_PRIORITY_CLASS,
      nil, PCurrentDir, StartupInfo, ProcessInfo) then
    begin
      CloseHandle(PipeWrite);
      PipeWrite := 0;
      if AbortPtr <> nil then
        {$IFDEF FPC}
        AbortPtr^ := 0;
        {$ELSE ~FPC}
        AbortPtr^ := False;
        {$ENDIF ~FPC}
      PipeBytesRead := 0;
      while ((AbortPtr = nil) or not LongBool(AbortPtr^)) and
        ReadFile(PipeRead, Buffer, BufferSize, PipeBytesRead, nil) and (PipeBytesRead > 0) do
        ProcessBuffer(Buffer, Line, PipeBytesRead);
      if (AbortPtr <> nil) and LongBool(AbortPtr^) then
        TerminateProcess(ProcessInfo.hProcess, Cardinal(ABORT_EXIT_CODE));
      if (WaitForSingleObject(ProcessInfo.hProcess, INFINITE) = WAIT_OBJECT_0) and
        not GetExitCodeProcess(ProcessInfo.hProcess, Result) then
          Result := $FFFFFFFF;
      CloseHandle(ProcessInfo.hThread);
      ProcessInfo.hThread := 0;
      CloseHandle(ProcessInfo.hProcess);
      ProcessInfo.hProcess := 0;
    end
    else
    begin
      CloseHandle(PipeWrite);
      PipeWrite := 0;
    end;
    CloseHandle(PipeRead);
    PipeRead := 0;
  finally
    if PipeRead <> 0 then
      CloseHandle(PipeRead);
    if PipeWrite <> 0 then
      CloseHandle(PipeWrite);
    if ProcessInfo.hThread <> 0 then
      CloseHandle(ProcessInfo.hThread);
    if ProcessInfo.hProcess <> 0 then
    begin
      TerminateProcess(ProcessInfo.hProcess, Cardinal(ABORT_EXIT_CODE));
      WaitForSingleObject(ProcessInfo.hProcess, INFINITE);
      GetExitCodeProcess(ProcessInfo.hProcess, Result);
      CloseHandle(ProcessInfo.hProcess);
    end;
  end;
{$ENDIF MSWINDOWS}
{$IFDEF UNIX}
var
  Pipe: PIOFile;
  Cmd: string;
begin
  Cmd := Format('%s 2>&1', [CommandLine]);
  Pipe := nil;
  try
    Pipe := Libc.popen(PChar(Cmd), 'r');
    { TODO : handle Abort }
    repeat
      PipeBytesRead := fread_unlocked(@Buffer, 1, BufferSize, Pipe);
      if PipeBytesRead > 0 then
        ProcessBuffer(Buffer, Line, PipeBytesRead);
    until PipeBytesRead = 0;
    Result := pclose(Pipe);
    Pipe := nil;
    wait(nil);
  finally
    if Pipe <> nil then
      pclose(Pipe);
    wait(nil);
  end;
{$ENDIF UNIX}
  if Line <> '' then
    if Assigned(OutputLineCallback) then
      // output wasn't terminated by a line feed...
      // (shouldn't happen, but you never know)
      ProcessLine(Line, Length(Line))
    else
      if RawOutput then
        Output := Output + Line
      else
        Output := Output + MuteCRTerminatedLines(Line);
end;

function TVCSClient.ExecuteCmd(const CommandLine: string;
                                var Output: string;
                                RawOutput: Boolean = False;
                                AbortPtr: PBoolean = nil;
                                const CurrentDir: string = ''): Cardinal;
begin
  Result := InternalExecute(CommandLine, Output, nil, RawOutput, AbortPtr, CurrentDir);
end;

function TVCSClient.ExecuteCmd(const CommandLine: string;
                                OutputLineCallback: TTextHandler;
                                RawOutput: Boolean = False;
                                AbortPtr: PBoolean = nil;
                                const CurrentDir: string = ''): Cardinal;
var
  Dummy: string;
begin
  Dummy := '';
  Result := InternalExecute(CommandLine, Dummy, OutputLineCallback, RawOutput, AbortPtr, CurrentDir);
end;

{$ENDREGION}


class function TVCSClient.CreateForPath(const APath: string): TVCSClient;
var
  VCSClass: TVCSClientClass;
  RootPath: string;
begin
  for VCSClass in FRegisteredVCSes do begin