From 3f5491e43c60ea5d14e196eef9310953a3b19e29 Mon Sep 17 00:00:00 2001 From: xulihang Date: Wed, 5 Jun 2019 17:11:50 +0800 Subject: [PATCH] use thread to make git push and fetch async --- BasicCAT/Project.bas | 36 +++++++++++++++--------------- BasicCAT/ProjectSettings.bas | 2 +- BasicCAT/git.bas | 43 +++++++++++++++++++++--------------- 3 files changed, 44 insertions(+), 37 deletions(-) diff --git a/BasicCAT/Project.bas b/BasicCAT/Project.bas index 457c3ef..f2d800f 100644 --- a/BasicCAT/Project.bas +++ b/BasicCAT/Project.bas @@ -462,8 +462,8 @@ Public Sub initAndPush fx.Msgbox(Main.MainForm,"Please configure your git account info first.","") Return End If - wait for (projectGit.push(username,password,"origin","master")) complete (result As String) - If result.StartsWith("error") Then + wait for (projectGit.pushAsync(username,password,"origin","master")) complete (result As Boolean) + If result=False Then fx.Msgbox(Main.MainForm,"Push Failed","") End If End Sub @@ -497,7 +497,7 @@ Public Sub commitAndPush(commitMessage As String) Main.enableAutosaveTimer(False) Main.updateOperation("commiting and pushing") - + Sleep(0) If projectGit.isConflicting Then Log("conflicting") @@ -508,14 +508,14 @@ Public Sub commitAndPush(commitMessage As String) Else projectGit.add(".") projectGit.rebase("CONTINUE","") - wait for (projectGit.push(username,password,"origin","master")) complete (result As String) - If result.StartsWith("error") Then + wait for (projectGit.pushAsync(username,password,"origin","master")) complete (pushResult As Boolean) + If pushResult=False Then fx.Msgbox(Main.MainForm,"Failed","") Return End If End If Else - updateLocalFileBasedonFetch(username,password,email) + wait for (updateLocalFileBasedonFetch(username,password,email)) Complete (success as Object) Dim diffList As List diffList=projectGit.diffList Log(diffList) @@ -524,20 +524,19 @@ Public Sub commitAndPush(commitMessage As String) projectGit.commit(commitMessage,username,email) End If - - If samelocalHeadAndRemoteHead(username,password,False)=False Then - wait for (projectGit.pullRebase(username,password)) complete (result As String) - + wait for (samelocalHeadAndRemoteHead(username,password,False)) Complete (isSame As Boolean) + If isSame = False Then + Dim rebaseResult As String - rebaseResult=result + rebaseResult=projectGit.pullRebase(username,password) Log("rebaseResult"&rebaseResult) If rebaseResult="STOPPED" Or rebaseResult="CONFLICTS" Then fx.Msgbox(Main.MainForm,"Conflits exist. Please solve the conflicts first.","") closeFile Return Else - wait for (projectGit.push(username,password,"origin","master")) complete (result As String) - If result.StartsWith("error") Then + wait for (projectGit.pushAsync(username,password,"origin","master")) complete (pushResult As Boolean) + If pushResult=False Then fx.Msgbox(Main.MainForm,"Push Failed","") Return End If @@ -549,7 +548,7 @@ Public Sub commitAndPush(commitMessage As String) Main.updateOperation("committed") End Sub -Sub samelocalHeadAndRemoteHead(username As String,password As String,fetch As Boolean) As Boolean +Sub samelocalHeadAndRemoteHead(username As String,password As String,fetch As Boolean) As ResumableSub Dim result As Boolean=True Dim refsPath As String refsPath=File.Combine(File.Combine(path,".git"),"refs") @@ -557,7 +556,7 @@ Sub samelocalHeadAndRemoteHead(username As String,password As String,fetch As Bo Dim previousRemoteHead As String previousRemoteHead=projectGit.getCommitIDofBranch("refs/remotes/origin/master") If fetch Then - projectGit.fetch(username,password) + wait for (projectGit.fetchAsync(username,password)) Complete (success As Object) End If Dim localHead,remoteHead As String localHead=projectGit.getCommitIDofBranch("refs/heads/master") @@ -576,8 +575,9 @@ Sub samelocalHeadAndRemoteHead(username As String,password As String,fetch As Bo Return result End Sub -Sub updateLocalFileBasedonFetch(username As String,password As String,email As String) - If samelocalHeadAndRemoteHead(username,password,True)=False Then +Sub updateLocalFileBasedonFetch(username As String,password As String,email As String) as ResumableSub + wait for (samelocalHeadAndRemoteHead(username,password,True)) Complete (isSame As Boolean) + If isSame = False Then Dim localHead,remoteHead As String localHead=projectGit.getCommitIDofBranch("refs/heads/master") remoteHead=projectGit.getCommitIDofBranch("refs/remotes/origin/master") @@ -629,7 +629,7 @@ Sub updateLocalFileBasedonFetch(username As String,password As String,email As S projectGit.add(".") projectGit.commit("sync",username,email) End If - wait for (projectGit.push(username,password,"origin","master")) complete (result As String) + wait for (projectGit.pushAsync(username,password,"origin","master")) complete (result As Boolean) Log("pushresult"&result) End If projectGit.unsetWorkdir diff --git a/BasicCAT/ProjectSettings.bas b/BasicCAT/ProjectSettings.bas index da8f827..f6daaad 100644 --- a/BasicCAT/ProjectSettings.bas +++ b/BasicCAT/ProjectSettings.bas @@ -387,7 +387,7 @@ End Sub Sub setRemoteButton_MouseClicked (EventData As MouseEvent) Main.currentProject.setGitRemoteAndPush(GitURITextField.Text) - Sleep(2000) + 'Sleep(2000) fx.Msgbox(frm,"Done","") End Sub diff --git a/BasicCAT/git.bas b/BasicCAT/git.bas index 093749b..8ba5538 100644 --- a/BasicCAT/git.bas +++ b/BasicCAT/git.bas @@ -8,10 +8,12 @@ Sub Class_Globals Private fx As JFX Private gitJO As JavaObject Private gitJOStatic As JavaObject + Private th As Thread End Sub 'Initializes the object. You can add parameters to this method if needed. Public Sub Initialize(path As String) + th.Initialise("th") gitJOStatic.InitializeStatic("org.eclipse.jgit.api.Git") If File.Exists(path,".git")=False Then Log("init") @@ -57,6 +59,12 @@ Sub setCredentialProvider(username As String,password As String) As JavaObject Return cp End Sub +Public Sub fetchAsync(username As String,password As String) As ResumableSub + th.Start(Me,"fetch",Array As Object(username,password)) + wait for th_Ended(endedOK As Boolean, error As String) + Return endedOK +End Sub + Public Sub fetch(username As String,password As String) Dim fetchCommand As JavaObject fetchCommand=gitJO.RunMethodJO("fetch",Null) @@ -109,8 +117,7 @@ Public Sub checkoutAllFiles(name As String,startpoint As String) checkoutCommand.RunMethod("call",Null) End Sub -Public Sub pullRebase(username As String,password As String) As ResumableSub - Sleep(0) +Public Sub pullRebase(username As String,password As String) As String Try Dim pullCommand As JavaObject pullCommand=gitJO.RunMethodJO("pull",Null) @@ -144,23 +151,23 @@ Public Sub addRemote(urlString As String,name As String) RemoteAddCommand.RunMethodJO("call",Null) End Sub +Public Sub pushAsync(username As String,password As String,remoteName As String,branchName As String) As ResumableSub + th.Start(Me,"push",Array As Object(username,password,remoteName,branchName)) + wait for th_Ended(endedOK As Boolean, error As String) + Return endedOK +End Sub + Public Sub push(username As String,password As String,remoteName As String,branchName As String) - Try - Dim PushCommand As JavaObject - PushCommand=gitJO.RunMethodJO("push",Null) - PushCommand.RunMethodJO("setRemote",Array(remoteName)) - PushCommand.RunMethodJO("add",Array(branchName)) - If username<>"" Then - Dim cp As JavaObject - cp=setCredentialProvider(username,password) - PushCommand.RunMethod("setCredentialsProvider",Array(cp)) - End If - PushCommand.RunMethodJo("call",Null) - Catch - Log(LastException) - Return "error"&LastException.Message - End Try - Return "success" + Dim PushCommand As JavaObject + PushCommand=gitJO.RunMethodJO("push",Null) + PushCommand.RunMethodJO("setRemote",Array(remoteName)) + PushCommand.RunMethodJO("add",Array(branchName)) + If username<>"" Then + Dim cp As JavaObject + cp=setCredentialProvider(username,password) + PushCommand.RunMethod("setCredentialsProvider",Array(cp)) + End If + PushCommand.RunMethodJo("call",Null) End Sub Public Sub getStatus