Skip to content

Commit 91631d6

Browse files
committed
Added in refs, fixed up readme, updated lib files, etc.
1 parent 6a0e9dc commit 91631d6

12 files changed

Lines changed: 226 additions & 17 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ Release information
120120
### v0.0.1: ###
121121
* 1:1 mapping of libgit2 read methods
122122
* An API that can be easily extended with convenience methods in JS
123+
* An API that offers a familiar clean syntax that will make adoption and use much more likely
123124

124125
### v0.0.2: ###
125126
* Write capabilities

example/convenience-repo.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@ var git = require('nodegit2');
22

33
// Read the current repository
44
git.repo( '.git', function( err, path, repo ) {
5-
// Read a commit
6-
this.commit( '5f2aa9407f7b3aeb531c621c3358953841ccfc98', function( err, details, commit ) {
7-
console.log( 'Message', details.message );
8-
console.log( 'Author\'s name', details.author.name );
9-
console.log( 'Author\'s email', details.author.email );
10-
});
5+
//
6+
this.find( 'HEAD', function( err, name, ref ) {
7+
// console.log( ref );
8+
});
9+
10+
// Read a commit
11+
this.commit( '5f2aa9407f7b3aeb531c621c3358953841ccfc98', function( err, details, commit ) {
12+
console.log( 'Message', details.message.trim() );
13+
console.log( 'Author\'s name', details.author.name );
14+
console.log( 'Author\'s email', details.author.email );
15+
});
1116
});

lib/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
var repo = require( './repo.js' );
1+
var repo = require( './repo.js' ).repo;
22
//commit = require( 'commit.js' );
33

44
exports.git2 = require( '../build/default/git2' );
5-
exports.repo = repo.repo;
5+
exports.repo = repo;
66
//exports.commit = commit.commit;

lib/ref.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
var git2 = require('../build/default/git2');
2+
3+
var Ref = function( ref ) {
4+
var self = {};
5+
6+
// Internal reference to a Git reference
7+
self.ref = ref || new git2.Ref();
8+
9+
return self;
10+
};
11+
12+
exports.ref = Ref;

lib/repo.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
var git2 = require('../build/default/git2');
1+
var git = require( 'nodegit2' );
22

33
var Repo = function( path, callback ) {
44
var self = {};
55

66
// Internal reference to a Git repository
7-
self.repo = new git2.Repo();
7+
self.repo = new git.git2.Repo();
88

99
self.commit = function( sha, callback ) {
10-
var oid = new git2.Oid(),
11-
commit = new git2.Commit();
10+
var oid = new git.git2.Oid(),
11+
commit = new git.git2.Commit();
1212

1313
oid.mkstr( sha );
1414

@@ -18,6 +18,14 @@ var Repo = function( path, callback ) {
1818
});
1919
};
2020

