@akiojin/claude-worktree
Version:
Interactive Git worktree manager for Claude Code with graphical branch selection
52 lines • 1.92 kB
JavaScript
import path from 'node:path';
/**
* Worktree操作のビジネスロジックを管理するService
*/
export class WorktreeService {
worktreeRepository;
gitRepository;
constructor(worktreeRepository, gitRepository) {
this.worktreeRepository = worktreeRepository;
this.gitRepository = gitRepository;
}
async listAdditionalWorktrees() {
const [allWorktrees, repoRoot] = await Promise.all([
this.listAllWorktrees(),
this.gitRepository.getRepositoryRoot()
]);
// メインworktree(リポジトリルート)を除外
return allWorktrees.filter(w => w.path !== repoRoot);
}
async listAllWorktrees() {
const worktrees = await this.worktreeRepository.list();
return worktrees.map(w => ({
path: w.path,
branch: w.branch,
head: w.head
}));
}
async createWorktree(config) {
await this.worktreeRepository.add(config.worktreePath, config.branchName);
}
async removeWorktree(worktreePath, force = false) {
await this.worktreeRepository.remove(worktreePath, force);
}
async getWorktreeByBranch(branchName) {
const worktrees = await this.listAllWorktrees();
return worktrees.find(w => w.branch === branchName);
}
async getRecommendedWorktreePath(branchName) {
const repoRoot = await this.gitRepository.getRepositoryRoot();
const repoName = path.basename(repoRoot);
const parentDir = path.dirname(repoRoot);
// ブランチ名からworktreeパスを生成
const safeBranchName = branchName
.replace(/\//g, '-')
.replace(/[^a-zA-Z0-9-_]/g, '');
return path.join(parentDir, `${repoName}-${safeBranchName}`);
}
async prune() {
await this.worktreeRepository.prune();
}
}
//# sourceMappingURL=worktree.service.js.map