Skip to content

Commit 2476d8d

Browse files
committed
chore: more stuff in lockfile
1 parent c2a76a7 commit 2476d8d

File tree

3 files changed

+103
-9
lines changed

3 files changed

+103
-9
lines changed

lockfile/io.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,15 @@ func Load(data []byte, opts ...LoadOption) (*LockFile, error) {
3939
lf.TrackedFiles = sequencedmap.New[string, TrackedFile]()
4040
}
4141

42-
// Migrate old "integrity" field to "last_write_checksum"
42+
// Migrate old fields to new structure
4343
for path := range lf.TrackedFiles.Keys() {
4444
tf, ok := lf.TrackedFiles.Get(path)
4545
if !ok {
4646
continue
4747
}
4848

49+
modified := false
50+
4951
// Check if integrity exists in AdditionalProperties
5052
if tf.AdditionalProperties != nil {
5153
if integrity, ok := tf.AdditionalProperties["integrity"].(string); ok {
@@ -55,8 +57,23 @@ func Load(data []byte, opts ...LoadOption) (*LockFile, error) {
5557
}
5658
// Remove from AdditionalProperties
5759
delete(tf.AdditionalProperties, "integrity")
58-
lf.TrackedFiles.Set(path, tf)
60+
modified = true
5961
}
62+
63+
// Migrate old "pristine_blob_hash" to "pristine_git_object"
64+
if pristineBlobHash, ok := tf.AdditionalProperties["pristine_blob_hash"].(string); ok {
65+
// Migrate to PristineGitObject if not already set
66+
if tf.PristineGitObject == "" {
67+
tf.PristineGitObject = pristineBlobHash
68+
}
69+
// Remove from AdditionalProperties
70+
delete(tf.AdditionalProperties, "pristine_blob_hash")
71+
modified = true
72+
}
73+
}
74+
75+
if modified {
76+
lf.TrackedFiles.Set(path, tf)
6077
}
6178
}
6279

lockfile/lockfile.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,35 @@ type TrackedFile struct {
2121
// UUID embedded in the file header. Allows detecting "Moves/Renames".
2222
ID string `yaml:"id,omitempty"`
2323

24-
// The Merge Base (Reference)
25-
// The Git Blob SHA-1 of the PURE generated file from the PREVIOUS run.
26-
// We pass this to `git cat-file -p` to get the BASE content.
27-
PristineBlobHash string `yaml:"pristine_blob_hash,omitempty"`
28-
2924
// The Dirty Check (Optimization)
3025
// The SHA-1 of the file content exactly as written to disk last time.
3126
// If Disk_SHA == LastWriteChecksum, we skip the merge (Fast Path).
3227
LastWriteChecksum string `yaml:"last_write_checksum,omitempty"`
3328

29+
// The O(1) Lookup Key
30+
// Stores the Blob Hash of the file from the PREVIOUS run.
31+
// Only populated if persistentEdits is enabled.
32+
PristineGitObject string `yaml:"pristine_git_object,omitempty"`
33+
3434
AdditionalProperties map[string]any `yaml:",inline"`
3535
}
3636

