also looking at this
fix: support linked Git worktrees
#551
4 files
+122
-13
| @@ -1,13 +1,35 @@ | ||
| 1 | import { execFileSync } from 'node:child_process' | |
| 1 | 2 | import fs from 'node:fs' |
| 2 | 3 | import path from 'node:path' |
| 3 | 4 | import process from 'node:process' |
| 4 | 5 | |
| 6 | /** Resolve a path below `.git` through Git so linked worktrees are supported. */ | |
| 7 | export function resolveCommitMessagePath(filePath: string, cwd: string = process.cwd()): string { | |
| 8 | const absolutePath = path.resolve(cwd, filePath) | |
| 9 | const relativePath = path.relative(cwd, absolutePath).split(path.sep).join('/') | |
| 10 | if (relativePath !== '.git' && !relativePath.startsWith('.git/')) return absolutePath | |
| 11 | ||
| 12 | const gitPath = relativePath === '.git' ? '' : relativePath.slice('.git/'.length) | |
| 13 | if (!gitPath) return absolutePath | |
| 14 | ||
| 15 | try { | |
| 16 | const resolved = execFileSync('git', ['rev-parse', '--git-path', gitPath], { | |
| 17 | cwd, | |
| 18 | encoding: 'utf8', | |
| 19 | }).trim() | |
| 20 | return path.resolve(cwd, resolved) | |
| 21 | } | |
| 22 | catch { | |
| 23 | return absolutePath | |
| 24 | } | |
| 25 | } | |
| 26 | ||
| 5 | 27 | /** |
| 6 | 28 | * Read the commit message from a file |
| 7 | 29 | */ |
| 8 | export function readCommitMessageFromFile(filePath: string): string { | |
| 30 | export function readCommitMessageFromFile(filePath: string, cwd: string = process.cwd()): string { | |
| 9 | 31 | try { |
| 10 | return fs.readFileSync(path.resolve(process.cwd(), filePath), 'utf8') | |
| 32 | return fs.readFileSync(resolveCommitMessagePath(filePath, cwd), 'utf8') | |
| 11 | 33 | } |
| 12 | 34 | catch (error: unknown) { |
| 13 | 35 | console.error(`Error reading commit message file: ${filePath}`) |