mirror of
https://github.com/go-gitea/gitea
synced 2024-11-18 14:19:31 +01:00
124b072f0b
Follows #19266, #8553, Close #18553, now there are only three `Run..(&RunOpts{})` functions. * before: `stdout, err := RunInDir(path)` * now: `stdout, _, err := RunStdString(&git.RunOpts{Dir:path})`
69 lines
1.4 KiB
Go
69 lines
1.4 KiB
Go
// Copyright 2015 The Gogs Authors. All rights reserved.
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
|
// Use of this source code is governed by a MIT-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package git
|
|
|
|
import (
|
|
"bytes"
|
|
"strings"
|
|
)
|
|
|
|
// NewTree create a new tree according the repository and tree id
|
|
func NewTree(repo *Repository, id SHA1) *Tree {
|
|
return &Tree{
|
|
ID: id,
|
|
repo: repo,
|
|
}
|
|
}
|
|
|
|
// SubTree get a sub tree by the sub dir path
|
|
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
|
|
}
|
|
|
|
// LsTree checks if the given filenames are in the tree
|
|
func (repo *Repository) LsTree(ref string, filenames ...string) ([]string, error) {
|
|
cmd := NewCommand(repo.Ctx, "ls-tree", "-z", "--name-only", "--", ref)
|
|
for _, arg := range filenames {
|
|
if arg != "" {
|
|
cmd.AddArguments(arg)
|
|
}
|
|
}
|
|
res, _, err := cmd.RunStdBytes(&RunOpts{Dir: 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
|
|
}
|