2016-11-03 23:16:01 +01:00
|
|
|
// Copyright 2015 The Gogs Authors. All rights reserved.
|
2019-04-19 14:17:27 +02:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2024-01-05 10:44:33 +01:00
|
|
|
// Copyright 2024 The Forgejo Authors c/o Codeberg e.V.. All rights reserved.
|
2022-11-27 19:20:29 +01:00
|
|
|
// SPDX-License-Identifier: MIT
|
2016-11-03 23:16:01 +01:00
|
|
|
|
|
|
|
package git
|
|
|
|
|
2024-08-12 17:16:55 +02:00
|
|
|
import (
|
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// GetTreeEntryByPath get the tree entries according the sub dir
|
|
|
|
func (t *Tree) GetTreeEntryByPath(relpath string) (*TreeEntry, error) {
|
|
|
|
if len(relpath) == 0 {
|
|
|
|
return &TreeEntry{
|
|
|
|
ptree: t,
|
|
|
|
ID: t.ID,
|
|
|
|
name: "",
|
|
|
|
fullName: "",
|
|
|
|
entryMode: EntryModeTree,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: This should probably use git cat-file --batch to be a bit more efficient
|
|
|
|
relpath = path.Clean(relpath)
|
|
|
|
parts := strings.Split(relpath, "/")
|
|
|
|
var err error
|
|
|
|
tree := t
|
|
|
|
for i, name := range parts {
|
|
|
|
if i == len(parts)-1 {
|
|
|
|
entries, err := tree.ListEntries()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, v := range entries {
|
|
|
|
if v.Name() == name {
|
|
|
|
return v, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
tree, err = tree.SubTree(name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, ErrNotExist{"", relpath}
|
|
|
|
}
|
2024-01-05 10:44:33 +01:00
|
|
|
|
2019-03-27 10:33:00 +01:00
|
|
|
// GetBlobByPath get the blob object according the path
|
2016-11-03 23:16:01 +01:00
|
|
|
func (t *Tree) GetBlobByPath(relpath string) (*Blob, error) {
|
|
|
|
entry, err := t.GetTreeEntryByPath(relpath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-05-21 23:32:52 +02:00
|
|
|
if !entry.IsDir() && !entry.IsSubModule() {
|
2016-11-03 23:16:01 +01:00
|
|
|
return entry.Blob(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, ErrNotExist{"", relpath}
|
|
|
|
}
|
2024-01-05 10:44:33 +01:00
|
|
|
|
|
|
|
// GetBlobByFoldedPath returns the blob object at relpath, regardless of the
|
|
|
|
// case of relpath. If there are multiple files with the same case-insensitive
|
|
|
|
// name, the first one found will be returned.
|
|
|
|
func (t *Tree) GetBlobByFoldedPath(relpath string) (*Blob, error) {
|
|
|
|
entries, err := t.ListEntries()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, entry := range entries {
|
|
|
|
if strings.EqualFold(entry.Name(), relpath) {
|
|
|
|
return t.GetBlobByPath(entry.Name())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, ErrNotExist{"", relpath}
|
|
|
|
}
|