2016-11-04 03:46:01 +05:30
|
|
|
// Copyright 2015 The Gogs Authors. All rights reserved.
|
2019-04-19 17:47:27 +05:30
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2016-11-04 03:46:01 +05:30
|
|
|
// 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 17:47:27 +05:30
|
|
|
|
2020-03-17 21:49:58 +05:30
|
|
|
"github.com/go-git/go-git/v5/plumbing/filemode"
|
|
|
|
"github.com/go-git/go-git/v5/plumbing/object"
|
2016-11-04 03:46:01 +05:30
|
|
|
)
|
|
|
|
|
2019-03-27 15:03:00 +05:30
|
|
|
// GetTreeEntryByPath get the tree entries according the sub dir
|
2016-11-04 03:46:01 +05:30
|
|
|
func (t *Tree) GetTreeEntryByPath(relpath string) (*TreeEntry, error) {
|
|
|
|
if len(relpath) == 0 {
|
|
|
|
return &TreeEntry{
|
2019-04-19 17:47:27 +05:30
|
|
|
ID: t.ID,
|
|
|
|
//Type: ObjectTree,
|
|
|
|
gogitTreeEntry: &object.TreeEntry{
|
|
|
|
Name: "",
|
|
|
|
Mode: filemode.Dir,
|
2019-07-24 00:20:39 +05:30
|
|
|
Hash: t.ID,
|
2019-04-19 17:47:27 +05:30
|
|
|
},
|
2016-11-04 03:46:01 +05:30
|
|
|
}, 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 {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, v := range entries {
|
2019-04-19 17:47:27 +05:30
|
|
|
if v.Name() == name {
|
2016-11-04 03:46:01 +05:30
|
|
|
return v, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
tree, err = tree.SubTree(name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, ErrNotExist{"", relpath}
|
|
|
|
}
|
|
|
|
|
2019-03-27 15:03:00 +05:30
|
|
|
// GetBlobByPath get the blob object according the path
|
2016-11-04 03:46:01 +05:30
|
|
|
func (t *Tree) GetBlobByPath(relpath string) (*Blob, error) {
|
|
|
|
entry, err := t.GetTreeEntryByPath(relpath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-05-22 03:02:52 +05:30
|
|
|
if !entry.IsDir() && !entry.IsSubModule() {
|
2016-11-04 03:46:01 +05:30
|
|
|
return entry.Blob(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, ErrNotExist{"", relpath}
|
|
|
|
}
|