2016-11-03 18:16:01 -04:00
|
|
|
// Copyright 2015 The Gogs Authors. All rights reserved.
|
2019-04-19 08:17:27 -04:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2016-11-03 18:16:01 -04:00
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package git
|
|
|
|
|
|
|
|
import (
|
2019-04-19 08:17:27 -04:00
|
|
|
"io"
|
2016-11-03 18:16:01 -04:00
|
|
|
"strings"
|
2019-04-19 08:17:27 -04:00
|
|
|
|
2020-03-17 12:19:58 -04:00
|
|
|
"github.com/go-git/go-git/v5/plumbing"
|
|
|
|
"github.com/go-git/go-git/v5/plumbing/object"
|
2016-11-03 18:16:01 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// Tree represents a flat directory listing.
|
|
|
|
type Tree struct {
|
2019-05-02 20:33:11 -04:00
|
|
|
ID SHA1
|
|
|
|
ResolvedID SHA1
|
|
|
|
repo *Repository
|
2019-04-19 08:17:27 -04:00
|
|
|
|
|
|
|
gogitTree *object.Tree
|
2016-11-03 18:16:01 -04:00
|
|
|
|
|
|
|
// parent tree
|
|
|
|
ptree *Tree
|
|
|
|
}
|
|
|
|
|
2019-04-19 08:17:27 -04:00
|
|
|
// NewTree create a new tree according the repository and tree id
|
2016-12-22 04:30:52 -05:00
|
|
|
func NewTree(repo *Repository, id SHA1) *Tree {
|
2016-11-03 18:16:01 -04:00
|
|
|
return &Tree{
|
|
|
|
ID: id,
|
|
|
|
repo: repo,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-22 04:30:52 -05:00
|
|
|
// SubTree get a sub tree by the sub dir path
|
2016-11-03 18:16:01 -04: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
|
|
|
|
}
|
|
|
|
|
2019-04-19 08:17:27 -04:00
|
|
|
func (t *Tree) loadTreeObject() error {
|
2019-07-23 14:50:39 -04:00
|
|
|
gogitTree, err := t.repo.gogitRepo.TreeObject(t.ID)
|
2019-04-19 08:17:27 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2016-11-03 18:16:01 -04:00
|
|
|
}
|
|
|
|
|
2019-04-19 08:17:27 -04:00
|
|
|
t.gogitTree = gogitTree
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListEntries returns all entries of current tree.
|
|
|
|
func (t *Tree) ListEntries() (Entries, error) {
|
|
|
|
if t.gogitTree == nil {
|
|
|
|
err := t.loadTreeObject()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-11-03 18:16:01 -04:00
|
|
|
}
|
2019-02-02 22:35:17 -05:00
|
|
|
|
2019-04-19 08:17:27 -04:00
|
|
|
entries := make([]*TreeEntry, len(t.gogitTree.Entries))
|
|
|
|
for i, entry := range t.gogitTree.Entries {
|
|
|
|
entries[i] = &TreeEntry{
|
|
|
|
ID: entry.Hash,
|
|
|
|
gogitTreeEntry: &t.gogitTree.Entries[i],
|
|
|
|
ptree: t,
|
|
|
|
}
|
2019-02-02 22:35:17 -05:00
|
|
|
}
|
|
|
|
|
2019-04-19 08:17:27 -04:00
|
|
|
return entries, nil
|
2016-11-03 18:16:01 -04:00
|
|
|
}
|
2018-11-28 02:00:25 -05:00
|
|
|
|
|
|
|
// ListEntriesRecursive returns all entries of current tree recursively including all subtrees
|
|
|
|
func (t *Tree) ListEntriesRecursive() (Entries, error) {
|
2019-04-19 08:17:27 -04:00
|
|
|
if t.gogitTree == nil {
|
|
|
|
err := t.loadTreeObject()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-11-28 02:00:25 -05:00
|
|
|
}
|
2019-02-02 22:35:17 -05:00
|
|
|
|
2019-04-19 08:17:27 -04:00
|
|
|
var entries []*TreeEntry
|
|
|
|
seen := map[plumbing.Hash]bool{}
|
|
|
|
walker := object.NewTreeWalker(t.gogitTree, true, seen)
|
|
|
|
for {
|
2019-05-02 20:33:11 -04:00
|
|
|
fullName, entry, err := walker.Next()
|
2019-04-19 08:17:27 -04:00
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if seen[entry.Hash] {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
convertedEntry := &TreeEntry{
|
|
|
|
ID: entry.Hash,
|
|
|
|
gogitTreeEntry: &entry,
|
|
|
|
ptree: t,
|
2019-05-02 20:33:11 -04:00
|
|
|
fullName: fullName,
|
2019-04-19 08:17:27 -04:00
|
|
|
}
|
|
|
|
entries = append(entries, convertedEntry)
|
2019-02-02 22:35:17 -05:00
|
|
|
}
|
|
|
|
|
2019-04-19 08:17:27 -04:00
|
|
|
return entries, nil
|
2018-11-28 02:00:25 -05:00
|
|
|
}
|