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