Skip to content

Commit e40a144

Browse files
committed
Replace execSync shell commands with execFileSync and use repository.add API for staging files
- Refactor git-repository-utils.ts to replace all instances of execSync with execFileSync by passing arguments as an array to prevent shell command injection, and replace the git add shell execution with the built-in repository.add(files_to_stage) method. └── apps └── editor ├── package.json └── src ├── commands │ ├── copy-merge-commit-details-command.ts │ ├── generate-commit-message-commands │ │ ├── actions │ │ │ ├── get-prompt-data.ts │ │ │ └── run-generate-action.ts │ │ ├── generate-commit-message-commands.ts │ │ └── utils │ │ └── build-commit-message-prompt.ts │ └── select-files-of-commit-command.ts ├── i18n │ └── translations │ └── utils │ ├── git-repository-utils.ts │ └── index.ts ├── utils │ └── git-repository-utils.ts └── views └── prompt └── backend └── utils └── symbols └── git └── replace-git-symbols.ts
1 parent c2d9b2d commit e40a144

1 file changed

Lines changed: 10 additions & 11 deletions

File tree

‎apps/editor/src/utils/git-repository-utils.ts‎

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { execSync } from 'child_process'
1+
import { execFileSync } from 'child_process'
22
import * as path from 'path'
33
import * as vscode from 'vscode'
44
import { t } from '@/i18n'
@@ -280,9 +280,13 @@ export const prepare_staged_changes = async (params: {
280280
lines.map((l: string) => '+' + l).join('\n')
281281
}
282282
} else {
283-
const raw_diff = execSync(`git diff -- "${change.uri.fsPath}"`, {
284-
cwd: params.repository.rootUri.fsPath
285-
}).toString()
283+
const raw_diff = execFileSync(
284+
'git',
285+
['diff', '--', change.uri.fsPath],
286+
{
287+
cwd: params.repository.rootUri.fsPath
288+
}
289+
).toString()
286290

287291
if (
288292
raw_diff.includes('\n+++ /dev/null') ||
@@ -522,16 +526,11 @@ export const prepare_staged_changes = async (params: {
522526
}
523527
}
524528

525-
const file_args = files_to_stage
526-
.map((file: string) => `"${file.replace(/"/g, '\\"')}"`)
527-
.join(' ')
528-
execSync(`git add -- ${file_args}`, {
529-
cwd: params.repository.rootUri.fsPath
530-
})
529+
await params.repository.add(files_to_stage)
531530
await params.repository.status()
532531
}
533532

534-
const diff = execSync('git diff --staged', {
533+
const diff = execFileSync('git', ['diff', '--staged'], {
535534
cwd: params.repository.rootUri.fsPath
536535
}).toString()
537536

0 commit comments

Comments
 (0)