Skip to content

Commit 64d5a5b

Browse files
committed
Introduced a new ConvenientLine class to wrap the lines returned from ConvenientHunk.
Overrode the line content method to provide more useful results.
1 parent c86625b commit 64d5a5b

File tree

4 files changed

+89
-3
lines changed

4 files changed

+89
-3
lines changed

generate/templates/templates/nodegit.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ require("./utils/lookup_wrapper");
7171
require("./utils/normalize_options");
7272

7373
// Load up extra types;
74+
require("./convenient_line");
7475
require("./convenient_hunk");
7576
require("./convenient_patch");
7677
require("./status_file");

lib/convenient_hunk.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var NodeGit = require("../");
2+
var ConvenientLine = NodeGit.ConvenientLine;
23

34
function ConvenientHunk(raw, i) {
45
this.raw = raw;
@@ -29,7 +30,7 @@ ConvenientHunk.prototype.size = function() {
2930
ConvenientHunk.prototype.lines = function() {
3031
var result = [];
3132
for (var i = 0; i < this.size(); i++) {
32-
result.push(this.raw.getLineInHunk(this.i, i));
33+
result.push(new ConvenientLine(this.raw.getLineInHunk(this.i, i), i));
3334
}
3435
return result;
3536
};

lib/convenient_line.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
var NodeGit = require("../");
2+
3+
function ConvenientLine(raw, i) {
4+
this.raw = raw;
5+
this.i = i;
6+
}
7+
8+
/**
9+
* The content of the line
10+
* @return {String}
11+
*/
12+
ConvenientLine.prototype.origin = function() {
13+
return this.raw.origin();
14+
}
15+
16+
/**
17+
* The content of the line
18+
* @return {String}
19+
*/
20+
ConvenientLine.prototype.oldLineno = function() {
21+
return this.raw.oldLineno();
22+
}
23+
24+
/**
25+
* The content of the line
26+
* @return {String}
27+
*/
28+
ConvenientLine.prototype.newLineno = function() {
29+
return this.raw.newLineno();
30+
}
31+
32+
/**
33+
* The content of the line
34+
* @return {String}
35+
*/
36+
ConvenientLine.prototype.numLines = function() {
37+
return this.raw.numLines();
38+
}
39+
40+
/**
41+
* The content of the line
42+
* @return {String}
43+
*/
44+
ConvenientLine.prototype.contentLen = function() {
45+
return this.raw.contentLen();
46+
}
47+
48+
49+
/**
50+
* The content of the line
51+
* @return {String}
52+
*/
53+
ConvenientLine.prototype.contentOffset = function() {
54+
return this.raw.contentOffset();
55+
}
56+
57+
/**
58+
* The content of the line
59+
* @return {String}
60+
*/
61+
ConvenientLine.prototype.rawContent = function() {
62+
return this.raw.content();
63+
}
64+
65+
/**
66+
* The relevant line
67+
* @return {String}
68+
*/
69+
ConvenientLine.prototype.content = function() {
70+
return this.raw.content().substring(0, this.raw.contentLen() - 1);
71+
}
72+
73+
NodeGit.ConvenientLine = ConvenientLine;

test/tests/diff.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,14 +108,14 @@ describe("Diff", function() {
108108

109109
var oldContent = "__Before submitting a pull request, please ensure " +
110110
"both unit tests and lint checks pass.__\n";
111-
assert.equal(lines[3].content(), oldContent);
111+
assert.equal(lines[3].rawContent(), oldContent);
112112
assert.equal(lines[3].origin(), Diff.LINE.DELETION);
113113
assert.equal(lines[3].contentLen(), 90);
114114

115115
var newContent = "__Before submitting a pull request, please ensure " +
116116
"both that you've added unit tests to cover your shiny new code, " +
117117
"and that all unit tests and lint checks pass.__\n";
118-
assert.equal(lines[4].content(), newContent);
118+
assert.equal(lines[4].rawContent(), newContent);
119119
assert.equal(lines[4].origin(), Diff.LINE.ADDITION);
120120
assert.equal(lines[4].contentLen(), 162);
121121
});
@@ -134,6 +134,17 @@ describe("Diff", function() {
134134
assert.equal(newFile.size(), 23);
135135
});
136136

137+
it("can resolve individual line chages from the patch hunks", function() {
138+
this.workdirDiff.patches().forEach(function(convenientPatch) {
139+
convenientPatch.hunks().forEach(function(convenientHunk) {
140+
convenientHunk.lines().forEach(function(line) {
141+
assert(!/\n/.exec(line.content()));
142+
assert(/\n/.exec(line.rawContent()));
143+
});
144+
});
145+
});
146+
});
147+
137148
it("can diff with a null tree", function() {
138149
var repo = this.repository;
139150
var tree = this.masterCommitTree;

0 commit comments

Comments
 (0)