Skip to content

Commit 2406411

Browse files
committed
Finally stable from libgit2 update
1 parent b410b1e commit 2406411

7 files changed

Lines changed: 97 additions & 147 deletions

File tree

src/reference.cc

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Copyright (c) 2011, Tim Branyen @tbranyen <tim@tabdeveloper.com>
55
#include <v8.h>
66
#include <node.h>
77
#include <node_events.h>
8+
#include <string>
89

910
#include "../vendor/libgit2/include/git2.h"
1011

@@ -25,6 +26,7 @@ void Reference::Initialize(Handle<Object> target) {
2526
constructor_template->SetClassName(String::NewSymbol("Ref"));
2627

2728
NODE_SET_PROTOTYPE_METHOD(constructor_template, "oid", _Oid);
29+
NODE_SET_PROTOTYPE_METHOD(constructor_template, "lookup", Lookup);
2830

2931
target->Set(String::NewSymbol("Ref"), constructor_template->GetFunction());
3032
}
@@ -37,6 +39,10 @@ void Reference::SetValue(git_reference *ref) {
3739
this->ref = ref;
3840
}
3941

42+
int Reference::Lookup(git_repository* repo, const char* name) {
43+
return git_reference_lookup(&this->ref, repo, name);
44+
}
45+
4046
const git_oid* Reference::_Oid() {
4147
return git_reference_oid(*&this->ref);
4248
}
@@ -51,6 +57,77 @@ Handle<Value> Reference::New(const Arguments& args) {
5157
return args.This();
5258
}
5359

