From c8618319fc5265bdb8cefae50040bb462e44c27f Mon Sep 17 00:00:00 2001 From: Kevin Date: Thu, 30 May 2024 12:07:45 -0400 Subject: [PATCH] Add support for git worktrees * Fix resolution of git root directory to support worktree folder structures * Use the resolved gitRoot path for commitmsg parsing and modification --- src/git.ts | 24 +++++++++++++----------- src/index.ts | 4 ++-- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/src/git.ts b/src/git.ts index 19dc063..b1d5ade 100644 --- a/src/git.ts +++ b/src/git.ts @@ -1,6 +1,6 @@ +import * as cp from 'child_process'; import * as fs from 'fs'; import * as path from 'path'; -import * as cp from 'child_process'; import { JPCMConfig } from './config'; import { debug, log } from './log'; @@ -203,20 +203,21 @@ function insertJiraTicketIntoMessage(messageInfo: MessageInfo, jiraTicket: strin export type GitRevParseResult = { prefix: string; gitCommonDir: string; + gitDir: string; }; export function gitRevParse(cwd = process.cwd(), gitRoot = ''): GitRevParseResult { const args = []; + args.push('rev-parse', '--show-prefix', '--git-common-dir', '--git-dir'); + // If git root is specified, checking existing work tree if (gitRoot !== '' && gitRoot !== '.') { log(`Git root is specified as ${gitRoot}`); - - args.push('--git-dir', gitRoot); + // add configured gitRoot as git-dir + args.push(gitRoot); } - args.push('rev-parse', '--show-prefix', '--git-common-dir'); - // https://github.com/typicode/husky/issues/580 // https://github.com/typicode/husky/issues/587 const { status, stderr, stdout } = cp.spawnSync('git', args, { cwd, encoding: 'utf-8' }); @@ -225,14 +226,14 @@ export function gitRevParse(cwd = process.cwd(), gitRoot = ''): GitRevParseResul throw new Error(stderr.toString()); } - const [prefix, gitCommonDir] = stdout + const [prefix, gitCommonDir, gitDir] = stdout .toString() .split('\n') .map((s) => s.trim()) // Normalize for Windows .map((s) => s.replace(/\\\\/, '/')); - return { prefix, gitCommonDir }; + return { prefix, gitCommonDir, gitDir }; } export function getRoot(gitRoot: string): string { @@ -240,7 +241,7 @@ export function getRoot(gitRoot: string): string { const cwd = process.cwd(); - const { gitCommonDir } = gitRevParse(cwd, gitRoot); + const { gitCommonDir, gitDir } = gitRevParse(cwd, gitRoot); // Git rev-parse returns unknown options as is. // If we get --absolute-git-dir in the output, @@ -251,7 +252,8 @@ export function getRoot(gitRoot: string): string { throw new Error('Husky requires Git >= 2.13.0, please upgrade Git'); } - return path.resolve(cwd, gitCommonDir); + // If gitDir is not present, use common + return path.resolve(cwd, gitDir ?? gitCommonDir); } export function getBranchName(gitRoot: string): string { @@ -286,10 +288,10 @@ export function getJiraTicket(branchName: string, config: JPCMConfig): string | return jiraTicket ? jiraTicket.toUpperCase() : null; } -export function writeJiraTicket(jiraTicket: string, config: JPCMConfig): void { +export function writeJiraTicket(jiraTicket: string, gitRoot: string, config: JPCMConfig): void { debug('writeJiraTicket'); - const messageFilePath = getMsgFilePath(config.gitRoot); + const messageFilePath = getMsgFilePath(gitRoot); let message; // Read file with commit message diff --git a/src/index.ts b/src/index.ts index ec1200c..bd1e032 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,7 +1,7 @@ #!/usr/bin/env node -import * as git from './git'; import { loadConfig } from './config'; +import * as git from './git'; import { error, log } from './log'; // eslint-disable-next-line @typescript-eslint/no-floating-promises @@ -34,7 +34,7 @@ import { error, log } from './log'; log(`The JIRA ticket ID is: ${ticket}`); - git.writeJiraTicket(ticket, config); + git.writeJiraTicket(ticket, gitRoot, config); } catch (err: unknown) { if (typeof err === 'string') { error(err);