( ctx context.Context, commit bufmodule.Commit, )
| 229 | } |
| 230 | |
| 231 | func (p *commitStore) putCommit( |
| 232 | ctx context.Context, |
| 233 | commit bufmodule.Commit, |
| 234 | ) (retErr error) { |
| 235 | createTime, err := commit.CreateTime() |
| 236 | if err != nil { |
| 237 | return err |
| 238 | } |
| 239 | moduleKey := commit.ModuleKey() |
| 240 | digest, err := moduleKey.Digest() |
| 241 | if err != nil { |
| 242 | return err |
| 243 | } |
| 244 | commitKey, err := bufmodule.ModuleKeyToCommitKey(moduleKey) |
| 245 | if err != nil { |
| 246 | return err |
| 247 | } |
| 248 | bucket := p.getReadWriteBucketForDir(ctx, commitKey) |
| 249 | path := getCommitStoreFilePath(commitKey) |
| 250 | registryLockPath := getCommitStoreLockPath(commitKey) |
| 251 | // Check if the commit already exists under a shared lock before acquiring |
| 252 | // an exclusive lock. |
| 253 | exists, err := p.commitExistsUnderRLock(ctx, bucket, path, registryLockPath) |
| 254 | if err != nil { |
| 255 | return err |
| 256 | } |
| 257 | if exists { |
| 258 | return nil |
| 259 | } |
| 260 | unlocker, err := p.locker.Lock(ctx, registryLockPath) |
| 261 | if err != nil { |
| 262 | return err |
| 263 | } |
| 264 | defer func() { |
| 265 | retErr = errors.Join(retErr, unlocker.Unlock()) |
| 266 | }() |
| 267 | // Re-check after acquiring the exclusive lock, as another process may have |
| 268 | // written the commit between releasing the shared lock and acquiring the |
| 269 | // exclusive lock. |
| 270 | if _, err := bucket.Stat(ctx, path); err == nil { |
| 271 | return nil |
| 272 | } |
| 273 | externalCommit := externalCommit{ |
| 274 | Version: externalCommitVersion, |
| 275 | Owner: moduleKey.FullName().Owner(), |
| 276 | Module: moduleKey.FullName().Name(), |
| 277 | CreateTime: createTime, |
| 278 | Digest: digest.String(), |
| 279 | } |
| 280 | if !externalCommit.isValid() { |
| 281 | return syserror.Newf("external commit is invalid: %+v", externalCommit) |
| 282 | } |
| 283 | data, err := json.Marshal(externalCommit) |
| 284 | if err != nil { |
| 285 | return err |
| 286 | } |
| 287 | return storage.PutPath(ctx, bucket, path, data, storage.PutWithAtomic()) |
| 288 | } |
no test coverage detected