Skip to content

Commit 3689c88

Browse files
authored
fix(release): ensure git add related commands run from root (#26497)
1 parent 29af58f commit 3689c88

File tree

1 file changed

+19
-8
lines changed
  • packages/nx/src/command-line/release/utils

1 file changed

+19
-8
lines changed

packages/nx/src/command-line/release/utils/git.ts

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* https://github.com/unjs/changelogen
44
*/
55
import { interpolate } from '../../../tasks-runner/utils';
6+
import { workspaceRoot } from '../../../utils/app-root';
67
import { execCommand } from './exec-command';
78

89
export interface GitCommitAuthor {
@@ -147,8 +148,10 @@ export async function getGitDiff(
147148
});
148149
}
149150

150-
export async function getChangedTrackedFiles(): Promise<Set<string>> {
151-
const result = await execCommand('git', ['status', '--porcelain']);
151+
async function getChangedTrackedFiles(cwd: string): Promise<Set<string>> {
152+
const result = await execCommand('git', ['status', '--porcelain'], {
153+
cwd,
154+
});
152155
const lines = result.split('\n').filter((l) => l.trim().length > 0);
153156
return new Set(lines.map((l) => l.substring(3)));
154157
}
@@ -159,19 +162,23 @@ export async function gitAdd({
159162
dryRun,
160163
verbose,
161164
logFn,
165+
cwd,
162166
}: {
163167
changedFiles?: string[];
164168
deletedFiles?: string[];
165169
dryRun?: boolean;
166170
verbose?: boolean;
171+
cwd?: string;
167172
logFn?: (...messages: string[]) => void;
168173
}): Promise<string> {
169174
logFn = logFn || console.log;
175+
// Default to running git add related commands from the workspace root
176+
cwd = cwd || workspaceRoot;
170177

171178
let ignoredFiles: string[] = [];
172179
let filesToAdd: string[] = [];
173180
for (const f of changedFiles ?? []) {
174-
const isFileIgnored = await isIgnored(f);
181+
const isFileIgnored = await isIgnored(f, cwd);
175182
if (isFileIgnored) {
176183
ignoredFiles.push(f);
177184
} else {
@@ -180,9 +187,9 @@ export async function gitAdd({
180187
}
181188

182189
if (deletedFiles?.length > 0) {
183-
const changedTrackedFiles = await getChangedTrackedFiles();
190+
const changedTrackedFiles = await getChangedTrackedFiles(cwd);
184191
for (const f of deletedFiles ?? []) {
185-
const isFileIgnored = await isIgnored(f);
192+
const isFileIgnored = await isIgnored(f, cwd);
186193
if (isFileIgnored) {
187194
ignoredFiles.push(f);
188195
// git add will fail if trying to add an untracked file that doesn't exist
@@ -216,13 +223,17 @@ export async function gitAdd({
216223
if (dryRun) {
217224
return;
218225
}
219-
return execCommand('git', commandArgs);
226+
return execCommand('git', commandArgs, {
227+
cwd,
228+
});
220229
}
221230

222-
async function isIgnored(filePath: string): Promise<boolean> {
231+
async function isIgnored(filePath: string, cwd: string): Promise<boolean> {
223232
try {
224233
// This command will error if the file is not ignored
225-
await execCommand('git', ['check-ignore', filePath]);
234+
await execCommand('git', ['check-ignore', filePath], {
235+
cwd,
236+
});
226237
return true;
227238
} catch {
228239
return false;

0 commit comments

Comments
 (0)