Skip to content

Commit 989733f

Browse files
committed
Added in git objects, updated a ton of oid and repo code, fixed dummyrepo, commits appear to be borked
1 parent ee17c54 commit 989733f

15 files changed

Lines changed: 250 additions & 76 deletions

File tree

‎example/raw-oid.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ console.log( oid.mkstr('1838CCD') );
88

99
// Test formatting
1010
console.log( oid.mkstr('5f2aa9407f7b3aeb531c621c3358953841ccfc98') );
11-
console.log( oid.fmt() );
11+
console.log( oid.toString(4) );

‎example/raw-revwalk.js‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,22 @@ var repo = new git2.Repo();
44

55
// Access existing repository
66
repo.open('./.git', function(err, path) {
7-
console.log(err, path);
87
var revwalk = new git2.RevWalk(repo),
98
oid = new git2.Oid(),
109
error = new git2.Error(),
10+
master = new git2.Ref(repo),
1111
commit = new git2.Commit(repo);
1212

1313
oid.mkstr('5f2aa9407f7b3aeb531c621c3358953841ccfc98')
1414

15-
commit.lookup( repo, oid, function( err, details ) {
16-
if( err ) { console.log(error.strError(err)); return; }
17-
console.log( error.strError( revwalk.push( commit ) ) );
15+
commit.lookup( repo, oid, function( err ) {
16+
console.log( 'Error: '+ error.strError(err) );
1817
});
18+
19+
20+
//repo.lookupRef( master, "refs/heads/master", function( err, ref ) {
21+
// console.log(master.toString(40));
22+
// if( err ) { console.log(error.strError(err)); return; }
23+
// console.log( error.strError( revwalk.push( master.oid() ) ) );
24+
//});
1925
});

‎src/base.cc‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,18 @@ Copyright (c) 2011, Tim Branyen @tbranyen <tim@tabdeveloper.com>
1313
#include "blob.h"
1414
#include "repo.h"
1515
#include "oid.h"
16+
#include "obj.h"
1617
#include "commit.h"
1718
#include "revwalk.h"
1819

19-
using namespace node;
20-
using namespace v8;
21-
22-
extern "C" void init(Handle<Object> target) {
20+
extern "C" void init(Handle<v8::Object> target) {
2321
HandleScope scope;
2422

2523
Reference::Initialize(target);
2624
Error::Initialize(target);
2725
Blob::Initialize(target);
2826
Oid::Initialize(target);
27+
Obj::Initialize(target);
2928
Repo::Initialize(target);
3029
Commit::Initialize(target);
3130
RevWalk::Initialize(target);

‎src/commit.cc‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,12 @@ void Commit::SetValue(git_commit* commit) {
3838
this->commit = commit;
3939
}
4040

41-
int Commit::New(git_repository *repo) {
41+
int Commit::New(git_repository* repo) {
4242
return git_commit_new(&this->commit, repo);
4343
}
4444

45-
int Commit::Lookup(Repo *repo, Oid *oid) {
46-
return git_commit_lookup(&this->commit, repo->GetValue(), oid->GetValue());
45+
int Commit::Lookup(git_repository* repo, git_oid* oid) {
46+
return git_commit_lookup(&this->commit, repo, oid);
4747
}
4848

4949
Handle<Value> Commit::New(const Arguments& args) {
@@ -100,7 +100,7 @@ Handle<Value> Commit::Lookup(const Arguments& args) {
100100
int Commit::EIO_Lookup(eio_req *req) {
101101
lookup_request *ar = static_cast<lookup_request *>(req->data);
102102

103-
ar->err = Persistent<Value>::New(Integer::New(ar->commit->Lookup(ar->repo, ar->oid)));
103+
ar->err = Persistent<Value>::New(Integer::New(ar->commit->Lookup(ar->repo->GetValue(), ar->oid->GetValue())));
104104

105105
return 0;
106106
}

‎src/commit.h‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class Commit : public EventEmitter {
2727
git_commit* GetValue();
2828
void SetValue(git_commit* commit);
2929
// Asynchronous
30-
int Lookup(Repo *repo, Oid *oid);
30+
int Lookup(git_repository* repo, git_oid* oid);
3131

3232
protected:
3333
Commit() {}
@@ -40,14 +40,15 @@ class Commit : public EventEmitter {
4040

4141
private:
4242
git_commit *commit;
43-
};
4443

45-
struct lookup_request {
46-
Commit *commit;
47-
Repo *repo;
48-
Oid *oid;
49-
Persistent<Value> err;
50-
Persistent<Function> callback;
44+
struct lookup_request {
45+
Commit *commit;
46+
Repo *repo;
47+
Oid *oid;
48+
Persistent<Value> err;
49+
Persistent<Function> callback;
50+
};
5151
};
5252

53+
5354
#endif

‎src/obj.cc‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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 "obj.h"
12+
#include "repo.h"
13+
14+
using namespace v8;
15+
using namespace node;
16+
17+
void Obj::Initialize (Handle<v8::Object> target) {
18+
HandleScope scope;
19+
20+
Local<FunctionTemplate> t = FunctionTemplate::New(New);
21+
22+
constructor_template = Persistent<FunctionTemplate>::New(t);
23+
constructor_template->InstanceTemplate()->SetInternalFieldCount(1);
24+
constructor_template->SetClassName(String::NewSymbol("Obj"));
25+
26+
target->Set(String::NewSymbol("Obj"), constructor_template->GetFunction());
27+
}
28+
29+
git_object* Obj::GetValue() {
30+
return this->obj;
31+
}
32+
33+
void Obj::SetValue(git_object* obj) {
34+
this->obj = obj;
35+
}
36+
37+
Handle<Value> Obj::New(const Arguments& args) {
38+
HandleScope scope;
39+
40+
Obj *obj = new Obj();
41+
42+
obj->Wrap(args.This());
43+
44+
return args.This();
45+
}
46+
Persistent<FunctionTemplate> Obj::constructor_template;

‎src/obj.h‎

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

‎src/oid.cc‎

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,30 +37,24 @@ git_oid* Oid::GetValue() {
3737
return &this->oid;
3838
}
3939

40-
void Oid::SetValue(git_oid *oid) {
40+
void Oid::SetValue(git_oid* oid) {
4141
this->oid = *oid;
4242
}
4343

4444
int Oid::Mkstr(const char* id) {
4545
return git_oid_mkstr(&this->oid, id);
4646
}
4747

48-
void Oid::Mkraw(const unsigned char *raw) {
48+
void Oid::Mkraw(const unsigned char* raw) {
4949
git_oid_mkraw(&this->oid, raw);
5050
}
5151

52-
char* Oid::Fmt() {
53-
char buffer[40];
54-
git_oid_fmt(buffer, &this->oid);
55-
56-
return buffer;
52+
char* Oid::Fmt(char* buffer) {
53+
git_oid_fmt(*&buffer, &this->oid);
5754
}
5855

59-
char* Oid::ToString(int len) {
60-
char buffer[len];
61-
git_oid_to_string(*&buffer, sizeof(buffer), (const git_oid*)&this->oid);
62-
63-
return buffer;
56+
char* Oid::ToString(char* buffer, size_t bufferSize) {
57+
git_oid_to_string(*&buffer, bufferSize, &this->oid);
6458
}
6559

6660
Handle<Value> Oid::New(const Arguments& args) {
@@ -106,7 +100,9 @@ Handle<Value> Oid::Fmt(const Arguments& args) {
106100

107101
HandleScope scope;
108102

109-
return String::New(oid->Fmt());
103+
char buffer[40];
104+
oid->Fmt(buffer);
105+
return String::New(buffer);
110106
}
111107

112108
Handle<Value> Oid::ToString(const Arguments& args) {
@@ -118,9 +114,9 @@ Handle<Value> Oid::ToString(const Arguments& args) {
118114
return ThrowException(Exception::Error(String::New("Length argument is required and must be a Number.")));
119115
}
120116

121-
int len = Local<Integer>::Cast(args[0])->Value();
122-
123-
return String::New(oid->ToString(len));
117+
char buffer[Int32::Cast(*args[0])->Value()+1];
118+
oid->ToString(buffer, sizeof(buffer));
119+
return String::New(buffer);
124120
}
125121

126122
Persistent<FunctionTemplate> Oid::constructor_template;

‎src/oid.h‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ class Oid : public ObjectWrap {
2222
git_oid* GetValue();
2323
void SetValue(git_oid* oid);
2424
// Synchronous
25-
int Mkstr(const char *str);
26-
void Mkraw(const unsigned char *raw);
27-
char* Fmt();
25+
int Mkstr(const char* str);
26+
void Mkraw(const unsigned char* raw);
27+
char* Fmt(char* buffer);
2828
//void pathfmt(char *str, const git_oid *oid)
2929
//char* allocfmt(const git_oid *oid)
30-
char* ToString(int len);
30+
char* ToString(char* buffer, size_t bufferSize);
3131
//void cpy(git_oid *out, const git_oid *src)
3232
//int cmp(const git_oid *a, const git_oid *b)
3333
Oid() {}

‎src/repo.cc‎

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

1111
#include "reference.h"
12+
#include "obj.h"
1213
#include "repo.h"
1314
#include "commit.h"
1415

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

2728
NODE_SET_PROTOTYPE_METHOD(constructor_template, "open", Open);
29+
NODE_SET_PROTOTYPE_METHOD(constructor_template, "lookup", Lookup);
2830
NODE_SET_PROTOTYPE_METHOD(constructor_template, "free", Free);
2931
NODE_SET_PROTOTYPE_METHOD(constructor_template, "init", Init);
3032
NODE_SET_PROTOTYPE_METHOD(constructor_template, "lookupRef", LookupRef);
@@ -137,6 +139,85 @@ int Repo::EIO_AfterOpen(eio_req *req) {
137139
return 0;
138140
}
139141

142+
Handle<Value> Repo::Lookup(const Arguments& args) {
143+
Repo *repo = ObjectWrap::Unwrap<Repo>(args.This());
144+
Local<Function> callback;
145+
146+
HandleScope scope;
147+
148+
if(args.Length() == 0 || !args[0]->IsObject()) {
149+
return ThrowException(Exception::Error(String::New("Git object is required and must be a Object.")));
150+
}
151+
152+
if(args.Length() == 1 || !args[1]->IsObject()) {
153+
return ThrowException(Exception::Error(String::New("Repo is required and must be a Object.")));
154+
}
155+
156+
if(args.Length() == 2 || !args[2]->IsObject()) {
157+
return ThrowException(Exception::Error(String::New("Oid is required and must be a Object.")));
158+
}
159+
160+
if(args.Length() == 3 || !args[3]->IsFunction()) {
161+
return ThrowException(Exception::Error(String::New("Callback is required and must be a Function.")));
162+
}
163+
164+
callback = Local<Function>::Cast(args[3]);
165+
166+
lookup_request *ar = new lookup_request();
167+
ar->repo = repo;
168+
ar->callback = Persistent<Function>::New(callback);
169+
170+
repo->Ref();
171+
172+
eio_custom(EIO_LookupRef, EIO_PRI_DEFAULT, EIO_AfterLookupRef, ar);
173+
ev_ref(EV_DEFAULT_UC);
174+
175+
return Undefined();
176+
}
177+
178+
int Repo::EIO_Lookup(eio_req *req) {
179+
lookupref_request *ar = static_cast<lookupref_request *>(req->data);
180+
181+
String::Utf8Value name(ar->name);
182+
git_reference *ref;
183+
184+
int err = ar->repo->LookupRef((git_reference **)ref, *name);
185+
ar->err = Persistent<Value>::New(Integer::New(err));
186+
187+
if(Int32::Cast(*ar->err)->Value() == 0) {
188+
ar->ref->SetValue(*&ref);
189+
}
190+
191+
return 0;
192+
}
193+
194+
int Repo::EIO_AfterLookup(eio_req *req) {
195+
HandleScope scope;
196+
197+
lookupref_request *ar = static_cast<lookupref_request *>(req->data);
198+
ev_unref(EV_DEFAULT_UC);
199+
ar->repo->Unref();
200+
201+
Local<Value> argv[2];
202+
argv[0] = Number::Cast(*ar->err);
203+
argv[1] = String::Cast(*ar->name);
204+
205+
TryCatch try_catch;
206+
207+
ar->callback->Call(Context::GetCurrent()->Global(), 2, argv);
208+
209+
if(try_catch.HasCaught())
210+
FatalException(try_catch);
211+
212+
ar->err.Dispose();
213+
ar->name.Dispose();
214+
ar->callback.Dispose();
215+
216+
delete ar;
217+
218+
return 0;
219+
}
220+
140221
Handle<Value> Repo::Free(const Arguments& args) {
141222
Repo *repo = ObjectWrap::Unwrap<Repo>(args.This());
142223

@@ -280,7 +361,6 @@ int Repo::EIO_AfterLookupRef(eio_req *req) {
280361
Local<Value> argv[2];
281362
argv[0] = Number::Cast(*ar->err);
282363
argv[1] = String::Cast(*ar->name);
283-
//argv[2] = ref->Wrap(*ar->ref);
284364

285365
TryCatch try_catch;
286366

0 commit comments

Comments
 (0)