Skip to content

Commit 1297e55

Browse files
committed
add fallback function invocation to retry push if go-git push fails
1 parent fe77431 commit 1297e55

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

git/git_fetch.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/neel1996/gitconvex-server/graph/model"
1212
"github.com/neel1996/gitconvex-server/utils"
1313
"io"
14+
"strings"
1415
)
1516

1617
// windowsFetch is used for fetching changes using the git client if the platform is windows
@@ -53,10 +54,10 @@ func FetchFromRemote(repo *git.Repository, remoteURL string, remoteBranch string
5354
b := new(bytes.Buffer)
5455
var fetchErr error
5556
gitSSHAuth, sshErr := ssh.NewSSHAgentAuth("git")
57+
w, _ := repo.Worktree()
5658

5759
if sshErr != nil {
5860
logger.Log("Authentication method failed -> "+sshErr.Error(), global.StatusError)
59-
w, _ := repo.Worktree()
6061
if w == nil {
6162
return &model.FetchResult{
6263
Status: "FETCH ERROR",
@@ -103,6 +104,10 @@ func FetchFromRemote(repo *git.Repository, remoteURL string, remoteBranch string
103104
FetchedItems: nil,
104105
}
105106
} else {
107+
if strings.Contains(fetchErr.Error(), "ssh: handshake failed: ssh:") {
108+
logger.Log("Fetch failed. Retrying fetch with git client", global.StatusWarning)
109+
return windowsFetch(w.Filesystem.Root(), remoteName, remoteBranch)
110+
}
106111
logger.Log(fetchErr.Error(), global.StatusError)
107112
return &model.FetchResult{
108113
Status: "FETCH ERROR",

git/git_pull.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/neel1996/gitconvex-server/utils"
1313
"go/types"
1414
"io"
15+
"strings"
1516
)
1617

1718
// windowsPull is used for pulling changes using the git client if the platform is windows
@@ -100,6 +101,10 @@ func PullFromRemote(repo *git.Repository, remoteURL string, remoteBranch string)
100101
PulledItems: []*string{&msg},
101102
}
102103
} else {
104+
if strings.Contains(pullErr.Error(), "ssh: handshake failed: ssh:") {
105+
logger.Log("Pull failed. Retrying pull with git client", global.StatusWarning)
106+
return windowsPull(w.Filesystem.Root(), remoteName, remoteBranch)
107+
}
103108
logger.Log(pullErr.Error(), global.StatusError)
104109
return &model.PullResult{
105110
Status: "PULL ERROR",

git/git_push.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/neel1996/gitconvex-server/global"
1111
"github.com/neel1996/gitconvex-server/utils"
1212
"io"
13+
"strings"
1314
)
1415

1516
// windowsPush is used for pushing changes using the git client if the platform is windows
@@ -32,13 +33,14 @@ func windowsPush(repoPath string, remoteName string, branch string) string {
3233
// By default it will choose the current branch and pushes to the matching remote branch
3334
func PushToRemote(repo *git.Repository, remoteName string, remoteBranch string) string {
3435
targetRefPsec := "refs/heads/" + remoteBranch + ":refs/heads/" + remoteBranch
36+
w, _ := repo.Worktree()
37+
3538
b := new(bytes.Buffer)
3639
sshAuth, sshErr := ssh.NewSSHAgentAuth("git")
3740
logger.Log(fmt.Sprintf("Pushing changes to remote -> %s : %s", remoteName, targetRefPsec), global.StatusInfo)
3841

3942
if sshErr != nil {
4043
logger.Log(fmt.Sprintf("Authentication failed -> %s", sshErr.Error()), global.StatusError)
41-
w, _ := repo.Worktree()
4244

4345
if w == nil {
4446
return "PUSH_FAILED"
@@ -63,6 +65,10 @@ func PushToRemote(repo *git.Repository, remoteName string, remoteBranch string)
6365
})
6466

6567
if err != nil {
68+
if strings.Contains(err.Error(), "ssh: handshake failed: ssh:") {
69+
logger.Log("push failed. Retrying push with git client", global.StatusWarning)
70+
return windowsPush(w.Filesystem.Root(), remoteName, remoteBranch)
71+
}
6672
logger.Log(fmt.Sprintf("Error occurred while pushing changes to -> %s : %s\n%s", remoteName, targetRefPsec, err.Error()), global.StatusError)
6773
return "PUSH_FAILED"
6874
} else {

0 commit comments

Comments
 (0)