2020-12-17 22:00:47 +08:00
|
|
|
// 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.
|
|
|
|
|
2021-08-25 00:47:09 +08:00
|
|
|
//go:build !gogit
|
2020-12-17 22:00:47 +08:00
|
|
|
// +build !gogit
|
|
|
|
|
|
|
|
package git
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (repo *Repository) getTree(id SHA1) (*Tree, error) {
|
2021-05-10 09:27:03 +08:00
|
|
|
wr, rd, cancel := repo.CatFileBatch()
|
|
|
|
defer cancel()
|
2020-12-17 22:00:47 +08:00
|
|
|
|
2021-05-10 09:27:03 +08:00
|
|
|
_, _ = wr.Write([]byte(id.String() + "\n"))
|
2020-12-17 22:00:47 +08:00
|
|
|
|
|
|
|
// ignore the SHA
|
2021-05-10 09:27:03 +08:00
|
|
|
_, typ, size, err := ReadBatchLine(rd)
|
2020-12-17 22:00:47 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch typ {
|
|
|
|
case "tag":
|
|
|
|
resolvedID := id
|
2021-09-22 13:38:34 +08:00
|
|
|
data, err := io.ReadAll(io.LimitReader(rd, size))
|
2020-12-17 22:00:47 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
tag, err := parseTagData(data)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
commit, err := tag.Commit()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
commit.Tree.ResolvedID = resolvedID
|
|
|
|
return &commit.Tree, nil
|
|
|
|
case "commit":
|
2021-05-10 09:27:03 +08:00
|
|
|
commit, err := CommitFromReader(repo, id, io.LimitReader(rd, size))
|
2020-12-17 22:00:47 +08:00
|
|
|
if err != nil {
|
2021-05-10 09:27:03 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if _, err := rd.Discard(1); err != nil {
|
2020-12-17 22:00:47 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
commit.Tree.ResolvedID = commit.ID
|
|
|
|
return &commit.Tree, nil
|
|
|
|
case "tree":
|
|
|
|
tree := NewTree(repo, id)
|
|
|
|
tree.ResolvedID = id
|
2021-05-10 09:27:03 +08:00
|
|
|
tree.entries, err = catBatchParseTreeEntries(tree, rd, size)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
tree.entriesParsed = true
|
2020-12-17 22:00:47 +08:00
|
|
|
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 {
|
2021-05-10 09:27:03 +08:00
|
|
|
res, err := repo.GetRefCommitID(idStr)
|
2020-12-17 22:00:47 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(res) > 0 {
|
2021-05-10 09:27:03 +08:00
|
|
|
idStr = res
|
2020-12-17 22:00:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
id, err := NewIDFromString(idStr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return repo.getTree(id)
|
|
|
|
}
|