Skip to content

Commit 31e62b6

Browse files
committed
tree: add IsLink method for TreeEntry class
Some callers may need to disambiguate between regular blob objects and those which represent symbolic links, so we add an IsLink() method to the TreeEntry class that returns true when the object is a blob and a symlink, and false otherwise. We also expand the test suite slightly to exercise this new method.
1 parent 57c3fe4 commit 31e62b6

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

tree.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,12 @@ func (e *TreeEntry) Type() ObjectType {
234234
}
235235
}
236236

237+
// IsLink returns true if the given TreeEntry is a blob which represents a
238+
// symbolic link (i.e., with a filemode of 0120000.
239+
func (e *TreeEntry) IsLink() bool {
240+
return e.Filemode & sIFMT == sIFLNK
241+
}
242+
237243
// SubtreeOrder is an implementation of sort.Interface that sorts a set of
238244
// `*TreeEntry`'s according to "subtree" order. This ordering is required to
239245
// write trees in a correct, readable format to the Git object database.

tree_test.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,10 @@ func TestMergeInsertElementsInSubtreeOrder(t *testing.T) {
166166
type TreeEntryTypeTestCase struct {
167167
Filemode int32
168168
Expected ObjectType
169+
IsLink bool
169170
}
170171

171-
func (c *TreeEntryTypeTestCase) Assert(t *testing.T) {
172+
func (c *TreeEntryTypeTestCase) AssertType(t *testing.T) {
172173
e := &TreeEntry{Filemode: c.Filemode}
173174

174175
got := e.Type()
@@ -177,14 +178,24 @@ func (c *TreeEntryTypeTestCase) Assert(t *testing.T) {
177178
"gitobj: expected type: %s, got: %s", c.Expected, got)
178179
}
179180

181+
func (c *TreeEntryTypeTestCase) AssertIsLink(t *testing.T) {
182+
e := &TreeEntry{Filemode: c.Filemode}
183+
184+
isLink := e.IsLink()
185+
186+
assert.Equal(t, c.IsLink, isLink,
187+
"gitobj: expected link: %v, got: %v, for type %s", c.IsLink, isLink, c.Expected)
188+
}
189+
180190
func TestTreeEntryTypeResolution(t *testing.T) {
181191
for desc, c := range map[string]*TreeEntryTypeTestCase{
182-
"blob": {0100644, BlobObjectType},
183-
"subtree": {040000, TreeObjectType},
184-
"symlink": {0120000, BlobObjectType},
185-
"commit": {0160000, CommitObjectType},
192+
"blob": {0100644, BlobObjectType, false},
193+
"subtree": {040000, TreeObjectType, false},
194+
"symlink": {0120000, BlobObjectType, true},
195+
"commit": {0160000, CommitObjectType, false},
186196
} {
187-
t.Run(desc, c.Assert)
197+
t.Run(desc, c.AssertType)
198+
t.Run(desc, c.AssertIsLink)
188199
}
189200
}
190201

0 commit comments

Comments
 (0)