fix(release): support workspace root as a subdirectory of git root (#28650)

This commit is contained in:
Shantanu Jain 2024-11-11 19:04:49 +05:30 committed by GitHub
parent b0a4291f05
commit d2c1067a08
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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 'node:path';
import { interpolate } from '../../../tasks-runner/utils';
import { workspaceRoot } from '../../../utils/workspace-root';
import { execCommand } from './exec-command';
@ -124,15 +125,21 @@ 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',
]);
];
// Support cases where the nx workspace root is located at a nested path within the git repo
const relativePath = await getGitRootRelativePath();
if (relativePath) {
args.push(`--relative=${relativePath}`);
}
const r = await execCommand('git', args);
return r
.split('----\n')
@ -558,3 +565,20 @@ export async function getFirstGitCommit() {
throw new Error(`Unable to find first commit in git history`);
}
}
async function getGitRoot() {
try {
return (await execCommand('git', ['rev-parse', '--show-toplevel'])).trim();
} catch (e) {
throw new Error('Unable to find git root');
}
}
let gitRootRelativePath: string;
async function getGitRootRelativePath() {
if (!gitRootRelativePath) {
const gitRoot = await getGitRoot();
gitRootRelativePath = relative(gitRoot, workspaceRoot);
}
return gitRootRelativePath;
}