Merge remote-tracking branch 'origin/main' into fix/1773-warn-logged-out-import · Entire
Merge remote-tracking branch 'origin/main' into fix/1773-warn-logged-out-import
d76186d→main·
karthik-rameshkumar·yesterday·5 files·+955 added/-25 removed
Changes
5
cmd/entire/cli
Msetup.go+22/-2
Asetup_enable_heal_test.go+143
strategy
Mcheckpoint_remote.go+51/-1
Mcheckpoint_remote_test.go+455
Mcommon.go+284/-22
1118 unmodified lines
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
183 unmodified lines
1320
1321
1322
1311
1323
1324
1325
1326
1327
1328
1329
1330
591 unmodified lines
1922
1923
1924
1909
1925
1926
1927
1928
1929
1930
1931
1932
1118 unmodified lines
}
}
// `entire enable` is an explicit, user-initiated recovery point. A repo
// enabled before the checkpoint_remote bootstrap existed may still carry a
// local orphan disjoint from the checkpoint remote (#1374); EnsureSetup with
// the bootstrap flag heals it via EnsurePrimaryRef. This is the only path to
// the heal for a bare `entire enable` (which otherwise short-circuits on the
// already-enabled branch below). EnsureSetup is idempotent and silent on a
// healthy repo (hooks stay installed, gitignore/vercel config already present),
// so this adds only the heal to the already-configured path.
if err := strategy.EnsureSetup(strategy.WithCheckpointRemoteBootstrap(ctx)); err != nil {
return fmt.Errorf("failed to setup strategy: %w", err)
}
// Resolve the target scope first, then decide whether there is anything to
// do. Enable writes to the scope resolved by settingsTargetFile, which is
// also what strategy/checkpoint-backend updates above use. Without this, a
183 unmodified lines
return fmt.Errorf("failed to save settings: %w", err)
}
if err := strategy.EnsureSetup(ctx); err != nil {
// Explicit, user-initiated setup: allow EnsurePrimaryRef to fetch a
// missing primary metadata ref from a configured checkpoint_remote
// (bootstrapPrimaryFromCheckpointRemote is otherwise a no-op — see
// strategy.WithCheckpointRemoteBootstrap).
if err := strategy.EnsureSetup(strategy.WithCheckpointRemoteBootstrap(ctx)); err != nil {
return fmt.Errorf("failed to setup strategy: %w", err)
}
591 unmodified lines
return err
}
if err := strategy.EnsureSetup(ctx); err != nil {
// Explicit, user-initiated setup: allow EnsurePrimaryRef to fetch a
// missing primary metadata ref from a configured checkpoint_remote
// (bootstrapPrimaryFromCheckpointRemote is otherwise a no-op — see
// strategy.WithCheckpointRemoteBootstrap).
if err := strategy.EnsureSetup(strategy.WithCheckpointRemoteBootstrap(ctx)); err != nil {
return fmt.Errorf("failed to setup strategy: %w", err)
}
Mcmd/entire/cli/setup.go+22/-2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package cli
import (
"context"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"github.com/entireio/cli/cmd/entire/cli/paths"
"github.com/entireio/cli/cmd/entire/cli/testutil"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// TestEnableCmd_BareEnableHealsEmptyOrphanFromCheckpointRemote verifies issue
// #1374 finding 4: a bare `entire enable` on an already-configured, already-enabled
// repo must reach the checkpoint_remote heal. That path (runEnableOnConfiguredRepo)
// previously short-circuited on the "already enabled" branch and never called
// EnsureSetup, so a pre-fix empty orphan could never be recovered by the most
// natural recovery command.
// Not parallel: setupTestRepo uses t.Chdir and t.Setenv.
func TestEnableCmd_BareEnableHealsEmptyOrphanFromCheckpointRemote(t *testing.T) {
// Checkpoint remote (device A): holds a real entire/checkpoints/v1 branch.
remoteDir := t.TempDir()
testutil.InitRepo(t, remoteDir)
testutil.WriteFile(t, remoteDir, "f.txt", "init")
testutil.GitAdd(t, remoteDir, "f.txt")
testutil.GitCommit(t, remoteDir, "init")
remoteDefault := healTestCurrentBranch(t, remoteDir)
healTestGit(t, remoteDir, "checkout", "--orphan", paths.MetadataBranchName)
healTestGit(t, remoteDir, "rm", "-rf", ".")
healTestWriteCheckpoint(t, remoteDir, "aaaaaaaaaaaa")
healTestGit(t, remoteDir, "checkout", remoteDefault)
remoteTip := healTestRevParse(t, remoteDir, paths.MetadataBranchName)
// Local repo (device B): configured + enabled with a checkpoint_remote, but
// carrying the pre-#1374 empty orphan. setupTestRepo inits the repo and chdirs
// into it.
setupTestRepo(t)
localDir, err := os.Getwd()
require.NoError(t, err)
testutil.WriteFile(t, localDir, "f.txt", "init")
testutil.GitAdd(t, localDir, "f.txt")
testutil.GitCommit(t, localDir, "init")
localDefault := healTestCurrentBranch(t, localDir)
// SSH origin so remote.FetchURL derives the github checkpoint URL.
healTestGit(t, localDir, "remote", "add", "origin", "git@github.com:org/main-repo.git")
healTestGit(t, localDir, "checkout", "--orphan", paths.MetadataBranchName)
healTestGit(t, localDir, "rm", "-rf", ".")
healTestGit(t, localDir, "commit", "--allow-empty", "-m", "Initialize metadata ref")
healTestGit(t, localDir, "checkout", localDefault)
writeSettings(t, `{"enabled": true, "strategy_options": {"checkpoint_remote": {"provider": "github", "repo": "org/checkpoints"}}}`)
writeClaudeHooksFixture(t)
// Redirect the derived checkpoint URL to the local checkpoint remote so the
// real fetch path runs hermetically.
healTestRedirectURL(t, localDir, "git@github.com:org/checkpoints.git", "file://"+remoteDir)
paths.ClearWorktreeRootCache()
orphanTip := healTestRevParse(t, localDir, paths.MetadataBranchName)
require.NotEqual(t, remoteTip, orphanTip, "test setup: local orphan must differ from the checkpoint remote tip")
cmd := newEnableCmd()
cmd.SetOut(&strings.Builder{})
cmd.SetErr(&strings.Builder{})
cmd.SetArgs([]string{})
require.NoError(t, cmd.Execute(), "bare enable should succeed")
healedTip := healTestRevParse(t, localDir, paths.MetadataBranchName)
assert.Equal(t, remoteTip, healedTip,
"a bare `entire enable` must heal the empty orphan from the checkpoint remote")
files := healTestMetadataFiles(t, localDir)
assert.Contains(t, files, "aa/aaaaaaaaaa/"+paths.MetadataFileName,
"the healed branch should contain the checkpoint remote data")
}
func healTestGit(t *testing.T, dir string, args ...string) {
t.Helper()
cmd := exec.CommandContext(context.Background(), "git", args...)
cmd.Dir = dir
cmd.Env = testutil.GitIsolatedEnv()
out, err := cmd.CombinedOutput()
require.NoError(t, err, "git %v in %s failed: %s", args, dir, out)
}
func healTestCurrentBranch(t *testing.T, dir string) string {
t.Helper()
cmd := exec.CommandContext(context.Background(), "git", "rev-parse", "--abbrev-ref", "HEAD")
cmd.Dir = dir
cmd.Env = testutil.GitIsolatedEnv()
out, err := cmd.Output()
require.NoError(t, err)
return strings.TrimSpace(string(out))
}
func healTestRevParse(t *testing.T, dir, rev string) string {
t.Helper()
cmd := exec.CommandContext(context.Background(), "git", "rev-parse", rev)
cmd.Dir = dir
cmd.Env = testutil.GitIsolatedEnv()
out, err := cmd.Output()
require.NoError(t, err)
return strings.TrimSpace(string(out))
}
func healTestWriteCheckpoint(t *testing.T, dir, checkpointID string) {
t.Helper()
checkpointDir := filepath.Join(dir, checkpointID[:2], checkpointID[2:])
require.NoError(t, os.MkdirAll(checkpointDir, 0o755))
require.NoError(t, os.WriteFile(
filepath.Join(checkpointDir, paths.MetadataFileName),
[]byte(fmt.Sprintf(`{"checkpoint_id":%q}`, checkpointID)),
0o644,
))
healTestGit(t, dir, "add", ".")
healTestGit(t, dir, "commit", "-m", "Checkpoint: "+checkpointID)
}
func healTestMetadataFiles(t *testing.T, dir string) string {
t.Helper()
cmd := exec.CommandContext(context.Background(), "git", "ls-tree", "-r", "--name-only", "refs/heads/"+paths.MetadataBranchName)
cmd.Dir = dir
cmd.Env = testutil.GitIsolatedEnv()
out, err := cmd.Output()
require.NoError(t, err)
return string(out)
}
func healTestRedirectURL(t *testing.T, repoDir, matchURL, replacementURL string) {
t.Helper()
configPath := filepath.Join(repoDir, ".git", "config")
f, err := os.OpenFile(configPath, os.O_APPEND|os.O_WRONLY, 0o600)
require.NoError(t, err)
defer func() { require.NoError(t, f.Close()) }()
_, err = fmt.Fprintf(f, "\n[url %q]\n\tinsteadOf = %s\n", replacementURL, matchURL)
require.NoError(t, err)
}
Mcmd/entire/cli/setup_enable_heal_test.go+143
130 unmodified lines
131
132
133
134
134
135
136
137
43 unmodified lines
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
130 unmodified lines
// fetches are globally enabled. Use noFilter for operations that need blob
// content (resume, explain) as opposed to sync operations (push recovery)
// that only need tree structure.
func fetchURLIntoTmpRef(ctx context.Context, dir, remoteURL, srcRef, tmpRef, label string, noFilter bool) error {
func fetchURLIntoTmpRef(ctx context.Context, dir, remoteURL, srcRef, tmpRef, label string, noFilter bool) error { //nolint:unparam // noFilter distinguishes blob-content fetches (true) from tree-only sync fetches (false); kept for the documented fetch-filtering contract even though current callers all need blob content
fetchCtx, cancel := context.WithTimeout(ctx, checkpointRemoteFetchTimeout)
defer cancel()
43 unmodified lines
logging.Info(ctx, "checkpoint-remote: fetched metadata branch from URL")
return nil
}
// resolveCheckpointFetchURL resolves the checkpoint_remote fetch URL for the repo
// rooted at worktreeRoot and verifies it actually targets the configured
// checkpoint repository. It returns ok=false (never an error) when no
// checkpoint_remote is configured or a dedicated checkpoint URL cannot be
// resolved, so callers fall back to keeping the local branch / creating an orphan.
//
// remote.FetchURL silently falls back to the origin remote URL in several paths
// (ENTIRE_CHECKPOINT_TOKEN short-circuit, unparseable origin, non-derivable origin
// protocol). Adopting from origin would contradict the rule that origin is never
// authoritative when a checkpoint_remote is configured (issue #1374), so a resolved
// URL that does not target the configured checkpoint repo is treated as unresolved.
func resolveCheckpointFetchURL(ctx context.Context, worktreeRoot string) (string, bool) {
s, err := settings.Load(ctx)
if err != nil {
logging.Debug(ctx, "checkpoint-remote: could not load settings for metadata fetch URL",
slog.String("error", err.Error()))
return "", false
}
config := s.GetCheckpointRemote()
if config == nil {
return "", false
}
url, err := remote.FetchURL(ctx, remote.FetchURLOptions{WorktreeRoot: worktreeRoot})
if err != nil || strings.TrimSpace(url) == "" {
logging.Debug(ctx, "checkpoint-remote: could not resolve fetch URL for metadata bootstrap",
slog.Any("error", err))
return "", false
}
if !urlTargetsCheckpointRepo(url, config) {
logging.Debug(ctx, "checkpoint-remote: fetch URL did not resolve to the configured checkpoint repo; not adopting from origin",
slog.String("repo", config.Repo))
return "", false
}
return url, true
}
// urlTargetsCheckpointRepo reports whether url points at the configured checkpoint
// repository (host-agnostic, case-insensitive owner/repo match). It distinguishes a
// derived checkpoint URL from remote.FetchURL's origin fallback, which targets the
// origin repository instead. A same-repo checkpoint_remote (origin == checkpoint
// repo) still matches, which is correct: adopting from that URL is adopting the
// checkpoint repo.
func urlTargetsCheckpointRepo(url string, config *settings.CheckpointRemoteConfig) bool {
info, err := remote.ParseURL(url)
if err != nil || info.Owner == "" || info.Repo == "" {
return false
}
return strings.EqualFold(info.Owner+"/"+info.Repo, config.Repo)
}
// TestEnsurePrimaryRef_CheckpointRemoteTakesPrecedenceOverOrigin verifies issue
// #1374: when a checkpoint_remote is configured, EnsurePrimaryRef adopts its branch
// even when a stale origin/entire/checkpoints/v1 tracking ref is present.
//
// Not parallel: uses t.Chdir().
func TestEnsurePrimaryRef_CheckpointRemoteTakesPrecedenceOverOrigin(t *testing.T) {
ctx := context.Background()
// Checkpoint remote holds the authoritative checkpoint.
checkpointRemoteDir := t.TempDir()
testutil.InitRepo(t, checkpointRemoteDir)
testutil.WriteFile(t, checkpointRemoteDir, "f.txt", "init")
testutil.GitAdd(t, checkpointRemoteDir, "f.txt")
testutil.GitCommit(t, checkpointRemoteDir, "init")
cpDefault := checkpointRemoteCurrentBranch(ctx, t, checkpointRemoteDir)
runCheckpointRemoteGit(ctx, t, checkpointRemoteDir, "checkout", "--orphan", paths.MetadataBranchName)
runCheckpointRemoteGit(ctx, t, checkpointRemoteDir, "rm", "-rf", ".")
commitCheckpointRemoteMetadata(ctx, t, checkpointRemoteDir, "aaaaaaaaaaaa", "authoritative")
runCheckpointRemoteGit(ctx, t, checkpointRemoteDir, "checkout", cpDefault)
cpTip := checkpointRemoteRevParse(ctx, t, checkpointRemoteDir, paths.MetadataBranchName)
// Local repo (device B): fetch origin so a stale origin tracking ref exists,
// then repoint origin at an SSH URL so FetchURL derives the github checkpoint
// URL. The stale tracking ref and its objects survive the set-url.
localDir := t.TempDir()
testutil.InitRepo(t, localDir)
testutil.WriteFile(t, localDir, "f.txt", "init")
testutil.GitAdd(t, localDir, "f.txt")
testutil.GitCommit(t, localDir, "init")
runCheckpointRemoteGit(ctx, t, localDir, "remote", "add", "origin", "file://"+originDir)
runCheckpointRemoteGit(ctx, t, localDir, "fetch", "origin")
runCheckpointRemoteGit(ctx, t, localDir, "remote", "set-url", "origin", "git@github.com:org/main-repo.git")
entireDir := filepath.Join(localDir, ".entire")
require.NoError(t, os.MkdirAll(entireDir, 0o755))
require.NoError(t, os.WriteFile(
filepath.Join(entireDir, paths.SettingsFileName),
[]byte(`{"enabled": true, "strategy_options": {"checkpoint_remote": {"provider": "github", "repo": "org/checkpoints"}}}`),
0o644,
))
// The SSH origin + github checkpoint_remote resolves (via remote.FetchURL) to
// git@github.com:org/checkpoints.git. Redirect that derived URL to the local
// checkpoint remote so the real fetch path runs hermetically.
redirectGitURL(t, localDir, "git@github.com:org/checkpoints.git", "file://"+checkpointRemoteDir)
}
// TestURLTargetsCheckpointRepo verifies the issue #1374 guard that distinguishes a
// derived checkpoint URL from remote.FetchURL's silent origin fallback: only URLs
// whose owner/repo match the configured checkpoint repo (host-agnostic,
// case-insensitive) are accepted.
func TestURLTargetsCheckpointRepo(t *testing.T) {
t.Parallel()
config := &settings.CheckpointRemoteConfig{Provider: "github", Repo: "org/checkpoints"}
tests := []struct {
name string
url string
want bool
}{
{"HTTPS checkpoint repo", "https://github.com/org/checkpoints.git", true},
{"SSH checkpoint repo", "git@github.com:org/checkpoints.git", true},
{"enterprise host, same repo path", "https://github.example.com/org/checkpoints.git", true},
{"case-insensitive owner/repo", "https://github.com/Org/Checkpoints.git", true},
{"origin fallback (different repo)", "https://github.com/org/main-repo.git", false},
{"different owner", "https://github.com/other/checkpoints.git", false},
{"unparseable url", "not a url", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
assert.Equal(t, tt.want, urlTargetsCheckpointRepo(tt.url, config))
})
}
}
// TestEnsurePrimaryRef_ReplacesExistingEmptyOrphanFromCheckpointRemote verifies
// that a *pre-existing* local empty orphan (the exact shape a pre-#1374
// `entire enable` left behind) is still healed on a later run of an explicit
// setup flow, not just the "local ref missing entirely" case. Origin does not
// track Primary here (checkpoint_remote strategy), so remoteRef is nil and the
// only recovery path is fetching from the configured checkpoint_remote.
func TestEnsurePrimaryRef_ReplacesExistingEmptyOrphanFromCheckpointRemote(t *testing.T) {
ctx := context.Background()
// Checkpoint remote: a repo that already holds entire/checkpoints/v1 with a
// real (non-empty) commit — models the branch created on device A.
checkpointRemoteDir := t.TempDir()
testutil.InitRepo(t, checkpointRemoteDir)
testutil.WriteFile(t, checkpointRemoteDir, "f.txt", "init")
testutil.GitAdd(t, checkpointRemoteDir, "f.txt")
testutil.GitCommit(t, checkpointRemoteDir, "init")
remoteDefaultBranch := checkpointRemoteCurrentBranch(ctx, t, checkpointRemoteDir)
runCheckpointRemoteGit(ctx, t, checkpointRemoteDir, "checkout", "--orphan", paths.MetadataBranchName)
runCheckpointRemoteGit(ctx, t, checkpointRemoteDir, "rm", "-rf", ".")
commitCheckpointRemoteMetadata(ctx, t, checkpointRemoteDir, "aaaaaaaaaaaa", "device-a")
runCheckpointRemoteGit(ctx, t, checkpointRemoteDir, "checkout", remoteDefaultBranch)
remoteTip := checkpointRemoteRevParse(ctx, t, checkpointRemoteDir, paths.MetadataBranchName)
// Local repo (device B): origin points at the main repo and a separate
// checkpoint_remote is configured. Unlike the "missing ref" test above, this
// device already has a local empty orphan on entire/checkpoints/v1 — the
// state left behind by the pre-#1374 `entire enable` before this fix existed.
localDir := t.TempDir()
testutil.InitRepo(t, localDir)
testutil.WriteFile(t, localDir, "f.txt", "init")
testutil.GitAdd(t, localDir, "f.txt")
testutil.GitCommit(t, localDir, "init")
runCheckpointRemoteGit(ctx, t, localDir, "remote", "add", "origin", "git@github.com:org/main-repo.git")
runCheckpointRemoteGit(ctx, t, localDir, "checkout", "--orphan", paths.MetadataBranchName)
runCheckpointRemoteGit(ctx, t, localDir, "rm", "-rf", ".")
runCheckpointRemoteGit(ctx, t, localDir, "commit", "--allow-empty", "-m", "Initialize metadata ref")
runCheckpointRemoteGit(ctx, t, localDir, "checkout", remoteDefaultBranch)
entireDir := filepath.Join(localDir, ".entire")
require.NoError(t, os.MkdirAll(entireDir, 0o755))
require.NoError(t, os.WriteFile(
filepath.Join(entireDir, "settings.json"),
[]byte(`{"enabled": true, "strategy_options": {"checkpoint_remote": {"provider": "github", "repo": "org/checkpoints"}}}`),
0o644,
))
// The SSH origin + github checkpoint_remote resolves (via remote.FetchURL)
// to git@github.com:org/checkpoints.git. Redirect that derived URL to the
// local file:// remote so the real fetch path runs hermetically.
redirectGitURL(t, localDir, "git@github.com:org/checkpoints.git", "file://"+checkpointRemoteDir)
// Sanity: derivation produces the URL we redirected.
url, err := remote.FetchURL(ctx)
require.NoError(t, err)
require.Equal(t, "git@github.com:org/checkpoints.git", url)
repo, err := OpenRepository(ctx)
require.NoError(t, err)
defer repo.Close()
// Only an explicit setup flow (WithCheckpointRemoteBootstrap) is allowed
// to fetch from the checkpoint remote here — see
// TestEnsurePrimaryRef_SkipsCheckpointRemoteBootstrapOutsideEnableFlow for
// the per-turn hot-path behavior.
require.NoError(t, EnsurePrimaryRef(WithCheckpointRemoteBootstrap(ctx), repo))
// The local metadata branch must now match the checkpoint remote's tip,
// not a fresh empty orphan.
localTip := checkpointRemoteRevParse(ctx, t, localDir, paths.MetadataBranchName)
assert.Equal(t, remoteTip, localTip,
"EnsurePrimaryRef must fetch entire/checkpoints/v1 from the configured checkpoint_remote instead of creating an empty orphan")
// And it must carry the real checkpoint data (proving it is not an empty tree).
files := checkpointRemoteMetadataFiles(ctx, t, localDir)
assert.Contains(t, files, "aa/aaaaaaaaaa/"+paths.MetadataFileName,
"the bootstrapped branch should contain the checkpoint committed on the remote")
}
// Other functions...
Mcmd/entire/cli/strategy/checkpoint_remote.go+51/-1
10 unmodified lines
11
12
13
14
15
16
17
18
19
20
21
928 unmodified lines
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
10 unmodified lines
"github.com/entireio/cli/cmd/entire/cli/checkpoint/remote"
"github.com/entireio/cli/cmd/entire/cli/paths"
"github.com/entireio/cli/cmd/entire/cli/settings"
"github.com/entireio/cli/cmd/entire/cli/testutil"
"github.com/entireio/cli/cmd/entire/cli/vercelconfig"
"github.com/go-git/go-git/v6/plumbing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
}
928 unmodified lines
assert.Contains(t, files, "bb/bbbbbbbbbb/metadata.json", "local-only checkpoint should be replayed when there is no common ancestor")
}
// TestEnsurePrimaryRef_HealsVercelOnlyOrphanFromCheckpointRemote verifies the issue
// #1374 heal covers a local metadata branch carrying only vercel.json — the
// orphan-init state in a vercel-enabled repo. A literal empty-tree check skipped it
// (the tree is not empty), leaving those devices divergent forever.
// Not parallel: uses t.Chdir().
func TestEnsurePrimaryRef_HealsVercelOnlyOrphanFromCheckpointRemote(t *testing.T) {
ctx := context.Background()
// Checkpoint remote holds the authoritative checkpoint.
remoteDir := t.TempDir()
testutil.InitRepo(t, remoteDir)
testutil.WriteFile(t, remoteDir, "f.txt", "init")
testutil.GitAdd(t, remoteDir, "f.txt")
testutil.GitCommit(t, remoteDir, "init")
remoteDefault := checkpointRemoteCurrentBranch(ctx, t, remoteDir)
runCheckpointRemoteGit(ctx, t, remoteDir, "checkout", "--orphan", paths.MetadataBranchName)
runCheckpointRemoteGit(ctx, t, remoteDir, "rm", "-rf", ".")
commitCheckpointRemoteMetadata(ctx, t, remoteDir, "aaaaaaaaaaaa", "device-a")
runCheckpointRemoteGit(ctx, t, remoteDir, "checkout", remoteDefault)
remoteTip := checkpointRemoteRevParse(ctx, t, remoteDir, paths.MetadataBranchName)
// Local repo (device B): a local orphan carrying only vercel.json — the
// vercel-enabled bug state left behind by orphan initialization.
localDir := t.TempDir()
testutil.InitRepo(t, localDir)
testutil.WriteFile(t, localDir, "f.txt", "init")
testutil.GitAdd(t, localDir, "f.txt")
testutil.GitCommit(t, localDir, "init")
runCheckpointRemoteGit(ctx, t, localDir, "remote", "add", "origin", "git@github.com:org/main-repo.git")
localDefault := checkpointRemoteCurrentBranch(ctx, t, localDir)
runCheckpointRemoteGit(ctx, t, localDir, "checkout", "--orphan", paths.MetadataBranchName)
runCheckpointRemoteGit(ctx, t, localDir, "rm", "-rf", ".")
testutil.WriteFile(t, localDir, vercelconfig.FileName, `{"git":{"deploymentEnabled":{"entire/**":false}}}`)
runCheckpointRemoteGit(ctx, t, localDir, "add", vercelconfig.FileName)
runCheckpointRemoteGit(ctx, t, localDir, "commit", "-m", "Initialize metadata branch")
runCheckpointRemoteGit(ctx, t, localDir, "checkout", localDefault)
entireDir := filepath.Join(localDir, ".entire")
require.NoError(t, os.MkdirAll(entireDir, 0o755))
require.NoError(t, os.WriteFile(
filepath.Join(entireDir, paths.SettingsFileName),
[]byte(`{"enabled": true, "strategy_options": {"checkpoint_remote": {"provider": "github", "repo": "org/checkpoints"}}}`),
0o644,
))
redirectGitURL(t, localDir, "git@github.com:org/checkpoints.git", "file://"+remoteDir)
t.Chdir(localDir)
paths.ClearWorktreeRootCache()
repo, err := OpenRepository(ctx)
require.NoError(t, err)
defer repo.Close()
vercelOnlyTip := checkpointRemoteRevParse(ctx, t, localDir, paths.MetadataBranchName)
require.NotEqual(t, remoteTip, vercelOnlyTip, "test setup: vercel-only orphan must differ from remote tip")
require.NoError(t, EnsurePrimaryRef(WithCheckpointRemoteBootstrap(ctx), repo))
healed := checkpointRemoteRevParse(ctx, t, localDir, paths.MetadataBranchName)
assert.Equal(t, remoteTip, healed,
"a vercel.json-only orphan should be treated as un-initialized and healed to the exact remote tip")
files := checkpointRemoteMetadataFiles(ctx, t, localDir)
assert.Contains(t, files, "aa/aaaaaaaaaa/"+paths.MetadataFileName, "the healed branch should contain the checkpoint remote data")
}
"