github.com/driusan/dgit@v0.0.0-20221118233547-f39f0c15edbb/git/entrymode.go (about)

     1  package git
     2  
     3  import (
     4  	"fmt"
     5  )
     6  
     7  // An EntryMode is like an os.FileMode, but restricted to the values
     8  // that are legal in git.
     9  type EntryMode uint32
    10  
    11  const (
    12  	ModeBlob    = EntryMode(0100644)
    13  	ModeExec    = EntryMode(0100755)
    14  	ModeSymlink = EntryMode(0120000)
    15  	ModeCommit  = EntryMode(0160000)
    16  	ModeTree    = EntryMode(0040000)
    17  )
    18  
    19  // we sometimes see these coming from git9, although they chouldn't be valid
    20  // according to the git spec. We need to handle them to clone some repos anyways
    21  const modeGit9Tree = EntryMode(0040755)
    22  
    23  // TreeType prints the entry mode as the type that shows up in the "git ls-tree"
    24  // command.
    25  func (e EntryMode) TreeType() string {
    26  
    27  	switch e {
    28  	case ModeBlob, ModeExec, ModeSymlink:
    29  		return "blob"
    30  	case ModeCommit:
    31  		return "commit"
    32  	case ModeTree, modeGit9Tree:
    33  		return "tree"
    34  	default:
    35  		panic(fmt.Sprintf("Invalid mode %o", e))
    36  	}
    37  }
    38  
    39  func ModeFromString(s string) (EntryMode, error) {
    40  	switch s {
    41  	case "100644":
    42  		return ModeBlob, nil
    43  	case "100755":
    44  		return ModeExec, nil
    45  	case "120000":
    46  		return ModeSymlink, nil
    47  	case "160000":
    48  		return ModeCommit, nil
    49  	case "040755":
    50  		return modeGit9Tree, fmt.Errorf("Bad tree type %v", s)
    51  	case "040000":
    52  		return ModeTree, nil
    53  	default:
    54  		return 0, fmt.Errorf("Unknown mode: %v", s)
    55  	}
    56  }