Skip to content

Commit f2fd93a

Browse files
committed
Added lookup to tree
1 parent bf8f05d commit f2fd93a

File tree

3 files changed

+96
-1
lines changed

3 files changed

+96
-1
lines changed

include/tree.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ class GitTree : public ObjectWrap {
4242

4343
static Handle<Value> New(const Arguments& args);
4444

45+
static Handle<Value> Lookup(const Arguments& args);
46+
static void LookupWork(uv_work_t* req);
47+
static void LookupAfterWork(uv_work_t* req);
48+
4549
static Handle<Value> Walk(const Arguments& args);
4650
static void WalkWork(void* payload);
4751
static int WalkWorkEntry(const char *root, const git_tree_entry *entry, void *payload);
@@ -56,6 +60,17 @@ class GitTree : public ObjectWrap {
5660

5761
git_tree* tree;
5862

63+
struct LookupBaton {
64+
uv_work_t request;
65+
const git_error* error;
66+
67+
git_oid rawOid;
68+
git_repository* rawRepo;
69+
git_tree* rawTree;
70+
71+
Persistent<Function> callback;
72+
};
73+
5974
struct WalkEntry {
6075
git_tree_entry* rawEntry;
6176
std::string root;

lib/tree.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,27 @@ var Tree = function(rawRepo, rawTree) {
2222
}
2323
};
2424

25+
/**
26+
* Retrieve the raw tree identified by the given Oid.
27+
*
28+
* @param {Oid} oid The Oid identifying a tree.
29+
* @param {Tree~lookupCallback} callback
30+
*/
31+
Tree.prototype.lookup = function(oid, callback) {
32+
/**
33+
* @callback Tree~lookupCallback Callback executed when the tree is retrieved.
34+
* @param {GitError|null} error An Error or null if successful.
35+
* @param {Tree|null} tree The tree object or null.
36+
*/
37+
var self = this;
38+
self.rawTree.lookup(oid.getRawOid(), self.rawRepo, function(error, rawTree) {
39+
if (success(error, callback)) {
40+
self.rawTree = rawTree;
41+
callback(null, self);
42+
}
43+
});
44+
};
45+
2546
/**
2647
* Retrieve the entry by path.
2748
*
@@ -32,7 +53,7 @@ Tree.prototype.entry = function(path, callback) {
3253
/**
3354
* @callback Tree~entryCallback Callback executed when an entry is retrieved.
3455
* @param {GitError|null} error An Error or null if successful.
35-
* @param {Entry|null} blob The tree entry object or null.
56+
* @param {Entry|null} entry The tree entry object or null.
3657
*/
3758
var self = this;
3859
self.rawTree.entryByPath(path, function(error, rawEntry) {

src/tree.cc

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ void GitTree::Initialize (Handle<v8::Object> target) {
3333
tpl->InstanceTemplate()->SetInternalFieldCount(1);
3434
tpl->SetClassName(String::NewSymbol("Tree"));
3535

36+
NODE_SET_PROTOTYPE_METHOD(tpl, "lookup", Lookup);
3637
NODE_SET_PROTOTYPE_METHOD(tpl, "walk", Walk);
3738
NODE_SET_PROTOTYPE_METHOD(tpl, "entryByPath", EntryByPath);
3839

@@ -57,6 +58,64 @@ Handle<Value> GitTree::New(const Arguments& args) {
5758
return scope.Close(args.This());
5859
}
5960

61+
Handle<Value> GitTree::Lookup(const Arguments& args) {
62+
HandleScope scope;
63+
64+
if(args.Length() == 0 || !args[0]->IsObject()) {
65+
return ThrowException(Exception::Error(String::New("Oid is required and must be a Object.")));
66+
}
67+
68+
if(args.Length() == 1 || !args[1]->IsObject()) {
69+
return ThrowException(Exception::Error(String::New("Repository is required and must be a Object.")));
70+
}
71+
72+
if(args.Length() == 2 || !args[2]->IsFunction()) {
73+
return ThrowException(Exception::Error(String::New("Callback is required and must be a Function.")));
74+
}
75+
76+
LookupBaton* baton = new LookupBaton;
77+
baton->request.data = baton;
78+
baton->error = NULL;
79+
baton->rawTree = ObjectWrap::Unwrap<GitTree>(args.This())->GetValue();
80+
baton->rawOid = ObjectWrap::Unwrap<GitOid>(args[0]->ToObject())->GetValue();
81+
baton->rawRepo = ObjectWrap::Unwrap<GitRepo>(args[1]->ToObject())->GetValue();
82+
baton->callback = Persistent<Function>::New(Local<Function>::Cast(args[2]));
83+
84+
uv_queue_work(uv_default_loop(), &baton->request, LookupWork, (uv_after_work_cb)LookupAfterWork);
85+
86+
return Undefined();
87+
}
88+
void GitTree::LookupWork(uv_work_t* req) {
89+
LookupBaton* baton = static_cast<LookupBaton*>(req->data);
90+
91+
int returnCode = git_tree_lookup(&baton->rawTree, baton->rawRepo, &baton->rawOid);
92+
if (returnCode != GIT_OK) {
93+
baton->error = giterr_last();
94+
}
95+
}
96+
void GitTree::LookupAfterWork(uv_work_t* req) {
97+
HandleScope scope;
98+
LookupBaton* baton = static_cast<LookupBaton* >(req->data);
99+
100+
if (success(baton->error, baton->callback)) {
101+
Local<Object> tree = GitTree::constructor_template->NewInstance();
102+
GitTree *treeInstance = ObjectWrap::Unwrap<GitTree>(tree);
103+
treeInstance->SetValue(baton->rawTree);
104+
105+
Handle<Value> argv[2] = {
106+
Local<Value>::New(Null()),
107+
tree
108+
};
109+
110+
TryCatch try_catch;
111+
baton->callback->Call(Context::GetCurrent()->Global(), 2, argv);
112+
if (try_catch.HasCaught()) {
113+
node::FatalException(try_catch);
114+
}
115+
}
116+
delete req;
117+
}
118+
60119
Handle<Value> GitTree::Walk(const Arguments& args) {
61120
HandleScope scope;
62121

0 commit comments

Comments
 (0)