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_entry_nogogit.go 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Copyright 2020 The Gitea 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. //go:build !gogit
  5. package git
  6. import "code.gitea.io/gitea/modules/log"
  7. // TreeEntry the leaf in the git tree
  8. type TreeEntry struct {
  9. ID SHA1
  10. ptree *Tree
  11. entryMode EntryMode
  12. name string
  13. size int64
  14. sized bool
  15. fullName string
  16. }
  17. // Name returns the name of the entry
  18. func (te *TreeEntry) Name() string {
  19. if te.fullName != "" {
  20. return te.fullName
  21. }
  22. return te.name
  23. }
  24. // Mode returns the mode of the entry
  25. func (te *TreeEntry) Mode() EntryMode {
  26. return te.entryMode
  27. }
  28. // Size returns the size of the entry
  29. func (te *TreeEntry) Size() int64 {
  30. if te.IsDir() {
  31. return 0
  32. } else if te.sized {
  33. return te.size
  34. }
  35. wr, rd, cancel := te.ptree.repo.CatFileBatchCheck(te.ptree.repo.Ctx)
  36. defer cancel()
  37. _, err := wr.Write([]byte(te.ID.String() + "\n"))
  38. if err != nil {
  39. log.Debug("error whilst reading size for %s in %s. Error: %v", te.ID.String(), te.ptree.repo.Path, err)
  40. return 0
  41. }
  42. _, _, te.size, err = ReadBatchLine(rd)
  43. if err != nil {
  44. log.Debug("error whilst reading size for %s in %s. Error: %v", te.ID.String(), te.ptree.repo.Path, err)
  45. return 0
  46. }
  47. te.sized = true
  48. return te.size
  49. }
  50. // IsSubModule if the entry is a sub module
  51. func (te *TreeEntry) IsSubModule() bool {
  52. return te.entryMode == EntryModeCommit
  53. }
  54. // IsDir if the entry is a sub dir
  55. func (te *TreeEntry) IsDir() bool {
  56. return te.entryMode == EntryModeTree
  57. }
  58. // IsLink if the entry is a symlink
  59. func (te *TreeEntry) IsLink() bool {
  60. return te.entryMode == EntryModeSymlink
  61. }
  62. // IsRegular if the entry is a regular file
  63. func (te *TreeEntry) IsRegular() bool {
  64. return te.entryMode == EntryModeBlob
  65. }
  66. // IsExecutable if the entry is an executable file (not necessarily binary)
  67. func (te *TreeEntry) IsExecutable() bool {
  68. return te.entryMode == EntryModeExec
  69. }
  70. // Blob returns the blob object the entry
  71. func (te *TreeEntry) Blob() *Blob {
  72. return &Blob{
  73. ID: te.ID,
  74. name: te.Name(),
  75. size: te.size,
  76. gotSize: te.sized,
  77. repo: te.ptree.repo,
  78. }
  79. }