2020-12-17 19:30:47 +05:30
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2022-11-27 23:50:29 +05:30
|
|
|
// SPDX-License-Identifier: MIT
|
2020-12-17 19:30:47 +05:30
|
|
|
|
2021-08-24 22:17:09 +05:30
|
|
|
//go:build !gogit
|
2020-12-17 19:30:47 +05:30
|
|
|
|
|
|
|
package git
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
)
|
|
|
|
|
2023-12-14 02:32:00 +05:30
|
|
|
func (repo *Repository) getTree(id ObjectID) (*Tree, error) {
|
2021-12-01 01:36:32 +05:30
|
|
|
wr, rd, cancel := repo.CatFileBatch(repo.Ctx)
|
2021-05-10 06:57:03 +05:30
|
|
|
defer cancel()
|
2020-12-17 19:30:47 +05:30
|
|
|
|
2021-05-10 06:57:03 +05:30
|
|
|
_, _ = wr.Write([]byte(id.String() + "\n"))
|
2020-12-17 19:30:47 +05:30
|
|
|
|
|
|
|
// ignore the SHA
|
2021-05-10 06:57:03 +05:30
|
|
|
_, typ, size, err := ReadBatchLine(rd)
|
2020-12-17 19:30:47 +05:30
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch typ {
|
|
|
|
case "tag":
|
|
|
|
resolvedID := id
|
2021-09-22 11:08:34 +05:30
|
|
|
data, err := io.ReadAll(io.LimitReader(rd, size))
|
2020-12-17 19:30:47 +05:30
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-12-14 02:32:00 +05:30
|
|
|
tag, err := parseTagData(id.Type(), data)
|
2020-12-17 19:30:47 +05:30
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-01-13 02:07:46 +05:30
|
|
|
commit, err := tag.Commit(repo)
|
2020-12-17 19:30:47 +05:30
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
commit.Tree.ResolvedID = resolvedID
|
|
|
|
return &commit.Tree, nil
|
|
|
|
case "commit":
|
2021-05-10 06:57:03 +05:30
|
|
|
commit, err := CommitFromReader(repo, id, io.LimitReader(rd, size))
|
2020-12-17 19:30:47 +05:30
|
|
|
if err != nil {
|
2021-05-10 06:57:03 +05:30
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if _, err := rd.Discard(1); err != nil {
|
2020-12-17 19:30:47 +05:30
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
commit.Tree.ResolvedID = commit.ID
|
|
|
|
return &commit.Tree, nil
|
|
|
|
case "tree":
|
|
|
|
tree := NewTree(repo, id)
|
|
|
|
tree.ResolvedID = id
|
2023-12-14 02:32:00 +05:30
|
|
|
tree.entries, err = catBatchParseTreeEntries(repo.objectFormat, tree, rd, size)
|
2021-05-10 06:57:03 +05:30
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
tree.entriesParsed = true
|
2020-12-17 19:30:47 +05:30
|
|
|
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) {
|
2023-12-14 02:32:00 +05:30
|
|
|
if len(idStr) != repo.objectFormat.FullLength() {
|
2021-05-10 06:57:03 +05:30
|
|
|
res, err := repo.GetRefCommitID(idStr)
|
2020-12-17 19:30:47 +05:30
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(res) > 0 {
|
2021-05-10 06:57:03 +05:30
|
|
|
idStr = res
|
2020-12-17 19:30:47 +05:30
|
|
|
}
|
|
|
|
}
|
2023-12-14 02:32:00 +05:30
|
|
|
id, err := repo.objectFormat.NewIDFromString(idStr)
|
2020-12-17 19:30:47 +05:30
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return repo.getTree(id)
|
|
|
|
}
|