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 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 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 09:10:34 +00:00
|
|
|
FORK base.TplName = "repo/pulls/fork"
|
2014-06-23 03:11:12 +00:00
|
|
|
PULLS base.TplName = "repo/pulls"
|
|
|
|
)
|
|
|
|
|
2015-08-08 09:10:34 +00:00
|
|
|
func getForkRepository(ctx *middleware.Context) *models.Repository {
|
|
|
|
forkRepo, err := models.GetRepositoryById(ctx.ParamsInt64(":repoid"))
|
|
|
|
if err != nil {
|
|
|
|
if models.IsErrRepoNotExist(err) {
|
|
|
|
ctx.Handle(404, "GetRepositoryById", nil)
|
|
|
|
} else {
|
|
|
|
ctx.Handle(500, "GetRepositoryById", err)
|
|
|
|
}
|
|
|
|
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 09:24:10 +00:00
|
|
|
repo, has := models.HasForkedRepo(ctxUser.Id, forkRepo.Id)
|
|
|
|
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 {
|
|
|
|
switch {
|
|
|
|
case models.IsErrRepoAlreadyExist(err):
|
|
|
|
ctx.Data["Err_RepoName"] = true
|
|
|
|
ctx.RenderWithErr(ctx.Tr("repo.settings.new_owner_has_same_repo"), FORK, &form)
|
|
|
|
case models.IsErrNameReserved(err):
|
|
|
|
ctx.Data["Err_RepoName"] = true
|
|
|
|
ctx.RenderWithErr(ctx.Tr("repo.form.name_reserved", err.(models.ErrNameReserved).Name), FORK, &form)
|
|
|
|
case models.IsErrNamePatternNotAllowed(err):
|
|
|
|
ctx.Data["Err_RepoName"] = true
|
|
|
|
ctx.RenderWithErr(ctx.Tr("repo.form.name_pattern_not_allowed", err.(models.ErrNamePatternNotAllowed).Pattern), FORK, &form)
|
|
|
|
default:
|
|
|
|
ctx.Handle(500, "ForkPost", err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Trace("Repository forked[%d]: %s/%s", forkRepo.Id, ctxUser.Name, repo.Name)
|
|
|
|
ctx.Redirect(setting.AppSubUrl + "/" + ctxUser.Name + "/" + repo.Name)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|