Skip to content

Commit 42c7fa7

Browse files
committed
Blob is now code-gen'd
1 parent 458e080 commit 42c7fa7

File tree

21 files changed

+673
-519
lines changed

21 files changed

+673
-519
lines changed

binding.gyp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
'src/tree_entry.cc',
1717
'src/diff_list.cc',
1818
'src/threads.cc',
19+
'src/wrapper.cc',
1920
'src/functions/string.cc',
2021
'src/functions/utilities.cc'
2122
],

gen.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ var idefs = JSON.parse(fs.readFileSync('v0.18.0.json')),
88

99
for (var i in idefs) {
1010
var idef = idefs[i];
11-
if (idef.jsClassName != "Oid") continue;
12-
13-
fs.writeFileSync(path.resolve("./include/" + idef.filename), headerTemplate(idef));
14-
fs.writeFileSync(path.resolve("./src/" + path.basename(idef.filename, '.h') + '.cc'), classTemplate(idef));
11+
if (["Oid", "Blob"].indexOf(idef.jsClassName) > -1) {
12+
fs.writeFileSync(
13+
path.resolve("./include/" + idef.filename), headerTemplate(idef));
14+
fs.writeFileSync(
15+
path.resolve("./src/" + path.basename(idef.filename, '.h') + '.cc'), classTemplate(idef));
16+
}
1517
}

include/blob.h

Lines changed: 37 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,112 +1,75 @@
11
/**
2-
* Copyright (c) 2011, Tim Branyen @tbranyen <tim@tabdeveloper.com>
3-
* @author Michael Robinson @codeofinterest <mike@pagesofinterest.net>
4-
*
5-
* Dual licensed under the MIT and GPL licenses.
6-
*/
2+
* This code is auto-generated; unless you know what you're doing, do not modify!
3+
**/
74

8-
#ifndef BLOB_H
9-
#define BLOB_H
5+
#ifndef GITBLOB_H
6+
#define GITBLOB_H
107

118
#include <v8.h>
129
#include <node.h>
13-
#include <string.h>
10+
#include <string>
1411

1512
#include "git2.h"
1613

17-
#include "repo.h"
18-
#include "oid.h"
19-
20-
using namespace v8;
2114
using namespace node;
15+
using namespace v8;
2216

