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_gogit.go 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. //go:build gogit
  6. package git
  7. import (
  8. "github.com/go-git/go-git/v5/plumbing"
  9. "github.com/go-git/go-git/v5/plumbing/filemode"
  10. "github.com/go-git/go-git/v5/plumbing/object"
  11. )
  12. // TreeEntry the leaf in the git tree
  13. type TreeEntry struct {
  14. ID SHA1
  15. gogitTreeEntry *object.TreeEntry
  16. ptree *Tree
  17. size int64
  18. sized bool
  19. fullName string
  20. }
  21. // Name returns the name of the entry
  22. func (te *TreeEntry) Name() string {
  23. if te.fullName != "" {
  24. return te.fullName
  25. }
  26. return te.gogitTreeEntry.Name
  27. }
  28. // Mode returns the mode of the entry
  29. func (te *TreeEntry) Mode() EntryMode {
  30. return EntryMode(te.gogitTreeEntry.Mode)
  31. }
  32. // Size returns the size of the entry
  33. func (te *TreeEntry) Size() int64 {
  34. if te.IsDir() {
  35. return 0
  36. } else if te.sized {
  37. return te.size
  38. }
  39. file, err := te.ptree.gogitTree.TreeEntryFile(te.gogitTreeEntry)
  40. if err != nil {
  41. return 0
  42. }
  43. te.sized = true
  44. te.size = file.Size
  45. return te.size
  46. }
  47. // IsSubModule if the entry is a sub module
  48. func (te *TreeEntry) IsSubModule() bool {
  49. return te.gogitTreeEntry.Mode == filemode.Submodule
  50. }
  51. // IsDir if the entry is a sub dir
  52. func (te *TreeEntry) IsDir() bool {
  53. return te.gogitTreeEntry.Mode == filemode.Dir
  54. }
  55. // IsLink if the entry is a symlink
  56. func (te *TreeEntry) IsLink() bool {
  57. return te.gogitTreeEntry.Mode == filemode.Symlink
  58. }
  59. // IsRegular if the entry is a regular file
  60. func (te *TreeEntry) IsRegular() bool {
  61. return te.gogitTreeEntry.Mode == filemode.Regular
  62. }
  63. // IsExecutable if the entry is an executable file (not necessarily binary)
  64. func (te *TreeEntry) IsExecutable() bool {
  65. return te.gogitTreeEntry.Mode == filemode.Executable
  66. }
  67. // Blob returns the blob object the entry
  68. func (te *TreeEntry) Blob() *Blob {
  69. encodedObj, err := te.ptree.repo.gogitRepo.Storer.EncodedObject(plumbing.AnyObject, te.gogitTreeEntry.Hash)
  70. if err != nil {
  71. return nil
  72. }
  73. return &Blob{
  74. ID: te.gogitTreeEntry.Hash,
  75. gogitEncodedObj: encodedObj,
  76. name: te.Name(),
  77. }
  78. }