diff --git a/private/buf/bufsync/bufsync.go b/private/buf/bufsync/bufsync.go index 53ad547fb3..28afcfe4fd 100644 --- a/private/buf/bufsync/bufsync.go +++ b/private/buf/bufsync/bufsync.go @@ -167,14 +167,13 @@ type Handler interface { branch string, ) (git.Hash, error) - // CheckSyncedGitCommits is invoked when syncing branches to know which commits hashes from a set - // are already synced inthe BSR. It expects to receive the commit hashes that are synced already. If - // an error is returned, sync will abort. - CheckSyncedGitCommits( + // IsGitCommitSynced is invoked when syncing branches to know if a Git commit is already synced. + // If an error is returned, sync will abort. + IsGitCommitSynced( ctx context.Context, module bufmoduleref.ModuleIdentity, - commitHashes map[string]struct{}, - ) (map[string]struct{}, error) + hash git.Hash, + ) (bool, error) // GetModuleReleaseBranch is invoked before syncing, to gather release branch names for all the // modules that are about to be synced. If the BSR module does not exist, the implementation should diff --git a/private/buf/bufsync/bufsync_test.go b/private/buf/bufsync/bufsync_test.go index ac183f21df..9702dd69a5 100644 --- a/private/buf/bufsync/bufsync_test.go +++ b/private/buf/bufsync/bufsync_test.go @@ -221,18 +221,13 @@ func (c *mockSyncHandler) SyncModuleCommit( return err } -func (c *mockSyncHandler) CheckSyncedGitCommits( +func (c *mockSyncHandler) IsGitCommitSynced( ctx context.Context, module bufmoduleref.ModuleIdentity, - commitHashes map[string]struct{}, -) (map[string]struct{}, error) { - syncedHashes := make(map[string]struct{}) - for hash := range commitHashes { - if _, isSynced := c.syncedCommitsSHAs[hash]; isSynced { - syncedHashes[hash] = struct{}{} - } - } - return syncedHashes, nil + hash git.Hash, +) (bool, error) { + _, isSynced := c.syncedCommitsSHAs[hash.Hex()] + return isSynced, nil } var _ bufsync.Handler = (*mockSyncHandler)(nil) diff --git a/private/buf/bufsync/syncer.go b/private/buf/bufsync/syncer.go index 7633d2e7a4..9c71e062e9 100644 --- a/private/buf/bufsync/syncer.go +++ b/private/buf/bufsync/syncer.go @@ -425,7 +425,7 @@ func (s *syncer) branchSyncableCommits( commitHash := commit.Hash().Hex() logger := logger.With(zap.String("commit", commitHash)) // check if this commit is already synced - isSynced, err := s.isGitCommitSynced(ctx, moduleIdentity, commitHash) + isSynced, err := s.isGitCommitSynced(ctx, moduleIdentity, commit.Hash()) if err != nil { return fmt.Errorf( "checking if module %s already synced git commit %s: %w", @@ -516,26 +516,25 @@ func (s *syncer) branchSyncableCommits( } // isGitCommitSynced checks if a commit hash is already synced to a BSR module. -func (s *syncer) isGitCommitSynced(ctx context.Context, moduleIdentity bufmoduleref.ModuleIdentity, commitHash string) (bool, error) { +func (s *syncer) isGitCommitSynced(ctx context.Context, moduleIdentity bufmoduleref.ModuleIdentity, commitHash git.Hash) (bool, error) { modIdentity := moduleIdentity.IdentityString() // check local cache first if syncedModuleCommits, ok := s.modulesIdentitiesToCommitsSyncedCache[modIdentity]; ok { - if _, commitSynced := syncedModuleCommits[commitHash]; commitSynced { + if _, commitSynced := syncedModuleCommits[commitHash.Hex()]; commitSynced { return true, nil } } // not in the cache, request BSR check - syncedModuleCommits, err := s.handler.CheckSyncedGitCommits(ctx, moduleIdentity, map[string]struct{}{commitHash: {}}) + commitSynced, err := s.handler.IsGitCommitSynced(ctx, moduleIdentity, commitHash) if err != nil { return false, err } - _, commitSynced := syncedModuleCommits[commitHash] if commitSynced { // populate local cache if s.modulesIdentitiesToCommitsSyncedCache[modIdentity] == nil { s.modulesIdentitiesToCommitsSyncedCache[modIdentity] = make(map[string]struct{}) } - s.modulesIdentitiesToCommitsSyncedCache[modIdentity][commitHash] = struct{}{} + s.modulesIdentitiesToCommitsSyncedCache[modIdentity][commitHash.Hex()] = struct{}{} } return commitSynced, nil } diff --git a/private/buf/cmd/buf/command/alpha/repo/reposync/sync_handler.go b/private/buf/cmd/buf/command/alpha/repo/reposync/sync_handler.go index 324998f935..063da4fb9f 100644 --- a/private/buf/cmd/buf/command/alpha/repo/reposync/sync_handler.go +++ b/private/buf/cmd/buf/command/alpha/repo/reposync/sync_handler.go @@ -31,7 +31,6 @@ import ( "github.com/bufbuild/buf/private/pkg/connectclient" "github.com/bufbuild/buf/private/pkg/git" "github.com/bufbuild/buf/private/pkg/storage" - "github.com/bufbuild/buf/private/pkg/stringutil" "go.uber.org/zap" "google.golang.org/protobuf/types/known/timestamppb" ) @@ -88,30 +87,22 @@ func (h *syncHandler) ResolveSyncPoint(ctx context.Context, module bufmoduleref. return hash, nil } -func (h *syncHandler) CheckSyncedGitCommits(ctx context.Context, module bufmoduleref.ModuleIdentity, commitHashes map[string]struct{}) (map[string]struct{}, error) { +func (h *syncHandler) IsGitCommitSynced(ctx context.Context, module bufmoduleref.ModuleIdentity, hash git.Hash) (bool, error) { service := connectclient.Make(h.clientConfig, module.Remote(), registryv1alpha1connect.NewLabelServiceClient) res, err := service.GetLabelsInNamespace(ctx, connect.NewRequest(®istryv1alpha1.GetLabelsInNamespaceRequest{ RepositoryOwner: module.Owner(), RepositoryName: module.Repository(), LabelNamespace: registryv1alpha1.LabelNamespace_LABEL_NAMESPACE_GIT_COMMIT, - LabelNames: stringutil.MapToSlice(commitHashes), + LabelNames: []string{hash.Hex()}, })) if err != nil { if connect.CodeOf(err) == connect.CodeNotFound { // Repo is not created - return nil, nil - } - return nil, fmt.Errorf("get labels in namespace: %w", err) - } - syncedHashes := make(map[string]struct{}) - for _, label := range res.Msg.Labels { - syncedHash := label.LabelName.Name - if _, expected := commitHashes[syncedHash]; !expected { - return nil, fmt.Errorf("received unexpected synced hash %q, expected %v", syncedHash, commitHashes) + return false, nil } - syncedHashes[syncedHash] = struct{}{} + return false, fmt.Errorf("get labels in namespace: %w", err) } - return syncedHashes, nil + return len(res.Msg.Labels) == 1, nil } func (h *syncHandler) GetModuleReleaseBranch(ctx context.Context, module bufmoduleref.ModuleIdentity) (string, error) {