From ca5accc20741b785b606494319c1ea4407634662 Mon Sep 17 00:00:00 2001 From: shantanuveve Date: Sat, 26 Oct 2024 16:19:56 +0530 Subject: [PATCH] fix(release): support workspace root to be subdirectory of git root closed #27995 --- .../nx/src/command-line/release/utils/git.ts | 27 ++++++++++++++++--- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/packages/nx/src/command-line/release/utils/git.ts b/packages/nx/src/command-line/release/utils/git.ts index 6f1b0d90ba373..6067856709908 100644 --- a/packages/nx/src/command-line/release/utils/git.ts +++ b/packages/nx/src/command-line/release/utils/git.ts @@ -2,6 +2,7 @@ * Special thanks to changelogen for the original inspiration for many of these utilities: * https://github.com/unjs/changelogen */ +import { relative } from 'path'; import { interpolate } from '../../../tasks-runner/utils'; import { workspaceRoot } from '../../../utils/workspace-root'; import { execCommand } from './exec-command'; @@ -124,15 +125,20 @@ export async function getGitDiff( // Use a unique enough separator that we can be relatively certain will not occur within the commit message itself const separator = 'ยงยงยง'; - - // https://git-scm.com/docs/pretty-formats - const r = await execCommand('git', [ + const args = [ '--no-pager', 'log', range, `--pretty="----%n%s${separator}%h${separator}%an${separator}%ae%n%b"`, '--name-status', - ]); + ]; + const relativePath = await getWorkspaceRelativePath(); + if (relativePath) { + args.push(`--relative=${relativePath}`); + } + + // https://git-scm.com/docs/pretty-formats + const r = await execCommand('git', args); return r .split('----\n') @@ -558,3 +564,16 @@ export async function getFirstGitCommit() { throw new Error(`Unable to find first commit in git history`); } } + +export async function getGitRoot() { + try { + return (await execCommand('git', ['rev-parse', '--show-toplevel'])).trim(); + } catch (e) { + throw new Error('Unable to find git root'); + } +} + +async function getWorkspaceRelativePath() { + const gitRoot = await getGitRoot(); + return relative(gitRoot, workspaceRoot); +}