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