Nicer diff API · nodegit/nodegit@4bf472d · GitHub
Skip to content

Commit 4bf472d

Browse files
committed
Nicer diff API
1 parent ec03074 commit 4bf472d

7 files changed

Lines changed: 132 additions & 56 deletions

File tree

TODO

Lines changed: 4 additions & 4 deletions

lib/commit.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ Commit.prototype.getParents = function(callback) {
127127
*
128128
* @param {Commit~parentsDiffTreesCallback} callback
129129
*/
130-
Commit.prototype.diff = function(callback) {
130+
Commit.prototype.getDiff = function(callback) {
131131
/**
132132
* @callback Commit~parentsDiffTreesCallback Callback executed on diff trees retrieval.
133133
* @param {GitError|null} error An Error or null if successful.

lib/convenient_hunk.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function ConvenientHunk(raw, i) {
2+
this.raw = raw;
3+
this.i = i;
4+
}
5+
6+
ConvenientHunk.prototype.size = function() {
7+
return this.raw.hunk(this.i).lines;
8+
};
9+
10+
ConvenientHunk.prototype.lines = function() {
11+
var result = [];
12+
for (var i = 0; i < this.size(); i++)
13+
result.push(this.raw.line(this.i, i));
14+
return result;
15+
};
16+
17+
exports.ConvenientHunk = ConvenientHunk;

lib/convenient_patch.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
var git = require('../'),
2+
DiffList = git.DiffList,
3+
ConvenientHunk = require('./convenient_hunk').ConvenientHunk;
4+
5+
function ConvenientPatch(raw) {
6+
this.raw = raw;
7+
}
8+
9+
ConvenientPatch.prototype.oldFile = function() {
10+
return this.raw.delta.oldFile();
11+
};
12+
13+
ConvenientPatch.prototype.newFile = function() {
14+
return this.raw.delta.newFile();
15+
};
16+
17+
ConvenientPatch.prototype.size = function() {
18+
return this.raw.patch.size();
19+
};
20+
21+
ConvenientPatch.prototype.status = function() {
22+
return this.raw.delta.status();
23+
};
24+
25+
ConvenientPatch.prototype.isUnmodified = function() {
26+
return this.status() == DiffList.Delta.Unmodified;
27+
};
28+
29+
ConvenientPatch.prototype.isAdded = function() {
30+
return this.status() == DiffList.Delta.Added;
31+
};
32+
33+
ConvenientPatch.prototype.isDeleted = function() {
34+
return this.status() == DiffList.Delta.Deleted;
35+
};
36+
37+
ConvenientPatch.prototype.isModified = function() {
38+
return this.status() == DiffList.Delta.Modified;
39+
};
40+
41+
ConvenientPatch.prototype.isRenamed = function() {
42+
return this.status() == DiffList.Delta.Renamed;
43+
};
44+
45+
ConvenientPatch.prototype.isCopied = function() {
46+
return this.status() == DiffList.Delta.Copied;
47+
};
48+
49+
ConvenientPatch.prototype.isIgnored = function() {
50+
return this.status() == DiffList.Delta.Ignored;
51+
};
52+
53+
ConvenientPatch.prototype.isUntracked = function() {
54+
return this.status() == DiffList.Delta.Untracked;
55+
};
56+
57+
ConvenientPatch.prototype.isTypeChange = function() {
58+
return this.status() == DiffList.Delta.TypeChange;
59+
};
60+
61+
ConvenientPatch.prototype.hunks = function() {
62+
var result = [];
63+
for (var i = 0; i < this.size(); i++)
64+
result.push(new ConvenientHunk(this.raw.patch, i));
65+
return result;
66+
};
67+
68+
exports.ConvenientPatch = ConvenientPatch;

lib/diff_list.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
var git = require('../'),
22
DiffList = git.DiffList,
3+
ConvenientPatch = require('./convenient_patch').ConvenientPatch,
34
events = require('events');
45

6+
console.log(ConvenientPatch);
7+
58
/**
69
* Refer to vendor/libgit2/include/git2/diff.h for delta type definitions.
710
*
@@ -37,7 +40,6 @@ DiffList.LineOrigin = {
3740
/** 'B' */ Binary: 102
3841
};
3942

40-
4143
/**
4244
* Retrieve patches
4345
*
@@ -47,7 +49,7 @@ DiffList.prototype.patches = function() {
4749
var size = this.size();
4850
result = [];
4951
for (var i = 0; i < size; i++) {
50-
result.push(this.patch(i));
52+
result.push(new ConvenientPatch(this.patch(i)));
5153
}
5254
return result;
5355
};

test/commit.js

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,65 +5,59 @@ var git = require('../'),
55
var historyCountKnownSHA = 'fce88902e66c72b5b93e75bdb5ae717038b221f6';
66

77
exports.message = function(test) {
8-
test.expect(3);
8+
test.expect(2);
99
git.Repo.open('../.git', function(error, repository) {
1010
repository.getCommit(historyCountKnownSHA, function(error, commit) {
1111
var message = commit.message();
1212
test.equals(error, null, 'There should be no error');
13-
test.notEqual(message, null, 'Message should not be null');
1413
test.equals(message, 'Update README.md', 'Message should match expected value');
1514
test.done();
1615
});
1716
});
1817
};
1918

2019
exports.sha = function(test) {
21-
test.expect(3);
20+
test.expect(2);
2221
git.Repo.open('../.git', function(error, repository) {
2322
repository.getCommit(historyCountKnownSHA, function(error, commit) {
2423
var sha = commit.sha();
2524
test.equals(error, null, 'There should be no error');
26-
test.notEqual(sha, null, 'SHA should not be null');
2725
test.equals(sha, historyCountKnownSHA, 'SHA should match expected value');
2826
test.done();
2927
});
3028
});
3129
};
3230

3331
exports.time = function(test) {
34-
test.expect(3);
32+
test.expect(2);
3533
git.Repo.open('../.git', function(error, repository) {
3634
repository.getCommit(historyCountKnownSHA, function(error, commit) {
3735
var time = commit.timeMs();
3836
test.equals(error, null, 'There should be no error');
39-
test.notEqual(time, null, 'Time should not be null');
4037
test.equals(time, 1362012884000, 'Time should match expected value');
4138
test.done();
4239
});
4340
});
4441
};
4542

4643
exports.date = function(test) {
47-
test.expect(4);
44+
test.expect(2);
4845
git.Repo.open('../.git', function(error, repository) {
4946
repository.getCommit(historyCountKnownSHA, function(error, commit) {
5047
var date = commit.date();
5148
test.equals(error, null, 'There should be no error');
52-
test.notEqual(date, null, 'Date should not be null');
53-
test.equal(date instanceof Date, true, 'Date should be a date object');
5449
test.equals(date.getTime(), 1362012884000, 'Date should match expected value');
5550
test.done();
5651
});
5752
});
5853
};
5954

6055
exports.offset = function(test) {
61-
test.expect(3);
56+
test.expect(2);
6257
git.Repo.open('../.git', function(error, repository) {
6358
repository.getCommit(historyCountKnownSHA, function(error, commit) {
6459
var offset = commit.offset();
6560
test.equals(error, null, 'There should be no error');
66-
test.notEqual(offset, null, 'Offset should not be null');
6761
test.equals(offset, 780, 'Offset should match expected value');
6862
test.done();
6963
});
@@ -226,14 +220,14 @@ exports.tree = function(test) {
226220
};
227221

228222
/**
229-
* Test that parentsDiffTrees works as expected.
223+
* Test that getDiff works as expected.
230224
*/
231-
exports.parentsDiffTrees = function(test) {
225+
exports.getDiff = function(test) {
232226
test.expect(1);
233227
git.Repo.open('../.git', function(error, repository) {
234228
repository.getCommit(historyCountKnownSHA, function(error, commit) {
235-
commit.diff(function(error, parentsDiffTrees) {
236-
test.equals(parentsDiffTrees.length, 1, 'Should be one item in parents diff trees');
229+
commit.getDiff(function(error, diff) {
230+
test.equals(diff.length, 1, 'Should be one item in parents diff trees');
237231
test.done();
238232
});
239233
});

test/difflist.js

Lines changed: 29 additions & 34 deletions

0 commit comments

Comments
 (0)