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.

59 lines
1.2 KiB

  1. // Copyright 2015 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. "path"
  7. "strings"
  8. )
  9. // GetTreeEntryByPath get the tree entries accroding the sub dir
  10. func (t *Tree) GetTreeEntryByPath(relpath string) (*TreeEntry, error) {
  11. if len(relpath) == 0 {
  12. return &TreeEntry{
  13. ID: t.ID,
  14. Type: ObjectTree,
  15. mode: EntryModeTree,
  16. }, nil
  17. }
  18. relpath = path.Clean(relpath)
  19. parts := strings.Split(relpath, "/")
  20. var err error
  21. tree := t
  22. for i, name := range parts {
  23. if i == len(parts)-1 {
  24. entries, err := tree.ListEntries()
  25. if err != nil {
  26. return nil, err
  27. }
  28. for _, v := range entries {
  29. if v.name == name {
  30. return v, nil
  31. }
  32. }
  33. } else {
  34. tree, err = tree.SubTree(name)
  35. if err != nil {
  36. return nil, err
  37. }
  38. }
  39. }
  40. return nil, ErrNotExist{"", relpath}
  41. }
  42. // GetBlobByPath get the blob object accroding the path
  43. func (t *Tree) GetBlobByPath(relpath string) (*Blob, error) {
  44. entry, err := t.GetTreeEntryByPath(relpath)
  45. if err != nil {
  46. return nil, err
  47. }
  48. if !entry.IsDir() {
  49. return entry.Blob(), nil
  50. }
  51. return nil, ErrNotExist{"", relpath}
  52. }