tree_blob.go 1.1 KB

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