Optimized recursive part of GetTree though utilizing git ls-tree -r command
This commit is contained in:
parent
b7a13d84dc
commit
487758f8f5
|
@ -7,9 +7,9 @@ package repo
|
||||||
import (
|
import (
|
||||||
"code.gitea.io/gitea/modules/context"
|
"code.gitea.io/gitea/modules/context"
|
||||||
"fmt"
|
"fmt"
|
||||||
_"code.gitea.io/git"
|
|
||||||
"code.gitea.io/gitea/modules/setting"
|
"code.gitea.io/gitea/modules/setting"
|
||||||
"strings"
|
"strings"
|
||||||
|
"code.gitea.io/git"
|
||||||
)
|
)
|
||||||
|
|
||||||
type TreeEntry struct {
|
type TreeEntry struct {
|
||||||
|
@ -34,59 +34,64 @@ func GetTree(ctx *context.APIContext) {
|
||||||
ctx.Error(400, "sha not provided", nil)
|
ctx.Error(400, "sha not provided", nil)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
Temp := GetTreeBySHA(ctx, nil, "", sha, ctx.QueryBool("recursive"))
|
Tree := GetTreeBySHA(ctx, sha)
|
||||||
if Temp != nil {
|
if Tree != nil {
|
||||||
ctx.JSON(200, Temp)
|
ctx.JSON(200, Tree)
|
||||||
} else {
|
} else {
|
||||||
ctx.Error(400, "sha invalid", nil)
|
ctx.Error(400, "sha invalid", nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetTreeBySHA(ctx *context.APIContext, tree *Tree, CurrentPath string, sha string, recursive bool) *Tree {
|
func GetTreeBySHA(ctx *context.APIContext, sha string) *Tree {
|
||||||
GitTree, err := ctx.Repo.GitRepo.GetTree(sha)
|
GitTree, err := ctx.Repo.GitRepo.GetTree(sha)
|
||||||
if err != nil {
|
if err != nil || GitTree == nil{
|
||||||
return tree
|
return nil
|
||||||
}
|
}
|
||||||
|
tree := new(Tree)
|
||||||
RepoID := strings.TrimRight(setting.AppURL, "/") + "/api/v1/repos/" + ctx.Repo.Repository.Owner.Name + "/" + ctx.Repo.Repository.Name
|
RepoID := strings.TrimRight(setting.AppURL, "/") + "/api/v1/repos/" + ctx.Repo.Repository.Owner.Name + "/" + ctx.Repo.Repository.Name
|
||||||
if tree == nil {
|
tree.SHA = GitTree.ID.String()
|
||||||
tree = new(Tree)
|
tree.URL = RepoID + "/trees/" + tree.SHA
|
||||||
if GitTree != nil {
|
var Entries git.Entries
|
||||||
tree.SHA = GitTree.ID.String()
|
if ctx.QueryBool("recursive") {
|
||||||
tree.URL = RepoID + "/trees/" + tree.SHA;
|
Entries, err = GitTree.ListEntriesRecursive()
|
||||||
}
|
} else {
|
||||||
|
Entries, err = GitTree.ListEntries()
|
||||||
}
|
}
|
||||||
if GitTree == nil {
|
|
||||||
return tree
|
|
||||||
}
|
|
||||||
Trees, err := GitTree.ListEntries()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return tree
|
return tree
|
||||||
}
|
}
|
||||||
if len(CurrentPath) != 0 {
|
RepoIDLen := len(RepoID)
|
||||||
CurrentPath += "/"
|
BlobURL := make([]byte, RepoIDLen + 47)
|
||||||
|
copy(BlobURL[:], RepoID)
|
||||||
|
copy(BlobURL[RepoIDLen:], "/blobs/")
|
||||||
|
TreeURL := make([]byte, RepoIDLen + 47)
|
||||||
|
copy(TreeURL[:], RepoID)
|
||||||
|
copy(TreeURL[RepoIDLen:], "/trees/")
|
||||||
|
CopyPos := len(TreeURL) - 40
|
||||||
|
|
||||||
|
if len(Entries) > 1000 {
|
||||||
|
tree.Entries = make([]TreeEntry, 1000)
|
||||||
|
} else {
|
||||||
|
tree.Entries = make([]TreeEntry, len(Entries))
|
||||||
}
|
}
|
||||||
for e := range Trees {
|
for e := range Entries {
|
||||||
if len(tree.Entries) > 1000 {
|
if e > 1000 {
|
||||||
tree.Truncated = true
|
tree.Truncated = true
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
E_URL := RepoID
|
|
||||||
if Trees[e].IsDir() {
|
|
||||||
E_URL += "/trees/"
|
|
||||||
} else {
|
|
||||||
E_URL += "/blobs/"
|
|
||||||
}
|
|
||||||
tree.Entries = append(tree.Entries, TreeEntry{
|
|
||||||
CurrentPath + Trees[e].Name(),
|
|
||||||
fmt.Sprintf("%06x", Trees[e].Mode()),
|
|
||||||
string(Trees[e].Type),
|
|
||||||
Trees[e].Size(),
|
|
||||||
Trees[e].ID.String(),
|
|
||||||
E_URL + Trees[e].ID.String()})
|
|
||||||
|
|
||||||
if recursive && Trees[e].IsDir() {
|
tree.Entries[e].Path = Entries[e].Name()
|
||||||
tree = GetTreeBySHA(ctx, tree, CurrentPath + Trees[e].Name(), Trees[e].ID.String(), recursive)
|
tree.Entries[e].Mode = fmt.Sprintf("%06x", Entries[e].Mode())
|
||||||
|
tree.Entries[e].Type = string(Entries[e].Type)
|
||||||
|
tree.Entries[e].Size = Entries[e].Size()
|
||||||
|
tree.Entries[e].SHA = Entries[e].ID.String()
|
||||||
|
|
||||||
|
if Entries[e].IsDir() {
|
||||||
|
copy(TreeURL[CopyPos:], Entries[e].ID.String())
|
||||||
|
tree.Entries[e].URL = string(TreeURL[:])
|
||||||
|
} else {
|
||||||
|
copy(BlobURL[CopyPos:], Entries[e].ID.String())
|
||||||
|
tree.Entries[e].URL = string(BlobURL[:])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return tree
|
return tree
|
||||||
|
|
Loading…
Reference in New Issue
Block a user