Skip to content

Commit 4bf472d

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

File tree

7 files changed

+132
-56
lines changed

7 files changed

+132
-56
lines changed

TODO

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
- make normalizeOid do more work.
2+
- extract out more partials
3+
14
- audit all ** methods so that all finds go through the repo in js land so that the .repo pointer is set.
25
- rename files remove .h
3-
- Cleanup diff api
4-
- free everything malloc'd
6+
- free everything malloc'd, or figure out the right coding conventions for copying.
57
- codegen documentation
6-
- extract out more partials
78
- audit return types for things that should be copied
89
- array handling
9-
- make normalizeOid do more work.

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
Original file line numberDiff line numberDiff line change
@@ -13,40 +13,35 @@ exports.walkingDiffs = function(test) {
1313
test.expect(16);
1414
git.Repo.open('../.git', function(error, repository) {
1515
repository.getCommit(historyCountKnownSHA, function(error, commit) {
16-
commit.getParents(function(error, parents) {
17-
parents[0].getTree(function(error, parentTree) {
18-
commit.getTree(function(error, commitTree) {
19-
parentTree.diff(commitTree, function(error, diffList) {
20-
test.equal(null, error, 'Should not error');
21-
22-
diffList.patches().forEach(function(patch) {
23-
var delta = patch.delta, patch = patch.patch;
24-
test.equal(null, error, 'Should not error');
25-
test.equal(delta.oldFile().path(), 'README.md', 'Old file path should match expected');
26-
test.equal(delta.newFile().path(), 'README.md', 'New file path should match expected');
27-
test.equal(patch.size(), 1, 'Content array should be of known length');
28-
var hunk = patch.hunk(0);
29-
30-
test.equal(hunk.lines, 5, 'Content array should be of known length');
31-
test.equal(delta.status(), git.DiffList.Delta.Modified, 'Status should be known type');
32-
33-
test.equal(patch.line(0, 0).lineOrigin, git.DiffList.LineOrigin.Context, 'First content item should be context');
34-
test.equal(patch.line(0, 1).lineOrigin, git.DiffList.LineOrigin.Context, 'Second content item should be context');
35-
test.equal(patch.line(0, 2).lineOrigin, git.DiffList.LineOrigin.Context, 'Third content item should be context');
36-
37-
var oldContent = '__Before submitting a pull request, please ensure both unit tests and lint checks pass.__\n';
38-
test.equal(patch.line(0, 3).content, oldContent, 'Old content should match known value');
39-
test.equal(patch.line(0, 3).lineOrigin, git.DiffList.LineOrigin.Deletion, 'Fourth content item should be deletion');
40-
test.equal(patch.line(0, 3).length, 90, 'Fourth content length should match known value');
41-
42-
var newContent = '__Before submitting a pull request, please ensure both that you\'ve added unit tests to cover your shiny new code, and that all unit tests and lint checks pass.__\n';
43-
test.equal(patch.line(0, 4).content, newContent, 'New content should match known value');
44-
test.equal(patch.line(0, 4).lineOrigin, git.DiffList.LineOrigin.Addition, 'Fifth content item should be addition');
45-
test.equal(patch.line(0, 4).length, 162, 'Fifth content length should match known value');
46-
test.done();
47-
});
48-
});
49-
});
16+
commit.getDiff(function(error, diffList) {
17+
test.equal(null, error, 'Should not error');
18+
19+
diffList[0].patches().forEach(function(patch) {
20+
test.equal(null, error, 'Should not error');
21+
22+
test.equal(patch.oldFile().path(), 'README.md', 'Old file path should match expected');
23+
test.equal(patch.newFile().path(), 'README.md', 'New file path should match expected');
24+
test.equal(patch.size(), 1, 'Content array should be of known length');
25+
test.ok(patch.isModified(), 'Status should be known type');
26+
27+
var hunk = patch.hunks()[0];
28+
test.equal(hunk.size(), 5, 'Content array should be of known length');
29+
var lines = hunk.lines();
30+
31+
test.equal(lines[0].lineOrigin, git.DiffList.LineOrigin.Context, 'First content item should be context');
32+
test.equal(lines[1].lineOrigin, git.DiffList.LineOrigin.Context, 'Second content item should be context');
33+
test.equal(lines[2].lineOrigin, git.DiffList.LineOrigin.Context, 'Third content item should be context');
34+
35+
var oldContent = '__Before submitting a pull request, please ensure both unit tests and lint checks pass.__\n';
36+
test.equal(lines[3].content, oldContent, 'Old content should match known value');
37+
test.equal(lines[3].lineOrigin, git.DiffList.LineOrigin.Deletion, 'Fourth content item should be deletion');
38+
test.equal(lines[3].length, 90, 'Fourth content length should match known value');
39+
40+
var newContent = '__Before submitting a pull request, please ensure both that you\'ve added unit tests to cover your shiny new code, and that all unit tests and lint checks pass.__\n';
41+
test.equal(lines[4].content, newContent, 'New content should match known value');
42+
test.equal(lines[4].lineOrigin, git.DiffList.LineOrigin.Addition, 'Fifth content item should be addition');
43+
test.equal(lines[4].length, 162, 'Fifth content length should match known value');
44+
test.done();
5045
});
5146
});
5247
});

0 commit comments

Comments
 (0)