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

9 years ago
  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. }
  43. func (t *Tree) GetBlobByPath(rpath string) (*Blob, error) {
  44. entry, err := t.GetTreeEntryByPath(rpath)
  45. if err != nil {
  46. return nil, err
  47. }
  48. if !entry.IsDir() {
  49. return entry.Blob(), nil
  50. }
  51. return nil, ErrNotExist
  52. }