60+
Handle<Value> Reference::Lookup(const Arguments& args) {
61+
Reference *ref = ObjectWrap::Unwrap<Reference>(args.This());
62+
Local<Function> callback;
63+
64+
HandleScope scope;
65+
66+
if(args.Length() == 0 || !args[0]->IsObject()) {
67+
return ThrowException(Exception::Error(String::New("Repo is required and must be a Object.")));
68+
}
69+
70+
if(args.Length() == 1 || !args[1]->IsString()) {
71+
return ThrowException(Exception::Error(String::New("Name is required and must be a String.")));
72+
}
73+
74+
if(args.Length() == 2 || !args[2]->IsFunction()) {
75+
return ThrowException(Exception::Error(String::New("Callback is required and must be a Function.")));
76+
}
77+
78+
callback = Local<Function>::Cast(args[2]);
79+
80+
lookup_request *ar = new lookup_request();
81+
ar->ref = ref;
82+
ar->repo = ObjectWrap::Unwrap<Repo>(args[0]->ToObject());
83+
84+
String::Utf8Value name(args[1]);
85+
ar->name = *name;
86+
87+
ar->callback = Persistent<Function>::New(callback);
88+
89+
ref->Ref();
90+
91+
eio_custom(EIO_Lookup, EIO_PRI_DEFAULT, EIO_AfterLookup, ar);
92+
ev_ref(EV_DEFAULT_UC);
93+
94+
return Undefined();
95+
}
96+
97+
int Reference::EIO_Lookup(eio_req *req) {
98+
lookup_request *ar = static_cast<lookup_request *>(req->data);
99+
100+
git_repository* repo = ar->repo->GetValue();
101+
102+
ar->err = ar->ref->Lookup(repo, ar->name.c_str());
103+
104+
return 0;
105+
}
106+
107+
int Reference::EIO_AfterLookup(eio_req *req) {
108+
HandleScope scope;
109+
110+
lookup_request *ar = static_cast<lookup_request *>(req->data);
111+
ev_unref(EV_DEFAULT_UC);
112+
ar->ref->Unref();
113+
114+
Local<Value> argv[1];
115+
argv[0] = Integer::New(ar->err);
116+
117+
TryCatch try_catch;
118+
119+
ar->callback->Call(Context::GetCurrent()->Global(), 1, argv);
120+
121+
if(try_catch.HasCaught())
122+
FatalException(try_catch);
123+
124+
ar->callback.Dispose();
125+
126+
delete ar;
127+
128+
return 0;
129+
}
130+
54131
Handle<Value> Reference::_Oid(const Arguments& args) {
55132
Reference *ref = ObjectWrap::Unwrap<Reference>(args.This());
56133
HandleScope scope;

src/reference.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ Copyright (c) 2011, Tim Branyen @tbranyen <tim@tabdeveloper.com>
88
#include <v8.h>
99
#include <node.h>
1010
#include <node_events.h>
11+
#include <string>
1112

1213
#include "../vendor/libgit2/include/git2.h"
1314

15+
#include "repo.h"
1416
#include "oid.h"
1517

1618
using namespace node;
@@ -22,17 +24,30 @@ class Reference : public EventEmitter {
2224
static void Initialize(Handle<v8::Object> target);
2325
git_reference* GetValue();
2426
void SetValue(git_reference* ref);
27+
int Lookup(git_repository* repo, const char* name);
2528
const git_oid* _Oid();
2629

2730
protected:
2831
Reference() {}
2932
~Reference() {}
3033
static Handle<Value> New(const Arguments& args);
3134

35+
static Handle<Value> Lookup(const Arguments& args);
36+
static int EIO_Lookup(eio_req* req);
37+
static int EIO_AfterLookup(eio_req* req);
38+
3239
static Handle<Value> _Oid(const Arguments& args);
3340

3441
private:
3542
git_reference *ref;
43+
44+
struct lookup_request {
45+
Reference* ref;
46+
Repo* repo;
47+
int err;
48+
std::string name;
49+
Persistent<Function> callback;
50+
};
3651
};
3752

3853
#endif

src/repo.cc

Lines changed: 2 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ Copyright (c) 2011, Tim Branyen @tbranyen <tim@tabdeveloper.com>
99

1010
#include "../vendor/libgit2/include/git2.h"
1111

12-
#include "reference.h"
1312
#include "object.h"
1413
#include "repo.h"
1514
#include "commit.h"
@@ -30,7 +29,6 @@ void Repo::Initialize(Handle<Object> target) {
3029
NODE_SET_PROTOTYPE_METHOD(constructor_template, "lookup", Lookup);
3130
NODE_SET_PROTOTYPE_METHOD(constructor_template, "free", Free);
3231
NODE_SET_PROTOTYPE_METHOD(constructor_template, "init", Init);
33-
NODE_SET_PROTOTYPE_METHOD(constructor_template, "lookupRef", LookupRef);
3432

3533
target->Set(String::NewSymbol("Repo"), constructor_template->GetFunction());
3634
}
@@ -62,10 +60,6 @@ int Repo::Init(const char* path, bool is_bare) {
6260
return err;
6361
}
6462

65-
int Repo::LookupRef(git_reference** ref, const char* name) {
66-
return git_repository_lookup_ref(ref, *&this->repo, name);
67-
}
68-
6963
Handle<Value> Repo::New(const Arguments& args) {
7064
HandleScope scope;
7165

@@ -169,8 +163,8 @@ Handle<Value> Repo::Lookup(const Arguments& args) {
169163

170164
repo->Ref();
171165

172-
eio_custom(EIO_LookupRef, EIO_PRI_DEFAULT, EIO_AfterLookupRef, ar);
173-
ev_ref(EV_DEFAULT_UC);
166+
//eio_custom(EIO_LookupRef, EIO_PRI_DEFAULT, EIO_AfterLookupRef, ar);
167+
//ev_ref(EV_DEFAULT_UC);
174168

175169
return Undefined();
176170
}
@@ -295,81 +289,4 @@ int Repo::EIO_AfterInit(eio_req *req) {
295289

296290
return 0;
297291
}
298-
299-
Handle<Value> Repo::LookupRef(const Arguments& args) {
300-
Repo *repo = ObjectWrap::Unwrap<Repo>(args.This());
301-
Local<Function> callback;
302-
303-
HandleScope scope;
304-
305-
if(args.Length() == 0 || !args[0]->IsObject()) {
306-
return ThrowException(Exception::Error(String::New("Reference is required and must be a Object.")));
307-
}
308-
309-
if(args.Length() == 1 || !args[1]->IsString()) {
310-
return ThrowException(Exception::Error(String::New("Name is required and must be a String.")));
311-
}
312-
313-
if(args.Length() == 2 || !args[2]->IsFunction()) {
314-
return ThrowException(Exception::Error(String::New("Callback is required and must be a Function.")));
315-
}
316-
317-
callback = Local<Function>::Cast(args[2]);
318-
319-
lookupref_request *ar = new lookupref_request();
320-
ar->repo = repo;
321-
ar->ref = ObjectWrap::Unwrap<Reference>(args[0]->ToObject());
322-
323-
String::Utf8Value name(args[1]);
324-
ar->name = *name;
325-
326-
ar->callback = Persistent<Function>::New(callback);
327-
328-
repo->Ref();
329-
330-
eio_custom(EIO_LookupRef, EIO_PRI_DEFAULT, EIO_AfterLookupRef, ar);
331-
ev_ref(EV_DEFAULT_UC);
332-
333-
return Undefined();
334-
}
335-
336-
int Repo::EIO_LookupRef(eio_req *req) {
337-
lookupref_request *ar = static_cast<lookupref_request *>(req->data);
338-
339-
git_reference* ref = ar->ref->GetValue();
340-
git_reference** out = &ref;
341-
342-
ar->err = ar->repo->LookupRef(out, ar->name.c_str());
343-
344-
if(ar->err == 0) {
345-
ar->ref->SetValue(*out);
346-
}
347-
348-
return 0;
349-
}
350-
351-
int Repo::EIO_AfterLookupRef(eio_req *req) {
352-
HandleScope scope;
353-
354-
lookupref_request *ar = static_cast<lookupref_request *>(req->data);
355-
ev_unref(EV_DEFAULT_UC);
356-
ar->repo->Unref();
357-
358-
Local<Value> argv[1];
359-
argv[0] = Integer::New(ar->err);
360-
361-
TryCatch try_catch;
362-
363-
ar->callback->Call(Context::GetCurrent()->Global(), 1, argv);
364-
365-
if(try_catch.HasCaught())
366-
FatalException(try_catch);
367-
368-
ar->callback.Dispose();
369-
370-
delete ar;
371-
372-
return 0;
373-
}
374-
375292
Persistent<FunctionTemplate> Repo::constructor_template;

src/repo.h

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ Copyright (c) 2011, Tim Branyen @tbranyen <tim@tabdeveloper.com>
1212

1313
#include "../vendor/libgit2/include/git2.h"
1414

15-
#include "reference.h"
1615
#include "object.h"
1716

1817
using namespace node;
@@ -27,7 +26,6 @@ class Repo : public EventEmitter {
2726
// Asynchronous
2827
int Open(const char* path);
2928
int Init(const char* path, bool is_bare);
30-
int LookupRef(git_reference** ref, const char* name);
3129
// Synchronous
3230
void Free();
3331

@@ -58,10 +56,6 @@ class Repo : public EventEmitter {
5856
static int EIO_Init(eio_req* req);
5957
static int EIO_AfterInit(eio_req* req);
6058

61-
static Handle<Value> LookupRef(const Arguments& args);
62-
static int EIO_LookupRef(eio_req* req);
63-
static int EIO_AfterLookupRef(eio_req* req);
64-
6559
private:
6660
git_repository* repo;
6761

@@ -84,14 +78,6 @@ class Repo : public EventEmitter {
8478
bool is_bare;
8579
Persistent<Function> callback;
8680
};
87-
88-
struct lookupref_request {
89-
Repo* repo;
90-
Reference* ref;
91-
int err;
92-
std::string name;
93-
Persistent<Function> callback;
94-
};
9581
};
9682

9783
#endif

test/convenience-repo.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ exports.constructor = function( test ){
3737

3838
// Test invalid repository
3939
git.repo( '/etc/hosts', function( err, path ) {
40-
test.equals( 'The specified repository is invalid', err, 'Invalid repository error code' );
40+
test.equals( 'Object does not exist in the scope searched.', err, 'Invalid repository error code' );
4141

4242
// Test valid repository
4343
git.repo( '../.git', function( err, path ) {

test/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ reporter.run(
3838
'raw-oid.js',
3939
'raw-commit.js',
4040
'raw-error.js',
41+
'raw-ref.js',
4142

4243
// TODO:
4344
//'raw-revwalk.js',

test/raw-repo.js

Lines changed: 1 addition & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ exports.open = function( test ) {
5858

5959
// Test invalid repository
6060
testRepo.open( '/etc/hosts', function( err ) {
61-
test.equals( -8, err, 'Invalid repository error code' );
61+
test.equals( -3, err, 'Invalid repository error code' );
6262

6363
// Test valid repository
6464
testRepo.open( path.resolve( '../.git' ), function( err ) {
@@ -127,49 +127,3 @@ exports.init = function( test ) {
127127
});
128128
});
129129
};
130-
131-
// Repo::LookupRef
132-
exports.lookupRef = function( test ) {
133-
var testRepo = new git.Repo(),
134-
master = new git.Ref(testRepo);
135-
136-
test.expect( 5 );
137-
138-
// Test for function
139-
helper.testFunction( test.equals, testRepo.lookupRef, 'Repo::LookupRef' );
140-
141-
// Test ref argument existence
142-
helper.testException( test.ok, function() {
143-
testRepo.lookupRef();
144-
}, 'Throw an exception if no reference' );
145-
146-
// Test name argument existence
147-
helper.testException( test.ok, function() {
148-
testRepo.lookupRef( master );
149-
}, 'Throw an exception if no name' );
150-
151-
// Test callback argument existence
152-
helper.testException( test.ok, function() {
153-
testRepo.lookupRef( master, 'refs/heads/master' );
154-
}, 'Throw an exception if no callback' );
155-
156-
// Cleanup, remove test repo directory - if it exists
157-
rimraf( './test.git', function() {
158-
// // Create bare repo and test for creation
159-
// testRepo.init( './test.git', true, function( err, path, is_bare ) {
160-
// test.equals( 0, err, 'Successfully created bare repository' );
161-
// // Verify repo exists
162-
// testRepo.open( './test.git', function(err, path) {
163-
// test.equals( 0, err, 'Valid repository created' );
164-
// test.equals( true, is_bare, 'Returns valid is_bare value' );
165-
166-
// testRepo.free();
167-
168-
// // Cleanup, remove test repo directory
169-
// rimraf( './test.git', function() {
170-
test.done();
171-
// });
172-
// });
173-
// });
174-
});
175-
};

0 commit comments

Comments
 (0)