mirror of
https://github.com/ckaczor/vscode-gitlens.git
synced 2026-02-16 10:58:34 -05:00
Adds better filename sanitization
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
import { Strings } from '../system';
|
||||||
import { findGitPath, IGit } from './gitLocator';
|
import { findGitPath, IGit } from './gitLocator';
|
||||||
import { Logger } from '../logger';
|
import { Logger } from '../logger';
|
||||||
import { spawnPromise } from 'spawn-rx';
|
import { spawnPromise } from 'spawn-rx';
|
||||||
@@ -99,8 +100,7 @@ export class Git {
|
|||||||
static async getVersionedFile(repoPath: string | undefined, fileName: string, branchOrSha: string) {
|
static async getVersionedFile(repoPath: string | undefined, fileName: string, branchOrSha: string) {
|
||||||
const data = await Git.show(repoPath, fileName, branchOrSha, 'binary');
|
const data = await Git.show(repoPath, fileName, branchOrSha, 'binary');
|
||||||
|
|
||||||
// TODO: Sanitize the filename
|
const suffix = Strings.truncate(Strings.sanitizeForFS(Git.isSha(branchOrSha) ? Git.shortenSha(branchOrSha) : branchOrSha), 50, '');
|
||||||
const suffix = Git.isSha(branchOrSha) ? Git.shortenSha(branchOrSha) : branchOrSha.replace(/\\/g, '_').replace(/\//g, '_');
|
|
||||||
const ext = path.extname(fileName);
|
const ext = path.extname(fileName);
|
||||||
return new Promise<string>((resolve, reject) => {
|
return new Promise<string>((resolve, reject) => {
|
||||||
tmp.file({ prefix: `${path.basename(fileName, ext)}-${suffix}__`, postfix: ext },
|
tmp.file({ prefix: `${path.basename(fileName, ext)}-${suffix}__`, postfix: ext },
|
||||||
|
|||||||
@@ -101,12 +101,20 @@ export namespace Strings {
|
|||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function truncate(s: string, truncateTo?: number) {
|
// Removes \ / : * ? " < > | and C0 and C1 control codes
|
||||||
if (!s || truncateTo === undefined) return s;
|
const illegalCharsForFSRegEx = /[\\/:*?"<>|\x00-\x1f\x80-\x9f]/g;
|
||||||
|
|
||||||
|
export function sanitizeForFS(s: string, replacement: string = '_') {
|
||||||
|
if (!s) return s;
|
||||||
|
return s.replace(illegalCharsForFSRegEx, replacement);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function truncate(s: string, truncateTo: number, ellipsis: string = '\u2026') {
|
||||||
|
if (!s) return s;
|
||||||
|
|
||||||
const len = getWidth(s);
|
const len = getWidth(s);
|
||||||
if (len <= truncateTo) return s;
|
if (len <= truncateTo) return s;
|
||||||
if (len === s.length) return `${s.substring(0, truncateTo - 1)}\u2026`;
|
if (len === s.length) return `${s.substring(0, truncateTo - 1)}${ellipsis}`;
|
||||||
|
|
||||||
// Skip ahead to start as far as we can by assuming all the double-width characters won't be truncated
|
// Skip ahead to start as far as we can by assuming all the double-width characters won't be truncated
|
||||||
let chars = Math.floor(truncateTo / (len / s.length));
|
let chars = Math.floor(truncateTo / (len / s.length));
|
||||||
@@ -119,6 +127,6 @@ export namespace Strings {
|
|||||||
chars--;
|
chars--;
|
||||||
}
|
}
|
||||||
|
|
||||||
return `${s.substring(0, chars)}\u2026`;
|
return `${s.substring(0, chars)}${ellipsis}`;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user