Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
wxiaoguang committed Jan 12, 2025
1 parent be4e961 commit d678515
Show file tree
Hide file tree
Showing 18 changed files with 170 additions and 187 deletions.
11 changes: 3 additions & 8 deletions models/repo/archiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,11 @@ func repoArchiverForRelativePath(relativePath string) (*RepoArchiver, error) {
if err != nil {
return nil, util.SilentWrap{Message: fmt.Sprintf("invalid storage path: %s", relativePath), Err: util.ErrInvalidArgument}
}
nameExts := strings.SplitN(parts[2], ".", 2)
if len(nameExts) != 2 {
commitID, archiveType := git.SplitArchiveNameType(parts[2])
if archiveType == git.ArchiveUnknown {
return nil, util.SilentWrap{Message: fmt.Sprintf("invalid storage path: %s", relativePath), Err: util.ErrInvalidArgument}
}

return &RepoArchiver{
RepoID: repoID,
CommitID: parts[1] + nameExts[0],
Type: git.ToArchiveType(nameExts[1]),
}, nil
return &RepoArchiver{RepoID: repoID, CommitID: commitID, Type: archiveType}, nil
}

// GetRepoArchiver get an archiver
Expand Down
4 changes: 4 additions & 0 deletions modules/git/ref.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ func RefNameFromTag(shortName string) RefName {
return RefName(TagPrefix + shortName)
}

func RefNameFromCommit(shortName string) RefName {
return RefName(shortName)
}

func (ref RefName) String() string {
return string(ref)
}
Expand Down
36 changes: 17 additions & 19 deletions modules/git/repo_archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,37 +16,35 @@ import (
type ArchiveType int

const (
// ZIP zip archive type
ZIP ArchiveType = iota + 1
// TARGZ tar gz archive type
TARGZ
// BUNDLE bundle archive type
BUNDLE
ArchiveUnknown ArchiveType = iota
ArchiveZip // 1
ArchiveTarGz // 2
ArchiveBundle // 3
)

// String converts an ArchiveType to string
// String converts an ArchiveType to string: the extension of the archive file without prefix dot
func (a ArchiveType) String() string {
switch a {
case ZIP:
case ArchiveZip:
return "zip"
case TARGZ:
case ArchiveTarGz:
return "tar.gz"
case BUNDLE:
case ArchiveBundle:
return "bundle"
}
return "unknown"
}

func ToArchiveType(s string) ArchiveType {
switch s {
case "zip":
return ZIP
case "tar.gz":
return TARGZ
case "bundle":
return BUNDLE
func SplitArchiveNameType(s string) (string, ArchiveType) {
switch {
case strings.HasSuffix(s, ".zip"):
return strings.TrimSuffix(s, ".zip"), ArchiveZip
case strings.HasSuffix(s, ".tar.gz"):
return strings.TrimSuffix(s, ".tar.gz"), ArchiveTarGz
case strings.HasSuffix(s, ".bundle"):
return strings.TrimSuffix(s, ".bundle"), ArchiveBundle
}
return 0
return s, ArchiveUnknown
}

// CreateArchive create archive content to the target path
Expand Down
32 changes: 32 additions & 0 deletions modules/git/repo_archive_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright 2024 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package git

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestArchiveType(t *testing.T) {
name, archiveType := SplitArchiveNameType("test.tar.gz")
assert.Equal(t, "test", name)
assert.Equal(t, "tar.gz", archiveType.String())

name, archiveType = SplitArchiveNameType("a/b/test.zip")
assert.Equal(t, "a/b/test", name)
assert.Equal(t, "zip", archiveType.String())

name, archiveType = SplitArchiveNameType("1234.bundle")
assert.Equal(t, "1234", name)
assert.Equal(t, "bundle", archiveType.String())

name, archiveType = SplitArchiveNameType("test")
assert.Equal(t, "test", name)
assert.Equal(t, "unknown", archiveType.String())

name, archiveType = SplitArchiveNameType("test.xz")
assert.Equal(t, "test.xz", name)
assert.Equal(t, "unknown", archiveType.String())
}
4 changes: 1 addition & 3 deletions options/locale/locale_en-US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -1115,9 +1115,7 @@ blame.ignore_revs = Ignoring revisions in <a href="%s">.git-blame-ignore-revs</a
blame.ignore_revs.failed = Failed to ignore revisions in <a href="%s">.git-blame-ignore-revs</a>.
user_search_tooltip = Shows a maximum of 30 users
tree_path_not_found_commit = Path %[1]s doesn't exist in commit %[2]s
tree_path_not_found_branch = Path %[1]s doesn't exist in branch %[2]s
tree_path_not_found_tag = Path %[1]s doesn't exist in tag %[2]s
tree_path_not_found = Path %[1]s doesn't exist in %[2]s

transfer.accept = Accept Transfer
transfer.accept_desc = Transfer to "%s"
Expand Down
8 changes: 4 additions & 4 deletions routers/api/v1/repo/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ func DownloadArchive(ctx *context.APIContext) {
var tp git.ArchiveType
switch ballType := ctx.PathParam("ball_type"); ballType {
case "tarball":
tp = git.TARGZ
tp = git.ArchiveTarGz
case "zipball":
tp = git.ZIP
tp = git.ArchiveZip
case "bundle":
tp = git.BUNDLE
tp = git.ArchiveBundle
default:
ctx.Error(http.StatusBadRequest, "", fmt.Sprintf("Unknown archive type: %s", ballType))
return
Expand All @@ -36,7 +36,7 @@ func DownloadArchive(ctx *context.APIContext) {
}
}

r, err := archiver_service.NewRequest(ctx.Repo.Repository.ID, ctx.Repo.GitRepo, ctx.PathParam("*"), tp)
r, err := archiver_service.NewRequest(ctx.Repo.Repository.ID, ctx.Repo.GitRepo, ctx.PathParam("*")+"."+tp.String())
if err != nil {
ctx.ServerError("NewRequest", err)
return
Expand Down
9 changes: 1 addition & 8 deletions routers/api/v1/repo/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,14 +293,7 @@ func GetArchive(ctx *context.APIContext) {
}

func archiveDownload(ctx *context.APIContext) {
uri := ctx.PathParam("*")
ext, tp, err := archiver_service.ParseFileName(uri)
if err != nil {
ctx.Error(http.StatusBadRequest, "ParseFileName", err)
return
}

aReq, err := archiver_service.NewRequest(ctx.Repo.Repository.ID, ctx.Repo.GitRepo, strings.TrimSuffix(uri, ext), tp)
aReq, err := archiver_service.NewRequest(ctx.Repo.Repository.ID, ctx.Repo.GitRepo, ctx.PathParam("*"))
if err != nil {
if errors.Is(err, archiver_service.ErrUnknownArchiveFormat{}) {
ctx.Error(http.StatusBadRequest, "unknown archive format", err)
Expand Down
2 changes: 1 addition & 1 deletion routers/web/feed/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func ShowFileFeed(ctx *context.Context, repo *repo.Repository, formatType string
}
commits, err := ctx.Repo.GitRepo.CommitsByFileAndRange(
git.CommitsByFileAndRangeOptions{
Revision: ctx.Repo.RefName,
Revision: ctx.Repo.RefFullName.ShortName(), // FIXME: legacy code used ShortName
File: fileName,
Page: 1,
})
Expand Down
4 changes: 2 additions & 2 deletions routers/web/repo/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ func FileHistory(ctx *context.Context) {
return
}

commitsCount, err := ctx.Repo.GitRepo.FileCommitsCount(ctx.Repo.RefName, fileName)
commitsCount, err := ctx.Repo.GitRepo.FileCommitsCount(ctx.Repo.RefFullName.ShortName(), fileName) // FIXME: legacy code used ShortName
if err != nil {
ctx.ServerError("FileCommitsCount", err)
return
Expand All @@ -238,7 +238,7 @@ func FileHistory(ctx *context.Context) {

commits, err := ctx.Repo.GitRepo.CommitsByFileAndRange(
git.CommitsByFileAndRangeOptions{
Revision: ctx.Repo.RefName,
Revision: ctx.Repo.RefFullName.ShortName(), // FIXME: legacy code used ShortName
File: fileName,
Page: page,
})
Expand Down
15 changes: 2 additions & 13 deletions routers/web/repo/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,14 @@
package repo

import (
"net/url"

"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/services/context"
)

func HandleGitError(ctx *context.Context, msg string, err error) {
if git.IsErrNotExist(err) {
refType := ""
switch {
case ctx.Repo.IsViewBranch:
refType = "branch"
case ctx.Repo.IsViewTag:
refType = "tag"
case ctx.Repo.IsViewCommit:
refType = "commit"
}
ctx.Data["NotFoundPrompt"] = ctx.Locale.Tr("repo.tree_path_not_found_"+refType, ctx.Repo.TreePath, url.PathEscape(ctx.Repo.RefName))
ctx.Data["NotFoundGoBackURL"] = ctx.Repo.RepoLink + "/src/" + refType + "/" + url.PathEscape(ctx.Repo.RefName)
ctx.Data["NotFoundPrompt"] = ctx.Locale.Tr("repo.tree_path_not_found", ctx.Repo.TreePath, ctx.Repo.RefTypeNameSubURL())
ctx.Data["NotFoundGoBackURL"] = ctx.Repo.RepoLink + "/src/" + ctx.Repo.RefTypeNameSubURL()
ctx.NotFound(msg, err)
} else {
ctx.ServerError(msg, err)
Expand Down
18 changes: 3 additions & 15 deletions routers/web/repo/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -463,13 +463,7 @@ func RedirectDownload(ctx *context.Context) {

// Download an archive of a repository
func Download(ctx *context.Context) {
uri := ctx.PathParam("*")
ext, tp, err := archiver_service.ParseFileName(uri)
if err != nil {
ctx.ServerError("ParseFileName", err)
return
}
aReq, err := archiver_service.NewRequest(ctx.Repo.Repository.ID, ctx.Repo.GitRepo, strings.TrimSuffix(uri, ext), tp)
aReq, err := archiver_service.NewRequest(ctx.Repo.Repository.ID, ctx.Repo.GitRepo, ctx.PathParam("*"))
if err != nil {
if errors.Is(err, archiver_service.ErrUnknownArchiveFormat{}) {
ctx.Error(http.StatusBadRequest, err.Error())
Expand Down Expand Up @@ -527,15 +521,9 @@ func download(ctx *context.Context, archiveName string, archiver *repo_model.Rep
// a request that's already in-progress, but the archiver service will just
// kind of drop it on the floor if this is the case.
func InitiateDownload(ctx *context.Context) {
uri := ctx.PathParam("*")
ext, tp, err := archiver_service.ParseFileName(uri)
if err != nil {
ctx.ServerError("ParseFileName", err)
return
}
aReq, err := archiver_service.NewRequest(ctx.Repo.Repository.ID, ctx.Repo.GitRepo, strings.TrimSuffix(uri, ext), tp)
aReq, err := archiver_service.NewRequest(ctx.Repo.Repository.ID, ctx.Repo.GitRepo, ctx.PathParam("*"))
if err != nil {
ctx.ServerError("archiver_service.NewRequest", err)
ctx.Error(http.StatusBadRequest, "invalid archive request")
return
}
if aReq == nil {
Expand Down
2 changes: 1 addition & 1 deletion routers/web/repo/view_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func prepareToRenderFile(ctx *context.Context, entry *git.TreeEntry) {
}
defer dataRc.Close()

ctx.Data["Title"] = ctx.Tr("repo.file.title", ctx.Repo.Repository.Name+"/"+path.Base(ctx.Repo.TreePath), ctx.Repo.RefName)
ctx.Data["Title"] = ctx.Tr("repo.file.title", ctx.Repo.Repository.Name+"/"+path.Base(ctx.Repo.TreePath), ctx.Repo.RefFullName.ShortName())
ctx.Data["FileIsSymlink"] = entry.IsLink()
ctx.Data["FileName"] = blob.Name()
ctx.Data["RawFileLink"] = ctx.Repo.RepoLink + "/raw/" + ctx.Repo.RefTypeNameSubURL() + "/" + util.PathEscapeSegments(ctx.Repo.TreePath)
Expand Down
2 changes: 1 addition & 1 deletion routers/web/repo/view_home.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func prepareToRenderDirectory(ctx *context.Context) {

if ctx.Repo.TreePath != "" {
ctx.Data["HideRepoInfo"] = true
ctx.Data["Title"] = ctx.Tr("repo.file.title", ctx.Repo.Repository.Name+"/"+path.Base(ctx.Repo.TreePath), ctx.Repo.RefName)
ctx.Data["Title"] = ctx.Tr("repo.file.title", ctx.Repo.Repository.Name+"/"+path.Base(ctx.Repo.TreePath), ctx.Repo.RefFullName.ShortName())
}

subfolder, readmeFile, err := findReadmeFileInEntries(ctx, entries, true)
Expand Down
Loading

0 comments on commit d678515

Please sign in to comment.