fix: repo submodule is nil

This commit is contained in:
viletyy 2021-07-09 11:08:17 +08:00
parent 90f228fd3e
commit 4674286786
1 changed files with 33 additions and 31 deletions

View File

@ -5,13 +5,14 @@
package repofiles
import (
"code.gitea.io/gitea/modules/log"
"fmt"
"net/url"
"path"
"strings"
"time"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/git"
api "code.gitea.io/gitea/modules/structs"
@ -40,12 +41,12 @@ func (ct *ContentType) String() string {
// GetContentsOrList gets the meta data of a file's contents (*ContentsResponse) if treePath not a tree
// directory, otherwise a listing of file contents ([]*ContentsResponse). Ref can be a branch, commit or tag
func GetContentsOrList(repo *models.Repository, treePath, ref string) (interface{}, error) {
startTotal:=time.Now()
log.Info("Enter time:%v",startTotal.Format("2006-01-02 15:04:05.000"))
startTotal := time.Now()
log.Info("Enter time:%v", startTotal.Format("2006-01-02 15:04:05.000"))
defer func() {
log.Info("***GetContentsOrList耗时:%v",time.Now().Sub(startTotal))
log.Info("***GetContentsOrList耗时:%v", time.Now().Sub(startTotal))
}()
start:=time.Now()
start := time.Now()
if repo.IsEmpty {
return make([]interface{}, 0), nil
}
@ -54,7 +55,7 @@ func GetContentsOrList(repo *models.Repository, treePath, ref string) (interface
}
origRef := ref
start=time.Now()
start = time.Now()
// Check that the path given in opts.treePath is valid (not a git path)
cleanTreePath := CleanUploadFileName(treePath)
if cleanTreePath == "" && treePath != "" {
@ -62,61 +63,61 @@ func GetContentsOrList(repo *models.Repository, treePath, ref string) (interface
Path: treePath,
}
}
log.Info("*****************GetContentsOrList.CleanUploadFileName:%v",time.Now().Sub(start))
start=time.Now()
log.Info("*****************GetContentsOrList.CleanUploadFileName:%v", time.Now().Sub(start))
start = time.Now()
treePath = cleanTreePath
gitRepo, err := git.OpenRepository(repo.RepoPath())
if err != nil {
return nil, err
}
defer gitRepo.Close()
log.Info("*****************GetContentsOrList.OpenRepository:%v",time.Now().Sub(start))
start=time.Now()
log.Info("*****************GetContentsOrList.OpenRepository:%v", time.Now().Sub(start))
start = time.Now()
// Get the commit object for the ref
commit, err := gitRepo.GetCommit(ref)
if err != nil {
return nil, err
}
log.Info("*****************GetContentsOrList.GetCommit:ret:%s treepath:%s", ref, treePath)
log.Info("*****************GetContentsOrList.GetCommit:%v",time.Now().Sub(start)," ref:",ref," treePath:",treePath)
start=time.Now()
log.Info("*****************GetContentsOrList.GetCommit:%v", time.Now().Sub(start), " ref:", ref, " treePath:", treePath)
start = time.Now()
entry, err := commit.GetTreeEntryByPath(treePath)
if err != nil {
return nil, err
}
log.Info("*****************GetContentsOrList.GetTreeEntryByPath:%v",time.Now().Sub(start))
log.Info("*****************GetContentsOrList.GetTreeEntryByPath:%v", time.Now().Sub(start))
if entry.Type() != "tree" {
a,b:= GetContents(repo, treePath, origRef, false)
log.Info("*****************GetContentsOrList.GetContents***:%v",time.Now().Sub(start))
return a,b
a, b := GetContents(repo, treePath, origRef, false)
log.Info("*****************GetContentsOrList.GetContents***:%v", time.Now().Sub(start))
return a, b
}
// We are in a directory, so we return a list of FileContentResponse objects
var fileList []*api.ContentsResponse
start=time.Now()
start = time.Now()
gitTree, err := commit.SubTree(treePath)
if err != nil {
return nil, err
}
log.Info("*****************GetContentsOrList.SubTree:%v",time.Now().Sub(start))
log.Info("*****************GetContentsOrList.SubTree:%v", time.Now().Sub(start))
entries, err := gitTree.ListEntries()
if err != nil {
return nil, err
}
start=time.Now()
start = time.Now()
//add by qiubing
commitsInfo, _, err := entries.GetCommitsInfo(commit, treePath, nil)
if err != nil {
return nil, err
}
//end by qiubing
start1:=time.Now()
log.Info("****GetContentsOrList.GetCommitsInfo:%v",time.Now().Sub(start))
start1 := time.Now()
log.Info("****GetContentsOrList.GetCommitsInfo:%v", time.Now().Sub(start))
for _, e := range entries {
subTreePath := path.Join(treePath, e.Name())
start2:=time.Now()
fileContentResponse, err := GetContentsExt(gitRepo,commit,repo, subTreePath, origRef, true)
log.Info("*****GetContentsOrList.GetContents:%s %v",e.Name(),time.Now().Sub(start2))
start2 := time.Now()
fileContentResponse, err := GetContentsExt(gitRepo, commit, repo, subTreePath, origRef, true)
log.Info("*****GetContentsOrList.GetContents:%s %v", e.Name(), time.Now().Sub(start2))
// add by qiubing
for _, commitInfo := range commitsInfo {
if treeEntry, ok := commitInfo[0].(*git.TreeEntry); ok {
@ -128,9 +129,9 @@ func GetContentsOrList(repo *models.Repository, treePath, ref string) (interface
entryCommit = value.Commit
}
fileContentResponse.LatestCommit = api.ContentsResponseCommit{
Message: entryCommit.CommitMessage,
Message: entryCommit.CommitMessage,
LatestCommitSha: entryCommit.ID.String(),
Created: entryCommit.Author.When.Unix(),
Created: entryCommit.Author.When.Unix(),
}
break
}
@ -143,14 +144,12 @@ func GetContentsOrList(repo *models.Repository, treePath, ref string) (interface
}
fileList = append(fileList, fileContentResponse)
}
log.Info("*****************GetContentsOrList.for-entries:%v",time.Now().Sub(start1))
log.Info("*****************GetContentsOrList.for-entries:%v", time.Now().Sub(start1))
return fileList, nil
}
// GetContents gets the meta data on a file's contents. Ref can be a branch, commit or tag
func GetContentsExt(gitRepo *git.Repository, commit *git.Commit,repo *models.Repository, treePath, ref string, forList bool) (*api.ContentsResponse, error) {
func GetContentsExt(gitRepo *git.Repository, commit *git.Commit, repo *models.Repository, treePath, ref string, forList bool) (*api.ContentsResponse, error) {
if ref == "" {
ref = repo.DefaultBranch
}
@ -223,7 +222,9 @@ func GetContentsExt(gitRepo *git.Repository, commit *git.Commit,repo *models.Rep
if err != nil {
return nil, err
}
contentsResponse.SubmoduleGitURL = &submodule.URL
if submodule != nil {
contentsResponse.SubmoduleGitURL = &submodule.URL
}
}
// Handle links
if entry.IsRegular() || entry.IsLink() {
@ -254,6 +255,7 @@ func GetContentsExt(gitRepo *git.Repository, commit *git.Commit,repo *models.Rep
return contentsResponse, nil
}
// GetContents gets the meta data on a file's contents. Ref can be a branch, commit or tag
func GetContents(repo *models.Repository, treePath, ref string, forList bool) (*api.ContentsResponse, error) {
if ref == "" {