Skip to content

Commit b7fb470

Browse files
committed
readme updated with new error returns. error class added along with tests, and the convenience api was updated.
1 parent 3db8b12 commit b7fb470

16 files changed

Lines changed: 216 additions & 16 deletions

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,14 @@ __ Reading a repository and commit data __
5959

6060
// Read the current repository
6161
git.repo( '.git', function( err, path, repo ) {
62+
// If success will return 0, if an error message throw it as an error string.
63+
if( !err ) throw err;
64+
6265
// Read a commit
6366
this.commit( '5f2aa9407f7b3aeb531c621c3358953841ccfc98', function( err, details, commit ) {
67+
// If success will return 0, if an error message throw it as an error string.
68+
if( !err ) throw err;
69+
6470
console.log( 'Message', details.message );
6571
console.log( 'Author name', details.author.name );
6672
console.log( 'Author email', details.author.email );
@@ -79,6 +85,9 @@ __ Accomplishing the same thing as above __
7985
var repo = new git.Repo();
8086
// Read the current repository
8187
repo.open( '.git', function( err, path ) {
88+
// If success will return 0, if an error message throw it as an error string.
89+
if( !err ) throw err;
90+
8291
// Create object id and set hash
8392
var oid = new git.Oid();
8493
oid.mkstr( '5f2aa9407f7b3aeb531c621c3358953841ccfc98' );
@@ -88,6 +97,9 @@ __ Accomplishing the same thing as above __
8897

8998
// Lookup commit
9099
commit.lookup( repo, oid, function( err, details ) {
100+
// If success will return 0, if an error message throw it as an error string.
101+
if( !err ) throw err;
102+
91103
console.log( 'Message', details.message );
92104
console.log( 'Author name', details.author.name );
93105
console.log( 'Author email', details.author.email );

example/raw-error.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
var git2 = require( '../lib' ).git2;
2+
3+
var error = new git2.Error();
4+
// Valid
5+
console.log( error.strError(0) );
6+
// Invalid
7+
console.log( error.strError(-2) );

example/raw-oid.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var git2 = require('../build/default/git2');
1+
var git2 = require( 'nodegit2' ).git2;
22

33
var oid = new git2.Oid();
44
// Valid
@@ -7,4 +7,5 @@ console.log( oid.mkstr('1810DFF58D8A660512D4832E740F692884338CCD') );
77
console.log( oid.mkstr('1838CCD') );
88

99
// Test formatting
10+
console.log( oid.mkstr('5f2aa9407f7b3aeb531c621c3358953841ccfc98') );
1011
console.log( oid.fmt() );

lib/error.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 Error = function( error ) {
4+
var self = {};
5+
6+
// Internal reference to a Git reference
7+
self.error = error || new git2.Error();
8+
9+
return self;
10+
};
11+
12+
exports.error = Error;

lib/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
var repo = require( './repo.js' ).repo;
1+
var repo = require( './repo.js' ).repo,
2+
error = require( './error.js' ).error;
23
//commit = require( 'commit.js' );
34

45
exports.git2 = require( '../build/default/git2' );
56
exports.repo = repo;
7+
exports.error = error;
68
//exports.commit = commit.commit;

lib/repo.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
1-
/*global require: true, jQuery: false */
21
var git = require( 'nodegit2' );
32

43
var Repo = function( path, callback ) {
54
// Public namespace
65
var self = {};
76

87
// Private internal use variables
9-
var _commits = [];
8+
var _commits = [],
9+
_error = git.error();
10+
11+
function error( err ) {
12+
if(err !== 0) {
13+
return _error.error.strError( err );
14+
}
15+
16+
return 0;
17+
}
18+
1019

1120
// Internal reference to a Git repository
1221
self.repo = new git.git2.Repo();
@@ -32,6 +41,8 @@ var Repo = function( path, callback ) {
3241

3342
commit.lookup( self.repo, oid, function() {
3443
var args = Array.prototype.slice.call( arguments );
44+
args[0] = error( args[0] );
45+
3546
callback && callback.apply( commit, (args.push( commit ), args) );
3647
});
3748
};
@@ -40,13 +51,17 @@ var Repo = function( path, callback ) {
4051
var ref = new git.git2.Ref();
4152
self.repo.lookupRef( ref, name, function() {
4253
var args = Array.prototype.slice.call( arguments );
54+
args[0] = error( args[0] );
55+
4356
callback && callback.apply( ref, (args.push( git.ref(ref) ), args) );
4457
});
4558
};
4659

4760
self.init = function( path, is_bare, callback ) {
4861
self.repo.init( path, is_bare, function() {
4962
var args = Array.prototype.slice.call( arguments );
63+
args[0] = error( args[0] );
64+
5065
callback && callback.apply( self, (args.push( self ), args) );
5166
});
5267

@@ -62,6 +77,8 @@ var Repo = function( path, callback ) {
6277
if( path && callback ) {
6378
self.repo.open( path, function() {
6479
var args = Array.prototype.slice.call( arguments );
80+
args[0] = error( args[0] );
81+
6582
callback && callback.apply( self, (args.push( self ), args) );
6683
});
6784
}

src/index.cc renamed to src/base.cc

Lines changed: 2 additions & 0 deletions
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 "error.h"
1213
#include "repo.h"
1314
#include "oid.h"
1415
#include "commit.h"
@@ -21,6 +22,7 @@ extern "C" void init(Handle<Object> target) {
2122
HandleScope scope;
2223

2324
Reference::Initialize(target);
25+
Error::Initialize(target);
2426
Oid::Initialize(target);
2527
Repo::Initialize(target);
2628
Commit::Initialize(target);

src/error.cc

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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 "error.h"
12+
13+
using namespace v8;
14+
using namespace node;
15+
16+
void Error::Initialize (Handle<v8::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("Error"));
24+
25+
NODE_SET_PROTOTYPE_METHOD(constructor_template, "strError", StrError);
26+
27+
target->Set(String::NewSymbol("Error"), constructor_template->GetFunction());
28+
}
29+
30+
Handle<Value> Error::New(const Arguments& args) {
31+
HandleScope scope;
32+
33+
Error *error = new Error();
34+
error->Wrap(args.This());
35+
36+
return args.This();
37+
}
38+
39+
Handle<Value> Error::StrError(const Arguments& args) {
40+
HandleScope scope;
41+
42+
if(args.Length() == 0 || !args[0]->IsNumber()) {
43+
return ThrowException(Exception::Error(String::New("Error is required and must be a Number.")));
44+
}
45+
Local<Integer> err = Local<Integer>::Cast(args[0]);
46+
47+
return String::New(git_strerror(err->Value()));
48+
}
49+
Persistent<FunctionTemplate> Error::constructor_template;

src/error.h

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

src/oid.cc

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,25 +28,24 @@ void Oid::Initialize(Handle<Object> target) {
2828
}
2929

3030
git_oid* Oid::GetValue() {
31-
return this->oid;
31+
return &this->oid;
3232
}
3333

3434
void Oid::SetValue(git_oid *oid) {
35-
this->oid = oid;
35+
this->oid = *oid;
3636
}
3737

3838
int Oid::Mkstr(const char* id) {
39-
return git_oid_mkstr(this->oid, id);
39+
return git_oid_mkstr(&this->oid, id);
4040
}
4141

4242
void Oid::Mkraw(const unsigned char *raw) {
43-
git_oid_mkraw(this->oid, raw);
43+
git_oid_mkraw(&this->oid, raw);
4444
}
4545

4646
char* Oid::Fmt() {
47-
char raw;
48-
git_oid_fmt(&raw, (const git_oid *)this->oid);
49-
47+
char* raw;
48+
git_oid_fmt(raw, &this->oid);
5049
return raw;
5150
}
5251

0 commit comments

Comments
 (0)