repo_tree.go 758 B

123456789101112131415161718192021222324252627282930313233
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package git
  5. import (
  6. "fmt"
  7. "github.com/Unknwon/com"
  8. )
  9. // Find the tree object in the repository.
  10. func (repo *Repository) GetTree(idStr string) (*Tree, error) {
  11. id, err := NewIdFromString(idStr)
  12. if err != nil {
  13. return nil, err
  14. }
  15. return repo.getTree(id)
  16. }
  17. func (repo *Repository) getTree(id sha1) (*Tree, error) {
  18. treePath := filepathFromSHA1(repo.Path, id.String())
  19. if !com.IsFile(treePath) {
  20. _, _, err := com.ExecCmdDir(repo.Path, "git", "ls-tree", id.String())
  21. if err != nil {
  22. return nil, fmt.Errorf("repo.getTree: %v", ErrNotExist)
  23. }
  24. }
  25. return NewTree(repo, id), nil
  26. }