Compare commits
2 commits
issue-4-re
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 8761c3af5f | |||
| a18e6fe64a |
5 changed files with 969 additions and 25 deletions
146
.pi/extensions/pi-worktree-create.ts
Normal file
146
.pi/extensions/pi-worktree-create.ts
Normal file
|
|
@ -0,0 +1,146 @@
|
|||
/**
|
||||
* Git worktree create — pure validation and command-building functions.
|
||||
* No pi SDK dependencies for testability.
|
||||
*/
|
||||
|
||||
// ─── Type Definitions ──────────────────────────────────────────────
|
||||
|
||||
/** Error returned when a worktree operation fails. */
|
||||
export interface WorktreeError {
|
||||
/** Human-readable error message from git or validation. */
|
||||
message: string;
|
||||
/** Suggested fix for the user. */
|
||||
suggestion?: string;
|
||||
}
|
||||
|
||||
/** Parameters for creating a new worktree. */
|
||||
export interface CreateWorktreeParams {
|
||||
/** Path for the new worktree (relative or absolute). */
|
||||
path: string;
|
||||
/** Branch name to check out. */
|
||||
branch: string;
|
||||
/** Whether to create the branch if it doesn't exist. Defaults to true. */
|
||||
createBranch?: boolean;
|
||||
}
|
||||
|
||||
// ─── Validation ────────────────────────────────────────────────────
|
||||
|
||||
/**
|
||||
* Validate input parameters for creating a worktree.
|
||||
*
|
||||
* Checks that path and branch name are non-empty and well-formed.
|
||||
* Branch names follow git conventions: no spaces, no leading/trailing whitespace,
|
||||
* no special characters like ~, ^, . at start or ?, *, [, ], ", ' in the middle (except / for nesting).
|
||||
*
|
||||
* @param worktreePath - Path for the new worktree
|
||||
* @param branchName - Branch name to check out
|
||||
* @returns null if valid, or a WorktreeError describing the problem
|
||||
*/
|
||||
export function validateCreateWorktreeInput(
|
||||
worktreePath: string,
|
||||
branchName: string,
|
||||
): WorktreeError | null {
|
||||
// Validate path is non-empty
|
||||
if (!worktreePath || !worktreePath.trim()) {
|
||||
return {
|
||||
message: "Path cannot be empty.",
|
||||
suggestion: "Provide a valid path for the new worktree (e.g., '../worktrees/feature-x').",
|
||||
};
|
||||
}
|
||||
|
||||
// Validate branch name is non-empty
|
||||
if (!branchName || !branchName.trim()) {
|
||||
return {
|
||||
message: "Branch name cannot be empty.",
|
||||
suggestion: "Provide a valid branch name (e.g., 'feature-x' or 'bugfix/issue-42').",
|
||||
};
|
||||
}
|
||||
|
||||
// Validate branch name doesn't contain spaces
|
||||
if (branchName.includes(" ")) {
|
||||
return {
|
||||
message: `Branch name '${branchName}' contains invalid characters (spaces).`,
|
||||
suggestion: "Use hyphens or underscores instead of spaces (e.g., 'feature-x' not 'my feature').",
|
||||
};
|
||||
}
|
||||
|
||||
// Validate branch name doesn't start/end with special chars
|
||||
if (/^[\/\-~^\.]/.test(branchName) || /[\/\-\.\s]$/.test(branchName)) {
|
||||
return {
|
||||
message: `Branch name '${branchName}' starts or ends with an invalid character.`,
|
||||
suggestion: "Branch names should start with a letter and not end with '/', '-', '.', or whitespace.",
|
||||
};
|
||||
}
|
||||
|
||||
// Validate branch name doesn't contain dangerous characters
|
||||
if (/[?*\[\]"'\\]/.test(branchName)) {
|
||||
return {
|
||||
message: `Branch name '${branchName}' contains invalid characters.`,
|
||||
suggestion: "Avoid characters like ?, *, [, ], \", ' in branch names.",
|
||||
};
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
// ─── Path Validation ──────────────────────────────────────────────
|
||||
|
||||
/**
|
||||
* Check if the target path already exists and return an error if so.
|
||||
*
|
||||
* @param worktreePath - Path to check
|
||||
* @param checkExists - Function that checks if a path exists (injected for testability)
|
||||
* @returns null if path is free, or a WorktreeError with the exact message format
|
||||
*/
|
||||
export function validatePathDoesNotExist(
|
||||
worktreePath: string,
|
||||
checkExists: (path: string) => boolean,
|
||||
): WorktreeError | null {
|
||||
if (checkExists(worktreePath)) {
|
||||
return {
|
||||
message: `Path '${worktreePath}' already exists.`,
|
||||
suggestion: "Choose a different location.",
|
||||
};
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
// ─── Branch Validation ──────────────────────────────────────────────
|
||||
|
||||
/**
|
||||
* Check if the target branch is the same as the current branch.
|
||||
*
|
||||
* @param targetBranch - The branch we want to create the worktree on
|
||||
* @param currentBranch - The currently checked out branch
|
||||
* @returns true if they match (worktree creation should be refused)
|
||||
*/
|
||||
export function isCurrentBranch(targetBranch: string, currentBranch: string): boolean {
|
||||
return targetBranch === currentBranch;
|
||||
}
|
||||
|
||||
// ─── Command Building ──────────────────────────────────────────────
|
||||
|
||||
/**
|
||||
* Build the `git worktree add` command arguments.
|
||||
*
|
||||
* Uses `-b` flag when createBranch is true to create a new branch.
|
||||
* Without `-b`, checks out an existing branch.
|
||||
*
|
||||
* @param params - Worktree creation parameters
|
||||
* @returns Array of command arguments (excluding 'git' itself)
|
||||
*/
|
||||
export function buildWorktreeAddCommand(params: CreateWorktreeParams): string[] {
|
||||
const { path: worktreePath, branch, createBranch = true } = params;
|
||||
|
||||
const args = ["worktree", "add"];
|
||||
|
||||
if (createBranch) {
|
||||
args.push("-b", branch);
|
||||
} else {
|
||||
args.push(branch);
|
||||
}
|
||||
|
||||
args.push(worktreePath);
|
||||
|
||||
return args;
|
||||
}
|
||||
176
.pi/extensions/pi-worktree-remove.test.ts
Normal file
176
.pi/extensions/pi-worktree-remove.test.ts
Normal file
|
|
@ -0,0 +1,176 @@
|
|||
/**
|
||||
* Unit tests for git_worktree_remove validation and command-building functions.
|
||||
*
|
||||
* Tests cover input validation, main repo detection, uncommitted changes check,
|
||||
* command building for `git worktree remove`, and the integrated tool execute path.
|
||||
*/
|
||||
|
||||
import { describe, it, expect, mock } from "bun:test";
|
||||
import {
|
||||
validateRemoveWorktreeInput,
|
||||
isMainRepository,
|
||||
hasUncommittedChanges,
|
||||
buildWorktreeRemoveCommand,
|
||||
} from "./pi-worktree-remove";
|
||||
|
||||
// ─── validateRemoveWorktreeInput ──────────────────────────────────────
|
||||
|
||||
describe("validateRemoveWorktreeInput (Issue #5)", () => {
|
||||
it("returns null for valid path", () => {
|
||||
const result = validateRemoveWorktreeInput("/tmp/worktrees/feature-x");
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
it("returns error for empty path", () => {
|
||||
const result = validateRemoveWorktreeInput("");
|
||||
expect(result).not.toBeNull();
|
||||
if (result) expect(result.message).toContain("cannot be empty");
|
||||
});
|
||||
|
||||
it("returns error for whitespace-only path", () => {
|
||||
const result = validateRemoveWorktreeInput(" ");
|
||||
expect(result).not.toBeNull();
|
||||
});
|
||||
|
||||
it("includes suggestion in error message", () => {
|
||||
const result = validateRemoveWorktreeInput("");
|
||||
expect(result).not.toBeNull();
|
||||
if (result) expect(result.suggestion).toBeDefined();
|
||||
});
|
||||
});
|
||||
|
||||
// ─── isMainRepository ────────────────────────────────────────────────
|
||||
|
||||
describe("isMainRepository (Issue #5)", () => {
|
||||
it("returns true when target path matches main repo root", () => {
|
||||
const result = isMainRepository("/home/user/project", "/home/user/project");
|
||||
expect(result).toBe(true);
|
||||
});
|
||||
|
||||
it("returns false when target path differs from main repo root", () => {
|
||||
const result = isMainRepository("/tmp/feature-x", "/home/user/project");
|
||||
expect(result).toBe(false);
|
||||
});
|
||||
|
||||
it("handles trailing slash differences in paths", () => {
|
||||
const result = isMainRepository("/home/user/project/", "/home/user/project");
|
||||
expect(result).toBe(true);
|
||||
});
|
||||
|
||||
it("returns false for relative path that resolves outside main repo", () => {
|
||||
// Use absolute baseDir to make the test deterministic regardless of process.cwd()
|
||||
const result = isMainRepository("../worktrees/feature-x", "/home/user/project", "/home/user/project");
|
||||
expect(result).toBe(false);
|
||||
});
|
||||
|
||||
it("returns true for relative path that resolves to main repo via baseDir", () => {
|
||||
const result = isMainRepository(".", "/home/user/project", "/home/user/project");
|
||||
expect(result).toBe(true);
|
||||
});
|
||||
|
||||
it("handles relative targetPath against absolute mainRepoRoot with explicit baseDir", () => {
|
||||
const result = isMainRepository(".", "/home/user/project", "/home/user/project");
|
||||
expect(result).toBe(true);
|
||||
});
|
||||
|
||||
it("handles both arguments as different relative paths with explicit baseDir", () => {
|
||||
const result = isMainRepository("worktrees/feature-x", ".", "/home/user/project");
|
||||
expect(result).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
// ─── hasUncommittedChanges ──────────────────────────────────────────
|
||||
|
||||
describe("hasUncommittedChanges (Issue #5)", () => {
|
||||
it("returns true when git status output has content", () => {
|
||||
const result = hasUncommittedChanges(" modified: file.txt\n");
|
||||
expect(result).toBe(true);
|
||||
});
|
||||
|
||||
it("returns false when git status output is empty", () => {
|
||||
const result = hasUncommittedChanges("");
|
||||
expect(result).toBe(false);
|
||||
});
|
||||
|
||||
it("returns false when git status output is whitespace only", () => {
|
||||
const result = hasUncommittedChanges(" \n ");
|
||||
expect(result).toBe(false);
|
||||
});
|
||||
|
||||
it("detects staged changes", () => {
|
||||
const result = hasUncommittedChanges("new file: added.txt\n");
|
||||
expect(result).toBe(true);
|
||||
});
|
||||
|
||||
it("detects untracked files in status output", () => {
|
||||
const result = hasUncommittedChanges("?? untracked-file.txt\n");
|
||||
expect(result).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
// ─── buildWorktreeRemoveCommand ──────────────────────────────────────
|
||||
|
||||
describe("buildWorktreeRemoveCommand (Issue #5)", () => {
|
||||
it("returns basic remove command without flags", () => {
|
||||
const result = buildWorktreeRemoveCommand("/tmp/worktrees/feature-x");
|
||||
expect(result).toEqual(["worktree", "remove", "/tmp/worktrees/feature-x"]);
|
||||
});
|
||||
|
||||
it("includes --force flag when force is true", () => {
|
||||
const result = buildWorktreeRemoveCommand("/tmp/worktrees/feature-x", { force: true });
|
||||
expect(result).toContain("--force");
|
||||
});
|
||||
|
||||
it("places path as the last argument", () => {
|
||||
const result = buildWorktreeRemoveCommand("/tmp/worktrees/feature-x", { force: true });
|
||||
expect(result[result.length - 1]).toBe("/tmp/worktrees/feature-x");
|
||||
});
|
||||
|
||||
it("builds correct command with force flag", () => {
|
||||
const result = buildWorktreeRemoveCommand("/tmp/worktrees/feature-x", { force: true });
|
||||
expect(result).toEqual(["worktree", "remove", "--force", "/tmp/worktrees/feature-x"]);
|
||||
});
|
||||
});
|
||||
|
||||
// ─── Integration: tool execute path ──────────────────────────────────
|
||||
|
||||
describe("git_worktree_remove execute path (Issue #5)", () => {
|
||||
it("returns input validation error for empty path", async () => {
|
||||
const inputError = validateRemoveWorktreeInput("");
|
||||
expect(inputError).not.toBeNull();
|
||||
expect(inputError!.message).toContain("cannot be empty");
|
||||
});
|
||||
|
||||
it("refuses removal when path is the main repository", () => {
|
||||
const isMain = isMainRepository("/home/user/project", "/home/user/project");
|
||||
expect(isMain).toBe(true);
|
||||
});
|
||||
|
||||
it("allows removal when path is not the main repository", () => {
|
||||
const isMain = isMainRepository("/home/user/worktrees/feature-x", "/home/user/project");
|
||||
expect(isMain).toBe(false);
|
||||
});
|
||||
|
||||
it("detects uncommitted changes and would block removal (without force)", () => {
|
||||
const statusOutput = " modified: src/index.ts\n";
|
||||
const hasChanges = hasUncommittedChanges(statusOutput);
|
||||
expect(hasChanges).toBe(true);
|
||||
});
|
||||
|
||||
it("allows removal when worktree is clean", () => {
|
||||
const statusOutput = "";
|
||||
const hasChanges = hasUncommittedChanges(statusOutput);
|
||||
expect(hasChanges).toBe(false);
|
||||
});
|
||||
|
||||
it("builds force command when force option is set", () => {
|
||||
const args = buildWorktreeRemoveCommand("/tmp/worktrees/feature-x", { force: true });
|
||||
expect(args).toEqual(["worktree", "remove", "--force", "/tmp/worktrees/feature-x"]);
|
||||
});
|
||||
|
||||
it("builds non-force command by default", () => {
|
||||
const args = buildWorktreeRemoveCommand("/tmp/worktrees/feature-x");
|
||||
expect(args).toEqual(["worktree", "remove", "/tmp/worktrees/feature-x"]);
|
||||
expect(args).not.toContain("--force");
|
||||
});
|
||||
});
|
||||
101
.pi/extensions/pi-worktree-remove.ts
Normal file
101
.pi/extensions/pi-worktree-remove.ts
Normal file
|
|
@ -0,0 +1,101 @@
|
|||
/**
|
||||
* Git worktree remove — pure validation and command-building functions.
|
||||
* No pi SDK dependencies for testability.
|
||||
*/
|
||||
|
||||
import path from "path";
|
||||
import type { WorktreeError } from "./pi-worktree-create";
|
||||
|
||||
// ─── Type Definitions ──────────────────────────────────────────────
|
||||
|
||||
// WorktreeError is imported from pi-worktree-create (shared error shape)
|
||||
|
||||
/** Options for removing a worktree. */
|
||||
export interface RemoveWorktreeOptions {
|
||||
/** Force removal even if the worktree has uncommitted changes. Defaults to false. */
|
||||
force?: boolean;
|
||||
}
|
||||
|
||||
// ─── Input Validation ──────────────────────────────────────────────
|
||||
|
||||
/**
|
||||
* Validate input parameters for removing a worktree.
|
||||
*
|
||||
* Checks that path is non-empty and well-formed.
|
||||
*
|
||||
* @param worktreePath - Path of the worktree to remove
|
||||
* @returns null if valid, or a WorktreeError describing the problem
|
||||
*/
|
||||
export function validateRemoveWorktreeInput(
|
||||
worktreePath: string,
|
||||
): WorktreeError | null {
|
||||
if (!worktreePath || !worktreePath.trim()) {
|
||||
return {
|
||||
message: "Path cannot be empty.",
|
||||
suggestion: "Provide a valid worktree path (e.g., '../worktrees/feature-x').",
|
||||
};
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
// ─── Main Repository Check ────────────────────────────────────────
|
||||
|
||||
/**
|
||||
* Check if the target path is the main repository root.
|
||||
*
|
||||
* Compares resolved paths to handle trailing slashes and relative paths.
|
||||
* Removing the main repo is not allowed — use `git worktree prune` instead.
|
||||
*
|
||||
* @param targetPath - Path of the worktree to remove
|
||||
* @param mainRepoRoot - Root path of the main repository
|
||||
* @param baseDir - Base directory to resolve relative paths against (defaults to process.cwd())
|
||||
* @returns true if target is the main repo (removal should be refused)
|
||||
*/
|
||||
export function isMainRepository(
|
||||
targetPath: string,
|
||||
mainRepoRoot: string,
|
||||
baseDir: string = process.cwd(),
|
||||
): boolean {
|
||||
return path.resolve(baseDir, targetPath) === path.resolve(mainRepoRoot);
|
||||
}
|
||||
|
||||
// ─── Uncommitted Changes Check ──────────────────────────────────────
|
||||
|
||||
/**
|
||||
* Check if the worktree has uncommitted changes based on `git status --porcelain` output.
|
||||
*
|
||||
* Any non-empty porcelain output indicates changes (staged, unstaged, or untracked).
|
||||
*
|
||||
* @param statusOutput - Raw stdout from `git status --porcelain` in the target worktree
|
||||
* @returns true if there are uncommitted changes
|
||||
*/
|
||||
export function hasUncommittedChanges(statusOutput: string): boolean {
|
||||
return statusOutput.trim().length > 0;
|
||||
}
|
||||
|
||||
// ─── Command Building ──────────────────────────────────────────────
|
||||
|
||||
/**
|
||||
* Build the `git worktree remove` command arguments.
|
||||
*
|
||||
* Uses `--force` flag when force option is true to bypass uncommitted changes check.
|
||||
*
|
||||
* @param worktreePath - Path of the worktree to remove
|
||||
* @param options - Optional removal configuration
|
||||
* @returns Array of command arguments (excluding 'git' itself)
|
||||
*/
|
||||
export function buildWorktreeRemoveCommand(
|
||||
worktreePath: string,
|
||||
options?: RemoveWorktreeOptions,
|
||||
): string[] {
|
||||
const args = ["worktree", "remove"];
|
||||
|
||||
if (options?.force) {
|
||||
args.push("--force");
|
||||
}
|
||||
|
||||
args.push(worktreePath);
|
||||
|
||||
return args;
|
||||
}
|
||||
|
|
@ -6,6 +6,12 @@
|
|||
|
||||
import { describe, it, expect } from "bun:test";
|
||||
import { parseWorktreeList } from "./pi-worktree-parser";
|
||||
import {
|
||||
validateCreateWorktreeInput,
|
||||
buildWorktreeAddCommand,
|
||||
validatePathDoesNotExist,
|
||||
isCurrentBranch,
|
||||
} from "./pi-worktree-create";
|
||||
|
||||
describe("parseWorktreeList", () => {
|
||||
describe("normal output", () => {
|
||||
|
|
@ -115,3 +121,324 @@ describe("parseWorktreeList", () => {
|
|||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("validateCreateWorktreeInput (Issue #4)", () => {
|
||||
it("returns null for valid path and branch", () => {
|
||||
const result = validateCreateWorktreeInput("/tmp/new-worktree", "feature-x");
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
it("returns error for empty path", () => {
|
||||
const result = validateCreateWorktreeInput("", "feature-x");
|
||||
expect(result).not.toBeNull();
|
||||
if (result) expect(result.message).toContain("cannot be empty");
|
||||
});
|
||||
|
||||
it("returns error for empty branch name", () => {
|
||||
const result = validateCreateWorktreeInput("/tmp/new-worktree", "");
|
||||
expect(result).not.toBeNull();
|
||||
if (result) expect(result.message).toContain("cannot be empty");
|
||||
});
|
||||
|
||||
it("returns error for path with only whitespace", () => {
|
||||
const result = validateCreateWorktreeInput(" ", "feature-x");
|
||||
expect(result).not.toBeNull();
|
||||
});
|
||||
|
||||
it("returns error for branch name with invalid characters (spaces)", () => {
|
||||
const result = validateCreateWorktreeInput("/tmp/new-worktree", "my bad branch");
|
||||
expect(result).not.toBeNull();
|
||||
if (result) expect(result.message).toContain("invalid characters");
|
||||
});
|
||||
|
||||
it("accepts branch names with hyphens and slashes", () => {
|
||||
const result = validateCreateWorktreeInput("/tmp/new-worktree", "feature/my-branch-name");
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
it("accepts branch names with underscores", () => {
|
||||
const result = validateCreateWorktreeInput("/tmp/new-worktree", "issue_42_fix");
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
// Task 007: Branch name start/end validation
|
||||
it("rejects branch name starting with dash", () => {
|
||||
const result = validateCreateWorktreeInput("/tmp/x", "-feature");
|
||||
expect(result).not.toBeNull();
|
||||
if (result) expect(result.message).toContain("invalid character");
|
||||
});
|
||||
|
||||
it("rejects branch name starting with slash", () => {
|
||||
const result = validateCreateWorktreeInput("/tmp/x", "/feature");
|
||||
expect(result).not.toBeNull();
|
||||
});
|
||||
|
||||
it("rejects branch name starting with tilde", () => {
|
||||
const result = validateCreateWorktreeInput("/tmp/x", "~user");
|
||||
expect(result).not.toBeNull();
|
||||
});
|
||||
|
||||
it("rejects branch name starting with caret", () => {
|
||||
const result = validateCreateWorktreeInput("/tmp/x", "^feature");
|
||||
expect(result).not.toBeNull();
|
||||
});
|
||||
|
||||
it("rejects branch name starting with dot", () => {
|
||||
const result = validateCreateWorktreeInput("/tmp/x", ".hidden");
|
||||
expect(result).not.toBeNull();
|
||||
});
|
||||
|
||||
it("rejects branch name ending with slash", () => {
|
||||
const result = validateCreateWorktreeInput("/tmp/x", "feature/");
|
||||
expect(result).not.toBeNull();
|
||||
if (result) expect(result.message).toContain("invalid character");
|
||||
});
|
||||
|
||||
it("rejects branch name ending with dash", () => {
|
||||
const result = validateCreateWorktreeInput("/tmp/x", "feature-");
|
||||
expect(result).not.toBeNull();
|
||||
});
|
||||
|
||||
it("rejects branch name ending with dot", () => {
|
||||
const result = validateCreateWorktreeInput("/tmp/x", "feature.");
|
||||
expect(result).not.toBeNull();
|
||||
});
|
||||
|
||||
// Task 008: Dangerous character validation
|
||||
it("rejects branch name with square brackets", () => {
|
||||
const result = validateCreateWorktreeInput("/tmp/x", "feature[v1]");
|
||||
expect(result).not.toBeNull();
|
||||
if (result) expect(result.message).toContain("invalid characters");
|
||||
});
|
||||
|
||||
it("rejects branch name with double quotes", () => {
|
||||
const result = validateCreateWorktreeInput("/tmp/x", 'my"branch');
|
||||
expect(result).not.toBeNull();
|
||||
});
|
||||
|
||||
it("rejects branch name with single quotes", () => {
|
||||
const result = validateCreateWorktreeInput("/tmp/x", "my'branch");
|
||||
expect(result).not.toBeNull();
|
||||
});
|
||||
|
||||
it("rejects branch name with backslash", () => {
|
||||
const result = validateCreateWorktreeInput("/tmp/x", "feature\\name");
|
||||
expect(result).not.toBeNull();
|
||||
});
|
||||
|
||||
it("rejects branch name with asterisk", () => {
|
||||
const result = validateCreateWorktreeInput("/tmp/x", "feature*name");
|
||||
expect(result).not.toBeNull();
|
||||
});
|
||||
|
||||
it("rejects branch name with question mark", () => {
|
||||
const result = validateCreateWorktreeInput("/tmp/x", "feature?name");
|
||||
expect(result).not.toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("buildWorktreeAddCommand (Issue #4)", () => {
|
||||
it("returns command with -b flag when create_branch is true", () => {
|
||||
const result = buildWorktreeAddCommand({
|
||||
path: "/tmp/new-worktree",
|
||||
branch: "feature-x",
|
||||
createBranch: true,
|
||||
});
|
||||
|
||||
expect(result).toEqual([
|
||||
"worktree",
|
||||
"add",
|
||||
"-b",
|
||||
"feature-x",
|
||||
"/tmp/new-worktree",
|
||||
]);
|
||||
});
|
||||
|
||||
it("returns command without -b flag when create_branch is false", () => {
|
||||
const result = buildWorktreeAddCommand({
|
||||
path: "/tmp/new-worktree",
|
||||
branch: "existing-branch",
|
||||
createBranch: false,
|
||||
});
|
||||
|
||||
expect(result).toEqual([
|
||||
"worktree",
|
||||
"add",
|
||||
"existing-branch",
|
||||
"/tmp/new-worktree",
|
||||
]);
|
||||
});
|
||||
|
||||
it("defaults createBranch to true when not provided", () => {
|
||||
const result = buildWorktreeAddCommand({
|
||||
path: "/tmp/new-worktree",
|
||||
branch: "new-feature",
|
||||
});
|
||||
|
||||
expect(result).toContain("-b");
|
||||
});
|
||||
|
||||
it("places path as the last argument", () => {
|
||||
const result = buildWorktreeAddCommand({
|
||||
path: "/tmp/new-worktree",
|
||||
branch: "feature-x",
|
||||
createBranch: true,
|
||||
});
|
||||
|
||||
expect(result[result.length - 1]).toBe("/tmp/new-worktree");
|
||||
});
|
||||
});
|
||||
|
||||
describe("validatePathDoesNotExist (Issue #4)", () => {
|
||||
it("returns null when path does not exist", () => {
|
||||
const result = validatePathDoesNotExist(
|
||||
"/tmp/nonexistent-path-abc123",
|
||||
(p) => p === "/existing/path",
|
||||
);
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
it("returns error with path in message when path exists", () => {
|
||||
const existingPath = "/tmp/existing-worktree";
|
||||
const result = validatePathDoesNotExist(existingPath, (p) => p === existingPath);
|
||||
|
||||
expect(result).not.toBeNull();
|
||||
if (result) expect(result.message).toContain("/tmp/existing-worktree");
|
||||
if (result) expect(result.message.toLowerCase()).toContain("already exists");
|
||||
});
|
||||
|
||||
it("suggests choosing a different location", () => {
|
||||
const existingPath = "/tmp/existing-worktree";
|
||||
const result = validatePathDoesNotExist(existingPath, (p) => p === existingPath);
|
||||
|
||||
expect(result).not.toBeNull();
|
||||
if (result) expect(result.suggestion?.toLowerCase()).toContain("different");
|
||||
});
|
||||
});
|
||||
|
||||
describe("isCurrentBranch (Issue #4)", () => {
|
||||
it("returns true when branch matches current branch", () => {
|
||||
expect(isCurrentBranch("main", "main")).toBe(true);
|
||||
});
|
||||
|
||||
it("returns false when branch differs from current branch", () => {
|
||||
expect(isCurrentBranch("feature-x", "main")).toBe(false);
|
||||
});
|
||||
|
||||
it("handles branch names with slashes", () => {
|
||||
expect(isCurrentBranch("feature/my-branch", "main")).toBe(false);
|
||||
expect(isCurrentBranch("feature/my-branch", "feature/my-branch")).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
// ─── Integration Tests for git_worktree_create execute flow (Task 004) ────
|
||||
|
||||
describe("git_worktree_create execute flow (Issue #4)", () => {
|
||||
// Minimal mock types matching what the extension needs
|
||||
interface MockExecResult {
|
||||
exitCode: number;
|
||||
stdout: string;
|
||||
stderr: string;
|
||||
}
|
||||
|
||||
interface MockContext {
|
||||
cwd: string;
|
||||
}
|
||||
|
||||
interface MockPI {
|
||||
exec: (cmd: string, args: string[], opts?: { cwd: string }) => Promise<MockExecResult>;
|
||||
}
|
||||
|
||||
describe("validation pipeline ordering", () => {
|
||||
it("rejects empty path before checking filesystem", async () => {
|
||||
const result = validateCreateWorktreeInput("", "feature-x");
|
||||
expect(result).not.toBeNull();
|
||||
expect(result?.message).toContain("cannot be empty");
|
||||
});
|
||||
|
||||
it("rejects invalid branch name before checking filesystem", async () => {
|
||||
const result = validateCreateWorktreeInput("/tmp/x", "-bad-name");
|
||||
expect(result).not.toBeNull();
|
||||
expect(result?.message).toContain("invalid character");
|
||||
});
|
||||
});
|
||||
|
||||
describe("path existence check", () => {
|
||||
it("returns error when path exists", () => {
|
||||
const existingPath = "/tmp/existing-worktree";
|
||||
const result = validatePathDoesNotExist(existingPath, (p) => p === existingPath);
|
||||
expect(result).not.toBeNull();
|
||||
expect(result?.message).toContain("/tmp/existing-worktree");
|
||||
expect(result?.suggestion).toContain("different");
|
||||
});
|
||||
|
||||
it("returns null when path does not exist", () => {
|
||||
const result = validatePathDoesNotExist(
|
||||
"/tmp/nonexistent-path-abc123",
|
||||
(p) => p === "/existing/path",
|
||||
);
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("current branch check", () => {
|
||||
it("detects when target matches current branch", () => {
|
||||
expect(isCurrentBranch("main", "main")).toBe(true);
|
||||
});
|
||||
|
||||
it("allows different branch from current", () => {
|
||||
expect(isCurrentBranch("feature-x", "main")).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("command building after all validations pass", () => {
|
||||
it("builds correct command with createBranch=true", () => {
|
||||
const args = buildWorktreeAddCommand({
|
||||
path: "/tmp/new-worktree",
|
||||
branch: "feature-x",
|
||||
createBranch: true,
|
||||
});
|
||||
expect(args).toEqual(["worktree", "add", "-b", "feature-x", "/tmp/new-worktree"]);
|
||||
});
|
||||
|
||||
it("builds correct command with createBranch=false", () => {
|
||||
const args = buildWorktreeAddCommand({
|
||||
path: "/tmp/new-worktree",
|
||||
branch: "existing-branch",
|
||||
createBranch: false,
|
||||
});
|
||||
expect(args).toEqual(["worktree", "add", "existing-branch", "/tmp/new-worktree"]);
|
||||
});
|
||||
|
||||
it("defaults createBranch to true when omitted", () => {
|
||||
const args = buildWorktreeAddCommand({
|
||||
path: "/tmp/new-worktree",
|
||||
branch: "new-feature",
|
||||
});
|
||||
expect(args).toContain("-b");
|
||||
});
|
||||
});
|
||||
|
||||
describe("error formatting consistency", () => {
|
||||
it("formats input validation error with suggestion", () => {
|
||||
const result = validateCreateWorktreeInput("", "feature-x");
|
||||
expect(result).not.toBeNull();
|
||||
expect(result?.suggestion).toBeDefined();
|
||||
expect(result?.suggestion).toContain("Provide a valid path");
|
||||
});
|
||||
|
||||
it("formats path exists error with suggestion", () => {
|
||||
const result = validatePathDoesNotExist("/tmp/x", () => true);
|
||||
expect(result).not.toBeNull();
|
||||
expect(result?.suggestion).toBeDefined();
|
||||
expect(result?.suggestion).toContain("different");
|
||||
});
|
||||
|
||||
it("formats branch name error with suggestion", () => {
|
||||
const result = validateCreateWorktreeInput("/tmp/x", "bad name here");
|
||||
expect(result).not.toBeNull();
|
||||
expect(result?.suggestion).toBeDefined();
|
||||
expect(result?.suggestion).toContain("hyphens");
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -14,9 +14,23 @@
|
|||
*/
|
||||
|
||||
import type { ExtensionAPI, ExtensionContext } from "@earendil-works/pi-coding-agent";
|
||||
import { stat } from "node:fs/promises";
|
||||
import { defineTool } from "@earendil-works/pi-coding-agent";
|
||||
import { Type } from "@earendil-works/pi-ai";
|
||||
import { parseWorktreeList, type WorktreeEntry } from "./pi-worktree-parser";
|
||||
import {
|
||||
validateCreateWorktreeInput,
|
||||
buildWorktreeAddCommand,
|
||||
validatePathDoesNotExist,
|
||||
isCurrentBranch,
|
||||
type WorktreeError,
|
||||
} from "./pi-worktree-create";
|
||||
import {
|
||||
validateRemoveWorktreeInput,
|
||||
isMainRepository,
|
||||
hasUncommittedChanges,
|
||||
buildWorktreeRemoveCommand,
|
||||
} from "./pi-worktree-remove";
|
||||
|
||||
// ─── Type Definitions ──────────────────────────────────────────────
|
||||
|
||||
|
|
@ -30,14 +44,6 @@ interface WorktreeListResult {
|
|||
currentPath: string;
|
||||
}
|
||||
|
||||
/** Error returned when a worktree operation fails. */
|
||||
interface WorktreeError {
|
||||
/** Human-readable error message from git or validation. */
|
||||
message: string;
|
||||
/** Suggested fix for the user. */
|
||||
suggestion?: string;
|
||||
}
|
||||
|
||||
// ─── Tool Parameter Schemas ────────────────────────────────────────
|
||||
|
||||
const createWorktreeParams = Type.Object({
|
||||
|
|
@ -45,7 +51,7 @@ const createWorktreeParams = Type.Object({
|
|||
branch: Type.Optional(Type.String({ description: "Branch name to check out. Defaults to 'main'." })),
|
||||
create_branch: Type.Optional(
|
||||
Type.Boolean({
|
||||
description: "If true and branch doesn't exist, create it. Defaults to false.",
|
||||
description: "If true and branch doesn't exist, create it. Defaults to true.",
|
||||
}),
|
||||
),
|
||||
});
|
||||
|
|
@ -58,28 +64,80 @@ const switchWorktreeParams = Type.Object({
|
|||
|
||||
const removeWorktreeParams = Type.Object({
|
||||
path: Type.String({ description: "Path of the worktree to remove." }),
|
||||
force: Type.Optional(Type.Boolean({ description: "Force removal even with uncommitted changes. Defaults to false." })),
|
||||
});
|
||||
|
||||
// ─── Helper Functions (stubs) ──────────────────────────────────────
|
||||
// ─── Helper Functions ──────────────────────────────────────────────
|
||||
|
||||
/**
|
||||
* Check if the target path exists.
|
||||
* @param checkPath - Path to check
|
||||
* @param ctx - Extension context for file system access
|
||||
* Check if the target path exists using Bun's fs module.
|
||||
* @param checkPath - Path to check (resolved against cwd)
|
||||
* @param ctx - Extension context for working directory
|
||||
* @returns True if the path exists
|
||||
*/
|
||||
async function pathExists(checkPath: string, ctx: ExtensionContext): Promise<boolean> {
|
||||
throw new Error("not implemented");
|
||||
const resolvedPath = checkPath.startsWith("/") ? checkPath : `${ctx.cwd}/${checkPath}`;
|
||||
|
||||
try {
|
||||
await stat(resolvedPath);
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current branch name from git.
|
||||
* @param pi - Extension API for executing commands
|
||||
* @param ctx - Extension context for working directory
|
||||
* @returns Current branch name, or "unknown" if detection fails
|
||||
*/
|
||||
async function getCurrentBranch(pi: ExtensionAPI, ctx: ExtensionContext): Promise<string> {
|
||||
const result = await pi.exec("git", ["rev-parse", "--abbrev-ref", "HEAD"], { cwd: ctx.cwd });
|
||||
|
||||
if (result.exitCode !== 0) {
|
||||
return "unknown";
|
||||
}
|
||||
|
||||
return result.stdout.trim();
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve a worktree target (path or branch name) to its full path.
|
||||
*
|
||||
* Used by git_worktree_switch — dead code until that tool ships.
|
||||
*
|
||||
* @param target - Path or branch name to resolve
|
||||
* @param worktrees - List of all worktrees
|
||||
* @returns The resolved path, or undefined if not found
|
||||
*/
|
||||
function resolveWorktreeTarget(target: string, worktrees: WorktreeEntry[]): string | undefined {
|
||||
throw new Error("not implemented");
|
||||
const exactMatch = worktrees.find((w) => w.path === target);
|
||||
if (exactMatch) return exactMatch.path;
|
||||
|
||||
const branchMatch = worktrees.find((w) => w.branch === target);
|
||||
return branchMatch?.path;
|
||||
}
|
||||
|
||||
/**
|
||||
* Format a WorktreeError into the tool response text.
|
||||
* @param error - The validation error
|
||||
* @returns Formatted error string with suggestion if available
|
||||
*/
|
||||
function formatValidationError(error: WorktreeError): string {
|
||||
const base = `Error: ${error.message}`;
|
||||
return error.suggestion ? `${base} Suggestion: ${error.suggestion}` : base;
|
||||
}
|
||||
|
||||
/**
|
||||
* Format a success response for worktree operations.
|
||||
* @param action - The action performed (e.g., "Created")
|
||||
* @param details - Key-value pairs to include in the message
|
||||
* @returns Formatted success string
|
||||
*/
|
||||
function formatSuccessResponse(action: string, details: Record<string, string>): string {
|
||||
const parts = Object.entries(details).map(([key, value]) => `${key} '${value}'`);
|
||||
return `${action} worktree ${parts.join(" on ")}.`;
|
||||
}
|
||||
|
||||
// ─── Tool Factories ────────────────────────────────────────────────
|
||||
|
|
@ -137,20 +195,77 @@ function createListWorktreeTool(pi: ExtensionAPI) {
|
|||
|
||||
/**
|
||||
* Create the git_worktree_create tool definition.
|
||||
* @param _pi - Extension API (unused in stub)
|
||||
* @param pi - Extension API for registering and executing tools
|
||||
* @returns The configured tool definition
|
||||
*/
|
||||
function createCreateWorktreeTool(_pi: ExtensionAPI) {
|
||||
function createCreateWorktreeTool(pi: ExtensionAPI) {
|
||||
return defineTool({
|
||||
name: "git_worktree_create",
|
||||
label: "Git Worktree Create",
|
||||
description: "Create a linked worktree for a branch in the current repo.",
|
||||
parameters: createWorktreeParams,
|
||||
async execute(_toolCallId, _params, _signal, _onUpdate, _ctx) {
|
||||
async execute(_toolCallId, params, _signal, _onUpdate, ctx) {
|
||||
const worktreePath = params.path;
|
||||
const branchName = params.branch || "main";
|
||||
const createBranch = params.create_branch ?? true;
|
||||
|
||||
// 1. Validate input parameters
|
||||
const inputError = validateCreateWorktreeInput(worktreePath, branchName);
|
||||
if (inputError) {
|
||||
return {
|
||||
content: [{ type: "text", text: "Error: git_worktree_create is not yet implemented. Suggestion: Use `git worktree add` manually until this tool is complete." }],
|
||||
content: [{ type: "text", text: formatValidationError(inputError) }],
|
||||
isError: true,
|
||||
};
|
||||
}
|
||||
|
||||
// 2. Check if path already exists — refuse if so
|
||||
const pathExistsResult = await pathExists(worktreePath, ctx);
|
||||
const pathError = validatePathDoesNotExist(worktreePath, () => pathExistsResult);
|
||||
if (pathError) {
|
||||
return {
|
||||
content: [{ type: "text", text: formatValidationError(pathError) }],
|
||||
isError: true,
|
||||
};
|
||||
}
|
||||
|
||||
// 3. Check if creating on current branch — refuse if so
|
||||
const currentBranch = await getCurrentBranch(pi, ctx);
|
||||
if (isCurrentBranch(branchName, currentBranch)) {
|
||||
const currentBranchError: WorktreeError = {
|
||||
message: "Can't create worktree on the current branch — switch first.",
|
||||
suggestion: "Choose a different branch name or switch branches with 'git switch'.",
|
||||
};
|
||||
return {
|
||||
content: [{ type: "text", text: formatValidationError(currentBranchError) }],
|
||||
isError: true,
|
||||
};
|
||||
}
|
||||
|
||||
// 4. Build and run git worktree add command
|
||||
const commandArgs = buildWorktreeAddCommand({
|
||||
path: worktreePath,
|
||||
branch: branchName,
|
||||
createBranch,
|
||||
});
|
||||
|
||||
const result = await pi.exec("git", commandArgs, { cwd: ctx.cwd });
|
||||
|
||||
if (result.exitCode !== 0) {
|
||||
return {
|
||||
content: [{
|
||||
type: "text",
|
||||
text: `Error creating worktree: ${result.stderr}. Suggestion: Check that the branch exists (or use create_branch=true) and that the parent directory is writable.`,
|
||||
}],
|
||||
isError: true,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
content: [{
|
||||
type: "text",
|
||||
text: formatSuccessResponse("Created", { at: worktreePath, branch: branchName }),
|
||||
}],
|
||||
};
|
||||
},
|
||||
});
|
||||
}
|
||||
|
|
@ -177,20 +292,99 @@ function createSwitchWorktreeTool(_pi: ExtensionAPI) {
|
|||
|
||||
/**
|
||||
* Create the git_worktree_remove tool definition.
|
||||
* @param _pi - Extension API (unused in stub)
|
||||
* @param pi - Extension API for executing commands
|
||||
* @returns The configured tool definition
|
||||
*/
|
||||
function createRemoveWorktreeTool(_pi: ExtensionAPI) {
|
||||
function createRemoveWorktreeTool(pi: ExtensionAPI) {
|
||||
return defineTool({
|
||||
name: "git_worktree_remove",
|
||||
label: "Git Worktree Remove",
|
||||
description: "Remove a linked worktree.",
|
||||
parameters: removeWorktreeParams,
|
||||
async execute(_toolCallId, _params, _signal, _onUpdate, _ctx) {
|
||||
async execute(_toolCallId, params, _signal, onUpdate, ctx) {
|
||||
const worktreePath = params.path;
|
||||
const force = params.force ?? false;
|
||||
|
||||
// 1. Validate input parameters
|
||||
const inputError = validateRemoveWorktreeInput(worktreePath);
|
||||
if (inputError) {
|
||||
return {
|
||||
content: [{ type: "text", text: "Error: git_worktree_remove is not yet implemented. Suggestion: Use `git worktree remove` manually until this tool is complete." }],
|
||||
content: [{ type: "text", text: formatValidationError(inputError) }],
|
||||
isError: true,
|
||||
};
|
||||
}
|
||||
|
||||
// 1b. Check that the worktree path exists
|
||||
const exists = await pathExists(worktreePath, ctx);
|
||||
if (!exists) {
|
||||
return {
|
||||
content: [{ type: "text", text: `Error: Worktree path '${worktreePath}' does not exist. Suggestion: List worktrees with git_worktree_list to find valid paths.` }],
|
||||
isError: true,
|
||||
};
|
||||
}
|
||||
|
||||
// 2. Get main repo root and check if target is the main repo
|
||||
const toplevelResult = await pi.exec("git", ["rev-parse", "--show-toplevel"], { cwd: ctx.cwd });
|
||||
if (toplevelResult.exitCode !== 0) {
|
||||
return {
|
||||
content: [{ type: "text", text: `Error getting repository root: ${toplevelResult.stderr}. Suggestion: Ensure you are inside a git repository.` }],
|
||||
isError: true,
|
||||
};
|
||||
}
|
||||
|
||||
const mainRepoRoot = toplevelResult.stdout.trim();
|
||||
if (isMainRepository(worktreePath, mainRepoRoot, ctx.cwd)) {
|
||||
return {
|
||||
content: [{ type: "text", text: `Error: Can't remove the main repository. Suggestion: Use 'git worktree prune' instead.` }],
|
||||
isError: true,
|
||||
};
|
||||
}
|
||||
|
||||
// 3. Check for uncommitted changes in the target worktree
|
||||
const statusResult = await pi.exec("git", ["status", "--porcelain"], { cwd: worktreePath });
|
||||
if (statusResult.exitCode !== 0) {
|
||||
return {
|
||||
content: [{ type: "text", text: `Error checking worktree status: ${statusResult.stderr}. Suggestion: Verify the path exists and is a valid worktree.` }],
|
||||
isError: true,
|
||||
};
|
||||
}
|
||||
if (!force && hasUncommittedChanges(statusResult.stdout)) {
|
||||
return {
|
||||
content: [{ type: "text", text: `Error: Worktree '${worktreePath}' has uncommitted changes. Suggestion: Stash or commit them first, or use force=true to remove anyway.` }],
|
||||
isError: true,
|
||||
};
|
||||
}
|
||||
if (force && hasUncommittedChanges(statusResult.stdout)) {
|
||||
onUpdate?.({ type: "text", text: `Warning: Force removing worktree '${worktreePath}' despite uncommitted changes.` });
|
||||
}
|
||||
|
||||
// 3b. Get the branch name before removal for the success response
|
||||
const branchResult = await pi.exec("git", ["-C", worktreePath, "rev-parse", "--abbrev-ref", "HEAD"]);
|
||||
const branchName = branchResult.exitCode === 0 ? branchResult.stdout.trim() : "unknown";
|
||||
|
||||
// 4. Build and run git worktree remove command
|
||||
const commandArgs = buildWorktreeRemoveCommand(worktreePath, { force });
|
||||
const result = await pi.exec("git", commandArgs, { cwd: ctx.cwd });
|
||||
|
||||
if (result.exitCode !== 0) {
|
||||
return {
|
||||
content: [{ type: "text", text: `Error removing worktree: ${result.stderr}. Suggestion: Check that the path is a valid worktree, or use force=true if the worktree has uncommitted changes.` }],
|
||||
isError: true,
|
||||
};
|
||||
}
|
||||
|
||||
// 5. List remaining worktrees for context
|
||||
const listResult = await pi.exec("git", ["worktree", "list"], { cwd: ctx.cwd });
|
||||
const remainingWorktrees = listResult.exitCode === 0 ? parseWorktreeList(listResult.stdout, ctx.cwd) : [];
|
||||
|
||||
return {
|
||||
content: [{ type: "text", text: formatSuccessResponse("Removed", { at: worktreePath, branch: branchName }) }],
|
||||
details: {
|
||||
removed: { path: worktreePath, branch: branchName },
|
||||
remaining: remainingWorktrees,
|
||||
remainingCount: remainingWorktrees.length,
|
||||
},
|
||||
};
|
||||
},
|
||||
});
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue