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

[feature] add support for git worktrees #747

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
29 changes: 26 additions & 3 deletions src/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -240,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,
Expand Down Expand Up @@ -289,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
Expand Down