Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Refactored Blob and Commit constructors

  • Loading branch information...
commit 9b2f3a37d46d47248d2704b6a46ec7e197bcd48c 1 parent 61e134a
@tbranyen tbranyen authored
View
16 .jshintrc
@@ -1,4 +1,18 @@
{
+ "evil": true,
+ "boss": true,
+ "immed": false,
+ "eqnull": true,
+ "maxlen": 80,
"node": true,
- "proto": true
+ "curly": true,
+ "quotmark": "double",
+ "trailing": true,
+ "unused": "vars",
+ "undef": true,
+ "validthis": true,
+ "globals": {
+ "global": true,
+ "define": true
+ }
}
View
9 generate/descriptor.json
@@ -34,6 +34,13 @@
}
},
+ "git_blob_is_binary": {
+ "ignore": false,
+ "args": [
+ { "isReturn": false, "isSelf": true }
+ ]
+ },
+
"git_blob_create_frombuffer": {
"ignore": false,
"isAsync": true,
@@ -671,7 +678,7 @@
"ignore": false,
"isAsync": true,
"isConstructorMethod": true,
- "args": [{ "isReturn": true }, { "isSelf": true }],
+ "args": [{ "isReturn": true }],
"return": {
"isErrorCode": true
}
View
27 lib/blob.js
@@ -1,31 +1,32 @@
-var git = require('../');
-var TreeEntry = git.TreeEntry;
-var Blob = git.Blob;
+var NodeGit = require("../");
+var TreeEntry = require("./tree_entry");
+var Blob = NodeGit.Blob;
-// Assign deprecated methods.
-Object.defineProperty(Blob.prototype, "size", {
- value: Blob.prototype.rawsize,
- enumerable: false
-});
+var FileMode = TreeEntry.FileMode;
/**
- * Retrieve the content of the blob.
- * @return {Buffer} content
+ * Retrieve the content of the Blob.
+ *
+ * @return {Buffer} Contents.
*/
Blob.prototype.content = function() {
return this.rawcontent().toBuffer(this.rawsize());
};
/**
- * Retrieve the blob's content as String.
+ * Retrieve the Blob's content as String.
+ *
+ * @return {string} Contents.
*/
Blob.prototype.toString = function() {
return this.content().toString();
};
/**
- * Retrieve the blob's type.
+ * Retrieve the Blob's type.
+ *
+ * @return {number} The filemode.
*/
Blob.prototype.filemode = function() {
- return this.isBinary() ? TreeEntry.FileMode.Executable : TreeEntry.FileMode.Blob;
+ return this.isBinary() ? FileMode.Executable : FileMode.Blob;
};
View
101 lib/commit.js
@@ -1,35 +1,14 @@
var Promise = require("promise");
-var git = require('../');
+var git = require("../");
var Commit = git.Commit;
-var Oid = git.Oid;
-var Revwalk = git.Revwalk;
-var events = require('events');
-var Tree = require("./tree");
-
-// Backwards compatibility.
-Object.defineProperties(Commit.prototype, {
- "oid": {
- value: Commit.prototype.id,
- enumerable: false
- },
-
- "offset": {
- value: Commit.prototype.timeOffset,
- enumerable: false
- },
-
- "parentCount": {
- value: Commit.prototype.parentcount,
- enumerable: false
- }
-});
+var events = require("events");
/**
* Retrieve the SHA.
* @return {String}
*/
Commit.prototype.sha = function() {
- return this.oid().toString();
+ return this.id().toString();
};
/**
@@ -53,7 +32,7 @@ Commit.prototype.date = function() {
* @return {Tree}
*/
Commit.prototype.getTree = function(callback) {
- this.repo.getTree(this.treeId(), callback);
+ return this.repo.getTree(this.treeId(), callback);
};
/**
@@ -65,18 +44,22 @@ Commit.prototype.getTree = function(callback) {
* @return {TreeEntry}
*/
Commit.prototype.getEntry = function(path, callback) {
- this.getTree(function(error, tree) {
- if (error) return callback(error);
+ return this.getTree().then(function(tree) {
+ return tree.getEntry(path).then(function(entry) {
+ if (callback) {
+ callback(entry);
+ }
- tree.getEntry(path, callback);
- });
+ return entry;
+ });
+ }, callback);
};
/**
* Walk the history from this commit backwards.
- * An EventEmitter is returned that will emit a 'commit' event for each
- * commit in the history, and one 'end' event when the walk is completed.
- * Don't forget to call `start()` on the returned event.
+ * An EventEmitter is returned that will emit a "commit" event for each
+ * commit in the history, and one "end" event when the walk is completed.
+ * Don"t forget to call `start()` on the returned event.
*
* @fires Commit#commit
* @fires Commit#end
@@ -85,7 +68,7 @@ Commit.prototype.getEntry = function(path, callback) {
*/
Commit.prototype.history = function() {
var event = new events.EventEmitter();
- var oid = this.oid();
+ var oid = this.id();
var revwalk = this.repo.createRevWalk();
revwalk.sorting.apply(revwalk, arguments);
@@ -94,14 +77,16 @@ Commit.prototype.history = function() {
event.start = function() {
revwalk.walk(oid, function commitRevWalk(error, commit) {
- if (error) return event.emit('error', error);
+ if (error) {
+ return event.emit("error", error);
+ }
if (!commit) {
- event.emit('end', commits);
+ event.emit("end", commits);
return;
}
- event.emit('commit', commit);
+ event.emit("commit", commit);
commits.push(commit);
});
};
@@ -110,14 +95,13 @@ Commit.prototype.history = function() {
};
/**
- * Retrieve the commit's parents -- as commit objects.
+ * Retrieve the commit"s parents -- as commit objects.
*
* @param {number} limit - Optional amount of parents to return.
* @param {Function} callback
* @return {[Commit]} array of commits
*/
Commit.prototype.getParents = function(limit, callback) {
- var commit = this;
var parents = [];
var i = 0;
@@ -127,7 +111,7 @@ Commit.prototype.getParents = function(limit, callback) {
}
// If no limit was set, default to the maximum parents.
- limit = typeof limit === "number" ? limit : this.parentCount();
+ limit = typeof limit === "number" ? limit : this.parentcount();
function processParents(commit, callback) {
var oid = commit.parentId(i);
@@ -163,14 +147,14 @@ Commit.prototype.getParents = function(limit, callback) {
};
/**
- * Retrieve the commit's parent shas.
+ * Retrieve the commit"s parent shas.
*
* @param {Function} callback
* @return {[Oid]} array of oids
*/
Commit.prototype.parents = function() {
var result = [];
- for (var i = 0; i < this.parentCount(); i++) {
+ for (var i = 0; i < this.parentcount(); i++) {
result.push(this.parentId(i));
}
return result;
@@ -184,30 +168,23 @@ Commit.prototype.parents = function() {
* @return {[Diff]} an array of diffs
*/
Commit.prototype.getDiff = function(callback) {
- var self = this;
- self.getParents(function commitParents(error, parents) {
- if (error) return callback(error);
-
- var parentDiffs = [];
- parents.forEach(function commitEachParent(parent) {
- parent.getTree(function(error, parentTree) {
- if (error) return callback(error);
-
- self.getTree(function(error, thisTree) {
- if (error) return callback(error);
-
- parentTree.diff(thisTree, function walkDiff(error, diff) {
- if (error) return callback(error);
-
- parentDiffs.push(diff);
- if (parentDiffs.length === parents.length) {
- callback(null, parentDiffs);
- }
- });
+ var commit = this;
+
+ return commit.getParents().then(function(parents) {
+ return parents.map(function(parent) {
+ return parent.getTree(function(parentTree) {
+ return commit.getTree(function(thisTree) {
+ return parentTree.diff(thisTree);
});
});
});
- });
+ }).then(function(parentDiffs) {
+ if (callback) {
+ callback(null, parentDiffs);
+ }
+
+ return parentDiffs;
+ }, callback);
};
/**
View
54 lib/repository.js
@@ -16,13 +16,6 @@ Object.defineProperty(git, "Repo", {
enumerable: false
});
-var oldGetReference = Reference.lookup,
- oldGetTree = Tree.lookup,
- oldGetTag = Repo.prototype.getTag,
- oldCreateRevwalk = Revwalk.createRevwalk,
- oldCreateCommit = Commit.createCommit,
- oldCreateBlobFromBuffer = Blob.createFrombuffer;
-
/**
* Look up a branch's most recent commit.
*
@@ -128,15 +121,21 @@ Repo.prototype.getBlob = function(oid, callback) {
* @return {Tree}
*/
Repo.prototype.getTree = function(oid, callback) {
- var self = this;
- oldGetTree.call(this, oid, function(error, tree) {
- if (error) return callback(error);
- tree.repo = self;
- callback(null, tree);
- });
+ oid = normalizeOid(oid);
+
+ var repository = this;
+
+
+ return Tree.lookup(repository, oid).then(function(tree) {
+ tree.repo = repository;
+
+ if (callback) {
+ callback(null, tree);
+ }
+
+ return tree;
+ }, callback);
};
-util.normalizeOid(Repo.prototype, 'getTree');
-util.makeSafe(Repo.prototype, 'getTree');
/**
* Retrieve the tag represented by the oid.
@@ -146,11 +145,20 @@ util.makeSafe(Repo.prototype, 'getTree');
* @return {Tag}
*/
Repo.prototype.getTag = function(oid, callback) {
- var self = this;
- oldGetTag.call(this, oid, callback);
+ oid = normalizeOid(oid);
+
+ var repository = this;
+
+ return Reference.lookup(repository, oid).then(function(reference) {
+ reference.repo = repository;
+
+ if (callback) {
+ callback(null, reference);
+ }
+
+ return reference;
+ }, callback);
};
-util.normalizeOid(Repo.prototype, 'getTag');
-util.makeSafe(Repo.prototype, 'getTag');
/**
* Instantiate a new revision walker for browsing the Repo's history.
@@ -161,7 +169,7 @@ util.makeSafe(Repo.prototype, 'getTag');
* @return {RevWalk}
*/
Repo.prototype.createRevWalk = function() {
- var revWalk = oldCreateRevwalk(this);
+ var revWalk = Revwalk.createRevwalk(this);
revWalk.repo = this;
return revWalk;
};
@@ -190,7 +198,7 @@ Repo.prototype.getMaster = function(callback) {
*/
Repo.prototype.createCommit = function(updateRef, author, committer, message, tree, parents, callback) {
if (tree instanceof Tree) {
- oldCreateCommit.call(
+ Commit.createCommit.call(
this,
updateRef,
author,
@@ -204,7 +212,7 @@ Repo.prototype.createCommit = function(updateRef, author, committer, message, tr
var self = this;
this.getTree(tree, function(error, tree) {
if (error) return callback(error);
- oldCreateCommit.call(
+ Commit.createCommit.call(
self,
updateRef,
author,
@@ -226,7 +234,7 @@ Repo.prototype.createCommit = function(updateRef, author, committer, message, tr
* @return {Blob}
*/
Repo.prototype.createBlobFromBuffer = function(buffer, callback) {
- oldCreateBlobFromBuffer.call(this, buffer, buffer.length, callback);
+ Blob.createFrombuffer.call(this, buffer, buffer.length, callback);
};
/**
View
17 lib/tree.js
@@ -11,7 +11,6 @@ var oldBuilder = TreeBuilder.create;
var oldEntryByIndex = Tree.prototype.entryByindex;
var oldEntryByName = Tree.prototype.entryByname;
var oldGetEntry = Tree.prototype.entryBypath;
-var oldTreeToTree = Diff.treeToTree;
// Backwards compatibility.
Object.defineProperties(Tree.prototype, {
@@ -28,7 +27,7 @@ Object.defineProperties(Tree.prototype, {
* @return {DiffList}
*/
Tree.prototype.diff = function(tree, callback) {
- oldTreeToTree.call(this.repo, tree, this, null, callback);
+ return Diff.treeToTree.call(this.repo, tree, this, null, callback);
};
/**
@@ -99,7 +98,7 @@ Tree.prototype.entries = function() {
* @return {EventEmitter}
*/
Tree.prototype.walk = function(blobsOnly) {
- if (typeof blobsOnly == 'undefined') blobsOnly = true;
+ blobsOnly = typeof blobsOnly === "boolean" ? blobsOnly : true;
var self = this,
event = new events.EventEmitter(),
@@ -112,10 +111,12 @@ Tree.prototype.walk = function(blobsOnly) {
// the recursive call to `entry.getTree(bfs)`
function bfs(error, tree) {
total--;
- if (error) return errors.push(error);
+
+ if (error) {
+ return event.emit('error', error);
+ }
tree.entries().forEach(function (entry) {
- entry.__proto__ = TreeEntry.prototype;
if (!blobsOnly || entry.isFile()) {
event.emit('entry', entry);
entries.push(entry);
@@ -126,8 +127,10 @@ Tree.prototype.walk = function(blobsOnly) {
entry.getTree(bfs);
}
});
- if (total === 0)
- event.emit('end', errors.length ? errors : null, entries);
+
+ if (total === 0) {
+ event.emit('end', entries);
+ }
}
event.start = function() {
View
6 lib/tree_entry.js
@@ -1,6 +1,6 @@
-var git = require('../'),
- TreeEntry = git.TreeEntry,
- path = require('path');
+var NodeGit = require('../');
+var TreeEntry = NodeGit.TreeEntry;
+var path = require('path');
// Backwards compatibility.
Object.defineProperty(TreeEntry.prototype, "name", {
View
27 test/commit.js
@@ -1,27 +1,4 @@
/**
- * Test that retrieving and walking a commit's tree works as expected.
- */
-exports.tree = function(test) {
- test.expect(2);
- git.Repo.open('repos/workdir/.git', function(error, repository) {
- repository.getCommit(historyCountKnownSHA, function(error, commit) {
- test.equals(error, null, 'Getting latest branch commit should not error');
-
- var commitTreeEntryCount = 0;
- var expectedCommitTreeEntryCount = 198;
- commit.getTree(function(error, tree) {
- tree.walk().on('entry', function(entry) {
- commitTreeEntryCount++;
- }).on('end', function(error, entries) {
- test.equals(commitTreeEntryCount, expectedCommitTreeEntryCount, 'Commit tree entry count does not match expected');
- test.done();
- }).start();
- });
- });
- });
-};
-
-/**
* Test that getDiff works as expected.
*/
exports.getDiff = function(test) {
@@ -35,7 +12,3 @@ exports.getDiff = function(test) {
});
});
};
-
-process.on('uncaughtException', function(err) {
- console.log(err.stack);
-});
View
31 test/tests/blob.js
@@ -1,27 +1,42 @@
var assert = require("assert");
var path = require("path");
-var nodegit = require("../../");
+var NodeGit = require("../../");
describe("Blob", function() {
var reposPath = path.resolve("test/repos/workdir/.git");
+ var oid = "111dd657329797f6165f52f5085f61ac976dcf04";
- var Oid = nodegit.Oid;
- var Repository = nodegit.Repository;
+ var Oid = NodeGit.Oid;
+ var Repository = NodeGit.Repository;
+ var FileMode = NodeGit.TreeEntry.FileMode;
before(function() {
var test = this;
return Repository.open(reposPath).then(function(repository) {
test.repository = repository;
+
+ return repository.getBlob(oid).then(function(blob) {
+ test.blob = blob;
+ });
});
});
- it("can fetch content from a commit", function() {
- var oid= Oid.fromstr("111dd657329797f6165f52f5085f61ac976dcf04");
+ it("can provide content as a buffer", function() {
+ var contents = this.blob.content();
- return this.repository.getBlob(oid).then(function(blob) {
- assert.equal(blob.toString().slice(0, 7), "@import");
- });
+ assert.ok(Buffer.isBuffer(contents));
+ });
+
+ it("can provide content as a string", function() {
+ var contents = this.blob.toString();
+
+ assert.equal(typeof contents, "string");
+ assert.equal(contents.slice(0, 7), "@import");
+ });
+
+ it("can determine if a blob is not a binary", function() {
+ assert.equal(this.blob.filemode(), FileMode.Blob);
});
});
View
48 test/tests/commit.js
@@ -3,12 +3,12 @@ var rimraf = require("rimraf");
var path = require("path");
var fs = require( "fs" );
-var nodegit = require("../../");
-var Repository = nodegit.Repository;
+var NodeGit = require("../../");
+var Repository = NodeGit.Repository;
describe("Commit", function() {
var reposPath = path.resolve("test/repos/workdir/.git");
- var historyCountKnownSHA = "fce88902e66c72b5b93e75bdb5ae717038b221f6";
+ var oid = "fce88902e66c72b5b93e75bdb5ae717038b221f6";
var Commit = require("./commit");
@@ -18,7 +18,7 @@ describe("Commit", function() {
return Repository.open(reposPath).then(function(repository) {
test.repository = repository;
- return repository.getCommit(historyCountKnownSHA).then(function(commit) {
+ return repository.getCommit(oid).then(function(commit) {
test.commit = commit;
});
});
@@ -35,7 +35,7 @@ describe("Commit", function() {
});
it("has a sha", function() {
- assert.equal(this.commit.sha(), historyCountKnownSHA);
+ assert.equal(this.commit.sha(), oid);
});
it("has a time", function() {
@@ -46,8 +46,8 @@ describe("Commit", function() {
assert.equal(this.commit.date().getTime(), 1362012884000);
});
- it("has an offset", function() {
- assert.equal(this.commit.offset(), 780);
+ it("has a time offset", function() {
+ assert.equal(this.commit.timeOffset(), 780);
});
describe("author", function() {
@@ -56,7 +56,7 @@ describe("Commit", function() {
});
it("is available", function() {
- assert.ok(this.author instanceof nodegit.Signature);
+ assert.ok(this.author instanceof NodeGit.Signature);
});
it("has a name", function() {
@@ -74,7 +74,7 @@ describe("Commit", function() {
});
it("is available", function() {
- assert.ok(this.author instanceof nodegit.Signature);
+ assert.ok(this.author instanceof NodeGit.Signature);
});
it("has a name", function() {
@@ -132,4 +132,34 @@ describe("Commit", function() {
assert.equal(parents.length, 0);
});
});
+
+ it("can retrieve and walk a commit tree", function(done) {
+ var commitTreeEntryCount = 0;
+ var expectedCommitTreeEntryCount = 198;
+
+ this.commit.getTree().then(function(tree) {
+ var walk = tree.walk();
+
+ walk.on("entry", function(entry) {
+ commitTreeEntryCount++;
+ });
+
+ walk.on("error", function() {
+ assert.ok(false);
+ });
+
+ walk.on("end", function(error, entries) {
+ assert.equal(commitTreeEntryCount, expectedCommitTreeEntryCount);
+ done();
+ });
+
+ walk.start();
+ });
+ });
+
+ it("can get the commit diff", function() {
+ return this.commit.getDiff().then(function(diff) {
+ assert.equal(diff.length, 1);
+ });
+ });
});
Please sign in to comment.
Something went wrong with that request. Please try again.