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.go 969B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. // Copyright 2014 The Gogs 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. package git
  5. import (
  6. "fmt"
  7. "path"
  8. "strings"
  9. )
  10. func (t *Tree) GetTreeEntryByPath(relpath string) (*TreeEntry, error) {
  11. if len(relpath) == 0 {
  12. return &TreeEntry{
  13. Id: t.Id,
  14. Type: TREE,
  15. mode: ModeTree,
  16. }, nil
  17. // return nil, fmt.Errorf("GetTreeEntryByPath(empty relpath): %v", ErrNotExist)
  18. }
  19. relpath = path.Clean(relpath)
  20. parts := strings.Split(relpath, "/")
  21. var err error
  22. tree := t
  23. for i, name := range parts {
  24. if i == len(parts)-1 {
  25. entries, err := tree.ListEntries(path.Dir(relpath))
  26. if err != nil {
  27. return nil, err
  28. }
  29. for _, v := range entries {
  30. if v.name == name {
  31. return v, nil
  32. }
  33. }
  34. } else {
  35. tree, err = tree.SubTree(name)
  36. if err != nil {
  37. return nil, err
  38. }
  39. }
  40. }
  41. return nil, fmt.Errorf("GetTreeEntryByPath: %v", ErrNotExist)
  42. }