本站源代码
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

68 行
1.4KB

  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/filemode"
  10. "gopkg.in/src-d/go-git.v4/plumbing/object"
  11. )
  12. // GetTreeEntryByPath get the tree entries according the sub dir
  13. func (t *Tree) GetTreeEntryByPath(relpath string) (*TreeEntry, error) {
  14. if len(relpath) == 0 {
  15. return &TreeEntry{
  16. ID: t.ID,
  17. //Type: ObjectTree,
  18. gogitTreeEntry: &object.TreeEntry{
  19. Name: "",
  20. Mode: filemode.Dir,
  21. Hash: t.ID,
  22. },
  23. }, nil
  24. }
  25. relpath = path.Clean(relpath)
  26. parts := strings.Split(relpath, "/")
  27. var err error
  28. tree := t
  29. for i, name := range parts {
  30. if i == len(parts)-1 {
  31. entries, err := tree.ListEntries()
  32. if err != nil {
  33. return nil, err
  34. }
  35. for _, v := range entries {
  36. if v.Name() == name {
  37. return v, nil
  38. }
  39. }
  40. } else {
  41. tree, err = tree.SubTree(name)
  42. if err != nil {
  43. return nil, err
  44. }
  45. }
  46. }
  47. return nil, ErrNotExist{"", relpath}
  48. }
  49. // GetBlobByPath get the blob object according the path
  50. func (t *Tree) GetBlobByPath(relpath string) (*Blob, error) {
  51. entry, err := t.GetTreeEntryByPath(relpath)
  52. if err != nil {
  53. return nil, err
  54. }
  55. if !entry.IsDir() && !entry.IsSubModule() {
  56. return entry.Blob(), nil
  57. }
  58. return nil, ErrNotExist{"", relpath}
  59. }
上海开阖软件有限公司 沪ICP备12045867号-1