Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for git worktrees #746

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions src/git.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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' });
Expand All @@ -225,22 +226,22 @@ 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 {
debug('getRoot');

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,
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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);
Expand Down