tighten committed-ref mirror visibility and ctx handling · Entire

tighten committed-ref mirror visibility and ctx handling

c3ae8f5→main·

pfleidi·1mo ago·3 files·+37 added/-3 removed

Three small visibility fixes for the v1.1 mirror best-effort path:

Sessions

3372c9e81e2dView transcript

Changes

3

474 unmodified lines

func mirrorSyncedMetadataBranch(ctx context.Context, repo *git.Repository, branchName string) {
    refs := checkpoint.ResolveCommittedRefs(ctx)
    if !refs.Primary.IsBranch() || branchName != refs.Primary.Short() {
        if !refs.Primary.IsBranch() {
            logging.Debug(ctx, "committed-ref mirror skipped after sync: primary metadata ref is not a branch",
                slog.String("primary", refs.Primary.String()))
            return
        }
        if branchName != refs.Primary.Short() {
            logging.Debug(ctx, "committed-ref mirror skipped after sync: branch name does not match primary",
                slog.String("branch_name", branchName),
                slog.String("primary_short", refs.Primary.Short()))
            return
        }
    }
    MirrorCommittedMetadataRefBestEffort(ctx, repo)
}

// ErrPrimaryMetadataMissing is returned by MirrorCommittedMetadataRef when the // primary ref does not exist yet. Callers can match this sentinel to // distinguish "expected on first use" from a real read failure, and from a // SetReference NotFound on the mirror itself. var ErrPrimaryMetadataMissing = errors.New("primary metadata ref missing")

// MirrorCommittedMetadataRef points the committed-metadata mirror at the primary // ref's tip. No-op when the topology has no mirror. func MirrorCommittedMetadataRef(ctx context.Context, repo *git.Repository, refs checkpoint.CommittedRefs) error { primaryRef, err := repo.Reference(refs.Primary, true) if err != nil { if errors.Is(err, plumbing.ErrReferenceNotFound) { return fmt.Errorf("primary metadata ref %s missing: %w", refs.Primary, err) return fmt.Errorf("primary metadata ref %s missing: %w", refs.Primary, ErrPrimaryMetadataMissing) } return fmt.Errorf("read primary metadata ref %s: %w", refs.Primary, err) } }

// MirrorCommittedMetadataRefBestEffort mirrors committed metadata for callers // where mirror failure must not affect the primary operation. // // The mirror runs under context.WithoutCancel so a parent deadline that is // already near-expired (e.g. the 2-minute fetch budget) cannot silently fail // settings.Load and skip the mirror with no log. Trace/value context is // preserved; only cancellation is detached. The mirror itself is short. func MirrorCommittedMetadataRefBestEffort(ctx context.Context, repo *git.Repository) { ctx = context.WithoutCancel(ctx)

refs := checkpoint.ResolveCommittedRefs(ctx) if !refs.HasMirror() { return }

if err := MirrorCommittedMetadataRef(ctx, repo, refs); err != nil { if errors.Is(err, plumbing.ErrReferenceNotFound) { if errors.Is(err, ErrPrimaryMetadataMissing) { // No primary metadata ref yet — nothing to mirror. Expected on first use. logging.Debug(ctx, "committed-ref mirror skipped: primary metadata ref unavailable", slog.String("error", err.Error())) } } } }


// Not parallel: uses t.Chdir().
func TestMirrorCommittedMetadataRef_SetReferenceNotFoundIsNotPrimaryMissing(t *testing.T) {
    repo := setupV1CustomRefRepo(t, `"1.1"`)
    setV1MetadataBranch(t, repo)
    repo.Storer = setReferenceErrorStorer{Storer: repo.Storer, err: plumbing.ErrReferenceNotFound}

err := MirrorCommittedMetadataRef(t.Context(), repo, v1CustomRefsForTest())
    require.Error(t, err)
    require.ErrorIs(t, err, plumbing.ErrReferenceNotFound, "SetReference NotFound should still unwrap to plumbing.ErrReferenceNotFound")
    require.NotErrorIs(t, err, ErrPrimaryMetadataMissing, "SetReference NotFound must not be mistaken for a missing primary")
}

// Not parallel: uses t.Chdir().
func TestMirrorCommittedMetadataRef_SetReferenceErrorNamesTarget(t *testing.T) {
    repo := setupV1CustomRefRepo(t, `"1.1"`)