-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathugitprogress.pas
More file actions
232 lines (211 loc) · 6.87 KB
/
ugitprogress.pas
File metadata and controls
232 lines (211 loc) · 6.87 KB
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
(******************************************************************************)
(* *)
(* Author : Uwe Schächterle (Corpsman) *)
(* *)
(* This file is part of GIT gui *)
(* *)
(* See the file license.md, located under: *)
(* https://github.com/PascalCorpsman/Software_Licenses/blob/main/license.md *)
(* for details about the license. *)
(* *)
(* It is not allowed to change or remove this text from any *)
(* source file of the project. *)
(* *)
(******************************************************************************)
Unit ugitprogress;
{$MODE ObjFPC}{$H+}
Interface
Uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ComCtrls, StdCtrls;
Type
{ TGitProgress }
TGitProgress = Class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
Memo1: TMemo;
ProgressBar1: TProgressBar;
Procedure Button1Click(Sender: TObject);
Procedure Button3Click(Sender: TObject);
Procedure FormCreate(Sender: TObject);
private
fAbort: Boolean;
ProjectRoot: String;
Function RunProcess(Workdir, Executeable: String; Params: Array Of String): Boolean;
public
Function RunVisualCommand(aDir, aCommand: String; params: Array Of String): boolean;
Procedure SetContinueButtonTo(aCaption: String); // TODO: um die Popup Einträge Erweitern ...
Procedure InitProgressbar(aMax: integer);
End;
Var
GitProgress: TGitProgress;
Implementation
Uses process, UTF8Process;
{$R *.lfm}
{ TGitProgress }
Procedure TGitProgress.Button1Click(Sender: TObject);
Begin
// Abort
fAbort := true;
End;
Procedure TGitProgress.Button3Click(Sender: TObject);
Var
p: TProcessUTF8;
fn: String;
Begin
// Push
Case button3.caption Of
'Commit': Begin
p := TProcessUTF8.Create(Nil);
fn := IncludeTrailingPathDelimiter(ExtractFilePath(ParamStr(0))) + 'g_commit';
{$IFDEF Windows}
fn := fn + '.exe';
{$ENDIF}
p.Options := [poWaitOnExit, poNoConsole];
p.Executable := fn;
p.Parameters.Add(ProjectRoot);
p.Execute;
Application.ProcessMessages;
sleep(100);
p.Free;
// Und Feierabend
ModalResult := mrOK;
End;
'Push': Begin
p := TProcessUTF8.Create(Nil);
fn := IncludeTrailingPathDelimiter(ExtractFilePath(ParamStr(0))) + 'g_push';
{$IFDEF Windows}
fn := fn + '.exe';
{$ENDIF}
p.Options := [poWaitOnExit, poNoConsole];
p.Executable := fn;
p.Parameters.Add(ProjectRoot);
p.Execute;
Application.ProcessMessages;
sleep(100);
p.Free;
// Und Feierabend
ModalResult := mrOK;
End;
Else Begin
showmessage(button3.caption + ' not yet implemented.');
End;
End;
End;
Procedure TGitProgress.FormCreate(Sender: TObject);
Begin
ProjectRoot := '';
button3.Enabled := false;
End;
Function TGitProgress.RunProcess(Workdir, Executeable: String;
Params: Array Of String): Boolean;
Const
BUF_SIZE = 2048; // Buffer size for reading the output in chunks
Var
P: TProcessUTF8;
i: Integer;
Buffer: Array[0..BUF_SIZE] Of char;
BytesRead: LongInt;
t, s, cd: String;
InBuffer: String;
index: Integer;
Begin
button2.Enabled := false;
button1.Enabled := true;
result := false;
cd := GetCurrentDir;
Try
SetCurrentDir(Workdir);
p := TProcessUTF8.Create(Nil);
p.Options := [poUsePipes, poStderrToOutPut, poNoConsole];
p.Executable := Executeable;
For i := 0 To high(Params) Do Begin
p.Parameters.Add(Params[i]);
End;
p.execute;
s := '';
InBuffer := '';
Buffer[0] := #0; // Compilerwarnung platt machen
(*
* Loop until Programm is finished and all buffers are empty
*)
While p.Running Or (p.Output.NumBytesAvailable > 0) Do Begin
While (p.Output.NumBytesAvailable > 0) Do Begin
BytesRead := p.Output.Read(Buffer, BUF_SIZE);
setlength(s, BytesRead);
For i := 1 To BytesRead Do Begin
s[i] := Buffer[i - 1];
End;
InBuffer := InBuffer + s;
// Zeilenweises Auslesen
index := pos(#10, InBuffer); // Git nutzt immer #10 als CRT ;)
While index <> 0 Do Begin
t := copy(InBuffer, 1, index - 1);
Memo1.Lines.Append(t);
delete(InBuffer, 1, index);
index := pos(#10, InBuffer);
End;
End;
// if user wants to interact with the scripts
//If SendToConsole <> '' Then Begin
//P.Input.Write(SendToConsole[1], Length(SendToConsole));
//SendToConsole := '';
//End;
Application.ProcessMessages;
Sleep(1);
// Der Benutzer will das das Programm beendet wird, also schiesen wir hier den Prozess händisch ab!
If fAbort Then Begin
p.Terminate(1);
End;
End;
If InBuffer <> '' Then Begin
Memo1.Lines.Append(InBuffer);
End;
result := p.ExitCode = 0;
p.free;
Finally
SetCurrentDir(cd);
button2.Enabled := true;
button1.Enabled := false;
End;
End;
Function TGitProgress.RunVisualCommand(aDir, aCommand: String;
params: Array Of String): boolean;
Var
t: QWord;
Begin
ProjectRoot := aDir;
result := true;
fAbort := false;
t := GetTickCount64;
If RunProcess(adir, acommand, params) Then Begin
ProgressBar1.Position := ProgressBar1.Max;
Memo1.Append('');
Memo1.Append('Success (' + inttostr(GetTickCount64 - t) + ' ms @ ' + FormatDateTime('dd.mm.yyyy hh:nn:ss', Now) + ')');
ProgressBar1.Color := clGreen;
Button3.Enabled := true;
End
Else Begin
Memo1.Append('');
Memo1.Append('Failed (' + inttostr(GetTickCount64 - t) + ' ms @ ' + FormatDateTime('dd.mm.yyyy hh:nn:ss', Now) + ')');
ProgressBar1.Color := clRed;
End;
// Wir Warten bis der User Beendet
While (ModalResult = mrNone) And (Not fAbort) Do Begin
Application.ProcessMessages;
sleep(1);
End;
hide;
result := Not fAbort;
End;
Procedure TGitProgress.SetContinueButtonTo(aCaption: String);
Begin
Button3.caption := aCaption;
End;
Procedure TGitProgress.InitProgressbar(aMax: integer);
Begin
ProgressBar1.Max := aMax;
ProgressBar1.Position := 0;
ProgressBar1.Color := clDefault;
End;
End.