summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorErich Eckner <git@eckner.net>2018-03-05 10:36:54 +0100
committerErich Eckner <git@eckner.net>2018-03-05 10:36:54 +0100
commit969e8b38aee700d0d7159dfde0f42098294cca4a (patch)
treeac6190845b02b5ddf69577981a4437063345a89f
parent281ea5eb2424f8ec494cb76bb298e178e78a4cc1 (diff)
downloadunits-969e8b38aee700d0d7159dfde0f42098294cca4a.tar.xz
gitupdateunit.pas: timeout eingebaut, pull parallelisiert und aufgeräumt
-rw-r--r--gitupdateunit.pas143
1 files changed, 114 insertions, 29 deletions
diff --git a/gitupdateunit.pas b/gitupdateunit.pas
index 66d0201..7e305a1 100644
--- a/gitupdateunit.pas
+++ b/gitupdateunit.pas
@@ -7,43 +7,128 @@ interface
uses
Classes, SysUtils;
+type
+ tGitUpdateThread = class(tThread)
+ private
+ gitVerzeichnis: string;
+ nummer: longint;
+ fertig,upgedatet: boolean;
+ public
+ constructor create(num: longint);
+ procedure execute; override;
+ end;
+
implementation
uses
process, systemunit, lowlevelunit, refreshExecutableUnit;
var
- updated: boolean;
- output: string;
- args,gitDirs: tStringArray;
- i: longint;
+ gitVerzeichnisse: tStringArray;
-initialization
- updated:=false;
-
- setLength(gitDirs,2);
- gitDirs[0]:=extractFilePath(paramstr(0));
- gitDirs[1]:=extractFilePath(leftStr(gitDirs[0],length(gitDirs[0])-1))+'units/';
-
- output:='';
- for i:=0 to length(gitDirs)-1 do begin
- updated:=updated or (sourceSha512Sum(gitDirs[i])<>binarySha512Sum(i));
- setLength(args,3);
- args[0]:='status';
- args[1]:='-sb';
- args[2]:='--porcelain';
- if not runCommandInDir(gitDirs[i],'git',args,output) then continue;
- output:=trim(output);
- if pos(' [behind ',output)=0 then
- continue;
- setLength(args,2);
- args[0]:='pull';
- args[1]:='--all';
- if not runCommandInDir(gitDirs[i],'git',args,output) then continue;
- updated:=true;
+// tGitUpdateThread ************************************************************
+
+constructor tGitUpdateThread.create(num: longint);
+begin
+ inherited create(true);
+ gitVerzeichnis:=extractFilePath(paramstr(0));
+ case num of
+ 0: ;
+ 1: begin
+ delete(gitVerzeichnis,length(gitVerzeichnis),1);
+ gitVerzeichnis:=extractFilePath(gitVerzeichnis)+'units/';
+ end
+ else
+ fehler('Gitverzeichnis Nummer '+intToStr(num)+' ist mir unbekannt!');
+ end{of case};
+ gitVerzeichnisse[num]:=gitVerzeichnis;
+ fertig:=false;
+ nummer:=num;
+ upgedatet:=false;
+ suspended:=false;
+end;
+
+procedure tGitUpdateThread.execute;
+var
+ args: tStringArray;
+ output,remote: string;
+ p: tProcess;
+ dummy: array of byte;
+ timeOut: extended;
+begin
+ setLength(args,1);
+ args[0]:='remote';
+ if runCommandInDir(gitVerzeichnis,'git',args,output) then begin
+ while pos(#13,output)>0 do
+ output[pos(#13,output)]:=#10;
+ while length(output)>0 do begin
+ remote:=erstesArgument(output,#10);
+ if remote='' then
+ continue;
+ p:=tProcess.create(nil);
+ p.executable:='git';
+ p.parameters.add('pull');
+ p.parameters.add('--ff-only');
+ p.parameters.add('-v');
+ p.parameters.add('--progress');
+ p.parameters.add(remote);
+ p.options:=[poUsePipes];
+
+ setLength(dummy,1024);
+ p.execute;
+ timeOut:=now;
+ p.closeInput;
+
+ while (timeOut+10/24/60/60 > now) and p.running do begin
+ if p.output.numBytesAvailable>0 then begin
+ p.output.read(dummy[0],length(dummy));
+ timeOut:=now;
+ continue;
+ end;
+ if p.stderr.numBytesAvailable>0 then begin
+ p.stderr.read(dummy[0],length(dummy));
+ timeOut:=now;
+ continue;
+ end;
+ sleep(10);
+ end;
+ p.closeOutput;
+ p.closeStderr;
+ p.free;
+ end;
end;
- if updated then
- refreshExecutable(gitDirs);
setLength(args,0);
+ setLength(dummy,0);
+ upgedatet:=sourceSha512Sum(gitVerzeichnis)<>binarySha512Sum(nummer);
+ fertig:=true;
+end;
+
+var
+ gitUpdateThreads: array of tGitUpdateThread;
+ fertig,upgedatet: boolean;
+ i: longint;
+
+initialization
+ setLength(gitUpdateThreads,2);
+ setLength(gitVerzeichnisse,length(gitUpdateThreads));
+ for i:=0 to length(gitUpdateThreads)-1 do
+ gitUpdateThreads[i]:=tGitUpdateThread.create(i);
+
+ repeat
+ fertig:=true;
+ upgedatet:=false;
+ for i:=0 to length(gitUpdateThreads)-1 do begin
+ fertig:=fertig and gitUpdateThreads[i].fertig;
+ upgedatet:=upgedatet or gitUpdateThreads[i].upgedatet;
+ end;
+ if not fertig then
+ sleep(10);
+ until fertig;
+
+ for i:=0 to length(gitUpdateThreads)-1 do
+ gitUpdateThreads[i].free;
+
+ if upgedatet then
+ refreshExecutable(gitVerzeichnisse);
end.