Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,12 @@ func (e *TreeEntry) Type() ObjectType {
}
}

// IsLink returns true if the given TreeEntry is a blob which represents a
// symbolic link (i.e., with a filemode of 0120000.
func (e *TreeEntry) IsLink() bool {
return e.Filemode & sIFMT == sIFLNK
}

// SubtreeOrder is an implementation of sort.Interface that sorts a set of
// `*TreeEntry`'s according to "subtree" order. This ordering is required to
// write trees in a correct, readable format to the Git object database.
Expand Down
23 changes: 17 additions & 6 deletions tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,10 @@ func TestMergeInsertElementsInSubtreeOrder(t *testing.T) {
type TreeEntryTypeTestCase struct {
Filemode int32
Expected ObjectType
IsLink bool
}

func (c *TreeEntryTypeTestCase) Assert(t *testing.T) {
func (c *TreeEntryTypeTestCase) AssertType(t *testing.T) {
e := &TreeEntry{Filemode: c.Filemode}

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

func (c *TreeEntryTypeTestCase) AssertIsLink(t *testing.T) {
e := &TreeEntry{Filemode: c.Filemode}

isLink := e.IsLink()

assert.Equal(t, c.IsLink, isLink,
"gitobj: expected link: %v, got: %v, for type %s", c.IsLink, isLink, c.Expected)
}

func TestTreeEntryTypeResolution(t *testing.T) {
for desc, c := range map[string]*TreeEntryTypeTestCase{
"blob": {0100644, BlobObjectType},
"subtree": {040000, TreeObjectType},
"symlink": {0120000, BlobObjectType},
"commit": {0160000, CommitObjectType},
"blob": {0100644, BlobObjectType, false},
"subtree": {040000, TreeObjectType, false},
"symlink": {0120000, BlobObjectType, true},
"commit": {0160000, CommitObjectType, false},
} {
t.Run(desc, c.Assert)
t.Run(desc, c.AssertType)
t.Run(desc, c.AssertIsLink)
}
}

Expand Down