Skip to content

Commit

Permalink
fix: changed filename contains special chars
Browse files Browse the repository at this point in the history
  • Loading branch information
YuTengjing authored and YuTengjing committed Apr 21, 2024
1 parent 7e777c2 commit 7d6dd35
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/features/searchInGitChangedFiles.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import cp from 'node:child_process';
import { relative, resolve } from 'node:path';
import util from 'node:util';

import vscode from 'vscode';

const exec = util.promisify(cp.exec);

export async function searchInGitChangedFiles() {
let cwd = vscode.workspace.workspaceFolders?.[0]?.uri.fsPath;
const cwd = vscode.workspace.workspaceFolders?.[0]?.uri.fsPath;
if (!cwd) {
return vscode.window.showErrorMessage('No workspace is opened');
}
Expand All @@ -18,14 +19,13 @@ export async function searchInGitChangedFiles() {
return vscode.window.showErrorMessage('Not a Git repository');
}

cwd = gitRepoDir;
const outputList = await Promise.all([
// staged
exec('git diff --name-only --cached', { cwd }),
exec('git diff --name-only --cached', { cwd: gitRepoDir }),
// changed and not staged
exec('git diff --name-only', { cwd }),
exec('git diff --name-only', { cwd: gitRepoDir }),
// new added
exec('git ls-files --others --exclude-standard', { cwd }),
exec('git ls-files --others --exclude-standard', { cwd: gitRepoDir }),
]);
if (outputList.some(({ stderr }) => stderr)) {
return vscode.window.showErrorMessage('Failed to detect Git changes');
Expand All @@ -36,7 +36,14 @@ export async function searchInGitChangedFiles() {
.join('\n')
.split(/\r?\n/)
.filter((line) => line.trim() !== '');
changedFiles = [...new Set(changedFiles)];
changedFiles = [...new Set(changedFiles)].map((fileName) => {
const absPath = resolve(gitRepoDir, fileName);
fileName = relative(cwd, absPath);
return fileName.replaceAll(/\[|\]/g, '[$&]');
});
if (changedFiles.length === 0) {
return vscode.window.showInformationMessage('No changed files!');
}

return vscode.commands.executeCommand('workbench.action.findInFiles', {
triggerSearch: true,
Expand Down

0 comments on commit 7d6dd35

Please sign in to comment.