Skip to content

Commit

Permalink
use thread to make git push and fetch async
Browse files Browse the repository at this point in the history
  • Loading branch information
xulihang committed Jun 5, 2019
1 parent ee74121 commit 3f5491e
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 37 deletions.
36 changes: 18 additions & 18 deletions BasicCAT/Project.bas
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -549,15 +548,15 @@ 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")
If File.Exists(refsPath,"remotes") Then
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")
Expand All @@ -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")
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion BasicCAT/ProjectSettings.bas
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
43 changes: 25 additions & 18 deletions BasicCAT/git.bas
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 3f5491e

Please sign in to comment.