2020-12-17 09:00:47 -05:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2022-11-27 13:20:29 -05:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-12-17 09:00:47 -05:00
|
|
|
|
2021-08-24 12:47:09 -04:00
|
|
|
//go:build !gogit
|
2020-12-17 09:00:47 -05:00
|
|
|
|
|
|
|
package git
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
)
|
|
|
|
|
2023-12-13 16:02:00 -05:00
|
|
|
func (repo *Repository) getTree(id ObjectID) (*Tree, error) {
|
2024-08-20 13:04:57 -04:00
|
|
|
wr, rd, cancel, err := repo.CatFileBatch(repo.Ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-05-09 21:27:03 -04:00
|
|
|
defer cancel()
|
2020-12-17 09:00:47 -05:00
|
|
|
|
2021-05-09 21:27:03 -04:00
|
|
|
_, _ = wr.Write([]byte(id.String() + "\n"))
|
2020-12-17 09:00:47 -05:00
|
|
|
|
|
|
|
// ignore the SHA
|
2021-05-09 21:27:03 -04:00
|
|
|
_, typ, size, err := ReadBatchLine(rd)
|
2020-12-17 09:00:47 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch typ {
|
|
|
|
case "tag":
|
|
|
|
resolvedID := id
|
2021-09-22 01:38:34 -04:00
|
|
|
data, err := io.ReadAll(io.LimitReader(rd, size))
|
2020-12-17 09:00:47 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-12-13 16:02:00 -05:00
|
|
|
tag, err := parseTagData(id.Type(), data)
|
2020-12-17 09:00:47 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-01-12 15:37:46 -05:00
|
|
|
commit, err := tag.Commit(repo)
|
2020-12-17 09:00:47 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
commit.Tree.ResolvedID = resolvedID
|
|
|
|
return &commit.Tree, nil
|
|
|
|
case "commit":
|
2021-05-09 21:27:03 -04:00
|
|
|
commit, err := CommitFromReader(repo, id, io.LimitReader(rd, size))
|
2020-12-17 09:00:47 -05:00
|
|
|
if err != nil {
|
2021-05-09 21:27:03 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if _, err := rd.Discard(1); err != nil {
|
2020-12-17 09:00:47 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
commit.Tree.ResolvedID = commit.ID
|
|
|
|
return &commit.Tree, nil
|
|
|
|
case "tree":
|
|
|
|
tree := NewTree(repo, id)
|
|
|
|
tree.ResolvedID = id
|
2024-03-12 00:21:27 -04:00
|
|
|
objectFormat, err := repo.GetObjectFormat()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
tree.entries, err = catBatchParseTreeEntries(objectFormat, tree, rd, size)
|
2021-05-09 21:27:03 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
tree.entriesParsed = true
|
2020-12-17 09:00:47 -05:00
|
|
|
return tree, nil
|
|
|
|
default:
|
2024-02-21 22:48:19 -05:00
|
|
|
if err := DiscardFull(rd, size+1); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-12-17 09:00:47 -05:00
|
|
|
return nil, ErrNotExist{
|
|
|
|
ID: id.String(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetTree find the tree object in the repository.
|
|
|
|
func (repo *Repository) GetTree(idStr string) (*Tree, error) {
|
2024-03-12 00:21:27 -04:00
|
|
|
objectFormat, err := repo.GetObjectFormat()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(idStr) != objectFormat.FullLength() {
|
2021-05-09 21:27:03 -04:00
|
|
|
res, err := repo.GetRefCommitID(idStr)
|
2020-12-17 09:00:47 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(res) > 0 {
|
2021-05-09 21:27:03 -04:00
|
|
|
idStr = res
|
2020-12-17 09:00:47 -05:00
|
|
|
}
|
|
|
|
}
|
2023-12-19 02:20:47 -05:00
|
|
|
id, err := NewIDFromString(idStr)
|
2020-12-17 09:00:47 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return repo.getTree(id)
|
|
|
|
}
|