gitea/routers/repo/pull.go

183 lines
4.5 KiB
Go
Raw Normal View History

2014-03-24 10:25:15 +00:00
// Copyright 2014 The Gogs Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package repo
import (
2015-08-08 14:43:14 +00:00
"fmt"
"strings"
2015-08-08 09:10:34 +00:00
"github.com/gogits/gogs/models"
"github.com/gogits/gogs/modules/auth"
2014-06-23 03:11:12 +00:00
"github.com/gogits/gogs/modules/base"
2015-08-08 14:43:14 +00:00
"github.com/gogits/gogs/modules/git"
2015-08-08 09:10:34 +00:00
"github.com/gogits/gogs/modules/log"
2014-03-24 10:25:15 +00:00
"github.com/gogits/gogs/modules/middleware"
2015-08-08 09:10:34 +00:00
"github.com/gogits/gogs/modules/setting"
2014-03-24 10:25:15 +00:00
)
2014-06-23 03:11:12 +00:00
const (
2015-08-08 14:43:14 +00:00
FORK base.TplName = "repo/pulls/fork"
COMPARE_PULL base.TplName = "repo/pulls/compare"
PULLS base.TplName = "repo/pulls"
2014-06-23 03:11:12 +00:00
)
2015-08-08 09:10:34 +00:00
func getForkRepository(ctx *middleware.Context) *models.Repository {
2015-08-08 14:43:14 +00:00
forkRepo, err := models.GetRepositoryByID(ctx.ParamsInt64(":repoid"))
2015-08-08 09:10:34 +00:00
if err != nil {
if models.IsErrRepoNotExist(err) {
2015-08-08 14:43:14 +00:00
ctx.Handle(404, "GetRepositoryByID", nil)
2015-08-08 09:10:34 +00:00
} else {
2015-08-08 14:43:14 +00:00
ctx.Handle(500, "GetRepositoryByID", err)
2015-08-08 09:10:34 +00:00
}
return nil
}
ctx.Data["repo_name"] = forkRepo.Name
ctx.Data["desc"] = forkRepo.Description
ctx.Data["IsPrivate"] = forkRepo.IsPrivate
if err = forkRepo.GetOwner(); err != nil {
ctx.Handle(500, "GetOwner", err)
return nil
}
ctx.Data["ForkFrom"] = forkRepo.Owner.Name + "/" + forkRepo.Name
if err := ctx.User.GetOrganizations(); err != nil {
ctx.Handle(500, "GetOrganizations", err)
return nil
}
ctx.Data["Orgs"] = ctx.User.Orgs
return forkRepo
}
func Fork(ctx *middleware.Context) {
ctx.Data["Title"] = ctx.Tr("new_fork")
getForkRepository(ctx)
if ctx.Written() {
return
}
ctx.Data["ContextUser"] = ctx.User
ctx.HTML(200, FORK)
}
func ForkPost(ctx *middleware.Context, form auth.CreateRepoForm) {
ctx.Data["Title"] = ctx.Tr("new_fork")
forkRepo := getForkRepository(ctx)
if ctx.Written() {
return
}
ctxUser := checkContextUser(ctx, form.Uid)
if ctx.Written() {
return
}
ctx.Data["ContextUser"] = ctxUser
if ctx.HasError() {
ctx.HTML(200, FORK)
return
}
2015-08-08 14:43:14 +00:00
repo, has := models.HasForkedRepo(ctxUser.Id, forkRepo.ID)
2015-08-08 09:24:10 +00:00
if has {
ctx.Redirect(setting.AppSubUrl + "/" + ctxUser.Name + "/" + repo.Name)
return
}
2015-08-08 09:10:34 +00:00
// Check ownership of organization.
if ctxUser.IsOrganization() {
if !ctxUser.IsOwnedBy(ctx.User.Id) {
ctx.Error(403)
return
}
}
repo, err := models.ForkRepository(ctxUser, forkRepo, form.RepoName, form.Description)
if err != nil {
2015-08-31 07:24:28 +00:00
ctx.Data["Err_RepoName"] = true
2015-08-08 09:10:34 +00:00
switch {
case models.IsErrRepoAlreadyExist(err):
ctx.RenderWithErr(ctx.Tr("repo.settings.new_owner_has_same_repo"), FORK, &form)
case models.IsErrNameReserved(err):
ctx.RenderWithErr(ctx.Tr("repo.form.name_reserved", err.(models.ErrNameReserved).Name), FORK, &form)
case models.IsErrNamePatternNotAllowed(err):
ctx.RenderWithErr(ctx.Tr("repo.form.name_pattern_not_allowed", err.(models.ErrNamePatternNotAllowed).Pattern), FORK, &form)
default:
ctx.Handle(500, "ForkPost", err)
}
return
}
2015-08-08 14:43:14 +00:00
log.Trace("Repository forked[%d]: %s/%s", forkRepo.ID, ctxUser.Name, repo.Name)
2015-08-08 09:10:34 +00:00
ctx.Redirect(setting.AppSubUrl + "/" + ctxUser.Name + "/" + repo.Name)
}
2015-08-08 14:43:14 +00:00
func CompareAndPullRequest(ctx *middleware.Context) {
2015-08-31 07:24:28 +00:00
ctx.Data["Title"] = ctx.Tr("repo.pulls.compare_changes")
ctx.Data["PageIsComparePull"] = true
repo := ctx.Repo.Repository
// Get compare branch information.
2015-08-08 14:43:14 +00:00
infos := strings.Split(ctx.Params("*"), "...")
if len(infos) != 2 {
ctx.Handle(404, "CompareAndPullRequest", nil)
return
}
baseBranch := infos[0]
ctx.Data["BaseBranch"] = baseBranch
headInfos := strings.Split(infos[1], ":")
if len(headInfos) != 2 {
ctx.Handle(404, "CompareAndPullRequest", nil)
return
}
headUser := headInfos[0]
headBranch := headInfos[1]
ctx.Data["HeadBranch"] = headBranch
// TODO: check if branches are valid.
fmt.Println(baseBranch, headUser, headBranch)
// TODO: add organization support
// Check if current user has fork of repository.
2015-08-31 07:24:28 +00:00
headRepo, has := models.HasForkedRepo(ctx.User.Id, repo.ID)
2015-08-08 14:43:14 +00:00
if !has {
ctx.Handle(404, "HasForkedRepo", nil)
return
}
headGitRepo, err := git.OpenRepository(models.RepoPath(ctx.User.Name, headRepo.Name))
if err != nil {
ctx.Handle(500, "OpenRepository", err)
return
}
headBranches, err := headGitRepo.GetBranches()
if err != nil {
ctx.Handle(500, "GetBranches", err)
return
}
ctx.Data["HeadBranches"] = headBranches
2015-08-31 07:24:28 +00:00
// Setup information for new form.
RetrieveRepoMetas(ctx, ctx.Repo.Repository)
if ctx.Written() {
return
}
// Get diff information.
2015-08-08 14:43:14 +00:00
ctx.HTML(200, COMPARE_PULL)
}
2014-07-26 04:24:27 +00:00
func Pulls(ctx *middleware.Context) {
2014-03-24 10:25:15 +00:00
ctx.Data["IsRepoToolbarPulls"] = true
2014-06-23 03:11:12 +00:00
ctx.HTML(200, PULLS)
2014-03-24 10:25:15 +00:00
}