Skip to content

Commit e77fae1

Browse files
committed
pkg/vcs: don't re-clone on network errors
There's no sense to react to `git checkout` or `git fetch` which failed due to network problems by re-cloning the whole repository - that operation will fail just as well. Detect at least one kind of such problems and just return an error from the Poll() method, without wiping everything out. For now, don't add tests as we would need some real remote git server implementation to properly test it. Using a folder as a "remote" repository, like we do in other tests, won't ever trigger networking errors. Closes #6099.
1 parent a568613 commit e77fae1

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

pkg/vcs/git.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,11 @@ func (git *gitRepo) Poll(repo, branch string) (*Commit, error) {
8484
return nil, err
8585
}
8686
}
87-
if _, err := git.Run("fetch", "--force"); err != nil {
88-
// Something else is wrong, re-clone.
87+
if output, err := git.Run("fetch", "--force"); err != nil {
88+
if git.isNetworkError(output) {
89+
// The clone operation will fail as well, so no sense to re-clone.
90+
return nil, err
91+
}
8992
if err := git.clone(repo, branch); err != nil {
9093
return nil, err
9194
}
@@ -99,6 +102,11 @@ func (git *gitRepo) Poll(repo, branch string) (*Commit, error) {
99102
return git.Commit(HEAD)
100103
}
101104

105+
func (git *gitRepo) isNetworkError(output []byte) bool {
106+
// The list is not exhaustive and is meant to be extended over time.
107+
return bytes.Contains(output, []byte("fatal: read error: Connection reset by peer"))
108+
}
109+
102110
func (git *gitRepo) CheckoutBranch(repo, branch string) (*Commit, error) {
103111
if err := git.repair(); err != nil {
104112
return nil, err

0 commit comments

Comments
 (0)