Skip to content

Commit cff2a0a

Browse files
author
John Haley
committed
Include new line char in blob content
This required a modification to how we're staging lines since that was expecting no new line char at the end and would append it itself. That's no longer needed. The tests have been updated to reflect the change.
1 parent 74c19f8 commit cff2a0a

File tree

6 files changed

+30
-41
lines changed

6 files changed

+30
-41
lines changed

lib/convenient_line.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,11 @@ ConvenientLine.prototype.numLines = function() {
3939
};
4040

4141
/**
42-
* Number of characters in the string
42+
* Number of bytes in the string
4343
* @return {Number}
4444
*/
4545
ConvenientLine.prototype.contentLen = function() {
46-
if (!this._cache.contentLen) {
47-
this._cache.contentLen = this.content().length;
48-
}
49-
50-
return this._cache.contentLen;
46+
return this.raw.contentLen();
5147
};
5248

5349
/**
@@ -74,8 +70,7 @@ ConvenientLine.prototype.content = function() {
7470
if (!this._cache.content) {
7571
this._cache.content = new Buffer(this.raw.content())
7672
.slice(0, this.raw.contentLen())
77-
.toString("utf8")
78-
.replace("\n", "");
73+
.toString("utf8");
7974
}
8075

8176
return this._cache.content;

lib/repository.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1286,9 +1286,6 @@ Repository.prototype.stageLines =
12861286
(!isStaged && (!newLine || newLine.length === 0))) {
12871287
if (hunkLine.origin() !== lineTypes.ADDED) {
12881288
newContent += hunkLine.content();
1289-
if (hunkLine.raw.numLines() !== 0) {
1290-
newContent += "\n";
1291-
}
12921289
}
12931290
if ((isStaged && hunkLine.origin() !== lineTypes.DELETED) ||
12941291
(!isStaged && hunkLine.origin() !== lineTypes.ADDED)) {
@@ -1299,9 +1296,6 @@ Repository.prototype.stageLines =
12991296
switch (hunkLine.origin()) {
13001297
case lineTypes.ADDED:
13011298
newContent += hunkLine.content();
1302-
if (hunkLine.raw.numLines() !== 0) {
1303-
newContent += "\n";
1304-
}
13051299
if (isStaged) {
13061300
oldIndex++;
13071301
}

test/tests/commit.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -520,10 +520,6 @@ describe("Commit", function() {
520520
" line q\nline r\nline s\nline t\n\nline u\n" +
521521
"line v1\nline w\nline x\n \nline y\nline z\n";
522522

523-
function getLineText(line) {
524-
return line.rawContent().substring(0, line.contentLen());
525-
}
526-
527523
return NodeGit.Repository.open(reposPath)
528524
.then(function(repoResult) {
529525
repo = repoResult;
@@ -559,33 +555,37 @@ describe("Commit", function() {
559555
//check all hunk lines
560556
assert.equal(lines.length, 12);
561557
assert.equal(lines[0].origin(), Diff.LINE.CONTEXT);
562-
assert.equal(lines[1].contentLen(), 8);
563558

564-
assert.equal(getLineText(lines[1]), "line s");
559+
assert.equal(lines[1].content().length, 9);
560+
assert.equal(lines[1].content(), "line s\n");
565561
assert.equal(lines[1].origin(), Diff.LINE.CONTEXT);
562+
566563
assert.equal(lines[2].origin(), Diff.LINE.CONTEXT);
567564

568-
assert.equal(lines[3].contentLen(), 0);
569-
assert.equal(getLineText(lines[3]), "");
565+
assert.equal(lines[3].content().length, 1);
566+
assert.equal(lines[3].content(), "\n");
570567
assert.equal(lines[3].origin(), Diff.LINE.ADDITION);
571568

572569
assert.equal(lines[4].origin(), Diff.LINE.CONTEXT);
573570

574-
assert.equal(lines[5].contentLen(), 6);
575-
assert.equal(getLineText(lines[5]), "line v");
571+
assert.equal(lines[5].content().length, 7);
572+
assert.equal(lines[5].content(), "line v\n");
576573
assert.equal(lines[5].origin(), Diff.LINE.DELETION);
577-
assert.equal(lines[6].contentLen(), 7);
578-
assert.equal(getLineText(lines[6]), "line v1");
574+
575+
assert.equal(lines[6].content().length, 8);
576+
assert.equal(lines[6].content(), "line v1\n");
579577
assert.equal(lines[6].origin(), Diff.LINE.ADDITION);
580578

581579
assert.equal(lines[7].origin(), Diff.LINE.CONTEXT);
580+
582581
assert.equal(lines[8].origin(), Diff.LINE.CONTEXT);
583582

584-
assert.equal(lines[9].contentLen(), 3);
585-
assert.equal(getLineText(lines[9]), "\t\t\t");
583+
assert.equal(lines[9].content().length, 4);
584+
assert.equal(lines[9].content(), "\t\t\t\n");
586585
assert.equal(lines[9].origin(), Diff.LINE.ADDITION);
587586

588587
assert.equal(lines[10].origin(), Diff.LINE.CONTEXT);
588+
589589
assert.equal(lines[11].origin(), Diff.LINE.CONTEXT);
590590
});
591591
});

test/tests/convenient_line.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ var local = path.join.bind(path, __dirname);
77

88
describe("ConvenientLine", function() {
99
var repoPath = local("../repos/convenientLineTest");
10-
var unicodeLine = "Ťḥ𝖎ṧ ℓỈ𝓃ệ çǒ𝚗ẗảḭṋṦ Û𝐧ǐ𝗰ṓḍ𝔢";
11-
var asciiLine = "but this line doesn't";
10+
var unicodeLine = "Ťḥ𝖎ṧ ℓỈ𝓃ệ çǒ𝚗ẗảḭṋṦ Û𝐧ǐ𝗰ṓḍ𝔢\n";
11+
var asciiLine = "but this line doesn't\n";
1212

1313
beforeEach(function() {
1414
var test = this;
@@ -18,7 +18,7 @@ describe("ConvenientLine", function() {
1818
return repoSetup.commitFileToRepo(
1919
repo,
2020
"fileWithUnicodeChars",
21-
unicodeLine + "\n" + asciiLine + "\n"
21+
unicodeLine + asciiLine
2222
);
2323
})
2424
.then(function(commit) {
@@ -46,7 +46,7 @@ describe("ConvenientLine", function() {
4646
it("can parse the byte length of a unicode string", function() {
4747
var line = this.unicodeLine;
4848

49-
assert.equal(line.contentLen(), 32);
49+
assert.equal(line.contentLen(), Buffer.byteLength(unicodeLine, "utf8"));
5050
});
5151

5252
it("can get a line that contains unicode", function() {
@@ -58,7 +58,7 @@ describe("ConvenientLine", function() {
5858
it("can parse the byte length of a ascii string", function() {
5959
var line = this.asciiLine;
6060

61-
assert.equal(line.contentLen(), 21);
61+
assert.equal(line.contentLen(), Buffer.byteLength(asciiLine, "utf8"));
6262
});
6363

6464
it("can get a line that contains ascii", function() {

test/tests/diff.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,17 +114,17 @@ describe("Diff", function() {
114114
assert.equal(lines[2].origin(), Diff.LINE.CONTEXT);
115115

116116
var oldContent = "__Before submitting a pull request, please ensure " +
117-
"both unit tests and lint checks pass.__";
117+
"both unit tests and lint checks pass.__\n";
118118
assert.equal(lines[3].content(), oldContent);
119119
assert.equal(lines[3].origin(), Diff.LINE.DELETION);
120-
assert.equal(lines[3].contentLen(), oldContent.length);
120+
assert.equal(lines[3].content().length, oldContent.length);
121121

122122
var newContent = "__Before submitting a pull request, please ensure " +
123123
"both that you've added unit tests to cover your shiny new code, " +
124-
"and that all unit tests and lint checks pass.__";
124+
"and that all unit tests and lint checks pass.__\n";
125125
assert.equal(lines[4].content(), newContent);
126126
assert.equal(lines[4].origin(), Diff.LINE.ADDITION);
127-
assert.equal(lines[4].contentLen(), newContent.length);
127+
assert.equal(lines[4].content().length, newContent.length);
128128
});
129129
});
130130

@@ -182,7 +182,7 @@ describe("Diff", function() {
182182
})
183183
.then(function(lines) {
184184
lines.forEach(function(line) {
185-
assert(!/\n/.exec(line.content()));
185+
assert(/\n/.exec(line.content()));
186186
assert(/\n/.exec(line.rawContent()));
187187
});
188188
});

test/tests/stage.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ function stagingTest(staging, newFileContent) {
245245
throw ("File change when no file change expected.");
246246
}
247247
} else {
248-
assert(delta.newFile().mode() - delta.oldFile().mode() ===
248+
assert(delta.newFile().mode() - delta.oldFile().mode() ===
249249
fileModeDifference);
250250
}
251251
return true;
@@ -265,7 +265,7 @@ function stagingTest(staging, newFileContent) {
265265
fileContent)
266266
.then(function() {
267267
//Then, change the permission locally.
268-
return fse.chmod(path.join(test.repository.workdir(), fileName),
268+
return fse.chmod(path.join(test.repository.workdir(), fileName),
269269
0755 /* new filemode */);
270270
});
271271
})
@@ -310,7 +310,7 @@ function stagingTest(staging, newFileContent) {
310310
fileContent)
311311
.then(function() {
312312
//Then, change the permission locally.
313-
return fse.chmod(path.join(test.repository.workdir(), fileName),
313+
return fse.chmod(path.join(test.repository.workdir(), fileName),
314314
0755 /* new filemode */);
315315
});
316316
})

0 commit comments

Comments
 (0)