From e02fac4968574694927859105e67aac3f665481d Mon Sep 17 00:00:00 2001 From: Unknwon Date: Fri, 14 Jul 2017 18:51:23 -0400 Subject: [PATCH] repo: fix panic on pull request submit (#4572) --- gogs.go | 2 +- models/repo.go | 9 ++++++--- models/user.go | 2 +- routes/repo/pull.go | 16 +++++++++++----- templates/.VERSION | 2 +- 5 files changed, 20 insertions(+), 11 deletions(-) diff --git a/gogs.go b/gogs.go index c4b397a9e..6761e3d30 100644 --- a/gogs.go +++ b/gogs.go @@ -16,7 +16,7 @@ import ( "github.com/gogits/gogs/pkg/setting" ) -const APP_VER = "0.11.25.0714" +const APP_VER = "0.11.26.0714" func init() { setting.AppVer = APP_VER diff --git a/models/repo.go b/models/repo.go index 88932449a..5a4deba43 100644 --- a/models/repo.go +++ b/models/repo.go @@ -2257,10 +2257,13 @@ func (repo *Repository) GetStargazers(page int) ([]*User, error) { // HasForkedRepo checks if given user has already forked a repository. // When user has already forked, it returns true along with the repository. -func HasForkedRepo(ownerID, repoID int64) (*Repository, bool) { +func HasForkedRepo(ownerID, repoID int64) (*Repository, bool, error) { repo := new(Repository) - has, _ := x.Where("owner_id = ? AND fork_id = ?", ownerID, repoID).Get(repo) - return repo, has + has, err := x.Where("owner_id = ? AND fork_id = ?", ownerID, repoID).Get(repo) + if err != nil { + return nil, false, err + } + return repo, has, repo.LoadAttributes() } // ForkRepository creates a fork of target repository under another user domain. diff --git a/models/user.go b/models/user.go index c4be871ed..e67def022 100644 --- a/models/user.go +++ b/models/user.go @@ -137,7 +137,7 @@ func (u *User) IsLocal() bool { // HasForkedRepo checks if user has already forked a repository with given ID. func (u *User) HasForkedRepo(repoID int64) bool { - _, has := HasForkedRepo(u.ID, repoID) + _, has, _ := HasForkedRepo(u.ID, repoID) return has } diff --git a/routes/repo/pull.go b/routes/repo/pull.go index 73757280d..48eb76e79 100644 --- a/routes/repo/pull.go +++ b/routes/repo/pull.go @@ -101,8 +101,11 @@ func ForkPost(c *context.Context, f form.CreateRepo) { return } - repo, has := models.HasForkedRepo(ctxUser.ID, baseRepo.ID) - if has { + repo, has, err := models.HasForkedRepo(ctxUser.ID, baseRepo.ID) + if err != nil { + c.ServerError("HasForkedRepo", err) + return + } else if has { c.Redirect(repo.Link()) return } @@ -119,7 +122,7 @@ func ForkPost(c *context.Context, f form.CreateRepo) { return } - repo, err := models.ForkRepository(c.User, ctxUser, baseRepo, f.RepoName, f.Description) + repo, err = models.ForkRepository(c.User, ctxUser, baseRepo, f.RepoName, f.Description) if err != nil { c.Data["Err_RepoName"] = true switch { @@ -475,8 +478,11 @@ func ParseCompareInfo(c *context.Context) (*models.User, *models.Repository, *gi // no need to check the fork relation. if !isSameRepo { var has bool - headRepo, has = models.HasForkedRepo(headUser.ID, baseRepo.ID) - if !has { + headRepo, has, err = models.HasForkedRepo(headUser.ID, baseRepo.ID) + if err != nil { + c.ServerError("HasForkedRepo", err) + return nil, nil, nil, nil, "", "" + } else if !has { log.Trace("ParseCompareInfo [base_repo_id: %d]: does not have fork or in same repository", baseRepo.ID) c.NotFound() return nil, nil, nil, nil, "", "" diff --git a/templates/.VERSION b/templates/.VERSION index e16dd497c..f9caef96c 100644 --- a/templates/.VERSION +++ b/templates/.VERSION @@ -1 +1 @@ -0.11.25.0714 \ No newline at end of file +0.11.26.0714 \ No newline at end of file