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 (
|
|
|
|
"path"
|
|
|
|
"strings"
|
2019-04-19 14:17:27 +02:00
|
|
|
|
2020-10-02 15:27:44 +02:00
|
|
|
"github.com/go-git/go-git/v5/plumbing"
|
2020-03-17 17:19:58 +01:00
|
|
|
"github.com/go-git/go-git/v5/plumbing/filemode"
|
|
|
|
"github.com/go-git/go-git/v5/plumbing/object"
|
2016-11-03 23:16:01 +01:00
|
|
|
)
|
|
|
|
|
2019-03-27 10:33:00 +01:00
|
|
|
// GetTreeEntryByPath get the tree entries according the sub dir
|
2016-11-03 23:16:01 +01:00
|
|
|
func (t *Tree) GetTreeEntryByPath(relpath string) (*TreeEntry, error) {
|
|
|
|
if len(relpath) == 0 {
|
|
|
|
return &TreeEntry{
|
2019-04-19 14:17:27 +02:00
|
|
|
ID: t.ID,
|
|
|
|
//Type: ObjectTree,
|
|
|
|
gogitTreeEntry: &object.TreeEntry{
|
|
|
|
Name: "",
|
|
|
|
Mode: filemode.Dir,
|
2019-07-23 20:50:39 +02:00
|
|
|
Hash: t.ID,
|
2019-04-19 14:17:27 +02:00
|
|
|
},
|
2016-11-03 23:16:01 +01:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
relpath = path.Clean(relpath)
|
|
|
|
parts := strings.Split(relpath, "/")
|
|
|
|
var err error
|
|
|
|
tree := t
|
|
|
|
for i, name := range parts {
|
|
|
|
if i == len(parts)-1 {
|
|
|
|
entries, err := tree.ListEntries()
|
|
|
|
if err != nil {
|
2020-10-02 15:27:44 +02:00
|
|
|
if err == plumbing.ErrObjectNotFound {
|
|
|
|
return nil, ErrNotExist{
|
|
|
|
RelPath: relpath,
|
|
|
|
}
|
|
|
|
}
|
2016-11-03 23:16:01 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, v := range entries {
|
2019-04-19 14:17:27 +02:00
|
|
|
if v.Name() == name {
|
2016-11-03 23:16:01 +01:00
|
|
|
return v, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
tree, err = tree.SubTree(name)
|
|
|
|
if err != nil {
|
2020-10-02 15:27:44 +02:00
|
|
|
if err == plumbing.ErrObjectNotFound {
|
|
|
|
return nil, ErrNotExist{
|
|
|
|
RelPath: relpath,
|
|
|
|
}
|
|
|
|
}
|
2016-11-03 23:16:01 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, ErrNotExist{"", relpath}
|
|
|
|
}
|
|
|
|
|
2019-03-27 10:33:00 +01:00
|
|
|
// GetBlobByPath get the blob object according the path
|
2016-11-03 23:16:01 +01:00
|
|
|
func (t *Tree) GetBlobByPath(relpath string) (*Blob, error) {
|
|
|
|
entry, err := t.GetTreeEntryByPath(relpath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-05-21 23:32:52 +02:00
|
|
|
if !entry.IsDir() && !entry.IsSubModule() {
|
2016-11-03 23:16:01 +01:00
|
|
|
return entry.Blob(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, ErrNotExist{"", relpath}
|
|
|
|
}
|