Browse Source

Fix download archive issue

pull/498/head
Unknwon 10 years ago
parent
commit
25268577a5
  1. 3
      cmd/web.go
  2. 2
      gogs.go
  3. 11
      modules/middleware/repo.go
  4. 52
      routers/repo/repo.go
  5. 2
      templates/.VERSION
  6. 8
      templates/repo/release/list.tmpl

3
cmd/web.go

@ -343,6 +343,7 @@ func runWeb(*cli.Context) {
r.Get("/issues/:index", repo.ViewIssue) r.Get("/issues/:index", repo.ViewIssue)
r.Get("/pulls", repo.Pulls) r.Get("/pulls", repo.Pulls)
r.Get("/branches", repo.Branches) r.Get("/branches", repo.Branches)
r.Get("/archive/*", repo.Download)
}, ignSignIn, middleware.RepoAssignment(true)) }, ignSignIn, middleware.RepoAssignment(true))
m.Group("/:username/:reponame", func(r *macaron.Router) { m.Group("/:username/:reponame", func(r *macaron.Router) {
@ -355,8 +356,6 @@ func runWeb(*cli.Context) {
r.Get("/commit/:branchname", repo.Diff) r.Get("/commit/:branchname", repo.Diff)
r.Get("/commit/:branchname/*", repo.Diff) r.Get("/commit/:branchname/*", repo.Diff)
r.Get("/releases", repo.Releases) r.Get("/releases", repo.Releases)
r.Get("/archive/:branchname/*.*", repo.Download)
r.Get("/archive/*.*", repo.Download)
r.Get("/compare/:before([a-z0-9]+)...:after([a-z0-9]+)", repo.CompareDiff) r.Get("/compare/:before([a-z0-9]+)...:after([a-z0-9]+)", repo.CompareDiff)
}, ignSignIn, middleware.RepoAssignment(true, true)) }, ignSignIn, middleware.RepoAssignment(true, true))

2
gogs.go

@ -17,7 +17,7 @@ import (
"github.com/gogits/gogs/modules/setting" "github.com/gogits/gogs/modules/setting"
) )
const APP_VER = "0.5.4.0923 Beta" const APP_VER = "0.5.4.0924 Beta"
func init() { func init() {
runtime.GOMAXPROCS(runtime.NumCPU()) runtime.GOMAXPROCS(runtime.NumCPU())

11
modules/middleware/repo.go

@ -200,7 +200,7 @@ func RepoAssignment(redirect bool, args ...bool) macaron.Handler {
ctx.Repo.Commit, err = gitRepo.GetCommitOfBranch(refName) ctx.Repo.Commit, err = gitRepo.GetCommitOfBranch(refName)
if err != nil { if err != nil {
ctx.Handle(404, "RepoAssignment invalid branch", nil) ctx.Handle(500, "RepoAssignment invalid branch", err)
return return
} }
ctx.Repo.CommitId = ctx.Repo.Commit.Id.String() ctx.Repo.CommitId = ctx.Repo.Commit.Id.String()
@ -209,14 +209,9 @@ func RepoAssignment(redirect bool, args ...bool) macaron.Handler {
ctx.Repo.IsTag = true ctx.Repo.IsTag = true
ctx.Repo.BranchName = refName ctx.Repo.BranchName = refName
ctx.Repo.Tag, err = gitRepo.GetTag(refName) ctx.Repo.Commit, err = gitRepo.GetCommitOfTag(refName)
if err != nil { if err != nil {
ctx.Handle(404, "RepoAssignment invalid tag", nil) ctx.Handle(500, "RepoAssignment invalid tag", err)
return
}
ctx.Repo.Commit, err = ctx.Repo.Tag.Commit()
if err != nil {
ctx.Handle(500, "RepoAssignment", fmt.Errorf("fail to get tag commit(%s): %v", refName, err))
return return
} }
ctx.Repo.CommitId = ctx.Repo.Commit.Id.String() ctx.Repo.CommitId = ctx.Repo.Commit.Id.String()

52
routers/repo/repo.go

@ -244,18 +244,25 @@ func Action(ctx *middleware.Context) {
} }
func Download(ctx *middleware.Context) { func Download(ctx *middleware.Context) {
ext := "." + ctx.Params(":ext") var (
uri = ctx.Params("*")
var archivePath string refName string
switch ext { ext string
case ".zip": archivePath string
)
switch {
case strings.HasSuffix(uri, ".zip"):
ext = ".zip"
archivePath = path.Join(ctx.Repo.GitRepo.Path, "archives/zip") archivePath = path.Join(ctx.Repo.GitRepo.Path, "archives/zip")
case ".tar.gz": case strings.HasSuffix(uri, ".tar.gz"):
ext = ".tar.gz"
archivePath = path.Join(ctx.Repo.GitRepo.Path, "archives/targz") archivePath = path.Join(ctx.Repo.GitRepo.Path, "archives/targz")
default: default:
ctx.Error(404) ctx.Error(404)
return return
} }
refName = strings.TrimSuffix(uri, ext)
if !com.IsDir(archivePath) { if !com.IsDir(archivePath) {
if err := os.MkdirAll(archivePath, os.ModePerm); err != nil { if err := os.MkdirAll(archivePath, os.ModePerm); err != nil {
@ -264,13 +271,42 @@ func Download(ctx *middleware.Context) {
} }
} }
// Get corresponding commit.
var (
commit *git.Commit
err error
)
gitRepo := ctx.Repo.GitRepo
if gitRepo.IsBranchExist(refName) {
commit, err = gitRepo.GetCommitOfBranch(refName)
if err != nil {
ctx.Handle(500, "Download", err)
return
}
} else if gitRepo.IsTagExist(refName) {
commit, err = gitRepo.GetCommitOfTag(refName)
if err != nil {
ctx.Handle(500, "Download", err)
return
}
} else if len(refName) == 40 {
commit, err = gitRepo.GetCommit(refName)
if err != nil {
ctx.Handle(404, "Download", nil)
return
}
} else {
ctx.Error(404)
return
}
archivePath = path.Join(archivePath, ctx.Repo.CommitId+ext) archivePath = path.Join(archivePath, ctx.Repo.CommitId+ext)
if !com.IsFile(archivePath) { if !com.IsFile(archivePath) {
if err := ctx.Repo.Commit.CreateArchive(archivePath, git.ZIP); err != nil { if err := commit.CreateArchive(archivePath, git.ZIP); err != nil {
ctx.Handle(500, "Download -> CreateArchive "+archivePath, err) ctx.Handle(500, "Download -> CreateArchive "+archivePath, err)
return return
} }
} }
ctx.ServeFile(archivePath, ctx.Repo.Repository.Name+"-"+base.ShortSha(ctx.Repo.CommitId)+ext) ctx.ServeFile(archivePath, ctx.Repo.Repository.Name+"-"+base.ShortSha(commit.Id.String())+ext)
} }

2
templates/.VERSION

@ -1 +1 @@
0.5.4.0923 Beta 0.5.4.0924 Beta

8
templates/repo/release/list.tmpl

@ -36,8 +36,8 @@
{{str2html .Note}} {{str2html .Note}}
</div> </div>
<p class="download"> <p class="download">
<a class="btn btn-default" href="{{$.RepoLink}}/archive/{{.TagName}}/{{$.Repository.Name}}.zip" rel="nofollow"><i class="fa fa-download"></i>Source Code (ZIP)</a> <a class="btn btn-default" href="{{$.RepoLink}}/archive/{{.TagName}}.zip" rel="nofollow"><i class="fa fa-download"></i>Source Code (ZIP)</a>
<a class="btn btn-default" href="{{$.RepoLink}}/archive/{{.TagName}}/{{$.Repository.Name}}.tar.gz"><i class="fa fa-download"></i>Source Code (TAR.GZ)</a> <a class="btn btn-default" href="{{$.RepoLink}}/archive/{{.TagName}}.tar.gz"><i class="fa fa-download"></i>Source Code (TAR.GZ)</a>
</p> </p>
<span class="dot">&nbsp;</span> <span class="dot">&nbsp;</span>
</div> </div>
@ -48,8 +48,8 @@
<div class="col-md-10"> <div class="col-md-10">
<h5 class="title"><a href="{{$.RepoLink}}/src/{{.TagName}}" rel="nofollow">{{.TagName}}</a><i class="fa fa-tag"></i></h5> <h5 class="title"><a href="{{$.RepoLink}}/src/{{.TagName}}" rel="nofollow">{{.TagName}}</a><i class="fa fa-tag"></i></h5>
<p class="download"> <p class="download">
<a class="download-link" href="{{$.RepoLink}}/archive/{{.TagName}}/{{$.Repository.Name}}.zip" rel="nofollow"><i class="fa fa-download"></i>zip</a> <a class="download-link" href="{{$.RepoLink}}/archive/{{.TagName}}.zip" rel="nofollow"><i class="fa fa-download"></i>zip</a>
<a class="download-link" href="{{$.RepoLink}}/archive/{{.TagName}}/{{$.Repository.Name}}.tar.gz"><i class="fa fa-download"></i>tar.gz</a> <a class="download-link" href="{{$.RepoLink}}/archive/{{.TagName}}.tar.gz"><i class="fa fa-download"></i>tar.gz</a>
</p> </p>
<span class="dot">&nbsp;</span> <span class="dot">&nbsp;</span>
</div> </div>

Loading…
Cancel
Save