2016-11-03 23:16:01 +01:00
|
|
|
// Copyright 2015 The Gogs Authors. All rights reserved.
|
2019-04-19 14:17:27 +02:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2016-11-03 23:16:01 +01:00
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package git
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2019-04-19 14:17:27 +02:00
|
|
|
// NewTree create a new tree according the repository and tree id
|
2016-12-22 10:30:52 +01:00
|
|
|
func NewTree(repo *Repository, id SHA1) *Tree {
|
2016-11-03 23:16:01 +01:00
|
|
|
return &Tree{
|
|
|
|
ID: id,
|
|
|
|
repo: repo,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-22 10:30:52 +01:00
|
|
|
// SubTree get a sub tree by the sub dir path
|
2016-11-03 23:16:01 +01: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
|
|
|
|
}
|