-
Notifications
You must be signed in to change notification settings - Fork 335
v2 checkpoints: add deterministic /full/root anchor commit #1201
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Soph
wants to merge
4
commits into
main
Choose a base branch
from
soph/improve-v2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| package checkpoint | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "time" | ||
|
|
||
| "github.com/entireio/cli/cmd/entire/cli/paths" | ||
| "github.com/go-git/go-git/v6" | ||
| "github.com/go-git/go-git/v6/plumbing" | ||
| "github.com/go-git/go-git/v6/plumbing/object" | ||
| ) | ||
|
|
||
| // The /full/root commit is constructed with fixed inputs (empty tree, fixed | ||
| // author, fixed Unix-epoch timestamp, fixed message, no signature) so every | ||
| // client produces an identical SHA. That lets concurrent first-time creation | ||
| // across machines converge — both clients write the same bytes, so the push | ||
| // is a no-op rather than a conflict. Don't change these inputs without | ||
| // understanding the migration consequence: clients on different versions | ||
| // would produce different SHAs. | ||
| const ( | ||
| v2FullRootAuthorName = "Entire Checkpoints" | ||
| v2FullRootAuthorEmail = "checkpoints@entire.io" | ||
| v2FullRootMessage = "Entire checkpoints v2 root\n" | ||
| ) | ||
|
|
||
| func buildV2FullRootCommit(ctx context.Context, repo *git.Repository) (plumbing.Hash, error) { | ||
| emptyTreeHash, err := BuildTreeFromEntries(ctx, repo, make(map[string]object.TreeEntry)) | ||
| if err != nil { | ||
| return plumbing.ZeroHash, fmt.Errorf("build empty tree for /full/root: %w", err) | ||
| } | ||
|
|
||
| sig := object.Signature{ | ||
| Name: v2FullRootAuthorName, | ||
| Email: v2FullRootAuthorEmail, | ||
| When: time.Unix(0, 0).UTC(), | ||
| } | ||
|
|
||
| commit := &object.Commit{ | ||
| TreeHash: emptyTreeHash, | ||
| Author: sig, | ||
| Committer: sig, | ||
| Message: v2FullRootMessage, | ||
| } | ||
| // Don't sign — signatures are non-deterministic and would break SHA convergence. | ||
|
|
||
| obj := repo.Storer.NewEncodedObject() | ||
| if err := commit.Encode(obj); err != nil { | ||
| return plumbing.ZeroHash, fmt.Errorf("encode /full/root commit: %w", err) | ||
| } | ||
|
|
||
| hash, err := repo.Storer.SetEncodedObject(obj) | ||
| if err != nil { | ||
| return plumbing.ZeroHash, fmt.Errorf("store /full/root commit: %w", err) | ||
| } | ||
| return hash, nil | ||
| } | ||
|
|
||
| // ensureV2FullRoot returns the local /full/root commit hash, building and | ||
| // setting the ref if it does not yet exist. Local-only; remote publication | ||
| // happens through the push pipeline. | ||
| func (s *V2GitStore) ensureV2FullRoot(ctx context.Context) (plumbing.Hash, error) { | ||
| refName := plumbing.ReferenceName(paths.V2FullRootRefName) | ||
|
|
||
| if ref, err := s.repo.Reference(refName, true); err == nil { | ||
| return ref.Hash(), nil | ||
| } | ||
|
|
||
| hash, err := buildV2FullRootCommit(ctx, s.repo) | ||
| if err != nil { | ||
| return plumbing.ZeroHash, fmt.Errorf("ensure /full/root: %w", err) | ||
| } | ||
|
|
||
| ref := plumbing.NewHashReference(refName, hash) | ||
| if err := s.repo.Storer.SetReference(ref); err != nil { | ||
| return plumbing.ZeroHash, fmt.Errorf("set local /full/root: %w", err) | ||
| } | ||
| return hash, nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| package checkpoint | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| "github.com/entireio/cli/cmd/entire/cli/paths" | ||
| "github.com/go-git/go-git/v6/plumbing" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| // Pinned SHA for the deterministic /full/root commit. Any accidental change | ||
| // to the inputs in v2_root.go (author, time, message, encoding) flips this. | ||
| // Updating it on purpose creates a cross-version migration problem — old | ||
| // and new clients would produce different SHAs and the race-resolves-to-no-op | ||
| // property would no longer hold. | ||
| const expectedV2FullRootHash = "c095af40b171ff4c3c4a781abacd39aa499e183b" | ||
|
|
||
| func TestBuildV2FullRootCommit_WellKnownSHA(t *testing.T) { | ||
| t.Parallel() | ||
| repo := initTestRepo(t) | ||
|
|
||
| hash, err := buildV2FullRootCommit(context.Background(), repo) | ||
| require.NoError(t, err) | ||
|
|
||
| require.Equal(t, expectedV2FullRootHash, hash.String(), | ||
| "deterministic root commit SHA changed — see comment on expectedV2FullRootHash") | ||
| } | ||
|
|
||
| func TestBuildV2FullRootCommit_AcrossDifferentRepos(t *testing.T) { | ||
| t.Parallel() | ||
| repoA := initTestRepo(t) | ||
| repoB := initTestRepo(t) | ||
|
|
||
| hashA, err := buildV2FullRootCommit(context.Background(), repoA) | ||
| require.NoError(t, err) | ||
|
|
||
| hashB, err := buildV2FullRootCommit(context.Background(), repoB) | ||
| require.NoError(t, err) | ||
|
|
||
| require.Equal(t, hashA, hashB, | ||
| "different repos must produce identical root commit SHA") | ||
| } | ||
|
|
||
| func TestEnsureV2FullRoot_CreatesRefAndCommit(t *testing.T) { | ||
| t.Parallel() | ||
| repo := initTestRepo(t) | ||
| store := NewV2GitStore(repo, "origin") | ||
|
|
||
| hash, err := store.ensureV2FullRoot(context.Background()) | ||
| require.NoError(t, err) | ||
| require.Equal(t, expectedV2FullRootHash, hash.String()) | ||
|
|
||
| ref, err := repo.Reference(plumbing.ReferenceName(paths.V2FullRootRefName), true) | ||
| require.NoError(t, err) | ||
| require.Equal(t, hash, ref.Hash(), | ||
| "local ref must point at the deterministic commit") | ||
| } | ||
|
|
||
| func TestEnsureV2FullRoot_IsIdempotent(t *testing.T) { | ||
| t.Parallel() | ||
| repo := initTestRepo(t) | ||
| store := NewV2GitStore(repo, "origin") | ||
|
|
||
| first, err := store.ensureV2FullRoot(context.Background()) | ||
| require.NoError(t, err) | ||
|
|
||
| second, err := store.ensureV2FullRoot(context.Background()) | ||
| require.NoError(t, err) | ||
|
|
||
| require.Equal(t, first, second, | ||
| "repeated calls must return the same hash without changing the ref") | ||
| } | ||
|
|
||
| func TestEnsureV2FullRoot_PreservesPreexistingRef(t *testing.T) { | ||
| t.Parallel() | ||
| repo := initTestRepo(t) | ||
| store := NewV2GitStore(repo, "origin") | ||
|
|
||
|
Soph marked this conversation as resolved.
Outdated
|
||
| first, err := store.ensureV2FullRoot(context.Background()) | ||
| require.NoError(t, err) | ||
|
|
||
| second, err := store.ensureV2FullRoot(context.Background()) | ||
| require.NoError(t, err) | ||
| require.Equal(t, first, second) | ||
|
|
||
| ref, err := repo.Reference(plumbing.ReferenceName(paths.V2FullRootRefName), true) | ||
| require.NoError(t, err) | ||
| require.Equal(t, first, ref.Hash()) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.