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

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

Merged
Merged
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
30 changes: 27 additions & 3 deletions packages/nx/src/command-line/release/utils/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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 = [
JamesHenry marked this conversation as resolved.
Show resolved Hide resolved
'--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')
Expand Down Expand Up @@ -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;
}