Permalink
Browse files

Finished commit tests, some refactorings

  • Loading branch information...
1 parent 4b3cc95 commit 784e731e3a0a3d9ec0da431018f710c82dfffb17 Christian Amor Kvalheim committed Jul 6, 2010
View
@@ -210,24 +210,33 @@ Commit.diff = function(repo, a, b, paths, callback) {
repo.git.diff.apply(this, paths)
}
+var process_diff = function(repo, diff, callback) {
+ if(diff.match(/diff --git a/)) {
+ diff = diff.substring(diff.match(/diff --git a/).index, diff.length);
+ } else {
+ diff = '';
+ }
+ // Return the diffs
+ Diff.list_from_string(repo, diff, callback);
+}
+
// Show the commits
Commit.prototype.show = function(callback) {
var parents = this.parents;
var diff = null
+ var self = this;
if(parents.length > 1) {
- // TODO TODO TODO TODO TODO TODO TODO TODO
- } else {
- // TODO TODO TODO TODO TODO TODO TODO TODO
- }
-
- if(diff.match(/diff --git a/)) {
- diff = diff.replace(/.+?(diff --git a)/m, '\1');
+ this.repo.git.native_call("diff " + parents[0].id + "..." + parents[1].id, {full_index:true}, function(err, diff) {
+ if(err) return callback(err, diff);
+ process_diff(this.repo, diff, callback);
+ });
} else {
- diff = '';
- }
- // Return the diffs
- callback(null, Diff.list_from_string(this.repo, diff));
+ this.repo.git.show({full_index:true, pretty:'raw'}, this.id, function(err, diff) {
+ if(err) return callback(err, diff);
+ process_diff(this.repo, diff, callback);
+ });
+ }
}
// Return the diffs for a commit
@@ -241,9 +250,15 @@ Commit.prototype.diffs = function(callback) {
}
}
+// To String method
+Commit.prototype.toString = function() {
+ return this.id;
+}
-
-
+// Convert commit into patch
+Commit.prototype.toPatch = function(callback) {
+ this.repo.git.format_patch({'1':true, stdout:true}, this.id, callback);
+}
View
@@ -32,7 +32,7 @@ Diff.list_from_string = function(repo, text, callback) {
var a_path, b_path, a_mode, b_mode, new_file = false, deleted_file = false;
var a_blob, b_blob;
- while(lines.length > 0) {
+ while(text.length > 0 && lines.length > 0) {
// Extract a line
var parts = lines.shift().match(/^diff --git a\/(.+?) b\/(.+)$/);
// Unpack parts
View
@@ -390,6 +390,12 @@ Git.prototype.select_existing_objects = function(object_ids, callback) {
callback(null, existing_object_ids);
}
+// Format the patch
+Git.prototype.format_patch = function(options, reference, callback) {
+ this.call_git('', 'format_patch', '', options, [reference], function(err, result) {
+ callback(err, result);
+ })
+}
View
@@ -2,6 +2,6 @@
NODE = node
test:
- @$(NODE) test/all_tests.js
+ @$(NODE) test/test_all.js
.PHONY: test
View
@@ -1,11 +0,0 @@
-require.paths.unshift("./spec/lib", "./lib", "./external-libs/node-httpclient/lib", "./external-libs/node-xml/lib",
- "./external-libs/node-async-testing", "./test");
-
-var sys = require('sys');
-
-// Run all tests
-// require('git_tests').suite.runTests(function() {});
-// require('head_tests').suite.runTests(function() {});
-// require('diff_tests').suite.runTests(function() {});
-// require('file_index_tests').suite.runTests(function() {});
-require('commit_tests').suite.runTests(function() {});
View
@@ -0,0 +1,11 @@
+require.paths.unshift("./spec/lib", "./lib", "./external-libs/node-httpclient/lib", "./external-libs/node-xml/lib",
+ "./external-libs/node-async-testing", "./test");
+
+var sys = require('sys');
+
+// Run all tests
+require('test_git').suite.runTests(function() {});
+require('test_head').suite.runTests(function() {});
+require('test_diff').suite.runTests(function() {});
+require('test_file_index').suite.runTests(function() {});
+require('test_commit').suite.runTests(function() {});
@@ -175,7 +175,7 @@ suite.addTests({
repo.git.show = function(options, sha, callback) {
assert.deepEqual({full_index:true, pretty:'raw'}, options);
assert.equal('634396b2f541a9f2d58b00be1a07f0c358b999b3', sha);
- callback(null, fixture('diff_i'));
+ callback(null, fixture('diff_i', true));
}
// Fetch the diff
@@ -188,7 +188,7 @@ suite.addTests({
assert.equal(null, diffs[0].a_blob);
assert.equal(null, diffs[0].b_mode);
assert.equal('81d2c27608b352814cbe979a6acd678d30219678', diffs[0].b_blob.id);
- assert.equal(false, diffs[0].new_file);
+ assert.equal(true, diffs[0].new_file);
assert.equal(false, diffs[0].deleted_file);
assert.equal("--- /dev/null\n+++ b/History.txt\n@@ -0,0 +1,5 @@\n+== 1.0.0 / 2007-10-09\n+\n+* 1 major enhancement\n+ * Birthday!\n+", diffs[0].diff);
@@ -198,9 +198,74 @@ suite.addTests({
assert.equal(true, diffs[5].new_file);
finished();
});
- });
-
- }
+ });
+ },
+
+ "Test diffs on initial import with empty commit":function(assert, finished) {
+ new Repo("./test/dot_git", {is_bare:true}, function(err, repo) {
+ repo.git.show = function(options, sha, callback) {
+ assert.deepEqual({full_index:true, pretty:'raw'}, options);
+ assert.equal('634396b2f541a9f2d58b00be1a07f0c358b999b3', sha);
+ callback(null, fixture('show_empty_commit', true));
+ }
+
+ // Fetch the diff
+ var commit = new Commit(repo, '634396b2f541a9f2d58b00be1a07f0c358b999b3');
+ commit.diffs(function(err, diffs) {
+ assert.deepEqual([], diffs);
+ finished();
+ });
+ });
+ },
+
+ "Test diffs with mode only change":function(assert, finished) {
+ new Repo("./test/dot_git", {is_bare:true}, function(err, repo) {
+ repo.git.diff = function(options, sha, callback) {
+ var args = Array.prototype.slice.call(arguments, 0);
+ callback = args.pop();
+ callback(null, fixture('diff_mode_only', true));
+ }
+
+ // Fetch the diff
+ var commit = new Commit(repo, '91169e1f5fa4de2eaea3f176461f5dc784796769');
+ commit.diffs(function(err, diffs) {
+ assert.equal(23, diffs.length);
+ assert.equal('100644', diffs[0].a_mode);
+ assert.equal('100755', diffs[0].b_mode);
+ finished();
+ });
+ });
+ },
+
+ // to String
+ "Test toString() override for the commit":function(assert, finished) {
+ new Repo("./test/dot_git", {is_bare:true}, function(err, repo) {
+ // Fetch the diff
+ var commit = new Commit(repo, 'abc');
+ assert.equal("abc", commit.toString());
+ finished();
+ });
+ },
+
+ // to patch
+ "Test create patch from commit":function(assert, finished) {
+ new Repo("./test/dot_git", {is_bare:true}, function(err, repo) {
+ // Fetch the diff
+ var commit = new Commit(repo, '80f136f500dfdb8c3e8abf4ae716f875f0a1b57f');
+ commit.toPatch(function(err, patch) {
+ // sys.puts(patch)
+ assert.ok(patch.indexOf('From 80f136f500dfdb8c3e8abf4ae716f875f0a1b57f Mon Sep 17 00:00:00 2001') != -1);
+ assert.ok(patch.indexOf('From: tom <tom@taco.(none)>') != -1);
+ assert.ok(patch.indexOf('Date: Tue, 20 Nov 2007 17:27:42 -0800') != -1);
+ assert.ok(patch.indexOf('Subject: [PATCH] fix tests on other machines') != -1);
+ assert.ok(patch.indexOf('test/test_reality.rb | 30 +++++++++++++++---------------') != -1);
+ assert.ok(patch.indexOf('@@ -1,17 +1,17 @@') != -1);
+ assert.ok(patch.indexOf('+# recurse(t)') != -1);
+ assert.ok(patch.indexOf('1.6.') != -1);
+ finished();
+ });
+ });
+ },
});
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 784e731

Please sign in to comment.