FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Updated lookup, added sha & oid getters · elfscript/nodegit@0335d8c · GitHub

Repository navigation

Commit 0335d8c

Browse files
committed
Updated lookup, added sha & oid getters
1 parent 0873d96 commit 0335d8c

2 files changed

Lines changed: 69 additions & 17 deletions

File tree

‎lib/commit.js‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,18 @@ Commit.prototype.lookup = function(oid, callback) {
4242
});
4343
};
4444

45+
Commit.prototype.oid = function(callback) {
46+
callback(null, new git.oid(this.rawCommit.oid()));
47+
};
48+
49+
Commit.prototype.sha = function(callback) {
50+
this.rawCommit.sha(function(error, sha) {
51+
if (success(error, callback)) {
52+
callback(null, sha);
53+
}
54+
});
55+
};
56+
4557
/**
4658
* Retrieve the tree for this commit.
4759
*

‎src/commit.cc‎

Lines changed: 57 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ void GitCommit::Initialize(Handle<Object> target) {
3535
tpl->SetClassName(String::NewSymbol("Commit"));
3636

3737
NODE_SET_PROTOTYPE_METHOD(tpl, "lookup", Lookup);
38+
NODE_SET_PROTOTYPE_METHOD(tpl, "oid", Oid);
39+
NODE_SET_PROTOTYPE_METHOD(tpl, "sha", Sha);
40+
3841
NODE_SET_PROTOTYPE_METHOD(tpl, "close", Close);
3942

4043
NODE_SET_PROTOTYPE_METHOD(tpl, "tree", Tree);
@@ -212,17 +215,16 @@ Handle<Value> GitCommit::Lookup(const Arguments& args) {
212215
void GitCommit::LookupWork(uv_work_t *req) {
213216
LookupBaton *baton = static_cast<LookupBaton *>(req->data);
214217

215-
git_oid rawOid = baton->rawOid;
216218
if (!baton->sha.empty()) {
217-
int returnCode = git_oid_fromstr(&rawOid, baton->sha.c_str());
219+
int returnCode = git_oid_fromstr(&baton->rawOid, baton->sha.c_str());
218220
if (returnCode != GIT_OK) {
219221
baton->error = giterr_last();
220222
return;
221223
}
222224
}
223225

224226
baton->rawCommit = NULL;
225-
int returnCode = git_commit_lookup(&baton->rawCommit, baton->repo, &rawOid);
227+
int returnCode = git_commit_lookup(&baton->rawCommit, baton->repo, &baton->rawOid);
226228
if (returnCode != GIT_OK) {
227229
baton->error = giterr_last();
228230
}
@@ -231,23 +233,11 @@ void GitCommit::LookupAfterWork(uv_work_t *req) {
231233
HandleScope scope;
232234
LookupBaton *baton = static_cast<LookupBaton *>(req->data);
233235

234-
if (baton->error) {
235-
Local<Value> argv[1] = {
236-
GitError::WrapError(baton->error)
237-
};
238-
239-
TryCatch try_catch;
240-
241-
baton->callback->Call(Context::GetCurrent()->Global(), 1, argv);
242-
243-
if (try_catch.HasCaught()) {
244-
node::FatalException(try_catch);
245-
}
246-
} else {
247-
236+
if (success(baton->error, baton->callback)) {
248237
Local<Object> commit = GitCommit::constructor_template->NewInstance();
249238
GitCommit *commitInstance = ObjectWrap::Unwrap<GitCommit>(commit);
250239
commitInstance->SetValue(baton->rawCommit);
240+
commitInstance->oid = &baton->rawOid;
251241

252242
Handle<Value> argv[2] = {
253243
Local<Value>::New(Null()),
@@ -263,6 +253,56 @@ void GitCommit::LookupAfterWork(uv_work_t *req) {
263253
delete req;
264254
}
265255

256+
Handle<Value> GitCommit::Oid(const Arguments& args) {
257+
HandleScope scope;
258+
259+
Local<Object> oid = GitOid::constructor_template->NewInstance();
260+
GitOid *oidInstance = ObjectWrap::Unwrap<GitOid>(oid);
261+
oidInstance->SetValue(*const_cast<git_oid *>(ObjectWrap::Unwrap<GitCommit>(args.This())->oid));
262+
263+
return scope.Close(oid);
264+
}
265+
266+
Handle<Value> GitCommit::Sha(const Arguments& args) {
267+
HandleScope scope;
268+
269+
if(args.Length() == 0 || !args[0]->IsFunction()) {
270+
return ThrowException(Exception::Error(String::New("Callback is required and must be a Function.")));
271+
}
272+
273+
ShaBaton* baton = new ShaBaton;
274+
baton->request.data = baton;
275+
baton->rawOid = ObjectWrap::Unwrap<GitCommit>(args.This())->oid;
276+
baton->callback = Persistent<Function>::New(Local<Function>::Cast(args[0]));
277+
278+
uv_queue_work(uv_default_loop(), &baton->request, ShaWork, (uv_after_work_cb)ShaAfterWork);
279+
280+
return Undefined();
281+
}
282+
void GitCommit::ShaWork(uv_work_t* req) {
283+
ShaBaton *baton = static_cast<ShaBaton *>(req->data);
284+
285+
baton->sha[GIT_OID_HEXSZ] = '\0';
286+
287+
git_oid_fmt(baton->sha, baton->rawOid);
288+
}
289+
void GitCommit::ShaAfterWork(uv_work_t* req) {
290+
HandleScope scope;
291+
ShaBaton *baton = static_cast<ShaBaton *>(req->data);
292+
293+
Handle<Value> argv[2] = {
294+
Local<Value>::New(Null()),
295+
String::New(baton->sha)
296+
};
297+
298+
TryCatch try_catch;
299+
baton->callback->Call(Context::GetCurrent()->Global(), 2, argv);
300+
if (try_catch.HasCaught()) {
301+
node::FatalException(try_catch);
302+
}
303+
delete req;
304+
}
305+
266306
Handle<Value> GitCommit::Close(const Arguments& args) {
267307
HandleScope scope;
268308

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL