Files
Gogs/routes/repo/repo.go

336 lines
8.6 KiB
Go
Raw Normal View History

2014-02-19 21:45:43 -05: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 (
"fmt"
2014-07-26 00:24:27 -04:00
"os"
2014-03-22 13:50:50 -04:00
"path"
"strings"
2014-03-22 13:50:50 -04:00
2014-07-26 00:24:27 -04:00
"github.com/Unknwon/com"
2017-02-09 19:29:59 -05:00
log "gopkg.in/clog.v1"
2014-05-05 19:58:13 -04:00
2018-05-27 08:53:48 +08:00
"github.com/gogs/git-module"
2018-05-27 08:53:48 +08:00
"github.com/gogs/gogs/models"
"github.com/gogs/gogs/models/errors"
"github.com/gogs/gogs/pkg/context"
"github.com/gogs/gogs/pkg/form"
"github.com/gogs/gogs/pkg/setting"
"github.com/gogs/gogs/pkg/tool"
2014-02-19 21:45:43 -05:00
)
2014-06-22 23:11:12 -04:00
const (
2017-04-05 09:17:21 -04:00
CREATE = "repo/create"
MIGRATE = "repo/migrate"
2014-06-22 23:11:12 -04:00
)
2017-06-03 07:26:09 -04:00
func MustBeNotBare(c *context.Context) {
if c.Repo.Repository.IsBare {
c.Handle(404, "MustBeNotBare", nil)
}
}
2017-06-03 07:26:09 -04:00
func checkContextUser(c *context.Context, uid int64) *models.User {
orgs, err := models.GetOwnedOrgsByUserIDDesc(c.User.ID, "updated_unix")
if err != nil {
2017-06-03 07:26:09 -04:00
c.Handle(500, "GetOwnedOrgsByUserIDDesc", err)
2015-08-28 18:33:09 +08:00
return nil
}
2017-06-03 07:26:09 -04:00
c.Data["Orgs"] = orgs
2015-08-28 18:33:09 +08:00
2015-07-19 17:11:16 +08:00
// Not equal means current user is an organization.
2017-06-03 07:26:09 -04:00
if uid == c.User.ID || uid == 0 {
return c.User
2015-07-19 17:11:16 +08:00
}
2015-08-08 22:43:14 +08:00
org, err := models.GetUserByID(uid)
if errors.IsUserNotExist(err) {
2017-06-03 07:26:09 -04:00
return c.User
2015-07-19 17:11:16 +08:00
}
if err != nil {
2017-06-03 07:26:09 -04:00
c.Handle(500, "GetUserByID", fmt.Errorf("[%d]: %v", uid, err))
2015-07-19 17:11:16 +08:00
return nil
2015-08-28 18:33:09 +08:00
}
// Check ownership of organization.
2017-06-03 07:26:09 -04:00
if !org.IsOrganization() || !(c.User.IsAdmin || org.IsOwnedBy(c.User.ID)) {
c.Error(403)
2015-07-19 17:11:16 +08:00
return nil
2014-11-05 23:30:04 -05:00
}
2015-07-19 17:11:16 +08:00
return org
2014-11-05 23:30:04 -05:00
}
2017-06-03 07:26:09 -04:00
func Create(c *context.Context) {
c.Data["Title"] = c.Tr("new_repo")
2014-07-26 00:24:27 -04:00
// Give default value for template to render.
2017-06-03 07:26:09 -04:00
c.Data["Gitignores"] = models.Gitignores
c.Data["Licenses"] = models.Licenses
c.Data["Readmes"] = models.Readmes
c.Data["readme"] = "Default"
c.Data["private"] = c.User.LastRepoVisibility
c.Data["IsForcedPrivate"] = setting.Repository.ForcePrivate
ctxUser := checkContextUser(c, c.QueryInt64("org"))
if c.Written() {
2014-11-05 23:30:04 -05:00
return
}
2017-06-03 07:26:09 -04:00
c.Data["ContextUser"] = ctxUser
2014-06-28 15:43:25 -04:00
2017-06-03 07:26:09 -04:00
c.HTML(200, CREATE)
2014-04-10 18:09:57 -04:00
}
2017-06-03 07:26:09 -04:00
func handleCreateError(c *context.Context, owner *models.User, err error, name, tpl string, form interface{}) {
2015-08-28 18:33:09 +08:00
switch {
case errors.IsReachLimitOfRepo(err):
2017-06-03 07:26:09 -04:00
c.RenderWithErr(c.Tr("repo.form.reach_limit_of_creation", owner.RepoCreationNum()), tpl, form)
2015-08-28 18:33:09 +08:00
case models.IsErrRepoAlreadyExist(err):
2017-06-03 07:26:09 -04:00
c.Data["Err_RepoName"] = true
c.RenderWithErr(c.Tr("form.repo_name_been_taken"), tpl, form)
2015-08-28 18:33:09 +08:00
case models.IsErrNameReserved(err):
2017-06-03 07:26:09 -04:00
c.Data["Err_RepoName"] = true
c.RenderWithErr(c.Tr("repo.form.name_reserved", err.(models.ErrNameReserved).Name), tpl, form)
2015-08-28 18:33:09 +08:00
case models.IsErrNamePatternNotAllowed(err):
2017-06-03 07:26:09 -04:00
c.Data["Err_RepoName"] = true
c.RenderWithErr(c.Tr("repo.form.name_pattern_not_allowed", err.(models.ErrNamePatternNotAllowed).Pattern), tpl, form)
2015-08-28 18:33:09 +08:00
default:
2017-06-03 07:26:09 -04:00
c.Handle(500, name, err)
2015-08-28 18:33:09 +08:00
}
}
2017-06-03 07:26:09 -04:00
func CreatePost(c *context.Context, f form.CreateRepo) {
c.Data["Title"] = c.Tr("new_repo")
2014-07-26 00:24:27 -04:00
2017-06-03 07:26:09 -04:00
c.Data["Gitignores"] = models.Gitignores
c.Data["Licenses"] = models.Licenses
c.Data["Readmes"] = models.Readmes
2014-02-19 21:45:43 -05:00
2017-06-03 07:26:09 -04:00
ctxUser := checkContextUser(c, f.UserID)
if c.Written() {
2015-07-19 17:11:16 +08:00
return
}
2017-06-03 07:26:09 -04:00
c.Data["ContextUser"] = ctxUser
2014-07-26 00:24:27 -04:00
2017-06-03 07:26:09 -04:00
if c.HasError() {
c.HTML(200, CREATE)
2014-03-22 16:00:46 -04:00
return
}
2017-06-03 07:26:09 -04:00
repo, err := models.CreateRepository(c.User, ctxUser, models.CreateRepoOptions{
Name: f.RepoName,
Description: f.Description,
Gitignores: f.Gitignores,
License: f.License,
Readme: f.Readme,
IsPrivate: f.Private || setting.Repository.ForcePrivate,
AutoInit: f.AutoInit,
2015-08-28 18:33:09 +08:00
})
2014-03-17 11:56:50 -04:00
if err == nil {
log.Trace("Repository created [%d]: %s/%s", repo.ID, ctxUser.Name, repo.Name)
2017-06-03 07:26:09 -04:00
c.Redirect(setting.AppSubURL + "/" + ctxUser.Name + "/" + repo.Name)
2014-03-08 21:25:38 -05:00
return
2014-03-09 20:06:29 -04:00
}
2014-04-12 20:35:35 -04:00
if repo != nil {
2016-07-24 01:08:22 +08:00
if errDelete := models.DeleteRepository(ctxUser.ID, repo.ID); errDelete != nil {
2014-07-26 00:24:27 -04:00
log.Error(4, "DeleteRepository: %v", errDelete)
2014-04-12 20:35:35 -04:00
}
}
2017-06-03 07:26:09 -04:00
handleCreateError(c, ctxUser, err, "CreatePost", CREATE, &f)
2014-04-10 18:09:57 -04:00
}
2014-04-09 21:28:00 +08:00
2017-06-03 07:26:09 -04:00
func Migrate(c *context.Context) {
c.Data["Title"] = c.Tr("new_migrate")
c.Data["private"] = c.User.LastRepoVisibility
c.Data["IsForcedPrivate"] = setting.Repository.ForcePrivate
c.Data["mirror"] = c.Query("mirror") == "1"
2014-08-01 00:06:19 -04:00
2017-06-03 07:26:09 -04:00
ctxUser := checkContextUser(c, c.QueryInt64("org"))
if c.Written() {
2014-11-05 23:30:04 -05:00
return
2014-08-01 00:06:19 -04:00
}
2017-06-03 07:26:09 -04:00
c.Data["ContextUser"] = ctxUser
2014-07-26 00:24:27 -04:00
2017-06-03 07:26:09 -04:00
c.HTML(200, MIGRATE)
}
2014-07-26 00:24:27 -04:00
2017-06-03 07:26:09 -04:00
func MigratePost(c *context.Context, f form.MigrateRepo) {
c.Data["Title"] = c.Tr("new_migrate")
2014-08-01 00:06:19 -04:00
2017-06-03 07:26:09 -04:00
ctxUser := checkContextUser(c, f.Uid)
if c.Written() {
2015-07-19 17:11:16 +08:00
return
2014-08-01 00:06:19 -04:00
}
2017-06-03 07:26:09 -04:00
c.Data["ContextUser"] = ctxUser
2014-07-26 00:24:27 -04:00
2017-06-03 07:26:09 -04:00
if c.HasError() {
c.HTML(200, MIGRATE)
return
}
2014-07-26 00:24:27 -04:00
2017-06-03 07:26:09 -04:00
remoteAddr, err := f.ParseRemoteAddr(c.User)
if err != nil {
if models.IsErrInvalidCloneAddr(err) {
2017-06-03 07:26:09 -04:00
c.Data["Err_CloneAddr"] = true
addrErr := err.(models.ErrInvalidCloneAddr)
switch {
case addrErr.IsURLError:
2017-06-03 07:26:09 -04:00
c.RenderWithErr(c.Tr("form.url_error"), MIGRATE, &f)
case addrErr.IsPermissionDenied:
2017-06-03 07:26:09 -04:00
c.RenderWithErr(c.Tr("repo.migrate.permission_denied"), MIGRATE, &f)
case addrErr.IsInvalidPath:
2017-06-03 07:26:09 -04:00
c.RenderWithErr(c.Tr("repo.migrate.invalid_local_path"), MIGRATE, &f)
default:
2017-06-03 07:26:09 -04:00
c.Handle(500, "Unknown error", err)
}
} else {
2017-06-03 07:26:09 -04:00
c.Handle(500, "ParseRemoteAddr", err)
}
return
}
2017-06-03 07:26:09 -04:00
repo, err := models.MigrateRepository(c.User, ctxUser, models.MigrateRepoOptions{
Name: f.RepoName,
Description: f.Description,
IsPrivate: f.Private || setting.Repository.ForcePrivate,
IsMirror: f.Mirror,
2015-10-25 04:26:26 -04:00
RemoteAddr: remoteAddr,
})
if err == nil {
log.Trace("Repository migrated [%d]: %s/%s", repo.ID, ctxUser.Name, f.RepoName)
2017-06-03 07:26:09 -04:00
c.Redirect(setting.AppSubURL + "/" + ctxUser.Name + "/" + f.RepoName)
return
}
2014-07-26 00:24:27 -04:00
if repo != nil {
2016-07-24 01:08:22 +08:00
if errDelete := models.DeleteRepository(ctxUser.ID, repo.ID); errDelete != nil {
log.Error(4, "DeleteRepository: %v", errDelete)
}
}
2014-07-26 00:24:27 -04:00
2015-11-19 13:45:07 -05:00
if strings.Contains(err.Error(), "Authentication failed") ||
2015-09-06 08:54:08 -04:00
strings.Contains(err.Error(), "could not read Username") {
2017-06-03 07:26:09 -04:00
c.Data["Err_Auth"] = true
c.RenderWithErr(c.Tr("form.auth_failed", models.HandleMirrorCredentials(err.Error(), true)), MIGRATE, &f)
return
2015-11-19 13:45:07 -05:00
} else if strings.Contains(err.Error(), "fatal:") {
2017-06-03 07:26:09 -04:00
c.Data["Err_CloneAddr"] = true
c.RenderWithErr(c.Tr("repo.migrate.failed", models.HandleMirrorCredentials(err.Error(), true)), MIGRATE, &f)
2015-11-19 13:45:07 -05:00
return
}
2017-06-03 07:26:09 -04:00
handleCreateError(c, ctxUser, err, "MigratePost", MIGRATE, &f)
}
2014-07-26 00:24:27 -04:00
2017-06-03 07:26:09 -04:00
func Action(c *context.Context) {
2014-08-10 20:11:18 -07:00
var err error
2017-06-03 07:26:09 -04:00
switch c.Params(":action") {
2014-08-10 20:11:18 -07:00
case "watch":
2017-06-03 07:26:09 -04:00
err = models.WatchRepo(c.User.ID, c.Repo.Repository.ID, true)
2014-08-10 20:11:18 -07:00
case "unwatch":
2017-06-03 07:26:09 -04:00
err = models.WatchRepo(c.User.ID, c.Repo.Repository.ID, false)
2014-08-10 20:11:18 -07:00
case "star":
2017-06-03 07:26:09 -04:00
err = models.StarRepo(c.User.ID, c.Repo.Repository.ID, true)
2014-08-10 20:11:18 -07:00
case "unstar":
2017-06-03 07:26:09 -04:00
err = models.StarRepo(c.User.ID, c.Repo.Repository.ID, false)
case "desc": // FIXME: this is not used
2017-06-03 07:26:09 -04:00
if !c.Repo.IsOwner() {
c.Error(404)
2014-08-10 20:11:18 -07:00
return
}
2017-06-03 07:26:09 -04:00
c.Repo.Repository.Description = c.Query("desc")
c.Repo.Repository.Website = c.Query("site")
err = models.UpdateRepository(c.Repo.Repository, false)
2014-08-10 20:11:18 -07:00
}
if err != nil {
2017-06-03 07:26:09 -04:00
c.Handle(500, fmt.Sprintf("Action (%s)", c.Params(":action")), err)
2014-08-10 20:11:18 -07:00
return
}
2015-07-24 04:50:05 +08:00
2017-06-03 07:26:09 -04:00
redirectTo := c.Query("redirect_to")
2015-07-24 04:50:05 +08:00
if len(redirectTo) == 0 {
2017-06-03 07:26:09 -04:00
redirectTo = c.Repo.RepoLink
2015-07-24 04:50:05 +08:00
}
2017-06-03 07:26:09 -04:00
c.Redirect(redirectTo)
2014-08-10 20:11:18 -07:00
}
2014-07-26 00:24:27 -04:00
2017-06-03 07:26:09 -04:00
func Download(c *context.Context) {
2014-09-24 17:43:33 -04:00
var (
2017-06-03 07:26:09 -04:00
uri = c.Params("*")
2014-09-24 17:43:33 -04:00
refName string
ext string
archivePath string
2014-11-02 14:18:37 +09:00
archiveType git.ArchiveType
2014-09-24 17:43:33 -04:00
)
switch {
case strings.HasSuffix(uri, ".zip"):
ext = ".zip"
2017-06-03 07:26:09 -04:00
archivePath = path.Join(c.Repo.GitRepo.Path, "archives/zip")
2014-11-02 14:18:37 +09:00
archiveType = git.ZIP
2014-09-24 17:43:33 -04:00
case strings.HasSuffix(uri, ".tar.gz"):
ext = ".tar.gz"
2017-06-03 07:26:09 -04:00
archivePath = path.Join(c.Repo.GitRepo.Path, "archives/targz")
2014-11-02 14:18:37 +09:00
archiveType = git.TARGZ
2014-07-26 00:24:27 -04:00
default:
log.Trace("Unknown format: %s", uri)
2017-06-03 07:26:09 -04:00
c.Error(404)
2014-06-25 05:35:23 -04:00
return
}
2014-09-24 17:43:33 -04:00
refName = strings.TrimSuffix(uri, ext)
2014-06-25 05:35:23 -04:00
2014-07-26 00:24:27 -04:00
if !com.IsDir(archivePath) {
if err := os.MkdirAll(archivePath, os.ModePerm); err != nil {
2017-06-03 07:26:09 -04:00
c.Handle(500, "Download -> os.MkdirAll(archivePath)", err)
2014-06-25 05:35:23 -04:00
return
}
}
2014-09-24 17:43:33 -04:00
// Get corresponding commit.
var (
commit *git.Commit
err error
)
2017-06-03 07:26:09 -04:00
gitRepo := c.Repo.GitRepo
2014-09-24 17:43:33 -04:00
if gitRepo.IsBranchExist(refName) {
commit, err = gitRepo.GetBranchCommit(refName)
2014-09-24 17:43:33 -04:00
if err != nil {
2017-06-03 07:26:09 -04:00
c.Handle(500, "GetBranchCommit", err)
2014-09-24 17:43:33 -04:00
return
}
} else if gitRepo.IsTagExist(refName) {
commit, err = gitRepo.GetTagCommit(refName)
2014-09-24 17:43:33 -04:00
if err != nil {
2017-06-03 07:26:09 -04:00
c.Handle(500, "GetTagCommit", err)
2014-09-24 17:43:33 -04:00
return
}
} else if len(refName) >= 7 && len(refName) <= 40 {
2014-09-24 17:43:33 -04:00
commit, err = gitRepo.GetCommit(refName)
if err != nil {
2017-06-03 07:26:09 -04:00
c.NotFound()
2014-09-24 17:43:33 -04:00
return
}
} else {
2017-06-03 07:26:09 -04:00
c.NotFound()
2014-09-24 17:43:33 -04:00
return
}
archivePath = path.Join(archivePath, tool.ShortSHA1(commit.ID.String())+ext)
2014-07-26 00:24:27 -04:00
if !com.IsFile(archivePath) {
2014-11-02 14:18:37 +09:00
if err := commit.CreateArchive(archivePath, archiveType); err != nil {
2017-06-03 07:26:09 -04:00
c.Handle(500, "Download -> CreateArchive "+archivePath, err)
2014-03-22 13:50:50 -04:00
return
}
}
2017-06-03 07:26:09 -04:00
c.ServeFile(archivePath, c.Repo.Repository.Name+"-"+refName+ext)
2014-03-22 13:50:50 -04:00
}