aboutsummaryrefslogtreecommitdiffstats
path: root/models/git
diff options
context:
space:
mode:
Diffstat (limited to 'models/git')
-rw-r--r--models/git/commit_status.go46
-rw-r--r--models/git/lfs.go11
-rw-r--r--models/git/lfs_lock.go76
3 files changed, 57 insertions, 76 deletions
diff --git a/models/git/commit_status.go b/models/git/commit_status.go
index f85e1b15e5..e255bca5d0 100644
--- a/models/git/commit_status.go
+++ b/models/git/commit_status.go
@@ -470,35 +470,31 @@ func NewCommitStatus(ctx context.Context, opts NewCommitStatusOptions) error {
return fmt.Errorf("NewCommitStatus[%s, %s]: no user specified", opts.Repo.FullName(), opts.SHA)
}
- ctx, committer, err := db.TxContext(ctx)
- if err != nil {
- return fmt.Errorf("NewCommitStatus[repo_id: %d, user_id: %d, sha: %s]: %w", opts.Repo.ID, opts.Creator.ID, opts.SHA, err)
- }
- defer committer.Close()
-
- // Get the next Status Index
- idx, err := GetNextCommitStatusIndex(ctx, opts.Repo.ID, opts.SHA.String())
- if err != nil {
- return fmt.Errorf("generate commit status index failed: %w", err)
- }
+ return db.WithTx(ctx, func(ctx context.Context) error {
+ // Get the next Status Index
+ idx, err := GetNextCommitStatusIndex(ctx, opts.Repo.ID, opts.SHA.String())
+ if err != nil {
+ return fmt.Errorf("generate commit status index failed: %w", err)
+ }
- opts.CommitStatus.Description = strings.TrimSpace(opts.CommitStatus.Description)
- opts.CommitStatus.Context = strings.TrimSpace(opts.CommitStatus.Context)
- opts.CommitStatus.TargetURL = strings.TrimSpace(opts.CommitStatus.TargetURL)
- opts.CommitStatus.SHA = opts.SHA.String()
- opts.CommitStatus.CreatorID = opts.Creator.ID
- opts.CommitStatus.RepoID = opts.Repo.ID
- opts.CommitStatus.Index = idx
- log.Debug("NewCommitStatus[%s, %s]: %d", opts.Repo.FullName(), opts.SHA, opts.CommitStatus.Index)
+ opts.CommitStatus.Description = strings.TrimSpace(opts.CommitStatus.Description)
+ opts.CommitStatus.Context = strings.TrimSpace(opts.CommitStatus.Context)
+ opts.CommitStatus.TargetURL = strings.TrimSpace(opts.CommitStatus.TargetURL)
+ opts.CommitStatus.SHA = opts.SHA.String()
+ opts.CommitStatus.CreatorID = opts.Creator.ID
+ opts.CommitStatus.RepoID = opts.Repo.ID
+ opts.CommitStatus.Index = idx
+ log.Debug("NewCommitStatus[%s, %s]: %d", opts.Repo.FullName(), opts.SHA, opts.CommitStatus.Index)
- opts.CommitStatus.ContextHash = hashCommitStatusContext(opts.CommitStatus.Context)
+ opts.CommitStatus.ContextHash = hashCommitStatusContext(opts.CommitStatus.Context)
- // Insert new CommitStatus
- if _, err = db.GetEngine(ctx).Insert(opts.CommitStatus); err != nil {
- return fmt.Errorf("insert CommitStatus[%s, %s]: %w", opts.Repo.FullName(), opts.SHA, err)
- }
+ // Insert new CommitStatus
+ if err = db.Insert(ctx, opts.CommitStatus); err != nil {
+ return fmt.Errorf("insert CommitStatus[%s, %s]: %w", opts.Repo.FullName(), opts.SHA, err)
+ }
- return committer.Commit()
+ return nil
+ })
}
// SignCommitWithStatuses represents a commit with validation of signature and status state.
diff --git a/models/git/lfs.go b/models/git/lfs.go
index e4fa2b446a..c471baf588 100644
--- a/models/git/lfs.go
+++ b/models/git/lfs.go
@@ -135,25 +135,18 @@ var ErrLFSObjectNotExist = db.ErrNotExist{Resource: "LFS Meta object"}
// NewLFSMetaObject stores a given populated LFSMetaObject structure in the database
// if it is not already present.
func NewLFSMetaObject(ctx context.Context, repoID int64, p lfs.Pointer) (*LFSMetaObject, error) {
- ctx, committer, err := db.TxContext(ctx)
- if err != nil {
- return nil, err
- }
- defer committer.Close()
-
m, exist, err := db.Get[LFSMetaObject](ctx, builder.Eq{"repository_id": repoID, "oid": p.Oid})
if err != nil {
return nil, err
} else if exist {
- return m, committer.Commit()
+ return m, nil
}
m = &LFSMetaObject{Pointer: p, RepositoryID: repoID}
if err = db.Insert(ctx, m); err != nil {
return nil, err
}
-
- return m, committer.Commit()
+ return m, nil
}
// GetLFSMetaObjectByOid selects a LFSMetaObject entry from database by its OID.
diff --git a/models/git/lfs_lock.go b/models/git/lfs_lock.go
index 07ce7d4abf..c5f9a4e6de 100644
--- a/models/git/lfs_lock.go
+++ b/models/git/lfs_lock.go
@@ -70,32 +70,28 @@ func (l *LFSLock) LoadOwner(ctx context.Context) error {
// CreateLFSLock creates a new lock.
func CreateLFSLock(ctx context.Context, repo *repo_model.Repository, lock *LFSLock) (*LFSLock, error) {
- dbCtx, committer, err := db.TxContext(ctx)
- if err != nil {
- return nil, err
- }
- defer committer.Close()
-
- if err := CheckLFSAccessForRepo(dbCtx, lock.OwnerID, repo, perm.AccessModeWrite); err != nil {
- return nil, err
- }
+ return db.WithTx2(ctx, func(ctx context.Context) (*LFSLock, error) {
+ if err := CheckLFSAccessForRepo(ctx, lock.OwnerID, repo, perm.AccessModeWrite); err != nil {
+ return nil, err
+ }
- lock.Path = util.PathJoinRel(lock.Path)
- lock.RepoID = repo.ID
+ lock.Path = util.PathJoinRel(lock.Path)
+ lock.RepoID = repo.ID
- l, err := GetLFSLock(dbCtx, repo, lock.Path)
- if err == nil {
- return l, ErrLFSLockAlreadyExist{lock.RepoID, lock.Path}
- }
- if !IsErrLFSLockNotExist(err) {
- return nil, err
- }
+ l, err := GetLFSLock(ctx, repo, lock.Path)
+ if err == nil {
+ return l, ErrLFSLockAlreadyExist{lock.RepoID, lock.Path}
+ }
+ if !IsErrLFSLockNotExist(err) {
+ return nil, err
+ }
- if err := db.Insert(dbCtx, lock); err != nil {
- return nil, err
- }
+ if err := db.Insert(ctx, lock); err != nil {
+ return nil, err
+ }
- return lock, committer.Commit()
+ return lock, nil
+ })
}
// GetLFSLock returns release by given path.
@@ -163,30 +159,26 @@ func CountLFSLockByRepoID(ctx context.Context, repoID int64) (int64, error) {
// DeleteLFSLockByID deletes a lock by given ID.
func DeleteLFSLockByID(ctx context.Context, id int64, repo *repo_model.Repository, u *user_model.User, force bool) (*LFSLock, error) {
- dbCtx, committer, err := db.TxContext(ctx)
- if err != nil {
- return nil, err
- }
- defer committer.Close()
-
- lock, err := GetLFSLockByID(dbCtx, id)
- if err != nil {
- return nil, err
- }
+ return db.WithTx2(ctx, func(ctx context.Context) (*LFSLock, error) {
+ lock, err := GetLFSLockByID(ctx, id)
+ if err != nil {
+ return nil, err
+ }
- if err := CheckLFSAccessForRepo(dbCtx, u.ID, repo, perm.AccessModeWrite); err != nil {
- return nil, err
- }
+ if err := CheckLFSAccessForRepo(ctx, u.ID, repo, perm.AccessModeWrite); err != nil {
+ return nil, err
+ }
- if !force && u.ID != lock.OwnerID {
- return nil, errors.New("user doesn't own lock and force flag is not set")
- }
+ if !force && u.ID != lock.OwnerID {
+ return nil, errors.New("user doesn't own lock and force flag is not set")
+ }
- if _, err := db.GetEngine(dbCtx).ID(id).Delete(new(LFSLock)); err != nil {
- return nil, err
- }
+ if _, err := db.GetEngine(ctx).ID(id).Delete(new(LFSLock)); err != nil {
+ return nil, err
+ }
- return lock, committer.Commit()
+ return lock, nil
+ })
}
// CheckLFSAccessForRepo check needed access mode base on action