mirror of
https://github.com/go-gitea/gitea.git
synced 2024-11-22 14:28:08 +08:00
c03baab678
When opening a repository, it will call `ensureValidRepository` and also `CatFileBatch`. But sometimes these will not be used until repository closed. So it's a waste of CPU to invoke 3 times git command for every open repository. This PR removed all of these from `OpenRepository` but only kept checking whether the folder exists. When a batch is necessary, the necessary functions will be invoked.
99 lines
2.0 KiB
Go
99 lines
2.0 KiB
Go
// Copyright 2020 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
//go:build !gogit
|
|
|
|
package git
|
|
|
|
import (
|
|
"io"
|
|
)
|
|
|
|
func (repo *Repository) getTree(id ObjectID) (*Tree, error) {
|
|
wr, rd, cancel, err := repo.CatFileBatch(repo.Ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer cancel()
|
|
|
|
_, _ = wr.Write([]byte(id.String() + "\n"))
|
|
|
|
// ignore the SHA
|
|
_, typ, size, err := ReadBatchLine(rd)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
switch typ {
|
|
case "tag":
|
|
resolvedID := id
|
|
data, err := io.ReadAll(io.LimitReader(rd, size))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
tag, err := parseTagData(id.Type(), data)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
commit, err := tag.Commit(repo)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
commit.Tree.ResolvedID = resolvedID
|
|
return &commit.Tree, nil
|
|
case "commit":
|
|
commit, err := CommitFromReader(repo, id, io.LimitReader(rd, size))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if _, err := rd.Discard(1); err != nil {
|
|
return nil, err
|
|
}
|
|
commit.Tree.ResolvedID = commit.ID
|
|
return &commit.Tree, nil
|
|
case "tree":
|
|
tree := NewTree(repo, id)
|
|
tree.ResolvedID = id
|
|
objectFormat, err := repo.GetObjectFormat()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
tree.entries, err = catBatchParseTreeEntries(objectFormat, tree, rd, size)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
tree.entriesParsed = true
|
|
return tree, nil
|
|
default:
|
|
if err := DiscardFull(rd, size+1); err != nil {
|
|
return nil, err
|
|
}
|
|
return nil, ErrNotExist{
|
|
ID: id.String(),
|
|
}
|
|
}
|
|
}
|
|
|
|
// GetTree find the tree object in the repository.
|
|
func (repo *Repository) GetTree(idStr string) (*Tree, error) {
|
|
objectFormat, err := repo.GetObjectFormat()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if len(idStr) != objectFormat.FullLength() {
|
|
res, err := repo.GetRefCommitID(idStr)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if len(res) > 0 {
|
|
idStr = res
|
|
}
|
|
}
|
|
id, err := NewIDFromString(idStr)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return repo.getTree(id)
|
|
}
|