mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-11-24 03:21:49 +01:00
4eb2a29910
The 4 functions are duplicated, especially as interface methods. I think we just need to keep `MustID` the only one and remove other 3. ``` MustID(b []byte) ObjectID MustIDFromString(s string) ObjectID NewID(b []byte) (ObjectID, error) NewIDFromString(s string) (ObjectID, error) ``` Introduced the new interfrace method `ComputeHash` which will replace the interface `HasherInterface`. Now we don't need to keep two interfaces. Reintroduced `git.NewIDFromString` and `git.MustIDFromString`. The new function will detect the hash length to decide which objectformat of it. If it's 40, then it's SHA1. If it's 64, then it's SHA256. This will be right if the commitID is a full one. So the parameter should be always a full commit id. @AdamMajer Please review.
84 lines
1.7 KiB
Go
84 lines
1.7 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 := 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))
|
|
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
|
|
tree.entries, err = catBatchParseTreeEntries(repo.objectFormat, tree, rd, size)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
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) != repo.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)
|
|
}
|