Skip to content

Commit 670dabe

Browse files
committed
treebuilder
1 parent f294c43 commit 670dabe

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+2207
-892
lines changed

binding.gyp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
'src/signature.cc',
1919
'src/time.cc',
2020
'src/tree.cc',
21+
'src/tree_builder.cc',
2122
'src/tree_entry.cc',
2223
'src/diff_find_options.cc',
2324
'src/diff_options.cc',

example/new-commit.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
var git = require('../'),
2+
path = require('path');
3+
4+
// This example opens a certain file, `README.md`, at a particular commit,
5+
// and prints the first 10 lines as well as some metadata.
6+
7+
git.Repo.open(path.resolve(__dirname, '../.git'), function(error, repo) {
8+
if (error) throw error;
9+
10+
repo.getCommit('0a70ef1237bc1ea50dbccd395e1a114201b75a68', function(error, commit) {
11+
if (error) throw error;
12+
13+
commit.getTree(function(error, tree) {
14+
if (error) throw error;
15+
16+
var builder = tree.builder(),
17+
buffer = new Buffer("this is a file\n");
18+
19+
builder.insertBlob("/lib/baz.txt", buffer, false)
20+
builder.write(function(error, treeId) {
21+
if (error) throw error;
22+
23+
var author = git.Signature.create("Scott Chacon", "schacon@gmail.com", 123456789, 60);
24+
var committer = git.Signature.create("Scott A Chacon", "scott@github.com", 987654321, 90);
25+
26+
repo.createCommit(null, author, committer, "message", treeId, [commit], function(error, commitId) {
27+
console.log("New Commit:", commitId.sha());
28+
});
29+
});
30+
});
31+
});
32+
});

include/blob.h

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -34,38 +34,6 @@ class GitBlob : public ObjectWrap {
3434
static Handle<Value> Oid(const Arguments& args);
3535
static Handle<Value> Content(const Arguments& args);
3636
static Handle<Value> Size(const Arguments& args);
37-
static Handle<Value> CreateFromFile(const Arguments& args);
38-
static void CreateFromFileWork(uv_work_t* req);
39-
static void CreateFromFileAfterWork(uv_work_t* req);
40-
41-
struct CreateFromFileBaton {
42-
uv_work_t request;
43-
int error_code;
44-
const git_error* error;
45-
git_oid * id;
46-
Persistent<Value> repoReference;
47-
git_repository * repo;
48-
Persistent<Value> pathReference;
49-
const char * path;
50-
Persistent<Function> callback;
51-
};
52-
static Handle<Value> CreateFromBuffer(const Arguments& args);
53-
static void CreateFromBufferWork(uv_work_t* req);
54-
static void CreateFromBufferAfterWork(uv_work_t* req);
55-
56-
struct CreateFromBufferBaton {
57-
uv_work_t request;
58-
int error_code;
59-
const git_error* error;
60-
git_oid * oid;
61-
Persistent<Value> repoReference;
62-
git_repository * repo;
63-
Persistent<Value> bufferReference;
64-
const void * buffer;
65-
Persistent<Value> lenReference;
66-
size_t len;
67-
Persistent<Function> callback;
68-
};
6937
static Handle<Value> IsBinary(const Arguments& args);
7038
git_blob *raw;
7139
};

include/repo.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,38 @@ class GitRepo : public ObjectWrap {
284284
unsigned int list_flags;
285285
Persistent<Function> callback;
286286
};
287+
static Handle<Value> CreateBlobFromBuffer(const Arguments& args);
288+
static void CreateBlobFromBufferWork(uv_work_t* req);
289+
static void CreateBlobFromBufferAfterWork(uv_work_t* req);
290+
291+
struct CreateBlobFromBufferBaton {
292+
uv_work_t request;
293+
int error_code;
294+
const git_error* error;
295+
git_oid * oid;
296+
Persistent<Value> repoReference;
297+
git_repository * repo;
298+
Persistent<Value> bufferReference;
299+
const void * buffer;
300+
Persistent<Value> lenReference;
301+
size_t len;
302+
Persistent<Function> callback;
303+
};
304+
static Handle<Value> CreateBlobFromFile(const Arguments& args);
305+
static void CreateBlobFromFileWork(uv_work_t* req);
306+
static void CreateBlobFromFileAfterWork(uv_work_t* req);
307+
308+
struct CreateBlobFromFileBaton {
309+
uv_work_t request;
310+
int error_code;
311+
const git_error* error;
312+
git_oid * id;
313+
Persistent<Value> repoReference;
314+
git_repository * repo;
315+
Persistent<Value> pathReference;
316+
const char * path;
317+
Persistent<Function> callback;
318+
};
287319
git_repository *raw;
288320
};
289321

include/tree.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ class GitTree : public ObjectWrap {
5151
const char * path;
5252
Persistent<Function> callback;
5353
};
54+
static Handle<Value> Builder(const Arguments& args);
5455
static Handle<Value> DiffTree(const Arguments& args);
5556
static void DiffTreeWork(uv_work_t* req);
5657
static void DiffTreeAfterWork(uv_work_t* req);

