reuse checkpoint remote for policy · Entire

reuse checkpoint remote for policy

e24caeb· pfleidi·3w ago·5 files·+22 added/-18 removed

Resolve checkpoint policy refs through the existing checkpoint remote fetch resolver.

This keeps user commands and hooks on one repo-wide policy target instead of threading the current push remote into policy resolution.

Sessions

9367989f9e6dView transcript

Changes

5

12 unmodified lines

13
14
15
16
17
16
17
18
13 unmodified lines

32
33
34
37
38
39
40
35
36
37
38
39
45
40
41
42
47
48
49
50
51
52
43
44
45

12 unmodified lines

"github.com/go-git/go-git/v6/plumbing/object"
)

const defaultBaseRemote = "origin"

const (
    sha1HexSize   = 40
    sha256HexSize = 64

)

Hash   plumbing.Hash
}

func ResolveTarget(ctx context.Context, baseRemote string) (Target, error) {
    if baseRemote == "" {
        baseRemote = defaultBaseRemote
    }
func ResolveTarget(ctx context.Context) (Target, error) {
    dir, err := paths.WorktreeRoot(ctx)
    if err != nil {
        return Target{}, fmt.Errorf("resolve worktree root: %w", err)
    }
    if target, dedicated, err := remote.ConfiguredURL(ctx, baseRemote, dir); err != nil {
        target, err := remote.FetchURL(ctx, remote.FetchURLOptions{WorktreeRoot: dir})
        if err != nil {
            return Target{}, fmt.Errorf("resolve checkpoint remote URL: %w", err)
        } else if dedicated {
            return Target{Remote: target, Dir: dir}, nil
        }
        target, _, err := remote.PushURL(ctx, baseRemote)
        if err != nil {
            return Target{}, fmt.Errorf("resolve checkpoint push URL: %w", err)
        }
        return Target{Remote: target, Dir: dir}, nil
    }
}

Mcmd/entire/cli/checkpointpolicy/remote.go+3/-13

127 unmodified lines

128
129
130
131
131
132
133
134
3 unmodified lines

138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157

127 unmodified lines

t.Chdir(localDir)
    paths.ClearWorktreeRootCache()

target, err := checkpointpolicy.ResolveTarget(t.Context(), "origin")
target, err := checkpointpolicy.ResolveTarget(t.Context())
require.NoError(t, err)
require.Equal(t, "git@github.com:org/checkpoints.git", target.Remote)
wantDir, err := filepath.EvalSymlinks(localDir)
3 unmodified lines

require.Equal(t, wantDir, gotDir)
}

func TestResolveTargetUsesFetchURLPolicyTarget(t *testing.T) {
    localDir, _ := initPolicyRepoWithDir(t)
    upstreamDir := filepath.Join(t.TempDir(), "upstream.git")
    _, err := git.PlainInit(upstreamDir, true)
    require.NoError(t, err)
    runPolicyGit(t, localDir, "remote", "add", "upstream", upstreamDir)

t.Chdir(localDir)
    paths.ClearWorktreeRootCache()

_, err = checkpointpolicy.ResolveTarget(t.Context())
    require.ErrorContains(t, err, "no fetch URL found")
}

func initPolicyRemoteFixture(t *testing.T) (string, *git.Repository, string) {
    t.Helper()
    localDir, repo := initPolicyRepoWithDir(t)

Mcmd/entire/cli/checkpointpolicy/remote_test.go+15/-1

38 unmodified lines

39
40
41
42
42
43
44
45

38 unmodified lines

}
    defer repo.Close()

target, err := checkpointpolicy.ResolveTarget(ctx, "origin")
target, err := checkpointpolicy.ResolveTarget(ctx)
    if err != nil {
        return fmt.Errorf("resolve checkpoint policy remote: %w", err)
    }

Mcmd/entire/cli/policy_checkpoint.go+1/-1

27 unmodified lines

28
29
30
31
31
32
33
34
3 unmodified lines

38
39
40
41
41
42
43
44

27 unmodified lines

return false
}

func syncCheckpointPolicyForPrePush(ctx context.Context, remoteName string) bool {
func syncCheckpointPolicyForPrePush(ctx context.Context) bool {
    repo, err := OpenRepository(ctx)
    if err != nil {
        logging.Warn(ctx, "checkpoint policy pre-push: failed to open repository; allowing checkpoint push",
3 unmodified lines

}
    defer repo.Close()

target, err := checkpointpolicy.ResolveTarget(ctx, remoteName)
target, err := checkpointpolicy.ResolveTarget(ctx)
    if err != nil {
        logging.Warn(ctx, "checkpoint policy pre-push: failed to resolve policy remote; allowing checkpoint push",
        slog.String("error", err.Error()),

Mcmd/entire/cli/strategy/checkpoint_policy.go+2/-2

43 unmodified lines

44
45
46
47
47
48
49
50

43 unmodified lines

}

refs := checkpoint.ResolveCommittedRefs(ctx)
    if !syncCheckpointPolicyForPrePush(ctx, remote) {
    if !syncCheckpointPolicyForPrePush(ctx) {
        return nil
    }

Mcmd/entire/cli/strategy/manual_commit_push.go+1/-1