github.com/gitbundle/modules@v0.0.0-20231025071548-85b91c5c3b01/git/tree_blob_nogogit.go (about) 1 // Copyright 2023 The GitBundle Inc. All rights reserved. 2 // Copyright 2017 The Gitea Authors. All rights reserved. 3 // Use of this source code is governed by a MIT-style 4 // license that can be found in the LICENSE file. 5 6 //go:build !gogit 7 8 package git 9 10 import ( 11 "path" 12 "strings" 13 ) 14 15 // GetTreeEntryByPath get the tree entries according the sub dir 16 func (t *Tree) GetTreeEntryByPath(relpath string) (*TreeEntry, error) { 17 if len(relpath) == 0 { 18 return &TreeEntry{ 19 ptree: t, 20 ID: t.ID, 21 name: "", 22 fullName: "", 23 entryMode: EntryModeTree, 24 }, nil 25 } 26 27 // FIXME: This should probably use git cat-file --batch to be a bit more efficient 28 relpath = path.Clean(relpath) 29 parts := strings.Split(relpath, "/") 30 var err error 31 tree := t 32 for i, name := range parts { 33 if i == len(parts)-1 { 34 entries, err := tree.ListEntries() 35 if err != nil { 36 return nil, err 37 } 38 for _, v := range entries { 39 if v.Name() == name { 40 return v, nil 41 } 42 } 43 } else { 44 tree, err = tree.SubTree(name) 45 if err != nil { 46 return nil, err 47 } 48 } 49 } 50 return nil, ErrNotExist{"", relpath} 51 }