You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tree_blob_nogogit.go 1.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // Copyright 2020 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. //go:build !gogit
  5. package git
  6. import (
  7. "path"
  8. "strings"
  9. )
  10. // GetTreeEntryByPath get the tree entries according the sub dir
  11. func (t *Tree) GetTreeEntryByPath(relpath string) (*TreeEntry, error) {
  12. if len(relpath) == 0 {
  13. return &TreeEntry{
  14. ptree: t,
  15. ID: t.ID,
  16. name: "",
  17. fullName: "",
  18. entryMode: EntryModeTree,
  19. }, nil
  20. }
  21. // FIXME: This should probably use git cat-file --batch to be a bit more efficient
  22. relpath = path.Clean(relpath)
  23. parts := strings.Split(relpath, "/")
  24. var err error
  25. tree := t
  26. for i, name := range parts {
  27. if i == len(parts)-1 {
  28. entries, err := tree.ListEntries()
  29. if err != nil {
  30. return nil, err
  31. }
  32. for _, v := range entries {
  33. if v.Name() == name {
  34. return v, nil
  35. }
  36. }
  37. } else {
  38. tree, err = tree.SubTree(name)
  39. if err != nil {
  40. return nil, err
  41. }
  42. }
  43. }
  44. return nil, ErrNotExist{"", relpath}
  45. }