Skip to content

Commit 3f5491e

Browse files
committed
use thread to make git push and fetch async
1 parent ee74121 commit 3f5491e

File tree

3 files changed

+44
-37
lines changed

3 files changed

+44
-37
lines changed

BasicCAT/Project.bas

+18-18
Original file line numberDiff line numberDiff line change
@@ -462,8 +462,8 @@ Public Sub initAndPush
462462
fx.Msgbox(Main.MainForm,"Please configure your git account info first.","")
463463
Return
464464
End If
465-
wait for (projectGit.push(username,password,"origin","master")) complete (result As String)
466-
If result.StartsWith("error") Then
465+
wait for (projectGit.pushAsync(username,password,"origin","master")) complete (result As Boolean)
466+
If result=False Then
467467
fx.Msgbox(Main.MainForm,"Push Failed","")
468468
End If
469469
End Sub
@@ -497,7 +497,7 @@ Public Sub commitAndPush(commitMessage As String)
497497

498498
Main.enableAutosaveTimer(False)
499499
Main.updateOperation("commiting and pushing")
500-
500+
Sleep(0)
501501

502502
If projectGit.isConflicting Then
503503
Log("conflicting")
@@ -508,14 +508,14 @@ Public Sub commitAndPush(commitMessage As String)
508508
Else
509509
projectGit.add(".")
510510
projectGit.rebase("CONTINUE","")
511-
wait for (projectGit.push(username,password,"origin","master")) complete (result As String)
512-
If result.StartsWith("error") Then
511+
wait for (projectGit.pushAsync(username,password,"origin","master")) complete (pushResult As Boolean)
512+
If pushResult=False Then
513513
fx.Msgbox(Main.MainForm,"Failed","")
514514
Return
515515
End If
516516
End If
517517
Else
518-
updateLocalFileBasedonFetch(username,password,email)
518+
wait for (updateLocalFileBasedonFetch(username,password,email)) Complete (success as Object)
519519
Dim diffList As List
520520
diffList=projectGit.diffList
521521
Log(diffList)
@@ -524,20 +524,19 @@ Public Sub commitAndPush(commitMessage As String)
524524
projectGit.commit(commitMessage,username,email)
525525
End If
526526

527-
528-
If samelocalHeadAndRemoteHead(username,password,False)=False Then
529-
wait for (projectGit.pullRebase(username,password)) complete (result As String)
530-
527+
wait for (samelocalHeadAndRemoteHead(username,password,False)) Complete (isSame As Boolean)
528+
If isSame = False Then
529+
531530
Dim rebaseResult As String
532-
rebaseResult=result
531+
rebaseResult=projectGit.pullRebase(username,password)
533532
Log("rebaseResult"&rebaseResult)
534533
If rebaseResult="STOPPED" Or rebaseResult="CONFLICTS" Then
535534
fx.Msgbox(Main.MainForm,"Conflits exist. Please solve the conflicts first.","")
536535
closeFile
537536
Return
538537
Else
539-
wait for (projectGit.push(username,password,"origin","master")) complete (result As String)
540-
If result.StartsWith("error") Then
538+
wait for (projectGit.pushAsync(username,password,"origin","master")) complete (pushResult As Boolean)
539+
If pushResult=False Then
541540
fx.Msgbox(Main.MainForm,"Push Failed","")
542541
Return
543542
End If
@@ -549,15 +548,15 @@ Public Sub commitAndPush(commitMessage As String)
549548
Main.updateOperation("committed")
550549
End Sub
551550

