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