Skip to content

Commit 683e49d

Browse files
committed
tree: don't use system stat constants
On most Unix systems, the stat constants (S_IFMT and friends) that Git uses are the same: that is, the system values are the same ones that Git uses. However, on zOS, this isn't the case, and the system constants are different. Let's add a set of our constants that are used by Git to determine the type of an object that will be right on all platforms instead of using the system constants. Note that the tests already cover this case; they'll just now pass on additional systems.
1 parent f177805 commit 683e49d

File tree

1 file changed

+19
-15
lines changed

1 file changed

+19
-15
lines changed

tree.go

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,21 @@ import (
99
"sort"
1010
"strconv"
1111
"strings"
12-
"syscall"
1312

1413
"github.com/git-lfs/gitobj/v2/pack"
1514
)
1615

16+
// We define these here instead of using the system ones because not all
17+
// operating systems use the traditional values. For example, zOS uses
18+
// different values.
19+
const (
20+
sIFMT = int32(0170000)
21+
sIFREG = int32(0100000)
22+
sIFDIR = int32(0040000)
23+
sIFLNK = int32(0120000)
24+
sIFGITLINK = int32(0160000)
25+
)
26+
1727
// Tree encapsulates a Git tree object.
1828
type Tree struct {
1929
// Entries is the list of entries held by this tree.
@@ -209,24 +219,18 @@ func (e *TreeEntry) Equal(other *TreeEntry) bool {
209219
// Type is the type of entry (either blob: BlobObjectType, or a sub-tree:
210220
// TreeObjectType).
211221
func (e *TreeEntry) Type() ObjectType {
212-
switch e.Filemode & syscall.S_IFMT {
213-
case syscall.S_IFREG:
222+
switch e.Filemode & sIFMT {
223+
case sIFREG:
214224
return BlobObjectType
215-
case syscall.S_IFDIR:
225+
case sIFDIR:
216226
return TreeObjectType
217-
case syscall.S_IFLNK:
227+
case sIFLNK:
218228
return BlobObjectType
229+
case sIFGITLINK:
230+
return CommitObjectType
219231
default:
220-
if e.Filemode == 0xe000 {
221-
// Mode 0xe000, or a gitlink, has no formal filesystem
222-
// (`syscall.S_IF<t>`) equivalent.
223-
//
224-
// Safeguard that catch here, or otherwise panic.
225-
return CommitObjectType
226-
} else {
227-
panic(fmt.Sprintf("gitobj: unknown object type: %o",
228-
e.Filemode))
229-
}
232+
panic(fmt.Sprintf("gitobj: unknown object type: %o",
233+
e.Filemode))
230234
}
231235
}
232236

0 commit comments

Comments
 (0)