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.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. fullName string
  15. }
  16. // Name returns the name of the entry
  17. func (te *TreeEntry) Name() string {
  18. if te.fullName != "" {
  19. return te.fullName
  20. }
  21. return te.name
  22. }
  23. // Mode returns the mode of the entry
  24. func (te *TreeEntry) Mode() EntryMode {
  25. return te.entryMode
  26. }
  27. // Size returns the size of the entry
  28. func (te *TreeEntry) Size() int64 {
  29. if te.IsDir() {
  30. return 0
  31. } else if te.sized {
  32. return te.size
  33. }
  34. wr, rd, cancel := te.ptree.repo.CatFileBatchCheck(te.ptree.repo.Ctx)
  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 sub module
  50. func (te *TreeEntry) IsSubModule() bool {
  51. return te.entryMode == EntryModeCommit
  52. }
  53. // IsDir if the entry is a sub dir
  54. func (te *TreeEntry) IsDir() bool {
  55. return te.entryMode == EntryModeTree
  56. }
  57. // IsLink if the entry is a symlink
  58. func (te *TreeEntry) IsLink() bool {
  59. return te.entryMode == EntryModeSymlink
  60. }
  61. // IsRegular if the entry is a regular file
  62. func (te *TreeEntry) IsRegular() bool {
  63. return te.entryMode == EntryModeBlob
  64. }
  65. // IsExecutable if the entry is an executable file (not necessarily binary)
  66. func (te *TreeEntry) IsExecutable() bool {
  67. return te.entryMode == EntryModeExec
  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. }