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

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