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 990B

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