diff --git a/internal/worktree/worktree.go b/internal/worktree/worktree.go index 0491f5925..cbd27e411 100644 --- a/internal/worktree/worktree.go +++ b/internal/worktree/worktree.go @@ -231,6 +231,28 @@ func (m *Manager) ensureMirror(ctx context.Context, owner, repo string) (string, return dir, nil } +// rebuildMirror recreates an existing mirror from the URL it tracks. +func (m *Manager) rebuildMirror(ctx context.Context, dir string) error { + r, err := openRepo(dir) + if err != nil { + return fmt.Errorf("open: %w", err) + } + cfg, err := r.Config() + if err != nil { + return err + } + remote, ok := cfg.Remotes[git.DefaultRemoteName] + if !ok || len(remote.URLs) == 0 { + return errors.New("mirror has no remote") + } + unlock, err := lockMirror(dir, true) + if err != nil { + return err + } + defer unlock() + return m.createMirror(ctx, dir, remote.URLs[0]) +} + func (m *Manager) fetchMirror(ctx context.Context, dir string) error { r, err := openRepo(dir) if err != nil { @@ -686,12 +708,25 @@ func (m *Manager) push(ctx context.Context, dir, branch, remoteURL string) error return fmt.Errorf("attest %s: %w", branch, err) } spec := gitconfig.RefSpec(fmt.Sprintf("%s:%s", ref, ref)) - if err := r.PushContext(ctx, &git.PushOptions{ - RemoteName: git.DefaultRemoteName, - RemoteURL: remoteURL, - RefSpecs: []gitconfig.RefSpec{spec}, - ClientOptions: m.auth(), - }); err != nil && !errors.Is(err, git.NoErrAlreadyUpToDate) { + publish := func() error { + return r.PushContext(ctx, &git.PushOptions{ + RemoteName: git.DefaultRemoteName, + RemoteURL: remoteURL, + RefSpecs: []gitconfig.RefSpec{spec}, + ClientOptions: m.auth(), + }) + } + err = publish() + if errors.Is(err, plumbing.ErrObjectNotFound) { + // The clone borrows objects from the shared mirror; a damaged mirror + // loses them. The forge still has every base object, so rebuild it. + m.logger().WarnContext(ctx, "push hit a missing object, rebuilding the mirror", "branch", branch, "err", err) + if rerr := m.rebuildMirror(ctx, filepath.Join(filepath.Dir(dir), mirrorName)); rerr != nil { + return fmt.Errorf("push %s: %w; rebuild mirror: %w", branch, err, rerr) + } + err = publish() + } + if err != nil && !errors.Is(err, git.NoErrAlreadyUpToDate) { return fmt.Errorf("push %s: %w", branch, err) } // The tracking ref marks what is published, so the next attest leaves