2016-11-04 07:16:01 +09:00
|
|
|
// Copyright 2015 The Gogs Authors. All rights reserved.
|
2019-04-19 21:17:27 +09:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2016-11-04 07:16:01 +09: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 22:16:20 +09:00
|
|
|
"bytes"
|
2016-11-04 07:16:01 +09:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2019-04-19 21:17:27 +09:00
|
|
|
// NewTree create a new tree according the repository and tree id
|
2016-12-22 18:30:52 +09:00
|
|
|
func NewTree(repo *Repository, id SHA1) *Tree {
|
2016-11-04 07:16:01 +09:00
|
|
|
return &Tree{
|
|
|
|
ID: id,
|
|
|
|
repo: repo,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-22 18:30:52 +09:00
|
|
|
// SubTree get a sub tree by the sub dir path
|
2016-11-04 07:16:01 +09: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 22:16:20 +09:00
|
|
|
|
|
|
|
// LsTree checks if the given filenames are in the tree
|
|
|
|
func (repo *Repository) LsTree(ref string, filenames ...string) ([]string, error) {
|
2022-02-07 04:01:47 +09:00
|
|
|
cmd := NewCommand(repo.Ctx, "ls-tree", "-z", "--name-only", "--", ref)
|
2021-07-20 22:16:20 +09:00
|
|
|
for _, arg := range filenames {
|
|
|
|
if arg != "" {
|
|
|
|
cmd.AddArguments(arg)
|
|
|
|
}
|
|
|
|
}
|
2022-04-01 11:55:30 +09:00
|
|
|
res, _, err := cmd.RunStdBytes(&RunOpts{Dir: repo.Path})
|
2021-07-20 22:16:20 +09: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
|
|
|
|
}
|