From e77fae1501ac8002d344f33dfea3a7b8444d74fc Mon Sep 17 00:00:00 2001 From: Aleksandr Nogikh Date: Mon, 16 Jun 2025 19:10:37 +0200 Subject: 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. --- pkg/vcs/git.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'pkg/vcs') diff --git a/pkg/vcs/git.go b/pkg/vcs/git.go index ef46259f3..72b4a32e6 100644 --- a/pkg/vcs/git.go +++ b/pkg/vcs/git.go @@ -84,8 +84,11 @@ func (git *gitRepo) Poll(repo, branch string) (*Commit, error) { return nil, err } } - if _, err := git.Run("fetch", "--force"); err != nil { - // Something else is wrong, re-clone. + if output, err := git.Run("fetch", "--force"); err != nil { + if git.isNetworkError(output) { + // The clone operation will fail as well, so no sense to re-clone. + return nil, err + } if err := git.clone(repo, branch); err != nil { return nil, err } @@ -99,6 +102,11 @@ func (git *gitRepo) Poll(repo, branch string) (*Commit, error) { return git.Commit(HEAD) } +func (git *gitRepo) isNetworkError(output []byte) bool { + // The list is not exhaustive and is meant to be extended over time. + return bytes.Contains(output, []byte("fatal: read error: Connection reset by peer")) +} + func (git *gitRepo) CheckoutBranch(repo, branch string) (*Commit, error) { if err := git.repair(); err != nil { return nil, err -- cgit mrf-deployment