github.com/gitbundle/modules@v0.0.0-20231025071548-85b91c5c3b01/git/tree_entry_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  import (
    13  	"github.com/go-git/go-git/v5/plumbing"
    14  	"github.com/go-git/go-git/v5/plumbing/filemode"
    15  	"github.com/go-git/go-git/v5/plumbing/object"
    16  )
    17  
    18  // TreeEntry the leaf in the git tree
    19  type TreeEntry struct {
    20  	ID SHA1
    21  
    22  	gogitTreeEntry *object.TreeEntry
    23  	ptree          *Tree
    24  
    25  	size     int64
    26  	sized    bool
    27  	fullName string
    28  }
    29  
    30  // Name returns the name of the entry
    31  func (te *TreeEntry) Name() string {
    32  	if te.fullName != "" {
    33  		return te.fullName
    34  	}
    35  	return te.gogitTreeEntry.Name
    36  }
    37  
    38  // Mode returns the mode of the entry
    39  func (te *TreeEntry) Mode() EntryMode {
    40  	return EntryMode(te.gogitTreeEntry.Mode)
    41  }
    42  
    43  // Size returns the size of the entry
    44  func (te *TreeEntry) Size() int64 {
    45  	if te.IsDir() {
    46  		return 0
    47  	} else if te.sized {
    48  		return te.size
    49  	}
    50  
    51  	file, err := te.ptree.gogitTree.TreeEntryFile(te.gogitTreeEntry)
    52  	if err != nil {
    53  		return 0
    54  	}
    55  
    56  	te.sized = true
    57  	te.size = file.Size
    58  	return te.size
    59  }
    60  
    61  // IsSubModule if the entry is a sub module
    62  func (te *TreeEntry) IsSubModule() bool {
    63  	return te.gogitTreeEntry.Mode == filemode.Submodule
    64  }
    65  
    66  // IsDir if the entry is a sub dir
    67  func (te *TreeEntry) IsDir() bool {
    68  	return te.gogitTreeEntry.Mode == filemode.Dir
    69  }
    70  
    71  // IsLink if the entry is a symlink
    72  func (te *TreeEntry) IsLink() bool {
    73  	return te.gogitTreeEntry.Mode == filemode.Symlink
    74  }
    75  
    76  // IsRegular if the entry is a regular file
    77  func (te *TreeEntry) IsRegular() bool {
    78  	return te.gogitTreeEntry.Mode == filemode.Regular
    79  }
    80  
    81  // IsExecutable if the entry is an executable file (not necessarily binary)
    82  func (te *TreeEntry) IsExecutable() bool {
    83  	return te.gogitTreeEntry.Mode == filemode.Executable
    84  }
    85  
    86  // Blob returns the blob object the entry
    87  func (te *TreeEntry) Blob() *Blob {
    88  	encodedObj, err := te.ptree.repo.gogitRepo.Storer.EncodedObject(plumbing.AnyObject, te.gogitTreeEntry.Hash)
    89  	if err != nil {
    90  		return nil
    91  	}
    92  
    93  	return &Blob{
    94  		ID:              te.gogitTreeEntry.Hash,
    95  		gogitEncodedObj: encodedObj,
    96  		name:            te.Name(),
    97  	}
    98  }