mirror of
https://github.com/google-gemini/gemini-cli.git
synced 2026-07-09 17:40:44 -07:00
feat(core): allow reusing existing worktrees with --worktree flag
This commit is contained in:
@@ -85,7 +85,8 @@ describe('worktree utilities', () => {
|
||||
});
|
||||
|
||||
describe('createWorktree', () => {
|
||||
it('should execute git worktree add with correct branch and path', async () => {
|
||||
it('should execute git worktree add with correct branch and path if it does not exist', async () => {
|
||||
vi.mocked(fs.access).mockRejectedValue(new Error('ENOENT'));
|
||||
vi.mocked(execa).mockResolvedValue({ stdout: '' } as never);
|
||||
|
||||
const resultPath = await createWorktree(projectRoot, worktreeName);
|
||||
@@ -98,7 +99,17 @@ describe('worktree utilities', () => {
|
||||
);
|
||||
});
|
||||
|
||||
it('should return existing path and not call git if it already exists', async () => {
|
||||
vi.mocked(fs.access).mockResolvedValue(undefined);
|
||||
|
||||
const resultPath = await createWorktree(projectRoot, worktreeName);
|
||||
|
||||
expect(resultPath).toBe(expectedPath);
|
||||
expect(execa).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should throw an error if git worktree add fails', async () => {
|
||||
vi.mocked(fs.access).mockRejectedValue(new Error('ENOENT'));
|
||||
vi.mocked(execa).mockRejectedValue(new Error('git failed'));
|
||||
|
||||
await expect(createWorktree(projectRoot, worktreeName)).rejects.toThrow(
|
||||
|
||||
@@ -120,6 +120,15 @@ export async function createWorktree(
|
||||
name: string,
|
||||
): Promise<string> {
|
||||
const worktreePath = getWorktreePath(projectRoot, name);
|
||||
|
||||
try {
|
||||
await fs.access(worktreePath);
|
||||
// Worktree path already exists, reuse it
|
||||
return worktreePath;
|
||||
} catch {
|
||||
// Does not exist, proceed to create
|
||||
}
|
||||
|
||||
const branchName = `worktree-${name}`;
|
||||
|
||||
await execa('git', ['worktree', 'add', worktreePath, '-b', branchName], {
|
||||
|
||||
Reference in New Issue
Block a user