gitea源码

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