fix(worktree): rebuild the mirror and retry once when a push hits a missing object (#1843)

This commit is contained in:
sam committed 2026-11-09 14:37:17 +11:00
1 parent e75d5c8ade
commit 96236b6186
1 file changed
+41 -6
+41 -6
View File
@@ -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