From ebb0652634025cc48cff3cc2bda8ed40337833f4 Mon Sep 17 00:00:00 2001 From: Konstantin Axt Date: Thu, 20 Jun 2024 17:05:35 +0200 Subject: [PATCH 1/2] [feature] add support for git worktrees --- src/git.ts | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/git.ts b/src/git.ts index 19dc063..b083c82 100644 --- a/src/git.ts +++ b/src/git.ts @@ -200,6 +200,23 @@ function insertJiraTicketIntoMessage(messageInfo: MessageInfo, jiraTicket: strin return lines.join('\n'); } +function isInsideWorktree(gitRoot: string) { + debug('isInsideWorktree'); + + const cwd = process.cwd(); + const args = []; + + args.push('-C', gitRoot, 'rev-parse', '--is-inside-work-tree'); + + const { status, stderr, stdout } = cp.spawnSync('git', args, { cwd, encoding: 'utf-8' }); + + if (status !== 0) { + throw new Error(stderr.toString()); + } + + return stdout.toString().trim() === 'true'; +} + export type GitRevParseResult = { prefix: string; gitCommonDir: string; @@ -215,7 +232,9 @@ export function gitRevParse(cwd = process.cwd(), gitRoot = ''): GitRevParseResul args.push('--git-dir', gitRoot); } - args.push('rev-parse', '--show-prefix', '--git-common-dir'); + const dirArgument = isInsideWorktree(gitRoot) ? '--git-dir' : '--git-common-dir'; + + args.push('rev-parse', '--show-prefix', dirArgument); // https://github.com/typicode/husky/issues/580 // https://github.com/typicode/husky/issues/587 From b443af6c0700acd06cc20f66b0c163543c38c728 Mon Sep 17 00:00:00 2001 From: Konstantin Axt Date: Fri, 21 Jun 2024 09:38:21 +0200 Subject: [PATCH 2/2] fix: gitRoot config should be set as default if git directory is a worktree --- src/git.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/git.ts b/src/git.ts index b083c82..f10c7f3 100644 --- a/src/git.ts +++ b/src/git.ts @@ -259,7 +259,11 @@ export function getRoot(gitRoot: string): string { const cwd = process.cwd(); - const { gitCommonDir } = gitRevParse(cwd, gitRoot); + const isWorktree = isInsideWorktree(gitRoot); + + const gitRootDir = isWorktree ? gitRoot : ''; + + const { gitCommonDir } = gitRevParse(cwd, gitRootDir); // Git rev-parse returns unknown options as is. // If we get --absolute-git-dir in the output, @@ -308,7 +312,7 @@ export function getJiraTicket(branchName: string, config: JPCMConfig): string | export function writeJiraTicket(jiraTicket: string, config: JPCMConfig): void { debug('writeJiraTicket'); - const messageFilePath = getMsgFilePath(config.gitRoot); + const messageFilePath = getMsgFilePath(getRoot(config.gitRoot)); let message; // Read file with commit message