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.go 1.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // Copyright 2019 The Gitea Authors. All rights reserved.
  3. // SPDX-License-Identifier: MIT
  4. package git
  5. import (
  6. "bytes"
  7. "strings"
  8. )
  9. // NewTree create a new tree according the repository and tree id
  10. func NewTree(repo *Repository, id ObjectID) *Tree {
  11. return &Tree{
  12. ID: id,
  13. repo: repo,
  14. }
  15. }
  16. // SubTree get a sub tree by the sub dir path
  17. func (t *Tree) SubTree(rpath string) (*Tree, error) {
  18. if len(rpath) == 0 {
  19. return t, nil
  20. }
  21. paths := strings.Split(rpath, "/")
  22. var (
  23. err error
  24. g = t
  25. p = t
  26. te *TreeEntry
  27. )
  28. for _, name := range paths {
  29. te, err = p.GetTreeEntryByPath(name)
  30. if err != nil {
  31. return nil, err
  32. }
  33. g, err = t.repo.getTree(te.ID)
  34. if err != nil {
  35. return nil, err
  36. }
  37. g.ptree = p
  38. p = g
  39. }
  40. return g, nil
  41. }
  42. // LsTree checks if the given filenames are in the tree
  43. func (repo *Repository) LsTree(ref string, filenames ...string) ([]string, error) {
  44. cmd := NewCommand(repo.Ctx, "ls-tree", "-z", "--name-only").
  45. AddDashesAndList(append([]string{ref}, filenames...)...)
  46. res, _, err := cmd.RunStdBytes(&RunOpts{Dir: repo.Path})
  47. if err != nil {
  48. return nil, err
  49. }
  50. filelist := make([]string, 0, len(filenames))
  51. for _, line := range bytes.Split(res, []byte{'\000'}) {
  52. filelist = append(filelist, string(line))
  53. }
  54. return filelist, err
  55. }