github.com/gitbundle/modules@v0.0.0-20231025071548-85b91c5c3b01/git/repo_tree_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 func (repo *Repository) getTree(id SHA1) (*Tree, error) { 13 gogitTree, err := repo.gogitRepo.TreeObject(id) 14 if err != nil { 15 return nil, err 16 } 17 18 tree := NewTree(repo, id) 19 tree.gogitTree = gogitTree 20 return tree, nil 21 } 22 23 // GetTree find the tree object in the repository. 24 func (repo *Repository) GetTree(idStr string) (*Tree, error) { 25 if len(idStr) != 40 { 26 res, _, err := NewCommand(repo.Ctx, "rev-parse", "--verify", idStr).RunStdString(&RunOpts{Dir: repo.Path}) 27 if err != nil { 28 return nil, err 29 } 30 if len(res) > 0 { 31 idStr = res[:len(res)-1] 32 } 33 } 34 id, err := NewIDFromString(idStr) 35 if err != nil { 36 return nil, err 37 } 38 resolvedID := id 39 commitObject, err := repo.gogitRepo.CommitObject(id) 40 if err == nil { 41 id = SHA1(commitObject.TreeHash) 42 } 43 treeObject, err := repo.getTree(id) 44 if err != nil { 45 return nil, err 46 } 47 treeObject.ResolvedID = resolvedID 48 return treeObject, nil 49 }