Skip to content

Commit 65fdd5a

Browse files
jayreeisomorphic-git-bot
authored andcommitted
fix: handle non utf8 files with autocrlf correctly (#1909)
* fix: handle non utf8 files with autocrlf correctly * docs: add @jayree as a contributor * chore: add test * chore: add crlf file test
1 parent 262f6ff commit 65fdd5a

File tree

6 files changed

+33
-42
lines changed

6 files changed

+33
-42
lines changed

js/isomorphic-git/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,7 @@ Thanks goes to these wonderful people ([emoji key](https://github.com/kentcdodds
370370
<td align="center"><a href="https://github.com/yarikoptic"><img src="https://avatars.githubusercontent.com/u/39889?v=4?s=60" width="60px;" alt=""/><br /><sub><b>Yaroslav Halchenko</b></sub></a><br /><a href="https://github.com/isomorphic-git/isomorphic-git/commits?author=yarikoptic" title="Documentation">📖</a></td>
371371
<td align="center"><a href="https://alex-v.blog/"><img src="https://avatars.githubusercontent.com/u/716334?v=4?s=60" width="60px;" alt=""/><br /><sub><b>Alex Villarreal</b></sub></a><br /><a href="https://github.com/isomorphic-git/isomorphic-git/commits?author=alexvy86" title="Code">💻</a></td>
372372
<td align="center"><a href="https://github.com/amrc-benmorrow"><img src="https://avatars.githubusercontent.com/u/120477944?v=4?s=60" width="60px;" alt=""/><br /><sub><b>Ben Morrow</b></sub></a><br /><a href="https://github.com/isomorphic-git/isomorphic-git/commits?author=amrc-benmorrow" title="Code">💻</a></td>
373+
<td align="center"><a href="https://github.com/jayree"><img src="https://avatars.githubusercontent.com/u/14836154?v=4?s=60" width="60px;" alt=""/><br /><sub><b>jayree</b></sub></a><br /><a href="https://github.com/isomorphic-git/isomorphic-git/commits?author=jayree" title="Code">💻</a> <a href="https://github.com/isomorphic-git/isomorphic-git/commits?author=jayree" title="Tests">⚠️</a></td>
373374
</tr>
374375
</table>
375376

js/isomorphic-git/index.cjs

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4182,17 +4182,14 @@ class GitWalkerFs {
41824182
entry._content = undefined;
41834183
} else {
41844184
const config = await GitConfigManager.get({ fs, gitdir });
4185-
const autocrlf = (await config.get('core.autocrlf')) || false;
4186-
const content = await fs.read(`${dir}/${entry._fullpath}`, {
4187-
encoding: 'utf8',
4188-
autocrlf,
4189-
});
4185+
const autocrlf = await config.get('core.autocrlf');
4186+
const content = await fs.read(`${dir}/${entry._fullpath}`, { autocrlf });
41904187
// workaround for a BrowserFS edge case
41914188
entry._actualSize = content.length;
41924189
if (entry._stat && entry._stat.size === -1) {
41934190
entry._stat.size = entry._actualSize;
41944191
}
4195-
entry._content = new TextEncoder().encode(content);
4192+
entry._content = new Uint8Array(content);
41964193
}
41974194
}
41984195
return entry._content
@@ -4563,8 +4560,14 @@ class FileSystem {
45634560
async read(filepath, options = {}) {
45644561
try {
45654562
let buffer = await this._readFile(filepath, options);
4566-
if (typeof buffer === 'string' && options.autocrlf) {
4567-
buffer = buffer.replace(/\r\n/g, '\n');
4563+
if (options.autocrlf === 'true') {
4564+
try {
4565+
buffer = new TextDecoder('utf8', { fatal: true }).decode(buffer);
4566+
buffer = buffer.replace(/\r\n/g, '\n');
4567+
buffer = new TextEncoder().encode(buffer);
4568+
} catch (error) {
4569+
// non utf8 file
4570+
}
45684571
}
45694572
// Convert plain ArrayBuffers to Buffers
45704573
if (typeof buffer !== 'string') {
@@ -5097,20 +5100,12 @@ async function addToIndex({
50975100
}
50985101
} else {
50995102
const config = await GitConfigManager.get({ fs, gitdir });
5100-
const autocrlf = (await config.get('core.autocrlf')) || false;
5103+
const autocrlf = await config.get('core.autocrlf');
51015104
const object = stats.isSymbolicLink()
51025105
? await fs.readlink(join(dir, currentFilepath)).then(posixifyPathBuffer)
5103-
: await fs.read(join(dir, currentFilepath), {
5104-
encoding: 'utf8',
5105-
autocrlf,
5106-
});
5106+
: await fs.read(join(dir, currentFilepath), { autocrlf });
51075107
if (object === null) throw new NotFoundError(currentFilepath)
5108-
const oid = await _writeObject({
5109-
fs,
5110-
gitdir,
5111-
type: 'blob',
5112-
object: new TextEncoder().encode(object),
5113-
});
5108+
const oid = await _writeObject({ fs, gitdir, type: 'blob', object });
51145109
index.insert({ filepath: currentFilepath, stats, oid });
51155110
}
51165111
});

js/isomorphic-git/index.js

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4176,17 +4176,14 @@ class GitWalkerFs {
41764176
entry._content = undefined;
41774177
} else {
41784178
const config = await GitConfigManager.get({ fs, gitdir });
4179-
const autocrlf = (await config.get('core.autocrlf')) || false;
4180-
const content = await fs.read(`${dir}/${entry._fullpath}`, {
4181-
encoding: 'utf8',
4182-
autocrlf,
4183-
});
4179+
const autocrlf = await config.get('core.autocrlf');
4180+
const content = await fs.read(`${dir}/${entry._fullpath}`, { autocrlf });
41844181
// workaround for a BrowserFS edge case
41854182
entry._actualSize = content.length;
41864183
if (entry._stat && entry._stat.size === -1) {
41874184
entry._stat.size = entry._actualSize;
41884185
}
4189-
entry._content = new TextEncoder().encode(content);
4186+
entry._content = new Uint8Array(content);
41904187
}
41914188
}
41924189
return entry._content
@@ -4557,8 +4554,14 @@ class FileSystem {
45574554
async read(filepath, options = {}) {
45584555
try {
45594556
let buffer = await this._readFile(filepath, options);
4560-
if (typeof buffer === 'string' && options.autocrlf) {
4561-
buffer = buffer.replace(/\r\n/g, '\n');
4557+
if (options.autocrlf === 'true') {
4558+
try {
4559+
buffer = new TextDecoder('utf8', { fatal: true }).decode(buffer);
4560+
buffer = buffer.replace(/\r\n/g, '\n');
4561+
buffer = new TextEncoder().encode(buffer);
4562+
} catch (error) {
4563+
// non utf8 file
4564+
}
45624565
}
45634566
// Convert plain ArrayBuffers to Buffers
45644567
if (typeof buffer !== 'string') {
@@ -5091,20 +5094,12 @@ async function addToIndex({
50915094
}
50925095
} else {
50935096
const config = await GitConfigManager.get({ fs, gitdir });
5094-
const autocrlf = (await config.get('core.autocrlf')) || false;
5097+
const autocrlf = await config.get('core.autocrlf');
50955098
const object = stats.isSymbolicLink()
50965099
? await fs.readlink(join(dir, currentFilepath)).then(posixifyPathBuffer)
5097-
: await fs.read(join(dir, currentFilepath), {
5098-
encoding: 'utf8',
5099-
autocrlf,
5100-
});
5100+
: await fs.read(join(dir, currentFilepath), { autocrlf });
51015101
if (object === null) throw new NotFoundError(currentFilepath)
5102-
const oid = await _writeObject({
5103-
fs,
5104-
gitdir,
5105-
type: 'blob',
5106-
object: new TextEncoder().encode(object),
5107-
});
5102+
const oid = await _writeObject({ fs, gitdir, type: 'blob', object });
51085103
index.insert({ filepath: currentFilepath, stats, oid });
51095104
}
51105105
});

0 commit comments

Comments
 (0)