github.com/gitbundle/modules@v0.0.0-20231025071548-85b91c5c3b01/git/repo_tree_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  	"io"
    12  )
    13  
    14  func (repo *Repository) getTree(id SHA1) (*Tree, error) {
    15  	wr, rd, cancel := repo.CatFileBatch(repo.Ctx)
    16  	defer cancel()
    17  
    18  	_, _ = wr.Write([]byte(id.String() + "\n"))
    19  
    20  	// ignore the SHA
    21  	_, typ, size, err := ReadBatchLine(rd)
    22  	if err != nil {
    23  		return nil, err
    24  	}
    25  
    26  	switch typ {
    27  	case "tag":
    28  		resolvedID := id
    29  		data, err := io.ReadAll(io.LimitReader(rd, size))
    30  		if err != nil {
    31  			return nil, err
    32  		}
    33  		tag, err := parseTagData(data)
    34  		if err != nil {
    35  			return nil, err
    36  		}
    37  		commit, err := tag.Commit(repo)
    38  		if err != nil {
    39  			return nil, err
    40  		}
    41  		commit.Tree.ResolvedID = resolvedID
    42  		return &commit.Tree, nil
    43  	case "commit":
    44  		commit, err := CommitFromReader(repo, id, io.LimitReader(rd, size))
    45  		if err != nil {
    46  			return nil, err
    47  		}
    48  		if _, err := rd.Discard(1); err != nil {
    49  			return nil, err
    50  		}
    51  		commit.Tree.ResolvedID = commit.ID
    52  		return &commit.Tree, nil
    53  	case "tree":
    54  		tree := NewTree(repo, id)
    55  		tree.ResolvedID = id
    56  		tree.entries, err = catBatchParseTreeEntries(tree, rd, size)
    57  		if err != nil {
    58  			return nil, err
    59  		}
    60  		tree.entriesParsed = true
    61  		return tree, nil
    62  	default:
    63  		return nil, ErrNotExist{
    64  			ID: id.String(),
    65  		}
    66  	}
    67  }
    68  
    69  // GetTree find the tree object in the repository.
    70  func (repo *Repository) GetTree(idStr string) (*Tree, error) {
    71  	if len(idStr) != 40 {
    72  		res, err := repo.GetRefCommitID(idStr)
    73  		if err != nil {
    74  			return nil, err
    75  		}
    76  		if len(res) > 0 {
    77  			idStr = res
    78  		}
    79  	}
    80  	id, err := NewIDFromString(idStr)
    81  	if err != nil {
    82  		return nil, err
    83  	}
    84  
    85  	return repo.getTree(id)
    86  }