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 1.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // Copyright 2019 The Gitea Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package git
  6. import (
  7. "path"
  8. "strings"
  9. "gopkg.in/src-d/go-git.v4/plumbing"
  10. "gopkg.in/src-d/go-git.v4/plumbing/filemode"
  11. "gopkg.in/src-d/go-git.v4/plumbing/object"
  12. )
  13. // GetTreeEntryByPath get the tree entries according the sub dir
  14. func (t *Tree) GetTreeEntryByPath(relpath string) (*TreeEntry, error) {
  15. if len(relpath) == 0 {
  16. return &TreeEntry{
  17. ID: t.ID,
  18. //Type: ObjectTree,
  19. gogitTreeEntry: &object.TreeEntry{
  20. Name: "",
  21. Mode: filemode.Dir,
  22. Hash: plumbing.Hash(t.ID),
  23. },
  24. }, nil
  25. }
  26. relpath = path.Clean(relpath)
  27. parts := strings.Split(relpath, "/")
  28. var err error
  29. tree := t
  30. for i, name := range parts {
  31. if i == len(parts)-1 {
  32. entries, err := tree.ListEntries()
  33. if err != nil {
  34. return nil, err
  35. }
  36. for _, v := range entries {
  37. if v.Name() == name {
  38. return v, nil
  39. }
  40. }
  41. } else {
  42. tree, err = tree.SubTree(name)
  43. if err != nil {
  44. return nil, err
  45. }
  46. }
  47. }
  48. return nil, ErrNotExist{"", relpath}
  49. }
  50. // GetBlobByPath get the blob object according the path
  51. func (t *Tree) GetBlobByPath(relpath string) (*Blob, error) {
  52. entry, err := t.GetTreeEntryByPath(relpath)
  53. if err != nil {
  54. return nil, err
  55. }
  56. if !entry.IsDir() {
  57. return entry.Blob(), nil
  58. }
  59. return nil, ErrNotExist{"", relpath}
  60. }