github.com/gitbundle/modules@v0.0.0-20231025071548-85b91c5c3b01/git/tree_blob_gogit.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 // Copyright 2015 The Gogs Authors. All rights reserved. 7 8 //go:build gogit 9 10 package git 11 12 import ( 13 "path" 14 "strings" 15 16 "github.com/go-git/go-git/v5/plumbing" 17 "github.com/go-git/go-git/v5/plumbing/filemode" 18 "github.com/go-git/go-git/v5/plumbing/object" 19 ) 20 21 // GetTreeEntryByPath get the tree entries according the sub dir 22 func (t *Tree) GetTreeEntryByPath(relpath string) (*TreeEntry, error) { 23 if len(relpath) == 0 { 24 return &TreeEntry{ 25 ID: t.ID, 26 // Type: ObjectTree, 27 gogitTreeEntry: &object.TreeEntry{ 28 Name: "", 29 Mode: filemode.Dir, 30 Hash: t.ID, 31 }, 32 }, nil 33 } 34 35 relpath = path.Clean(relpath) 36 parts := strings.Split(relpath, "/") 37 var err error 38 tree := t 39 for i, name := range parts { 40 if i == len(parts)-1 { 41 entries, err := tree.ListEntries() 42 if err != nil { 43 if err == plumbing.ErrObjectNotFound { 44 return nil, ErrNotExist{ 45 RelPath: relpath, 46 } 47 } 48 return nil, err 49 } 50 for _, v := range entries { 51 if v.Name() == name { 52 return v, nil 53 } 54 } 55 } else { 56 tree, err = tree.SubTree(name) 57 if err != nil { 58 if err == plumbing.ErrObjectNotFound { 59 return nil, ErrNotExist{ 60 RelPath: relpath, 61 } 62 } 63 return nil, err 64 } 65 } 66 } 67 return nil, ErrNotExist{"", relpath} 68 }