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.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. package git
  6. import (
  7. "bytes"
  8. "strings"
  9. )
  10. // NewTree create a new tree according the repository and tree id
  11. func NewTree(repo *Repository, id SHA1) *Tree {
  12. return &Tree{
  13. ID: id,
  14. repo: repo,
  15. }
  16. }
  17. // SubTree get a sub tree by the sub dir path
  18. func (t *Tree) SubTree(rpath string) (*Tree, error) {
  19. if len(rpath) == 0 {
  20. return t, nil
  21. }
  22. paths := strings.Split(rpath, "/")
  23. var (
  24. err error
  25. g = t
  26. p = t
  27. te *TreeEntry
  28. )
  29. for _, name := range paths {
  30. te, err = p.GetTreeEntryByPath(name)
  31. if err != nil {
  32. return nil, err
  33. }
  34. g, err = t.repo.getTree(te.ID)
  35. if err != nil {
  36. return nil, err
  37. }
  38. g.ptree = p
  39. p = g
  40. }
  41. return g, nil
  42. }
  43. // LsTree checks if the given filenames are in the tree
  44. func (repo *Repository) LsTree(ref string, filenames ...string) ([]string, error) {
  45. cmd := NewCommand("ls-tree", "-z", "--name-only", "--", ref)
  46. for _, arg := range filenames {
  47. if arg != "" {
  48. cmd.AddArguments(arg)
  49. }
  50. }
  51. res, err := cmd.RunInDirBytes(repo.Path)
  52. if err != nil {
  53. return nil, err
  54. }
  55. filelist := make([]string, 0, len(filenames))
  56. for _, line := range bytes.Split(res, []byte{'\000'}) {
  57. filelist = append(filelist, string(line))
  58. }
  59. return filelist, err
  60. }