fix(worktree): rebuild the repo mirror when a refresh fails on a corrupt mirror (#1831)

This commit is contained in:
sam committed 2026-11-09 09:41:39 +11:00
1 parent c98666d6b7
commit 8e5d4e8061
1 file changed
+18 -3
+18 -3
View File
@@ -206,17 +206,32 @@ func (m *Manager) ensureMirror(ctx context.Context, owner, repo string) (string,
}
return dir, nil
}
if err := m.fetchMirror(ctx, dir); err != nil {
if ctx.Err() != nil {
return "", fmt.Errorf("refresh mirror %s/%s: %w", owner, repo, err)
}
// A fetch cut short (full disk) can leave a corrupt ref that fails every
// later fetch. The mirror is a cache and createMirror swaps it in only
// after a complete fetch, so rebuild it.
if cerr := m.createMirror(ctx, dir, url); cerr != nil {
return "", fmt.Errorf("refresh mirror %s/%s: %w; rebuild: %w", owner, repo, err, cerr)
}
}
return dir, nil
}
func (m *Manager) fetchMirror(ctx context.Context, dir string) error {
r, err := openRepo(dir)
if err != nil {
return "", fmt.Errorf("open mirror %s/%s: %w", owner, repo, err)
return fmt.Errorf("open: %w", err)
}
// Force: the mirror copies the forge, so a moved tag or rewritten branch
// replaces the local ref instead of failing every later fetch.
if err := r.FetchContext(ctx, &git.FetchOptions{ClientOptions: m.auth(), Tags: plumbing.AllTags, Force: true}); err != nil &&
!errors.Is(err, git.NoErrAlreadyUpToDate) {
return "", fmt.Errorf("fetch mirror %s/%s: %w", owner, repo, err)
return fmt.Errorf("fetch: %w", err)
}
return dir, nil
return nil
}
// createMirror builds a bare repository tracking every branch of url. It is