552-
Sub samelocalHeadAndRemoteHead(username As String,password As String,fetch As Boolean) As Boolean
551+
Sub samelocalHeadAndRemoteHead(username As String,password As String,fetch As Boolean) As ResumableSub
553552
Dim result As Boolean=True
554553
Dim refsPath As String
555554
refsPath=File.Combine(File.Combine(path,".git"),"refs")
556555
If File.Exists(refsPath,"remotes") Then
557556
Dim previousRemoteHead As String
558557
previousRemoteHead=projectGit.getCommitIDofBranch("refs/remotes/origin/master")
559558
If fetch Then
560-
projectGit.fetch(username,password)
559+
wait for (projectGit.fetchAsync(username,password)) Complete (success As Object)
561560
End If
562561
Dim localHead,remoteHead As String
563562
localHead=projectGit.getCommitIDofBranch("refs/heads/master")
@@ -576,8 +575,9 @@ Sub samelocalHeadAndRemoteHead(username As String,password As String,fetch As Bo
576575
Return result
577576
End Sub
578577

579-
Sub updateLocalFileBasedonFetch(username As String,password As String,email As String)
580-
If samelocalHeadAndRemoteHead(username,password,True)=False Then
578+
Sub updateLocalFileBasedonFetch(username As String,password As String,email As String) as ResumableSub
579+
wait for (samelocalHeadAndRemoteHead(username,password,True)) Complete (isSame As Boolean)
580+
If isSame = False Then
581581
Dim localHead,remoteHead As String
582582
localHead=projectGit.getCommitIDofBranch("refs/heads/master")
583583
remoteHead=projectGit.getCommitIDofBranch("refs/remotes/origin/master")
@@ -629,7 +629,7 @@ Sub updateLocalFileBasedonFetch(username As String,password As String,email As S
629629
projectGit.add(".")
630630
projectGit.commit("sync",username,email)
631631
End If
632-
wait for (projectGit.push(username,password,"origin","master")) complete (result As String)
632+
wait for (projectGit.pushAsync(username,password,"origin","master")) complete (result As Boolean)
633633
Log("pushresult"&result)
634634
End If
635635
projectGit.unsetWorkdir

BasicCAT/ProjectSettings.bas

+1-1
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ End Sub
387387

388388
Sub setRemoteButton_MouseClicked (EventData As MouseEvent)
389389
Main.currentProject.setGitRemoteAndPush(GitURITextField.Text)
390-
Sleep(2000)
390+
'Sleep(2000)
391391
fx.Msgbox(frm,"Done","")
392392
End Sub
393393

BasicCAT/git.bas

+25-18
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@ Sub Class_Globals
88
Private fx As JFX
99
Private gitJO As JavaObject
1010
Private gitJOStatic As JavaObject
11+
Private th As Thread
1112
End Sub
1213

1314
'Initializes the object. You can add parameters to this method if needed.
1415
Public Sub Initialize(path As String)
16+
th.Initialise("th")
1517
gitJOStatic.InitializeStatic("org.eclipse.jgit.api.Git")
1618
If File.Exists(path,".git")=False Then
1719
Log("init")
@@ -57,6 +59,12 @@ Sub setCredentialProvider(username As String,password As String) As JavaObject
5759
Return cp
5860
End Sub
5961

62+
Public Sub fetchAsync(username As String,password As String) As ResumableSub
63+
th.Start(Me,"fetch",Array As Object(username,password))
64+
wait for th_Ended(endedOK As Boolean, error As String)
65+
Return endedOK
66+
End Sub
67+
6068
Public Sub fetch(username As String,password As String)
6169
Dim fetchCommand As JavaObject
6270
fetchCommand=gitJO.RunMethodJO("fetch",Null)
@@ -109,8 +117,7 @@ Public Sub checkoutAllFiles(name As String,startpoint As String)
109117
checkoutCommand.RunMethod("call",Null)
110118
End Sub
111119

112-
Public Sub pullRebase(username As String,password As String) As ResumableSub
113-
Sleep(0)
120+
Public Sub pullRebase(username As String,password As String) As String
114121
Try
115122
Dim pullCommand As JavaObject
116123
pullCommand=gitJO.RunMethodJO("pull",Null)
@@ -144,23 +151,23 @@ Public Sub addRemote(urlString As String,name As String)
144151
RemoteAddCommand.RunMethodJO("call",Null)
145152
End Sub
146153

154+
Public Sub pushAsync(username As String,password As String,remoteName As String,branchName As String) As ResumableSub
155+
th.Start(Me,"push",Array As Object(username,password,remoteName,branchName))
156+
wait for th_Ended(endedOK As Boolean, error As String)
157+
Return endedOK
158+
End Sub
159+
147160
Public Sub push(username As String,password As String,remoteName As String,branchName As String)
148-
Try
149-
Dim PushCommand As JavaObject
150-
PushCommand=gitJO.RunMethodJO("push",Null)
151-
PushCommand.RunMethodJO("setRemote",Array(remoteName))
152-
PushCommand.RunMethodJO("add",Array(branchName))
153-
If username<>"" Then
154-
Dim cp As JavaObject
155-
cp=setCredentialProvider(username,password)
156-
PushCommand.RunMethod("setCredentialsProvider",Array(cp))
157-
End If
158-
PushCommand.RunMethodJo("call",Null)
159-
Catch
160-
Log(LastException)
161-
Return "error"&LastException.Message
162-
End Try
163-
Return "success"
161+
Dim PushCommand As JavaObject
162+
PushCommand=gitJO.RunMethodJO("push",Null)
163+
PushCommand.RunMethodJO("setRemote",Array(remoteName))
164+
PushCommand.RunMethodJO("add",Array(branchName))
165+
If username<>"" Then
166+
Dim cp As JavaObject
167+
cp=setCredentialProvider(username,password)
168+
PushCommand.RunMethod("setCredentialsProvider",Array(cp))
169+
End If
170+
PushCommand.RunMethodJo("call",Null)
164171
End Sub
165172

166173
Public Sub getStatus

0 commit comments

Comments
 (0)