From 6bbe6c3968040153f08ea9835889e151971466d7 Mon Sep 17 00:00:00 2001 From: Ibrahim Serdar Acikgoz Date: Wed, 2 Jan 2019 22:34:19 +0300 Subject: add failover to pull/fetch --- pkg/git/cmd-fetch.go | 2 +- pkg/git/cmd-pull.go | 14 +++++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/pkg/git/cmd-fetch.go b/pkg/git/cmd-fetch.go index 2c66c1c..6090f7c 100644 --- a/pkg/git/cmd-fetch.go +++ b/pkg/git/cmd-fetch.go @@ -152,7 +152,7 @@ func fetchWithGoGit(e *RepoEntity, options FetchOptions, refspec string) (err er return ErrAuthenticationRequired } else { log.Warn(err.Error()) - return err + return fetchWithGit(e, options) } } diff --git a/pkg/git/cmd-pull.go b/pkg/git/cmd-pull.go index 8f2cf92..0c3954e 100644 --- a/pkg/git/cmd-pull.go +++ b/pkg/git/cmd-pull.go @@ -9,6 +9,7 @@ import ( "gopkg.in/src-d/go-git.v4/plumbing" "gopkg.in/src-d/go-git.v4/plumbing/transport" "gopkg.in/src-d/go-git.v4/plumbing/transport/http" + "gopkg.in/src-d/go-git.v4/storage/memory" ) var ( @@ -109,9 +110,19 @@ func pullWithGoGit(e *RepoEntity, options PullOptions) (err error) { if err = w.Pull(opt); err != nil { if err == git.NoErrAlreadyUpToDate { + // log.Error("error: " + err.Error()) // Already up-to-date log.Warn(err.Error()) // TODO: submit a PR for this kind of error, this type of catch is lame + } else if err == memory.ErrRefHasChanged && pullTryCount < pullMaxTry { + pullTryCount++ + log.Error("trying to fetch") + if err := Fetch(e, FetchOptions{ + RemoteName: options.RemoteName, + }); err != nil { + return err + } + return Pull(e, options) } else if strings.Contains(err.Error(), "SSH_AUTH_SOCK") { // The env variable SSH_AUTH_SOCK is not defined, maybe git can handle this return pullWithGit(e, options) @@ -120,9 +131,10 @@ func pullWithGoGit(e *RepoEntity, options PullOptions) (err error) { return ErrAuthenticationRequired } else { log.Warn(err.Error()) - return err + return pullWithGit(e, options) } } + e.SetState(Success) return e.Refresh() } -- cgit v1.2.3