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 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. // +build !gogit
  6. package git
  7. import (
  8. "strconv"
  9. "strings"
  10. )
  11. // TreeEntry the leaf in the git tree
  12. type TreeEntry struct {
  13. ID SHA1
  14. ptree *Tree
  15. entryMode EntryMode
  16. name string
  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.name
  27. }
  28. // Mode returns the mode of the entry
  29. func (te *TreeEntry) Mode() EntryMode {
  30. return te.entryMode
  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. stdout, err := NewCommand("cat-file", "-s", te.ID.String()).RunInDir(te.ptree.repo.Path)
  40. if err != nil {
  41. return 0
  42. }
  43. te.sized = true
  44. te.size, _ = strconv.ParseInt(strings.TrimSpace(stdout), 10, 64)
  45. return te.size
  46. }
  47. // IsSubModule if the entry is a sub module
  48. func (te *TreeEntry) IsSubModule() bool {
  49. return te.entryMode == EntryModeCommit
  50. }
  51. // IsDir if the entry is a sub dir
  52. func (te *TreeEntry) IsDir() bool {
  53. return te.entryMode == EntryModeTree
  54. }
  55. // IsLink if the entry is a symlink
  56. func (te *TreeEntry) IsLink() bool {
  57. return te.entryMode == EntryModeSymlink
  58. }
  59. // IsRegular if the entry is a regular file
  60. func (te *TreeEntry) IsRegular() bool {
  61. return te.entryMode == EntryModeBlob
  62. }
  63. // IsExecutable if the entry is an executable file (not necessarily binary)
  64. func (te *TreeEntry) IsExecutable() bool {
  65. return te.entryMode == EntryModeExec
  66. }
  67. // Blob returns the blob object the entry
  68. func (te *TreeEntry) Blob() *Blob {
  69. return &Blob{
  70. ID: te.ID,
  71. name: te.Name(),
  72. size: te.size,
  73. gotSize: te.sized,
  74. repo: te.ptree.repo,
  75. }
  76. }