Skip to content

Commit

Permalink
fix(release): support workspace root as a subdirectory of git root (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
shantanu1227 authored Nov 11, 2024
1 parent b0a4291 commit d2c1067
Showing 1 changed file with 27 additions and 3 deletions.
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 = [
'--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;
}

0 comments on commit d2c1067

Please sign in to comment.