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_nogogit.go 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // Copyright 2020 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. //go:build !gogit
  4. package git
  5. import (
  6. "io"
  7. "strings"
  8. )
  9. // Tree represents a flat directory listing.
  10. type Tree struct {
  11. ID ObjectID
  12. ResolvedID ObjectID
  13. repo *Repository
  14. // parent tree
  15. ptree *Tree
  16. entries Entries
  17. entriesParsed bool
  18. entriesRecursive Entries
  19. entriesRecursiveParsed bool
  20. }
  21. // ListEntries returns all entries of current tree.
  22. func (t *Tree) ListEntries() (Entries, error) {
  23. if t.entriesParsed {
  24. return t.entries, nil
  25. }
  26. if t.repo != nil {
  27. wr, rd, cancel := t.repo.CatFileBatch(t.repo.Ctx)
  28. defer cancel()
  29. _, _ = wr.Write([]byte(t.ID.String() + "\n"))
  30. _, typ, sz, err := ReadBatchLine(rd)
  31. if err != nil {
  32. return nil, err
  33. }
  34. if typ == "commit" {
  35. treeID, err := ReadTreeID(rd, sz)
  36. if err != nil && err != io.EOF {
  37. return nil, err
  38. }
  39. _, _ = wr.Write([]byte(treeID + "\n"))
  40. _, typ, sz, err = ReadBatchLine(rd)
  41. if err != nil {
  42. return nil, err
  43. }
  44. }
  45. if typ == "tree" {
  46. t.entries, err = catBatchParseTreeEntries(t.ID.Type(), t, rd, sz)
  47. if err != nil {
  48. return nil, err
  49. }
  50. t.entriesParsed = true
  51. return t.entries, nil
  52. }
  53. // Not a tree just use ls-tree instead
  54. if err := DiscardFull(rd, sz+1); err != nil {
  55. return nil, err
  56. }
  57. }
  58. stdout, _, runErr := NewCommand(t.repo.Ctx, "ls-tree", "-l").AddDynamicArguments(t.ID.String()).RunStdBytes(&RunOpts{Dir: t.repo.Path})
  59. if runErr != nil {
  60. if strings.Contains(runErr.Error(), "fatal: Not a valid object name") || strings.Contains(runErr.Error(), "fatal: not a tree object") {
  61. return nil, ErrNotExist{
  62. ID: t.ID.String(),
  63. }
  64. }
  65. return nil, runErr
  66. }
  67. var err error
  68. t.entries, err = parseTreeEntries(stdout, t)
  69. if err == nil {
  70. t.entriesParsed = true
  71. }
  72. return t.entries, err
  73. }
  74. // listEntriesRecursive returns all entries of current tree recursively including all subtrees
  75. // extraArgs could be "-l" to get the size, which is slower
  76. func (t *Tree) listEntriesRecursive(extraArgs TrustedCmdArgs) (Entries, error) {
  77. if t.entriesRecursiveParsed {
  78. return t.entriesRecursive, nil
  79. }
  80. stdout, _, runErr := NewCommand(t.repo.Ctx, "ls-tree", "-t", "-r").
  81. AddArguments(extraArgs...).
  82. AddDynamicArguments(t.ID.String()).
  83. RunStdBytes(&RunOpts{Dir: t.repo.Path})
  84. if runErr != nil {
  85. return nil, runErr
  86. }
  87. var err error
  88. t.entriesRecursive, err = parseTreeEntries(stdout, t)
  89. if err == nil {
  90. t.entriesRecursiveParsed = true
  91. }
  92. return t.entriesRecursive, err
  93. }
  94. // ListEntriesRecursiveFast returns all entries of current tree recursively including all subtrees, no size
  95. func (t *Tree) ListEntriesRecursiveFast() (Entries, error) {
  96. return t.listEntriesRecursive(nil)
  97. }
  98. // ListEntriesRecursiveWithSize returns all entries of current tree recursively including all subtrees, with size
  99. func (t *Tree) ListEntriesRecursiveWithSize() (Entries, error) {
  100. return t.listEntriesRecursive(TrustedCmdArgs{"--long"})
  101. }