generated from tjx666/awesome-vscode-extension-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: new command
Search in Git Changed Files
- Loading branch information
YuTengjing
authored and
YuTengjing
committed
Apr 20, 2024
1 parent
9713787
commit 89cd3be
Showing
3 changed files
with
55 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import cp from 'node:child_process'; | ||
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; | ||
if (!cwd) { | ||
return vscode.window.showErrorMessage('No workspace is opened'); | ||
} | ||
|
||
let { stdout: gitRepoDir } = await exec('git rev-parse --show-toplevel', { cwd }); | ||
gitRepoDir = gitRepoDir.trim(); | ||
if (!gitRepoDir) { | ||
return vscode.window.showErrorMessage('Not a Git repository'); | ||
} | ||
|
||
cwd = gitRepoDir; | ||
// Step 1: Detect Git changes | ||
const [modifiedFilesOutput, addedFilesOutput] = await Promise.all([ | ||
exec('git diff --name-only', { cwd }), | ||
exec('git ls-files --others --exclude-standard', { cwd }), | ||
]); | ||
if (modifiedFilesOutput.stderr || addedFilesOutput.stderr) { | ||
return vscode.window.showErrorMessage('Failed to detect Git changes'); | ||
} | ||
|
||
const changedFiles = `${modifiedFilesOutput.stdout}\n${addedFilesOutput.stdout}` | ||
.split(/\r?\n/) | ||
.filter(Boolean); | ||
|
||
return vscode.commands.executeCommand('workbench.action.findInFiles', { | ||
triggerSearch: true, | ||
filesToInclude: changedFiles.join(', '), | ||
}); | ||
} |