37+
type PersistentEdits struct {
38+
// Maps to refs/speakeasy/gen/<UUID>
39+
GenerationID string `yaml:"generation_id,omitempty"`
40+
41+
// Parent Commit (Links history for compression)
42+
PristineCommitHash string `yaml:"pristine_commit_hash,omitempty"`
43+
44+
// Content Checksum (Enables No-Op/Determinism check)
45+
PristineTreeHash string `yaml:"pristine_tree_hash,omitempty"`
46+
}
47+
3748
type LockFile struct {
3849
LockVersion string `yaml:"lockVersion"`
3950
ID string `yaml:"id"`
4051
Management Management `yaml:"management"`
52+
PersistentEdits *PersistentEdits `yaml:"persistentEdits,omitempty"`
4153
Features map[string]map[string]string `yaml:"features,omitempty"`
4254
TrackedFiles TrackedFiles `yaml:"trackedFiles,omitempty"`
4355
Examples Examples `yaml:"examples,omitempty"`

lockfile/lockfile_test.go

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,24 +45,88 @@ func TestLoad_NewStructure(t *testing.T) {
4545
lockVersion: "2.0.0"
4646
id: "test-uuid"
4747
management: {}
48+
persistentEdits:
49+
generation_id: "uuid-550e"
50+
pristine_commit_hash: "a1b2c3"
51+
pristine_tree_hash: "tree-e5f6"
52+
trackedFiles:
53+
"pkg/models/user.go":
54+
id: "uuid-breadcrumb-123"
55+
last_write_checksum: "sha1:file-hash-789"
56+
pristine_git_object: "blob-123"
57+
`)
58+
59+
lf, err := lockfile.Load(newYAML)
60+
require.NoError(t, err)
61+
62+
// Verify persistentEdits
63+
require.NotNil(t, lf.PersistentEdits)
64+
assert.Equal(t, "uuid-550e", lf.PersistentEdits.GenerationID)
65+
assert.Equal(t, "a1b2c3", lf.PersistentEdits.PristineCommitHash)
66+
assert.Equal(t, "tree-e5f6", lf.PersistentEdits.PristineTreeHash)
67+
68+
// Verify tracked file
69+
tf, ok := lf.TrackedFiles.Get("pkg/models/user.go")
70+
require.True(t, ok)
71+
72+
assert.Equal(t, "uuid-breadcrumb-123", tf.ID)
73+
assert.Equal(t, "sha1:file-hash-789", tf.LastWriteChecksum)
74+
assert.Equal(t, "blob-123", tf.PristineGitObject)
75+
}
76+
77+
func TestLoad_MigratesPristineBlobHashToGitObject(t *testing.T) {
78+
// Legacy lockfile YAML with 'pristine_blob_hash' field
79+
legacyYAML := []byte(`
80+
lockVersion: "2.0.0"
81+
id: "test-uuid"
82+
management: {}
4883
trackedFiles:
4984
"src/model.go":
5085
id: "file-uuid-123"
5186
pristine_blob_hash: "git-hash-456"
5287
last_write_checksum: "sha1:file-hash-789"
5388
`)
5489

55-
lf, err := lockfile.Load(newYAML)
90+
lf, err := lockfile.Load(legacyYAML)
5691
require.NoError(t, err)
92+
require.NotNil(t, lf)
5793

94+
// Verify the file exists in tracked files
5895
tf, ok := lf.TrackedFiles.Get("src/model.go")
5996
require.True(t, ok)
6097

98+
// Verify migration
99+
assert.Equal(t, "git-hash-456", tf.PristineGitObject, "Should migrate pristine_blob_hash to pristine_git_object")
100+
101+
// Verify cleanup
102+
_, exists := tf.AdditionalProperties["pristine_blob_hash"]
103+
assert.False(t, exists, "Should remove pristine_blob_hash from AdditionalProperties")
104+
105+
// Verify other properties preserved
61106
assert.Equal(t, "file-uuid-123", tf.ID)
62-
assert.Equal(t, "git-hash-456", tf.PristineBlobHash)
63107
assert.Equal(t, "sha1:file-hash-789", tf.LastWriteChecksum)
64108
}
65109

110+
func TestLoad_OmitsEmptyPersistentEdits(t *testing.T) {
111+
// Lockfile YAML without persistentEdits section
112+
yamlWithoutPE := []byte(`
113+
lockVersion: "2.0.0"
114+
id: "test-uuid"
115+
management: {}
116+
trackedFiles:
117+
"src/file.go":
118+
id: "file-uuid"
119+
last_write_checksum: "sha1:hash"
120+
`)
121+
122+
lf, err := lockfile.Load(yamlWithoutPE)
123+
require.NoError(t, err)
124+
require.NotNil(t, lf)
125+
126+
// Verify persistentEdits is nil when not present
127+
assert.Nil(t, lf.PersistentEdits, "PersistentEdits should be nil when not in YAML")
128+
}
129+
66130
func TestNew_CreatesValidLockFile(t *testing.T) {
67131
lf := lockfile.New()
68132
require.NotNil(t, lf)
@@ -71,4 +135,5 @@ func TestNew_CreatesValidLockFile(t *testing.T) {
71135
assert.NotEmpty(t, lf.ID)
72136
assert.NotNil(t, lf.TrackedFiles)
73137
assert.Equal(t, 0, lf.TrackedFiles.Len())
138+
assert.Nil(t, lf.PersistentEdits, "PersistentEdits should be nil in new lockfile")
74139
}

0 commit comments

Comments
 (0)