// Copyright 2020 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
//go:build !gogit
// +build !gogit
package git
import (
"io"
)
func (repo *Repository) getTree(id SHA1) (*Tree, error) {
wr, rd, cancel := repo.CatFileBatch(repo.Ctx)
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))
tag, err := parseTagData(data)
commit, err := tag.Commit(repo)
commit.Tree.ResolvedID = resolvedID
return &commit.Tree, nil
case "commit":
commit, err := CommitFromReader(repo, id, io.LimitReader(rd, size))
if _, err := rd.Discard(1); err != nil {
commit.Tree.ResolvedID = commit.ID
case "tree":
tree := NewTree(repo, id)
tree.ResolvedID = id
tree.entries, err = catBatchParseTreeEntries(tree, rd, size)
tree.entriesParsed = true
return tree, nil
default:
return nil, ErrNotExist{
ID: id.String(),
// GetTree find the tree object in the repository.
func (repo *Repository) GetTree(idStr string) (*Tree, error) {
if len(idStr) != 40 {
res, err := repo.GetRefCommitID(idStr)
if len(res) > 0 {
idStr = res
id, err := NewIDFromString(idStr)
return repo.getTree(id)