Skip to content

Commit

Permalink
add fallback function to retry pushing changes when it fails due to a…
Browse files Browse the repository at this point in the history
…uthentication in windows (resolves #101)
  • Loading branch information
neel1996 committed Nov 18, 2020
1 parent 73c4ea8 commit 8ff0196
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
25 changes: 24 additions & 1 deletion git/git_push.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,26 @@ import (
"github.com/go-git/go-git/v5/plumbing/protocol/packp/sideband"
"github.com/go-git/go-git/v5/plumbing/transport/ssh"
"github.com/neel1996/gitconvex-server/global"
"github.com/neel1996/gitconvex-server/utils"
"io"
)

// windowsPush is used for pushing changes using the git client if the platform is windows
// go-git push fails in windows due to SSH authentication error
func windowsPush(repoPath string, remoteName string, branch string) string {
args := []string{"push", "-u", remoteName, branch}
cmd := utils.GetGitClient(repoPath, args)
cmdStr, cmdErr := cmd.Output()

if cmdErr != nil {
logger.Log(fmt.Sprintf("Push failed -> %s", cmdErr.Error()), global.StatusError)
return "PUSH_FAILED"
} else {
logger.Log(fmt.Sprintf("Changes pushed to remote -> %s", cmdStr), global.StatusInfo)
return "PUSH_SUCCESS"
}
}

// PushToRemote pushed the commits to the selected remote repository
// By default it will choose the current branch and pushes to the matching remote branch
func PushToRemote(repo *git.Repository, remoteName string, remoteBranch string) string {
Expand All @@ -21,7 +38,13 @@ func PushToRemote(repo *git.Repository, remoteName string, remoteBranch string)

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

if w == nil {
return "PUSH_FAILED"
}
logger.Log("Falling back to native git client for pushing changes", global.StatusWarning)
return windowsPush(w.Filesystem.Root(), remoteName, remoteBranch)
}

remote, remoteErr := repo.Remote(remoteName)
Expand Down
5 changes: 3 additions & 2 deletions graph/schema.resolvers.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,12 @@ func (r *mutationResolver) PushToRemote(ctx context.Context, repoID string, remo
repoChan := make(chan git.RepoDetails)
go git.Repo(repoID, repoChan)
repo := <-repoChan
if head, _ := repo.GitRepo.Head(); repo.GitRepo == nil || head == nil {
remoteName := git.GetRemoteName(repo.GitRepo, remoteHost)

if head, _ := repo.GitRepo.Head(); repo.GitRepo == nil || head == nil || remoteName == "" {
return "PUSH_FAILED", nil
}

remoteName := git.GetRemoteName(repo.GitRepo, remoteHost)
return git.PushToRemote(repo.GitRepo, remoteName, branch), nil
}

Expand Down

0 comments on commit 8ff0196

Please sign in to comment.