3
3
* https://github.com/unjs/changelogen
4
4
*/
5
5
import { interpolate } from '../../../tasks-runner/utils' ;
6
+ import { workspaceRoot } from '../../../utils/app-root' ;
6
7
import { execCommand } from './exec-command' ;
7
8
8
9
export interface GitCommitAuthor {
@@ -147,8 +148,10 @@ export async function getGitDiff(
147
148
} ) ;
148
149
}
149
150
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
+ } ) ;
152
155
const lines = result . split ( '\n' ) . filter ( ( l ) => l . trim ( ) . length > 0 ) ;
153
156
return new Set ( lines . map ( ( l ) => l . substring ( 3 ) ) ) ;
154
157
}
@@ -159,19 +162,23 @@ export async function gitAdd({
159
162
dryRun,
160
163
verbose,
161
164
logFn,
165
+ cwd,
162
166
} : {
163
167
changedFiles ?: string [ ] ;
164
168
deletedFiles ?: string [ ] ;
165
169
dryRun ?: boolean ;
166
170
verbose ?: boolean ;
171
+ cwd ?: string ;
167
172
logFn ?: ( ...messages : string [ ] ) => void ;
168
173
} ) : Promise < string > {
169
174
logFn = logFn || console . log ;
175
+ // Default to running git add related commands from the workspace root
176
+ cwd = cwd || workspaceRoot ;
170
177
171
178
let ignoredFiles : string [ ] = [ ] ;
172
179
let filesToAdd : string [ ] = [ ] ;
173
180
for ( const f of changedFiles ?? [ ] ) {
174
- const isFileIgnored = await isIgnored ( f ) ;
181
+ const isFileIgnored = await isIgnored ( f , cwd ) ;
175
182
if ( isFileIgnored ) {
176
183
ignoredFiles . push ( f ) ;
177
184
} else {
@@ -180,9 +187,9 @@ export async function gitAdd({
180
187
}
181
188
182
189
if ( deletedFiles ?. length > 0 ) {
183
- const changedTrackedFiles = await getChangedTrackedFiles ( ) ;
190
+ const changedTrackedFiles = await getChangedTrackedFiles ( cwd ) ;
184
191
for ( const f of deletedFiles ?? [ ] ) {
185
- const isFileIgnored = await isIgnored ( f ) ;
192
+ const isFileIgnored = await isIgnored ( f , cwd ) ;
186
193
if ( isFileIgnored ) {
187
194
ignoredFiles . push ( f ) ;
188
195
// git add will fail if trying to add an untracked file that doesn't exist
@@ -216,13 +223,17 @@ export async function gitAdd({
216
223
if ( dryRun ) {
217
224
return ;
218
225
}
219
- return execCommand ( 'git' , commandArgs ) ;
226
+ return execCommand ( 'git' , commandArgs , {
227
+ cwd,
228
+ } ) ;
220
229
}
221
230
222
- async function isIgnored ( filePath : string ) : Promise < boolean > {
231
+ async function isIgnored ( filePath : string , cwd : string ) : Promise < boolean > {
223
232
try {
224
233
// 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
+ } ) ;
226
237
return true ;
227
238
} catch {
228
239
return false ;
0 commit comments