2016-11-03 17:16:01 -05:00
|
|
|
// Copyright 2015 The Gogs Authors. All rights reserved.
|
2019-04-19 07:17:27 -05:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2016-11-03 17:16:01 -05:00
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package git
|
|
|
|
|
|
|
|
import (
|
2021-07-20 08:16:20 -05:00
|
|
|
"bytes"
|
2016-11-03 17:16:01 -05:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2019-04-19 07:17:27 -05:00
|
|
|
// NewTree create a new tree according the repository and tree id
|
2016-12-22 03:30:52 -06:00
|
|
|
func NewTree(repo *Repository, id SHA1) *Tree {
|
2016-11-03 17:16:01 -05:00
|
|
|
return &Tree{
|
|
|
|
ID: id,
|
|
|
|
repo: repo,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-22 03:30:52 -06:00
|
|
|
// SubTree get a sub tree by the sub dir path
|
2016-11-03 17:16:01 -05:00
|
|
|
func (t *Tree) SubTree(rpath string) (*Tree, error) {
|
|
|
|
if len(rpath) == 0 {
|
|
|
|
return t, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
paths := strings.Split(rpath, "/")
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
g = t
|
|
|
|
p = t
|
|
|
|
te *TreeEntry
|
|
|
|
)
|
|
|
|
for _, name := range paths {
|
|
|
|
te, err = p.GetTreeEntryByPath(name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
g, err = t.repo.getTree(te.ID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
g.ptree = p
|
|
|
|
p = g
|
|
|
|
}
|
|
|
|
return g, nil
|
|
|
|
}
|
2021-07-20 08:16:20 -05:00
|
|
|
|
|
|
|
// LsTree checks if the given filenames are in the tree
|
|
|
|
func (repo *Repository) LsTree(ref string, filenames ...string) ([]string, error) {
|
2022-02-06 13:01:47 -06:00
|
|
|
cmd := NewCommand(repo.Ctx, "ls-tree", "-z", "--name-only", "--", ref)
|
2021-07-20 08:16:20 -05:00
|
|
|
for _, arg := range filenames {
|
|
|
|
if arg != "" {
|
|
|
|
cmd.AddArguments(arg)
|
|
|
|
}
|
|
|
|
}
|
2022-03-31 21:55:30 -05:00
|
|
|
res, _, err := cmd.RunStdBytes(&RunOpts{Dir: repo.Path})
|
2021-07-20 08:16:20 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
filelist := make([]string, 0, len(filenames))
|
|
|
|
for _, line := range bytes.Split(res, []byte{'\000'}) {
|
|
|
|
filelist = append(filelist, string(line))
|
|
|
|
}
|
|
|
|
|
|
|
|
return filelist, err
|
|
|
|
}
|