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 (
|
2014-07-26 02:28:04 -04:00
|
|
|
"fmt"
|
2014-07-26 00:24:27 -04:00
|
|
|
"os"
|
2014-03-22 13:50:50 -04:00
|
|
|
"path"
|
2014-07-26 02:28:04 -04:00
|
|
|
"strings"
|
2014-03-22 13:50:50 -04:00
|
|
|
|
2019-10-23 23:03:17 -07:00
|
|
|
"github.com/unknwon/com"
|
2020-02-20 02:25:02 +08:00
|
|
|
log "unknwon.dev/clog/v2"
|
2014-05-05 19:58:13 -04:00
|
|
|
|
2018-05-27 08:53:48 +08:00
|
|
|
"github.com/gogs/git-module"
|
2015-12-09 20:46:05 -05:00
|
|
|
|
2019-10-24 01:51:46 -07:00
|
|
|
"gogs.io/gogs/internal/context"
|
|
|
|
|
"gogs.io/gogs/internal/db"
|
|
|
|
|
"gogs.io/gogs/internal/db/errors"
|
|
|
|
|
"gogs.io/gogs/internal/form"
|
|
|
|
|
"gogs.io/gogs/internal/setting"
|
|
|
|
|
"gogs.io/gogs/internal/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)
|
2016-01-07 11:07:17 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-24 01:51:46 -07:00
|
|
|
func checkContextUser(c *context.Context, uid int64) *db.User {
|
|
|
|
|
orgs, err := db.GetOwnedOrgsByUserIDDesc(c.User.ID, "updated_unix")
|
2015-09-07 13:58:23 -04:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2019-10-24 01:51:46 -07:00
|
|
|
org, err := db.GetUserByID(uid)
|
2017-03-16 17:18:43 -04:00
|
|
|
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) {
|
2018-06-11 21:06:24 +08:00
|
|
|
c.Title("new_repo")
|
|
|
|
|
c.RequireAutosize()
|
2014-07-26 00:24:27 -04:00
|
|
|
|
|
|
|
|
// Give default value for template to render.
|
2019-10-24 01:51:46 -07:00
|
|
|
c.Data["Gitignores"] = db.Gitignores
|
|
|
|
|
c.Data["Licenses"] = db.Licenses
|
|
|
|
|
c.Data["Readmes"] = db.Readmes
|
2017-06-03 07:26:09 -04:00
|
|
|
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
|
2014-07-26 02:28:04 -04:00
|
|
|
}
|
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
|
|
|
}
|
2014-03-07 16:05:18 -05:00
|
|
|
|
2019-10-24 01:51:46 -07:00
|
|
|
func handleCreateError(c *context.Context, owner *db.User, err error, name, tpl string, form interface{}) {
|
2015-08-28 18:33:09 +08:00
|
|
|
switch {
|
2017-04-07 16:00:25 -04:00
|
|
|
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)
|
2019-10-24 01:51:46 -07:00
|
|
|
case db.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)
|
2019-10-24 01:51:46 -07:00
|
|
|
case db.IsErrNameReserved(err):
|
2017-06-03 07:26:09 -04:00
|
|
|
c.Data["Err_RepoName"] = true
|
2019-10-24 01:51:46 -07:00
|
|
|
c.RenderWithErr(c.Tr("repo.form.name_reserved", err.(db.ErrNameReserved).Name), tpl, form)
|
|
|
|
|
case db.IsErrNamePatternNotAllowed(err):
|
2017-06-03 07:26:09 -04:00
|
|
|
c.Data["Err_RepoName"] = true
|
2019-10-24 01:51:46 -07:00
|
|
|
c.RenderWithErr(c.Tr("repo.form.name_pattern_not_allowed", err.(db.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
|
|
|
|
2019-10-24 01:51:46 -07:00
|
|
|
c.Data["Gitignores"] = db.Gitignores
|
|
|
|
|
c.Data["Licenses"] = db.Licenses
|
|
|
|
|
c.Data["Readmes"] = db.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
|
2014-07-26 02:28:04 -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, CREATE)
|
2014-03-22 16:00:46 -04:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-24 01:51:46 -07:00
|
|
|
repo, err := db.CreateRepository(c.User, ctxUser, db.CreateRepoOptions{
|
2017-02-23 11:39:09 -05:00
|
|
|
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 {
|
2016-08-15 05:53:47 -07:00
|
|
|
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 {
|
2019-10-24 01:51:46 -07:00
|
|
|
if errDelete := db.DeleteRepository(ctxUser.ID, repo.ID); errDelete != nil {
|
2020-02-20 02:25:02 +08:00
|
|
|
log.Error("DeleteRepository: %v", errDelete)
|
2014-04-12 20:35:35 -04:00
|
|
|
}
|
|
|
|
|
}
|
2015-03-26 17:11:47 -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 02:28:04 -04:00
|
|
|
}
|
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)
|
2014-07-26 02:28:04 -04:00
|
|
|
return
|
|
|
|
|
}
|
2014-07-26 00:24:27 -04:00
|
|
|
|
2017-06-03 07:26:09 -04:00
|
|
|
remoteAddr, err := f.ParseRemoteAddr(c.User)
|
2015-11-03 18:40:52 -05:00
|
|
|
if err != nil {
|
2019-10-24 01:51:46 -07:00
|
|
|
if db.IsErrInvalidCloneAddr(err) {
|
2017-06-03 07:26:09 -04:00
|
|
|
c.Data["Err_CloneAddr"] = true
|
2019-10-24 01:51:46 -07:00
|
|
|
addrErr := err.(db.ErrInvalidCloneAddr)
|
2015-11-03 18:40:52 -05:00
|
|
|
switch {
|
|
|
|
|
case addrErr.IsURLError:
|
2017-06-03 07:26:09 -04:00
|
|
|
c.RenderWithErr(c.Tr("form.url_error"), MIGRATE, &f)
|
2015-11-03 18:40:52 -05:00
|
|
|
case addrErr.IsPermissionDenied:
|
2017-06-03 07:26:09 -04:00
|
|
|
c.RenderWithErr(c.Tr("repo.migrate.permission_denied"), MIGRATE, &f)
|
2015-11-03 18:40:52 -05:00
|
|
|
case addrErr.IsInvalidPath:
|
2017-06-03 07:26:09 -04:00
|
|
|
c.RenderWithErr(c.Tr("repo.migrate.invalid_local_path"), MIGRATE, &f)
|
2015-11-03 18:40:52 -05:00
|
|
|
default:
|
2017-06-03 07:26:09 -04:00
|
|
|
c.Handle(500, "Unknown error", err)
|
2015-11-03 18:40:52 -05:00
|
|
|
}
|
|
|
|
|
} else {
|
2017-06-03 07:26:09 -04:00
|
|
|
c.Handle(500, "ParseRemoteAddr", err)
|
2015-02-22 09:49:25 -05:00
|
|
|
}
|
2015-01-02 12:14:11 +02:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-24 01:51:46 -07:00
|
|
|
repo, err := db.MigrateRepository(c.User, ctxUser, db.MigrateRepoOptions{
|
2017-02-23 11:39:09 -05:00
|
|
|
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,
|
|
|
|
|
})
|
2014-07-26 02:28:04 -04:00
|
|
|
if err == nil {
|
2017-02-23 11:39:09 -05:00
|
|
|
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)
|
2014-07-26 02:28:04 -04:00
|
|
|
return
|
|
|
|
|
}
|
2014-07-26 00:24:27 -04:00
|
|
|
|
2014-07-26 02:28:04 -04:00
|
|
|
if repo != nil {
|
2019-10-24 01:51:46 -07:00
|
|
|
if errDelete := db.DeleteRepository(ctxUser.ID, repo.ID); errDelete != nil {
|
2020-02-20 02:25:02 +08:00
|
|
|
log.Error("DeleteRepository: %v", errDelete)
|
2014-07-26 02:28:04 -04:00
|
|
|
}
|
|
|
|
|
}
|
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
|
2019-10-24 01:51:46 -07:00
|
|
|
c.RenderWithErr(c.Tr("form.auth_failed", db.HandleMirrorCredentials(err.Error(), true)), MIGRATE, &f)
|
2014-07-26 02:28:04 -04:00
|
|
|
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
|
2019-10-24 01:51:46 -07:00
|
|
|
c.RenderWithErr(c.Tr("repo.migrate.failed", db.HandleMirrorCredentials(err.Error(), true)), MIGRATE, &f)
|
2015-11-19 13:45:07 -05:00
|
|
|
return
|
2014-07-26 02:28:04 -04:00
|
|
|
}
|
2015-03-26 17:11:47 -04:00
|
|
|
|
2017-06-03 07:26:09 -04:00
|
|
|
handleCreateError(c, ctxUser, err, "MigratePost", MIGRATE, &f)
|
2014-07-26 02:28:04 -04:00
|
|
|
}
|
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":
|
2019-10-24 01:51:46 -07:00
|
|
|
err = db.WatchRepo(c.User.ID, c.Repo.Repository.ID, true)
|
2014-08-10 20:11:18 -07:00
|
|
|
case "unwatch":
|
2019-09-22 06:37:20 +03:00
|
|
|
if userID := c.QueryInt64("user_id"); userID != 0 {
|
|
|
|
|
if c.User.IsAdmin {
|
2019-10-24 01:51:46 -07:00
|
|
|
err = db.WatchRepo(userID, c.Repo.Repository.ID, false)
|
2019-09-22 06:37:20 +03:00
|
|
|
}
|
|
|
|
|
} else {
|
2019-10-24 01:51:46 -07:00
|
|
|
err = db.WatchRepo(c.User.ID, c.Repo.Repository.ID, false)
|
2019-09-22 06:37:20 +03:00
|
|
|
}
|
2014-08-10 20:11:18 -07:00
|
|
|
case "star":
|
2019-10-24 01:51:46 -07:00
|
|
|
err = db.StarRepo(c.User.ID, c.Repo.Repository.ID, true)
|
2014-08-10 20:11:18 -07:00
|
|
|
case "unstar":
|
2019-10-24 01:51:46 -07:00
|
|
|
err = db.StarRepo(c.User.ID, c.Repo.Repository.ID, false)
|
2016-01-07 11:07:17 +08:00
|
|
|
case "desc": // FIXME: this is not used
|
2017-06-03 07:26:09 -04:00
|
|
|
if !c.Repo.IsOwner() {
|
2018-09-28 23:19:08 -04:00
|
|
|
c.NotFound()
|
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")
|
2019-10-24 01:51:46 -07:00
|
|
|
err = db.UpdateRepository(c.Repo.Repository, false)
|
2014-08-10 20:11:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
2018-09-28 23:19:08 -04:00
|
|
|
c.ServerError(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")
|
2018-09-28 23:19:08 -04:00
|
|
|
if !tool.IsSameSiteURLPath(redirectTo) {
|
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:
|
2015-12-13 18:20:39 -05:00
|
|
|
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) {
|
2015-12-09 20:46:05 -05:00
|
|
|
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) {
|
2015-12-09 20:46:05 -05:00
|
|
|
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
|
|
|
|
|
}
|
2017-02-12 18:28:42 -05:00
|
|
|
} 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
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-06 17:13:53 -04:00
|
|
|
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
|
|
|
}
|