|
1 | | -import { exec } from "node:child_process"; |
| 1 | +import { exec, execFile } from "node:child_process"; |
2 | 2 | import * as fs from "node:fs/promises"; |
3 | 3 | import * as path from "node:path"; |
4 | 4 | import { promisify } from "node:util"; |
5 | 5 | import type { WorktreeInfo } from "./types.js"; |
6 | 6 | import { Logger } from "./utils/logger.js"; |
7 | 7 |
|
8 | 8 | const execAsync = promisify(exec); |
| 9 | +const execFileAsync = promisify(execFile); |
9 | 10 |
|
10 | 11 | export interface WorktreeConfig { |
11 | 12 | mainRepoPath: string; |
@@ -503,9 +504,9 @@ export class WorktreeManager { |
503 | 504 |
|
504 | 505 | private async runGitCommand(command: string): Promise<string> { |
505 | 506 | try { |
506 | | - const { stdout } = await execAsync( |
507 | | - `cd "${this.mainRepoPath}" && git ${command}`, |
508 | | - ); |
| 507 | + const { stdout } = await execAsync(`git ${command}`, { |
| 508 | + cwd: this.mainRepoPath, |
| 509 | + }); |
509 | 510 | return stdout.trim(); |
510 | 511 | } catch (error) { |
511 | 512 | throw new Error(`Git command failed: ${command}\n${error}`); |
@@ -659,8 +660,14 @@ export class WorktreeManager { |
659 | 660 | this.logger.info("Deleting worktree", { worktreePath }); |
660 | 661 |
|
661 | 662 | try { |
662 | | - // First, try to remove the worktree via git |
663 | | - await this.runGitCommand(`worktree remove "${worktreePath}" --force`); |
| 663 | + // First, try to remove the worktree via git using execFileAsync for safety |
| 664 | + await execFileAsync( |
| 665 | + "git", |
| 666 | + ["worktree", "remove", worktreePath, "--force"], |
| 667 | + { |
| 668 | + cwd: this.mainRepoPath, |
| 669 | + }, |
| 670 | + ); |
664 | 671 | this.logger.info("Worktree deleted successfully", { worktreePath }); |
665 | 672 | } catch (error) { |
666 | 673 | this.logger.warn( |
@@ -747,7 +754,8 @@ export class WorktreeManager { |
747 | 754 | async isWorktree(repoPath: string): Promise<boolean> { |
748 | 755 | try { |
749 | 756 | const { stdout } = await execAsync( |
750 | | - `cd "${repoPath}" && git rev-parse --is-inside-work-tree`, |
| 757 | + "git rev-parse --is-inside-work-tree", |
| 758 | + { cwd: repoPath }, |
751 | 759 | ); |
752 | 760 | if (stdout.trim() !== "true") { |
753 | 761 | return false; |
|
0 commit comments