diff --git a/models/avatar.go b/models/avatar.go index 45fa90eb856fd..e81b876667abc 100644 --- a/models/avatar.go +++ b/models/avatar.go @@ -95,7 +95,7 @@ func HashedAvatarLink(email string, size int) string { Hash: sum, } // OK we're going to open a session just because I think that that might hide away any problems with postgres reporting errors - if err := db.WithTx(func(ctx db.Context) error { + if err := db.WithTx(func(ctx *db.Context) error { has, err := ctx.Engine().Where("email = ? AND hash = ?", emailHash.Email, emailHash.Hash).Get(new(EmailHash)) if has || err != nil { // Seriously we don't care about any DB problems just return the lowerEmail - we expect the transaction to fail most of the time diff --git a/models/db/context.go b/models/db/context.go index ae84ac641cf15..9b1a3010b6fcd 100644 --- a/models/db/context.go +++ b/models/db/context.go @@ -42,30 +42,30 @@ type Committer interface { } // TxContext represents a transaction Context -func TxContext() (Context, Committer, error) { +func TxContext() (*Context, Committer, error) { sess := x.NewSession() if err := sess.Begin(); err != nil { sess.Close() - return Context{}, nil, err + return nil, nil, err } - return Context{sess}, sess, nil + return &Context{sess}, sess, nil } // WithContext represents executing database operations -func WithContext(f func(ctx Context) error) error { - return f(Context{x}) +func WithContext(f func(ctx *Context) error) error { + return f(&Context{x}) } // WithTx represents executing database operations on a transaction -func WithTx(f func(ctx Context) error) error { +func WithTx(f func(ctx *Context) error) error { sess := x.NewSession() defer sess.Close() if err := sess.Begin(); err != nil { return err } - if err := f(Context{sess}); err != nil { + if err := f(&Context{sess}); err != nil { return err } @@ -80,7 +80,7 @@ func Iterate(ctx *Context, tableBean interface{}, cond builder.Cond, fun func(id } // Insert inserts records into database -func Insert(ctx Context, beans ...interface{}) error { +func Insert(ctx *Context, beans ...interface{}) error { _, err := ctx.e.Insert(beans...) return err } diff --git a/modules/repository/adopt.go b/modules/repository/adopt.go index 9bb58805e3508..9371822fbcc1e 100644 --- a/modules/repository/adopt.go +++ b/modules/repository/adopt.go @@ -47,7 +47,7 @@ func AdoptRepository(doer, u *models.User, opts models.CreateRepoOptions) (*mode IsEmpty: !opts.AutoInit, } - if err := db.WithTx(func(ctx db.Context) error { + if err := db.WithTx(func(ctx *db.Context) error { repoPath := models.RepoPath(u.Name, repo.Name) isExist, err := util.IsExist(repoPath) if err != nil { @@ -61,16 +61,16 @@ func AdoptRepository(doer, u *models.User, opts models.CreateRepoOptions) (*mode } } - if err := models.CreateRepository(&ctx, doer, u, repo, true); err != nil { + if err := models.CreateRepository(ctx, doer, u, repo, true); err != nil { return err } - if err := adoptRepository(&ctx, repoPath, doer, repo, opts); err != nil { + if err := adoptRepository(ctx, repoPath, doer, repo, opts); err != nil { return fmt.Errorf("createDelegateHooks: %v", err) } // Initialize Issue Labels if selected if len(opts.IssueLabels) > 0 { - if err := models.InitializeLabels(&ctx, repo.ID, opts.IssueLabels, false); err != nil { + if err := models.InitializeLabels(ctx, repo.ID, opts.IssueLabels, false); err != nil { return fmt.Errorf("InitializeLabels: %v", err) } } diff --git a/modules/repository/create.go b/modules/repository/create.go index d673a961c50ca..80f446e83f05d 100644 --- a/modules/repository/create.go +++ b/modules/repository/create.go @@ -55,8 +55,8 @@ func CreateRepository(doer, u *models.User, opts models.CreateRepoOptions) (*mod var rollbackRepo *models.Repository - if err := db.WithTx(func(ctx db.Context) error { - if err := models.CreateRepository(&ctx, doer, u, repo, false); err != nil { + if err := db.WithTx(func(ctx *db.Context) error { + if err := models.CreateRepository(ctx, doer, u, repo, false); err != nil { return err } @@ -86,7 +86,7 @@ func CreateRepository(doer, u *models.User, opts models.CreateRepoOptions) (*mod } } - if err = initRepository(&ctx, repoPath, doer, repo, opts); err != nil { + if err = initRepository(ctx, repoPath, doer, repo, opts); err != nil { if err2 := util.RemoveAll(repoPath); err2 != nil { log.Error("initRepository: %v", err) return fmt.Errorf( @@ -97,7 +97,7 @@ func CreateRepository(doer, u *models.User, opts models.CreateRepoOptions) (*mod // Initialize Issue Labels if selected if len(opts.IssueLabels) > 0 { - if err = models.InitializeLabels(&ctx, repo.ID, opts.IssueLabels, false); err != nil { + if err = models.InitializeLabels(ctx, repo.ID, opts.IssueLabels, false); err != nil { rollbackRepo = repo rollbackRepo.OwnerID = u.ID return fmt.Errorf("InitializeLabels: %v", err) diff --git a/modules/repository/fork.go b/modules/repository/fork.go index fd8f670de65f1..ff69f75b32a84 100644 --- a/modules/repository/fork.go +++ b/modules/repository/fork.go @@ -79,17 +79,17 @@ func ForkRepository(doer, owner *models.User, opts models.ForkRepoOptions) (_ *m panic(panicErr) }() - err = db.WithTx(func(ctx db.Context) error { - if err = models.CreateRepository(&ctx, doer, owner, repo, false); err != nil { + err = db.WithTx(func(ctx *db.Context) error { + if err = models.CreateRepository(ctx, doer, owner, repo, false); err != nil { return err } - if err = models.IncrementRepoForkNum(&ctx, opts.BaseRepo.ID); err != nil { + if err = models.IncrementRepoForkNum(ctx, opts.BaseRepo.ID); err != nil { return err } // copy lfs files failure should not be ignored - if err = models.CopyLFS(&ctx, repo, opts.BaseRepo); err != nil { + if err = models.CopyLFS(ctx, repo, opts.BaseRepo); err != nil { return err } @@ -136,8 +136,8 @@ func ForkRepository(doer, owner *models.User, opts models.ForkRepoOptions) (_ *m // ConvertForkToNormalRepository convert the provided repo from a forked repo to normal repo func ConvertForkToNormalRepository(repo *models.Repository) error { - err := db.WithTx(func(ctx db.Context) error { - repo, err := models.GetRepositoryByIDCtx(&ctx, repo.ID) + err := db.WithTx(func(ctx *db.Context) error { + repo, err := models.GetRepositoryByIDCtx(ctx, repo.ID) if err != nil { return err } @@ -146,7 +146,7 @@ func ConvertForkToNormalRepository(repo *models.Repository) error { return nil } - if err := models.DecrementRepoForkNum(&ctx, repo.ForkID); err != nil { + if err := models.DecrementRepoForkNum(ctx, repo.ForkID); err != nil { log.Error("Unable to decrement repo fork num for old root repo %d of repository %-v whilst converting from fork. Error: %v", repo.ForkID, repo, err) return err } @@ -154,7 +154,7 @@ func ConvertForkToNormalRepository(repo *models.Repository) error { repo.IsFork = false repo.ForkID = 0 - if err := models.UpdateRepositoryCtx(&ctx, repo, false); err != nil { + if err := models.UpdateRepositoryCtx(ctx, repo, false); err != nil { log.Error("Unable to update repository %-v whilst converting from fork. Error: %v", repo, err) return err } diff --git a/modules/repository/update.go b/modules/repository/update.go index a48966f08d7f1..d9ff12e1ad566 100644 --- a/modules/repository/update.go +++ b/modules/repository/update.go @@ -17,11 +17,11 @@ import ( // PushUpdateAddDeleteTags updates a number of added and delete tags func PushUpdateAddDeleteTags(repo *models.Repository, gitRepo *git.Repository, addTags, delTags []string) error { - return db.WithTx(func(ctx db.Context) error { - if err := models.PushUpdateDeleteTagsContext(&ctx, repo, delTags); err != nil { + return db.WithTx(func(ctx *db.Context) error { + if err := models.PushUpdateDeleteTagsContext(ctx, repo, delTags); err != nil { return err } - return pushUpdateAddTags(&ctx, repo, gitRepo, addTags) + return pushUpdateAddTags(ctx, repo, gitRepo, addTags) }) } diff --git a/services/archiver/archiver.go b/services/archiver/archiver.go index 690ea96e8f421..bd40d217dfaab 100644 --- a/services/archiver/archiver.go +++ b/services/archiver/archiver.go @@ -104,7 +104,7 @@ func doArchive(r *ArchiveRequest) (*models.RepoArchiver, error) { } defer commiter.Close() - archiver, err := models.GetRepoArchiver(&ctx, r.RepoID, r.Type, r.CommitID) + archiver, err := models.GetRepoArchiver(ctx, r.RepoID, r.Type, r.CommitID) if err != nil { return nil, err } @@ -122,7 +122,7 @@ func doArchive(r *ArchiveRequest) (*models.RepoArchiver, error) { CommitID: r.CommitID, Status: models.RepoArchiverGenerating, } - if err := models.AddRepoArchiver(&ctx, archiver); err != nil { + if err := models.AddRepoArchiver(ctx, archiver); err != nil { return nil, err } } @@ -136,7 +136,7 @@ func doArchive(r *ArchiveRequest) (*models.RepoArchiver, error) { if err == nil { if archiver.Status == models.RepoArchiverGenerating { archiver.Status = models.RepoArchiverReady - return archiver, models.UpdateRepoArchiverStatus(&ctx, archiver) + return archiver, models.UpdateRepoArchiverStatus(ctx, archiver) } return archiver, nil } @@ -202,7 +202,7 @@ func doArchive(r *ArchiveRequest) (*models.RepoArchiver, error) { if archiver.Status == models.RepoArchiverGenerating { archiver.Status = models.RepoArchiverReady - if err = models.UpdateRepoArchiverStatus(&ctx, archiver); err != nil { + if err = models.UpdateRepoArchiverStatus(ctx, archiver); err != nil { return nil, err } } diff --git a/services/attachment/attachment.go b/services/attachment/attachment.go index 9068617980ab7..06f79be01b9e3 100644 --- a/services/attachment/attachment.go +++ b/services/attachment/attachment.go @@ -23,7 +23,7 @@ func NewAttachment(attach *models.Attachment, file io.Reader) (*models.Attachmen return nil, fmt.Errorf("attachment %s should belong to a repository", attach.Name) } - err := db.WithTx(func(ctx db.Context) error { + err := db.WithTx(func(ctx *db.Context) error { attach.UUID = uuid.New().String() size, err := storage.Attachments.Save(attach.RelativePath(), file, -1) if err != nil { diff --git a/services/release/release.go b/services/release/release.go index d2f6e7498ca0e..b3c75cbc49e9e 100644 --- a/services/release/release.go +++ b/services/release/release.go @@ -195,18 +195,18 @@ func UpdateRelease(doer *models.User, gitRepo *git.Repository, rel *models.Relea } defer commiter.Close() - if err = models.UpdateRelease(&ctx, rel); err != nil { + if err = models.UpdateRelease(ctx, rel); err != nil { return err } - if err = models.AddReleaseAttachments(&ctx, rel.ID, addAttachmentUUIDs); err != nil { + if err = models.AddReleaseAttachments(ctx, rel.ID, addAttachmentUUIDs); err != nil { return fmt.Errorf("AddReleaseAttachments: %v", err) } var deletedUUIDsMap = make(map[string]bool) if len(delAttachmentUUIDs) > 0 { // Check attachments - attachments, err := models.GetAttachmentsByUUIDs(&ctx, delAttachmentUUIDs) + attachments, err := models.GetAttachmentsByUUIDs(ctx, delAttachmentUUIDs) if err != nil { return fmt.Errorf("GetAttachmentsByUUIDs [uuids: %v]: %v", delAttachmentUUIDs, err) } @@ -217,7 +217,7 @@ func UpdateRelease(doer *models.User, gitRepo *git.Repository, rel *models.Relea deletedUUIDsMap[attach.UUID] = true } - if _, err := models.DeleteAttachments(&ctx, attachments, false); err != nil { + if _, err := models.DeleteAttachments(ctx, attachments, false); err != nil { return fmt.Errorf("DeleteAttachments [uuids: %v]: %v", delAttachmentUUIDs, err) } } @@ -228,7 +228,7 @@ func UpdateRelease(doer *models.User, gitRepo *git.Repository, rel *models.Relea updateAttachmentsList = append(updateAttachmentsList, k) } // Check attachments - attachments, err := models.GetAttachmentsByUUIDs(&ctx, updateAttachmentsList) + attachments, err := models.GetAttachmentsByUUIDs(ctx, updateAttachmentsList) if err != nil { return fmt.Errorf("GetAttachmentsByUUIDs [uuids: %v]: %v", updateAttachmentsList, err) } @@ -240,7 +240,7 @@ func UpdateRelease(doer *models.User, gitRepo *git.Repository, rel *models.Relea for uuid, newName := range editAttachments { if !deletedUUIDsMap[uuid] { - if err = models.UpdateAttachmentByUUID(&ctx, &models.Attachment{ + if err = models.UpdateAttachmentByUUID(ctx, &models.Attachment{ UUID: uuid, Name: newName, }, "name"); err != nil { diff --git a/services/repository/generate.go b/services/repository/generate.go index 3ba33cb870404..5a9f6aa6584bf 100644 --- a/services/repository/generate.go +++ b/services/repository/generate.go @@ -21,50 +21,50 @@ func GenerateRepository(doer, owner *models.User, templateRepo *models.Repositor } var generateRepo *models.Repository - if err = db.WithTx(func(ctx db.Context) error { - generateRepo, err = repo_module.GenerateRepository(&ctx, doer, owner, templateRepo, opts) + if err = db.WithTx(func(ctx *db.Context) error { + generateRepo, err = repo_module.GenerateRepository(ctx, doer, owner, templateRepo, opts) if err != nil { return err } // Git Content if opts.GitContent && !templateRepo.IsEmpty { - if err = repo_module.GenerateGitContent(&ctx, templateRepo, generateRepo); err != nil { + if err = repo_module.GenerateGitContent(ctx, templateRepo, generateRepo); err != nil { return err } } // Topics if opts.Topics { - if err = models.GenerateTopics(&ctx, templateRepo, generateRepo); err != nil { + if err = models.GenerateTopics(ctx, templateRepo, generateRepo); err != nil { return err } } // Git Hooks if opts.GitHooks { - if err = models.GenerateGitHooks(&ctx, templateRepo, generateRepo); err != nil { + if err = models.GenerateGitHooks(ctx, templateRepo, generateRepo); err != nil { return err } } // Webhooks if opts.Webhooks { - if err = models.GenerateWebhooks(&ctx, templateRepo, generateRepo); err != nil { + if err = models.GenerateWebhooks(ctx, templateRepo, generateRepo); err != nil { return err } } // Avatar if opts.Avatar && len(templateRepo.Avatar) > 0 { - if err = models.GenerateAvatar(&ctx, templateRepo, generateRepo); err != nil { + if err = models.GenerateAvatar(ctx, templateRepo, generateRepo); err != nil { return err } } // Issue Labels if opts.IssueLabels { - if err = models.GenerateIssueLabels(&ctx, templateRepo, generateRepo); err != nil { + if err = models.GenerateIssueLabels(ctx, templateRepo, generateRepo); err != nil { return err } }