23-
/**
24-
* Wrapper for libgit2 git_blob.
25-
*/
2617
class GitBlob : public ObjectWrap {
2718
public:
2819

2920
static Persistent<Function> constructor_template;
21+
static void Initialize (Handle<v8::Object> target);
3022

31-
static void Initialize(Handle<Object> target);
32-
33-
git_blob* GetValue();
34-
void SetValue(git_blob* blob);
23+
git_blob *GetValue();
3524

36-
protected:
37-
GitBlob() {};
38-
~GitBlob() {};
25+
private:
26+
GitBlob(git_blob *raw);
27+
~GitBlob();
3928

4029
static Handle<Value> New(const Arguments& args);
41-
static Handle<Value> Free(const Arguments& args);
4230

4331
static Handle<Value> Lookup(const Arguments& args);
4432
static void LookupWork(uv_work_t* req);
4533
static void LookupAfterWork(uv_work_t* req);
4634

47-
static Handle<Value> RawContent(const Arguments& args);
48-
static void RawContentWork(uv_work_t* req);
49-
static void RawContentAfterWork(uv_work_t* req);
50-
35+
struct LookupBaton {
36+
uv_work_t request;
37+
const git_error* error;
38+
git_blob *out;
39+
git_repository * repo;
40+
const git_oid * id;
41+
Persistent<Function> callback;
42+
};
43+
static Handle<Value> Oid(const Arguments& args);
44+
static Handle<Value> Content(const Arguments& args);
45+
static Handle<Value> Size(const Arguments& args);
5146
static Handle<Value> CreateFromFile(const Arguments& args);
5247
static void CreateFromFileWork(uv_work_t* req);
5348
static void CreateFromFileAfterWork(uv_work_t* req);
5449

50+
struct CreateFromFileBaton {
51+
uv_work_t request;
52+
const git_error* error;
53+
git_oid * id;
54+
git_repository * repo;
55+
const char * path;
56+
Persistent<Function> callback;
57+
};
5558
static Handle<Value> CreateFromBuffer(const Arguments& args);
5659
static void CreateFromBufferWork(uv_work_t* req);
5760
static void CreateFromBufferAfterWork(uv_work_t* req);
5861

59-
private:
60-
61-
git_blob* blob;
62-
63-
struct LookupBaton {
62+
struct CreateFromBufferBaton {
6463
uv_work_t request;
6564
const git_error* error;
66-
67-
GitBlob* blob;
68-
git_blob* rawBlob;
69-
git_repository* rawRepo;
70-
git_oid rawOid;
71-
65+
git_oid * oid;
66+
git_repository * repo;
67+
const void * buffer;
68+
size_t len;
7269
Persistent<Function> callback;
7370
};
74-
75-
struct RawContentBaton {
76-
uv_work_t request;
77-
78-
GitBlob* blob;
79-
git_blob* rawBlob;
80-
std::string rawContent;
81-
int rawSize;
82-
83-
Persistent<Function> callback;
84-
};
85-
86-
struct CreateFromFileBaton {
87-
uv_work_t request;
88-
const git_error* error;
89-
90-
GitBlob* blob;
91-
git_blob* rawBlob;
92-
git_repository* rawRepo;
93-
std::string path;
94-
95-
Persistent<Function> callback;
96-
};
97-
98-
struct CreateFromBufferBaton {
99-
uv_work_t request;
100-
const git_error* error;
101-
102-
GitBlob* blob;
103-
git_blob* rawBlob;
104-
git_repository* rawRepo;
105-
const void* data;
106-
size_t dataLength;
107-
108-
Persistent<Function> callback;
109-
};
71+
static Handle<Value> IsBinary(const Arguments& args);
72+
git_blob *raw;
11073
};
11174

11275
#endif

include/oid.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class GitOid : public ObjectWrap {
2020
static Persistent<Function> constructor_template;
2121
static void Initialize (Handle<v8::Object> target);
2222

23-
git_oid GetValue();
23+
git_oid *GetValue();
2424

2525
private:
2626
GitOid(git_oid *raw);

include/wrapper.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* This code is auto-generated; unless you know what you're doing, do not modify!
3+
**/
4+
5+
#ifndef WRAPPER_H
6+
#define WRAPPER_H
7+
8+
#include <v8.h>
9+
#include <node.h>
10+
11+
using namespace node;
12+
using namespace v8;
13+
14+
class Wrapper : public ObjectWrap {
15+
public:
16+
17+
static Persistent<Function> constructor_template;
18+
static void Initialize (Handle<v8::Object> target);
19+
20+
void *GetValue();
21+
22+
private:
23+
Wrapper(void *raw);
24+
25+
static Handle<Value> New(const Arguments& args);
26+
static Handle<Value> ToBuffer(const Arguments& args);
27+
28+
void *raw;
29+
};
30+
31+
#endif

lib/blob.js

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -45,38 +45,17 @@ Blob.prototype.lookup = function(oid, callback) {
4545

4646
/**
4747
* Retrieve the blob's raw content buffer.
48-
*
49-
* @param {Blob~rawContentCallback} callback
5048
*/
51-
Blob.prototype.rawContent = function(callback) {
52-
/**
53-
* @callback Blob~rawContentCallback Callback executed after raw content is retrieved.
54-
* @param {GitError|null} error An Error or null if successful.
55-
* @param {Buffer|null} content The raw content of the blob or null.
56-
*/
57-
this.rawBlob.rawContent(function(error, content) {
58-
if (success(error, callback)) {
59-
callback(null, content);
60-
}
61-
});
49+
Blob.prototype.rawContent = function() {
50+
return this.rawBlob.content().toBuffer(this.rawBlob.size());
6251
};
6352

6453
/**
6554
* Retrieve the blob's content.
66-
*
67-
* @param {Blob~contentCallback} callback
6855
*/
6956
Blob.prototype.content = function(callback) {
70-
/**
71-
* @callback Blob~contentCallback Callback executed after content is retrieved.
72-
* @param {GitError|null} error An Error or null if successful.
73-
* @param {String|null} content The content of the blob or null.
74-
*/
75-
this.rawContent(function(error, content) {
76-
if (success(error, callback)) {
77-
callback(null, content.toString());
78-
}
79-
});
57+
var content = this.rawContent();
58+
return content.toString();
8059
};
8160

8261
/**

lib/tree_entry.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -208,11 +208,8 @@ TreeEntry.prototype.content = function(callback) {
208208
if (!success(error, callback)) {
209209
return;
210210
}
211-
blob.content(function blobContent(error, content) {
212-
if (success(error, callback)) {
213-
callback(null, content);
214-
}
215-
});
211+
var content = blob.content();
212+
callback(null, content);
216213
});
217214
};
218215

src/base.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#include "git2.h"
1212

13+
#include "../include/wrapper.h"
1314
#include "../include/reference.h"
1415
#include "../include/signature.h"
1516
#include "../include/error.h"
@@ -24,6 +25,10 @@
2425
#include "../include/threads.h"
2526

2627
extern "C" void init(Handle<v8::Object> target) {
28+
HandleScope scope;
29+
30+
Wrapper::Initialize(target);
31+
2732
GitError::Initialize(target);
2833

2934
GitReference::Initialize(target);

0 commit comments

Comments
 (0)