diff --git a/CHANGELOG.md b/CHANGELOG.md index bc1f404..1ad0081 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +## [1.3.2](https://github.com/philstainer/git-worktree/compare/v1.3.1...v1.3.2) (2025-08-04) + +### Code Refactoring + +- **getWorktrees:** update command and improve worktree parsing ([ad48171](https://github.com/philstainer/git-worktree/commit/ad481714de5697af4aa46eeccf665505ec71da8a)) + ## [1.3.1](https://github.com/philstainer/git-worktree/compare/v1.3.0...v1.3.1) (2025-04-04) ### Continuous Integration diff --git a/package.json b/package.json index b75a630..24074be 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "git-worktree", "displayName": "Git Worktree", "description": "Manage git worktrees with ease", - "version": "1.3.1", + "version": "1.3.2", "publisher": "philstainer", "homepage": "https://github.com/philstainer/git-worktree/blob/main/README.md", "bugs": { diff --git a/src/helpers/worktree/getWorktrees.ts b/src/helpers/worktree/getWorktrees.ts index 56e8d22..9b25763 100644 --- a/src/helpers/worktree/getWorktrees.ts +++ b/src/helpers/worktree/getWorktrees.ts @@ -1,13 +1,11 @@ import { executeCommand } from '#/helpers/general'; -import { BARE_REPOSITORY } from '#/src/config/constants'; import { getCurrentBranchName, isInsideBareRepository } from '../git'; -import { removeFirstAndLastCharacter } from '../string'; export const getWorktrees = async ( withBareRepo = false, showCurrentWorktree = false ) => { - const command = 'git worktree list'; + const command = 'git worktree list --porcelain'; try { const { stdout } = await executeCommand(command); @@ -34,15 +32,29 @@ const getFilteredWorktrees = async ( const currentWorktree = await getCurrentBranchName(); let splitWorktrees = stdout - .split('\n') - .filter((str) => str !== '') - .map((str) => { - const [path, hash, worktree] = str.split(' ').filter((str) => str !== ''); + .trim() + .split('\n\n') + .map((path): { path: string; hash: string; worktree: string } => { + let worktree: string | null = null; + let commit: string | null = null; + let branch: string | null = null; + + path.split('\n').forEach((line) => { + if (line.startsWith('worktree ')) { + worktree = line.slice(9); + } else if (line.startsWith('HEAD ')) { + commit = line.slice(5, 12); // Short commit hash + } else if (line.startsWith('branch refs/heads/')) { + branch = line.slice(18); + } + }); + + if (!worktree) throw new Error('Missing worktree!'); return { - path, - hash: worktree ? hash : '', - worktree: removeFirstAndLastCharacter(worktree ? worktree : hash), + path: worktree ?? '', + hash: commit ?? '', + worktree: branch ?? commit ?? '', }; }); @@ -52,8 +64,9 @@ const getFilteredWorktrees = async ( ); if (!includeBare) + // Filter out bare worktree and worktrees that are not in the bare directory e.g have been manually moved splitWorktrees = splitWorktrees.filter( - ({ worktree }) => worktree !== BARE_REPOSITORY + ({ path }) => !path.endsWith('.bare') ); return splitWorktrees;