include/tree_builder.h

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* This code is auto-generated; unless you know what you're doing, do not modify!
3+
**/
4+
5+
#ifndef GITTREEBUILDER_H
6+
#define GITTREEBUILDER_H
7+
8+
#include <v8.h>
9+
#include <node.h>
10+
#include <string>
11+
12+
#include "git2.h"
13+
14+
using namespace node;
15+
using namespace v8;
16+
17+
class GitTreeBuilder : public ObjectWrap {
18+
public:
19+
20+
static Persistent<Function> constructor_template;
21+
static void Initialize (Handle<v8::Object> target);
22+
23+
git_treebuilder *GetValue();
24+
25+
static Handle<Value> New(void *raw);
26+
27+
private:
28+
GitTreeBuilder(git_treebuilder *raw);
29+
~GitTreeBuilder();
30+
31+
static Handle<Value> New(const Arguments& args);
32+
33+
34+
static Handle<Value> Create(const Arguments& args);
35+
static Handle<Value> Clear(const Arguments& args);
36+
static Handle<Value> Size(const Arguments& args);
37+
static Handle<Value> Get(const Arguments& args);
38+
static Handle<Value> Insert(const Arguments& args);
39+
static Handle<Value> GitTreebuilderRemove(const Arguments& args);
40+
static Handle<Value> Write(const Arguments& args);
41+
static void WriteWork(uv_work_t* req);
42+
static void WriteAfterWork(uv_work_t* req);
43+
44+
struct WriteBaton {
45+
uv_work_t request;
46+
int error_code;
47+
const git_error* error;
48+
git_oid * id;
49+
Persistent<Value> repoReference;
50+
git_repository * repo;
51+
Persistent<Value> bldReference;
52+
git_treebuilder * bld;
53+
Persistent<Function> callback;
54+
};
55+
git_treebuilder *raw;
56+
};
57+
58+
#endif

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ require('./lib/revwalk.js');
2929
require('./lib/tree.js');
3030
require('./lib/diff_list.js');
3131
require('./lib/tree_entry.js');
32+
require('./lib/tree_builder.js');
3233

3334
// Set version
3435
exports.version = require('./package').version;

lib/blob.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var git = require('../'),
2+
TreeEntry = git.TreeEntry,
23
Blob = git.Blob;
34

45
/**
@@ -15,3 +16,10 @@ Blob.prototype.content = function() {
1516
Blob.prototype.toString = function() {
1617
return this.content().toString();
1718
};
19+
20+
/**
21+
* Retrieve the blob's type.
22+
*/
23+
Blob.prototype.filemode = function() {
24+
return this.isBinary() ? TreeEntry.FileMode.Executable : TreeEntry.FileMode.Blob;
25+
};

lib/commit.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ Commit.prototype.history = function() {
9898
};
9999

100100
/**
101-
* Retrieve the commit's parents.
101+
* Retrieve the commit's parents -- as commit objects.
102102
*
103103
* @param {Commit~parentsCallback} callback
104104
*/
@@ -121,6 +121,19 @@ Commit.prototype.getParents = function(callback) {
121121
processParents(this, this.parentCount() - 1, [], callback);
122122
};
123123

124+
/**
125+
* Retrieve the commit's parent shas.
126+
*
127+
* @param {Commit~parentsCallback} callback
128+
*/
129+
Commit.prototype.parents = function() {
130+
var result = [];
131+
for (var i = 0; i < this.parentCount(); i++) {
132+
result.push(this.parentId(i));
133+
}
134+
return result;
135+
}
136+
124137
/**
125138
* Generate an array of diff trees showing changes between this commit
126139
* and its parent(s).
@@ -161,5 +174,4 @@ Commit.prototype.getDiff = function(callback) {
161174

162175
Commit.prototype.toString = function() {
163176
return this.sha();
164-
}
165-
177+
};

lib/repo.js

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
var git = require('../'),
22
util = require('./util.js'),
33
Repo = git.Repo,
4+
Tree = git.Tree,
45
Reference = git.Reference;
56

67
/**
@@ -146,20 +147,52 @@ Repo.prototype.getMaster = function(callback) {
146147

147148
/**
148149
* Create a commit
149-
*
150-
* @param {String|Oid} String sha or Oid
151-
* @param {Blob~lookupCallback} callback
150+
* @param {String} updateRef
151+
* @param {Signature} author
152+
* @param {Signature} commiter
153+
* @param {String} message
154+
* @param {Tree|Oid|String} Tree
155+
* @param {Array} parents
156+
* @param {Function} callback
152157
*/
153158
var oldCreateCommit = Repo.prototype.createCommit;
154159
Repo.prototype.createCommit = function(updateRef, author, committer, message, tree, parents, callback) {
155-
oldCreateCommit.call(
156-
this,
157-
updateRef,
158-
author,
159-
committer,
160-
null /* use default message encoding */,
161-
message,
162-
tree,
163-
parents.length, parents,
164-
callback);
160+
if (tree instanceof Tree) {
161+
oldCreateCommit.call(
162+
this,
163+
updateRef,
164+
author,
165+
committer,
166+
null /* use default message encoding */,
167+
message,
168+
tree,
169+
parents.length, parents,
170+
callback);
171+
} else {
172+
var self = this;
173+
this.getTree(tree, function(error, tree) {
174+
if (error) return callback(error);
175+
oldCreateCommit.call(
176+
self,
177+
updateRef,
178+
author,
179+
committer,
180+
null /* use default message encoding */,
181+
message,
182+
tree,
183+
parents.length, parents,
184+
callback);
185+
});
186+
}
165187
};
188+
189+
/**
190+
* Create a blob from a buffer
191+
*
192+
* @param {Buffer} buffer
193+
* @param {Function} callback
194+
*/
195+
var oldCreateBlobFromBuffer = Repo.prototype.createBlobFromBuffer;
196+
Repo.prototype.createBlobFromBuffer = function(buffer, callback) {
197+
oldCreateBlobFromBuffer.call(this, buffer, buffer.length, callback);
198+
};

0 commit comments

Comments
 (0)