Skip to content

Commit

Permalink
Find tree entry by id
Browse files Browse the repository at this point in the history
Add support for 'git_tree_entry_byid'.
  • Loading branch information
clns committed Jul 24, 2015
1 parent b4ba35d commit 64c160f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
18 changes: 18 additions & 0 deletions tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,24 @@ func (t Tree) EntryByName(filename string) *TreeEntry {
return newTreeEntry(entry)
}

// EntryById performs a lookup for a tree entry with the given SHA value.
//
// It returns a *TreeEntry that is owned by the Tree. You don't have to
// free it, but you must not use it after the Tree is freed.
//
// Warning: this must examine every entry in the tree, so it is not fast.
func (t Tree) EntryById(id *Oid) *TreeEntry {
runtime.LockOSThread()
defer runtime.UnlockOSThread()

entry := C.git_tree_entry_byid(t.cast_ptr, id.toC())
if entry == nil {
return nil
}

return newTreeEntry(entry)
}

// EntryByPath looks up an entry by its full path, recursing into
// deeper trees if necessary (i.e. if there are slashes in the path)
func (t Tree) EntryByPath(path string) (*TreeEntry, error) {
Expand Down
22 changes: 22 additions & 0 deletions tree_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package git

import "testing"

func TestTreeEntryById(t *testing.T) {
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)

_, treeID := seedTestRepo(t, repo)

tree, err := repo.LookupTree(treeID)
checkFatal(t, err)

id, err := NewOid("257cc5642cb1a054f08cc83f2d943e56fd3ebe99")
checkFatal(t, err)

entry := tree.EntryById(id)

if entry == nil {
t.Fatalf("entry id %v was not found", id)
}
}

0 comments on commit 64c160f

Please sign in to comment.