/simplify: centralize ULID minting, simplify re-stamp, single walk · Entire
/simplify: centralize ULID minting, simplify re-stamp, single walk
c5e10dd·
Soph·1w ago·5 files·+102 added/-107 removed
Cleanups from /simplify on the migrate-to-ulid tool (quality only):
- Move timestamped ULID minting into the id package as id.GenerateULIDAt(t) (GenerateULID now delegates to it). Removes migrate_ulid.go's duplicate oklog/ulid construction and keeps ULID generation in one place; the test moves to the id package too.
- Simplify the metadata re-stamp: restampMetadataDir(repo, treeHash, sessionDir, newID) reads, patches, and grafts in one step returning the new tree hash, replacing the []string path list + the awkward (hash, mode, ok, error) 4-value return (unchanged trees now just return the input hash).
- Collapse the command's double walk into one: MigrateBranchHexToULIDRefs is called once with !apply. The previous dry-then-real pair walked every checkpoint twice and minted different random ULIDs, so the printed preview didn't match what got written.
Skipped (noted): hardcoded "Entire-Checkpoint: " in the sed script (stable contract; threading trailers.CheckpointTrailerKey through a format string reads worse for one-shot tooling); go-git for ref listing/deletion (deletion deliberately uses the git CLI for packed-refs correctness — see cleanup.go/ephemeral.go/common.go — and the command already requires git for filter-branch); per-session tree reads + JSON key reorder (negligible one-shot).
Sessions
755d3a0d9603View transcript
Changes
5
cmd/entire/cli
checkpoint
id
Mid.go+16/-3
Mid_test.go+41
Mmigrate_ulid.go+37/-55
Mmigrate_ulid_test.go-37
Mdoctor_migrate_ulid.go+8/-12
// (chosen by checkpoint.GenerateCheckpointID); the value is canonical and passes
// KindOf/Validate as KindULID.
//
// The timestamp is Unix epoch milliseconds (via ulid.Now), so it is inherently
// timezone-independent — the machine's local zone does not affect the ID.
// The timestamp is Unix epoch milliseconds, so it is inherently timezone-
// independent — the machine's local zone does not affect the ID.
func GenerateULID() (CheckpointID, error) {
u, err := ulid.New(ulid.Now(), rand.Reader)
return GenerateULIDAt(time.Now())
}
// GenerateULIDAt is GenerateULID with an explicit timestamp: the ULID's
// millisecond prefix is taken from t (falling back to now when t is zero), with
// crypto-random entropy so same-millisecond IDs stay unique. Use it to mint an ID
// that sorts at a historical time — e.g. re-identifying existing checkpoints as
// ULIDs by their original creation time.
func GenerateULIDAt(t time.Time) (CheckpointID, error) {
if t.IsZero() {
t = time.Now()
}
u, err := ulid.New(ulid.Timestamp(t.UTC()), rand.Reader)
if err != nil {
return EmptyCheckpointID, fmt.Errorf("failed to generate ULID checkpoint ID: %w", err)
}
}
func TestGenerateULIDAt(t *testing.T) {
t.Parallel()
when := time.Date(2025, 3, 4, 5, 6, 7, 0, time.UTC)
a, err := GenerateULIDAt(when)
if err != nil {
t.Fatalf("GenerateULIDAt() error = %v", err)
}
if a.Kind() != KindULID {
t.Errorf("Kind() = %v, want KindULID for %q", a.Kind(), a)
}
}
func TestMintULIDAt(t *testing.T) {
t.Parallel()
t.Run("carries the given timestamp", func(t *testing.T) {
t.Parallel()
when := time.Date(2025, 3, 4, 5, 6, 7, 0, time.UTC)
cid, err := mintULIDAt(when)
require.NoError(t, err)
require.Equal(t, id.KindULID, cid.Kind())
})
// additional tests omitted for brevity
}