code.gitea.io/gitea@v1.19.3/modules/git/tree_entry_nogogit.go (about)

     1  // Copyright 2020 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  //go:build !gogit
     5  
     6  package git
     7  
     8  import "code.gitea.io/gitea/modules/log"
     9  
    10  // TreeEntry the leaf in the git tree
    11  type TreeEntry struct {
    12  	ID SHA1
    13  
    14  	ptree *Tree
    15  
    16  	entryMode EntryMode
    17  	name      string
    18  
    19  	size     int64
    20  	sized    bool
    21  	fullName string
    22  }
    23  
    24  // Name returns the name of the entry
    25  func (te *TreeEntry) Name() string {
    26  	if te.fullName != "" {
    27  		return te.fullName
    28  	}
    29  	return te.name
    30  }
    31  
    32  // Mode returns the mode of the entry
    33  func (te *TreeEntry) Mode() EntryMode {
    34  	return te.entryMode
    35  }
    36  
    37  // Size returns the size of the entry
    38  func (te *TreeEntry) Size() int64 {
    39  	if te.IsDir() {
    40  		return 0
    41  	} else if te.sized {
    42  		return te.size
    43  	}
    44  
    45  	wr, rd, cancel := te.ptree.repo.CatFileBatchCheck(te.ptree.repo.Ctx)
    46  	defer cancel()
    47  	_, err := wr.Write([]byte(te.ID.String() + "\n"))
    48  	if err != nil {
    49  		log.Debug("error whilst reading size for %s in %s. Error: %v", te.ID.String(), te.ptree.repo.Path, err)
    50  		return 0
    51  	}
    52  	_, _, te.size, err = ReadBatchLine(rd)
    53  	if err != nil {
    54  		log.Debug("error whilst reading size for %s in %s. Error: %v", te.ID.String(), te.ptree.repo.Path, err)
    55  		return 0
    56  	}
    57  
    58  	te.sized = true
    59  	return te.size
    60  }
    61  
    62  // IsSubModule if the entry is a sub module
    63  func (te *TreeEntry) IsSubModule() bool {
    64  	return te.entryMode == EntryModeCommit
    65  }
    66  
    67  // IsDir if the entry is a sub dir
    68  func (te *TreeEntry) IsDir() bool {
    69  	return te.entryMode == EntryModeTree
    70  }
    71  
    72  // IsLink if the entry is a symlink
    73  func (te *TreeEntry) IsLink() bool {
    74  	return te.entryMode == EntryModeSymlink
    75  }
    76  
    77  // IsRegular if the entry is a regular file
    78  func (te *TreeEntry) IsRegular() bool {
    79  	return te.entryMode == EntryModeBlob
    80  }
    81  
    82  // IsExecutable if the entry is an executable file (not necessarily binary)
    83  func (te *TreeEntry) IsExecutable() bool {
    84  	return te.entryMode == EntryModeExec
    85  }
    86  
    87  // Blob returns the blob object the entry
    88  func (te *TreeEntry) Blob() *Blob {
    89  	return &Blob{
    90  		ID:      te.ID,
    91  		name:    te.Name(),
    92  		size:    te.size,
    93  		gotSize: te.sized,
    94  		repo:    te.ptree.repo,
    95  	}
    96  }