types.go RawHistoryFolder
1// SPDX-License-Identifier: MIT
2
3package git
4
5import "strconv"
6
7// Type is the type of a Git object. The values are those of pack entries.
8type Type uint8
9
10// Object types.
11const (
12 TypeCommit Type = 1
13 TypeTree Type = 2
14 TypeBlob Type = 3
15 TypeTag Type = 4
16)
17
18// String returns the name Git uses for the type ("commit", "tree", "blob",
19// "tag").
20func (t Type) String() string {
21 switch t {
22 case TypeCommit:
23 return "commit"
24 case TypeTree:
25 return "tree"
26 case TypeBlob:
27 return "blob"
28 case TypeTag:
29 return "tag"
30 }
31 return "type(" + strconv.Itoa(int(t)) + ")"
32}
33
34func parseTypeName(b []byte) (Type, bool) {
35 switch string(b) {
36 case "commit":
37 return TypeCommit, true
38 case "tree":
39 return TypeTree, true
40 case "blob":
41 return TypeBlob, true
42 case "tag":
43 return TypeTag, true
44 }
45 return 0, false
46}
47
48// Mode is the file mode of a tree entry.
49type Mode uint32
50
51// Tree entry modes.
52const (
53 ModeTree Mode = 0o040000
54 ModeFile Mode = 0o100644
55 ModeExec Mode = 0o100755
56 ModeSymlink Mode = 0o120000
57 ModeGitlink Mode = 0o160000 // a submodule
58)
59
60const modeTypeMask Mode = 0o170000
61
62// IsTree reports whether the entry is a directory.
63func (m Mode) IsTree() bool { return m&modeTypeMask == ModeTree }
64
65// IsGitlink reports whether the entry is a submodule commit.
66func (m Mode) IsGitlink() bool { return m&modeTypeMask == ModeGitlink }
67
68// IsSymlink reports whether the entry is a symbolic link.
69func (m Mode) IsSymlink() bool { return m&modeTypeMask == ModeSymlink }
70
71// IsRegular reports whether the entry is a regular file, executable or not.
72func (m Mode) IsRegular() bool { return m&modeTypeMask == 0o100000 }
73
74// Type returns the type of the object the entry designates: a tree for a
75// directory, a commit for a submodule, a blob otherwise.
76func (m Mode) Type() Type {
77 switch m & modeTypeMask {
78 case ModeTree:
79 return TypeTree
80 case ModeGitlink:
81 return TypeCommit
82 }
83 return TypeBlob
84}
85
86// String returns the mode as Git prints it: six octal digits (five for a
87// directory is printed as "040000").
88func (m Mode) String() string {
89 s := strconv.FormatUint(uint64(m), 8)
90 for len(s) < 6 {
91 s = "0" + s
92 }
93 return s
94}