21+
self.find = function( name ) {
22+
var ref = new git.git2.Ref();
23+
self.repo.lookup_ref( ref, name, function( ref ) {
24+
var args = Array.prototype.slice.call( arguments );
25+
callback && callback.apply( ref, (args.push( git.ref(ref) ), args) );
26+
});
27+
};
28+
2129
self.init = function( path, is_bare, callback ) {
2230
self.repo.init( path, is_bare, function() {
2331
var args = Array.prototype.slice.call( arguments );
@@ -27,7 +35,10 @@ var Repo = function( path, callback ) {
2735
return self;
2836
};
2937

30-
self.free = function() { };
38+
self.free = function() {
39+
self.repo.free();
40+
delete self.repo;
41+
};
3142

3243
// Constructor use
3344
if( path && callback ) {

src/commit.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,9 @@ Handle<Value> Commit::Lookup(const Arguments& args) {
6464
return ThrowException(Exception::Error(String::New("Callback is required and must be a Function.")));
6565
}
6666

67-
lookup_request *ar = new lookup_request();
6867
callback = Local<Function>::Cast(args[2]);
68+
69+
lookup_request *ar = new lookup_request();
6970
ar->commit = commit;
7071
ar->repo = ObjectWrap::Unwrap<Repo>(args[0]->ToObject());
7172
ar->oid = ObjectWrap::Unwrap<Oid>(args[1]->ToObject());

src/index.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Copyright (c) 2011, Tim Branyen @tbranyen <tim@tabdeveloper.com>
88

99
#include <git2.h>
1010

11+
#include "ref.h"
1112
#include "repo.h"
1213
#include "oid.h"
1314
#include "commit.h"
@@ -18,7 +19,8 @@ using namespace v8;
1819
extern "C" void init(Handle<Object> target) {
1920
HandleScope scope;
2021

21-
Repo::Initialize(target);
22+
Ref::Initialize(target);
2223
Oid::Initialize(target);
24+
Repo::Initialize(target);
2325
Commit::Initialize(target);
2426
}

src/ref.cc

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
Copyright (c) 2011, Tim Branyen @tbranyen <tim@tabdeveloper.com>
3+
*/
4+
5+
#include <v8.h>
6+
#include <node.h>
7+
#include <node_events.h>
8+
9+
#include <git2.h>
10+
11+
#include "ref.h"
12+
13+
using namespace v8;
14+
using namespace node;
15+
16+
void Ref::Initialize(Handle<Object> target) {
17+
HandleScope scope;
18+
19+
Local<FunctionTemplate> t = FunctionTemplate::New(New);
20+
21+
constructor_template = Persistent<FunctionTemplate>::New(t);
22+
constructor_template->InstanceTemplate()->SetInternalFieldCount(1);
23+
constructor_template->SetClassName(String::NewSymbol("Ref"));
24+
25+
target->Set(String::NewSymbol("Ref"), constructor_template->GetFunction());
26+
}
27+
28+
git_reference* Ref::GetValue() {
29+
return this->ref;
30+
}
31+
32+
void Ref::SetValue(git_reference *ref) {
33+
this->ref = ref;
34+
}
35+
36+
Handle<Value> Ref::New(const Arguments& args) {
37+
HandleScope scope;
38+
39+
Ref *ref = new Ref();
40+
ref->Wrap(args.This());
41+
42+
return args.This();
43+
}
44+
45+
Persistent<FunctionTemplate> Ref::constructor_template;

src/ref.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
Copyright (c) 2011, Tim Branyen @tbranyen <tim@tabdeveloper.com>
3+
*/
4+
5+
#ifndef REF_H
6+
#define REF_H
7+
8+
#include <v8.h>
9+
#include <node.h>
10+
#include <node_events.h>
11+
12+
#include <git2.h>
13+
14+
using namespace node;
15+
using namespace v8;
16+
17+
class Ref : public EventEmitter {
18+
public:
19+
static Persistent<FunctionTemplate> constructor_template;
20+
static void Initialize (Handle<v8::Object> target);
21+
git_reference* GetValue();
22+
void SetValue(git_reference* ref);
23+
24+
protected:
25+
Ref() {}
26+
~Ref() {}
27+
static Handle<Value> New(const Arguments& args);
28+
29+
private:
30+
git_reference *ref;
31+
};
32+
33+
#endif

src/repo.cc

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ Copyright (c) 2011, Tim Branyen @tbranyen <tim@tabdeveloper.com>
99
#include <git2.h>
1010

1111
#include "repo.h"
12+
#include "ref.h"
13+
#include "commit.h"
1214

1315
using namespace v8;
1416
using namespace node;
@@ -25,6 +27,7 @@ void Repo::Initialize(Handle<Object> target) {
2527
NODE_SET_PROTOTYPE_METHOD(constructor_template, "open", Open);
2628
NODE_SET_PROTOTYPE_METHOD(constructor_template, "free", Free);
2729
NODE_SET_PROTOTYPE_METHOD(constructor_template, "init", Init);
30+
NODE_SET_PROTOTYPE_METHOD(constructor_template, "lookup_ref", LookupRef);
2831

2932
target->Set(String::NewSymbol("Repo"), constructor_template->GetFunction());
3033
}
@@ -52,6 +55,12 @@ int Repo::Init(const char* path, bool is_bare) {
5255
return err;
5356
}
5457

58+
int Repo::LookupRef(git_reference* ref, const char* name) {
59+
int err = git_repository_lookup_ref(&ref, this->repo, name);
60+
61+
return err;
62+
}
63+
5564
Handle<Value> Repo::New(const Arguments& args) {
5665
HandleScope scope;
5766

@@ -209,4 +218,80 @@ int Repo::EIO_AfterInit(eio_req *req) {
209218
return 0;
210219
}
211220

221+
Handle<Value> Repo::LookupRef(const Arguments& args) {
222+
Repo *repo = ObjectWrap::Unwrap<Repo>(args.This());
223+
Local<Function> callback;
224+
225+
HandleScope scope;
226+
227+
if(args.Length() == 0 || !args[0]->IsObject()) {
228+
return ThrowException(Exception::Error(String::New("Ref is required and must be an Object.")));
229+
}
230+
231+
if(args.Length() == 1 || !args[1]->IsString()) {
232+
return ThrowException(Exception::Error(String::New("Name is required and must be a String.")));
233+
}
234+
235+
if(args.Length() == 2 || !args[2]->IsFunction()) {
236+
return ThrowException(Exception::Error(String::New("Callback is required and must be a Function.")));
237+
}
238+
239+
callback = Local<Function>::Cast(args[2]);
240+
241+
lookupref_request *ar = new lookupref_request();
242+
ar->repo = repo;
243+
//git_reference* ref;
244+
//ar->ref = ObjectWrap::Unwrap<Ref>(args[0]->ToObject());
245+
ar->name = Persistent<Value>::New(args[1]);
246+
ar->callback = Persistent<Function>::New(callback);
247+
248+
repo->Ref();
249+
250+
eio_custom(EIO_LookupRef, EIO_PRI_DEFAULT, EIO_AfterLookupRef, ar);
251+
ev_ref(EV_DEFAULT_UC);
252+
253+
return Undefined();
254+
}
255+
256+
int Repo::EIO_LookupRef(eio_req *req) {
257+
lookupref_request *ar = static_cast<lookupref_request *>(req->data);
258+
259+
String::Utf8Value name(ar->name);
260+
ar->err = Persistent<Value>::New(Integer::New(ar->repo->LookupRef(ar->ref->GetValue(), *name)));
261+
262+
//if(Int32::Cast(*ar->err)->Value() == 0) {
263+
// //ar->ref->SetValue(ref);
264+
//}
265+
266+
return 0;
267+
}
268+
269+
int Repo::EIO_AfterLookupRef(eio_req *req) {
270+
HandleScope scope;
271+
272+
lookupref_request *ar = static_cast<lookupref_request *>(req->data);
273+
ev_unref(EV_DEFAULT_UC);
274+
ar->repo->Unref();
275+
276+
Local<Value> argv[2];
277+
argv[0] = Number::Cast(*ar->err);
278+
argv[1] = String::Cast(*ar->name);
279+
//argv[2] = ref->Wrap(*ar->ref);
280+
281+
TryCatch try_catch;
282+
283+
ar->callback->Call(Context::GetCurrent()->Global(), 2, argv);
284+
285+
if(try_catch.HasCaught())
286+
FatalException(try_catch);
287+
288+
ar->err.Dispose();
289+
ar->name.Dispose();
290+
ar->callback.Dispose();
291+
292+
delete ar;
293+
294+
return 0;
295+
}
296+
212297
Persistent<FunctionTemplate> Repo::constructor_template;

0 commit comments

Comments
 (0)