@@ -16,6 +16,7 @@ const promiseRetry = require('promise-retry')
16
16
const uniqueFilename = require ( 'unique-filename' )
17
17
const which = BB . promisify ( require ( 'which' ) )
18
18
const semver = require ( 'semver' )
19
+ const inferOwner = require ( 'infer-owner' )
19
20
20
21
const GOOD_ENV_VARS = new Set ( [
21
22
'GIT_ASKPASS' ,
@@ -181,10 +182,24 @@ function revs (repo, opts) {
181
182
} )
182
183
}
183
184
185
+ // infer the owner from the cwd git is operating in, if not the
186
+ // process cwd, but only if we're root.
187
+ // See: https://github.com/npm/cli/issues/624
188
+ module . exports . _cwdOwner = cwdOwner
189
+ function cwdOwner ( gitOpts , opts ) {
190
+ const isRoot = process . getuid && process . getuid ( ) === 0
191
+ if ( ! isRoot || ! gitOpts . cwd ) { return Promise . resolve ( ) }
192
+
193
+ return BB . resolve ( inferOwner ( gitOpts . cwd ) . then ( owner => {
194
+ gitOpts . uid = owner . uid
195
+ gitOpts . gid = owner . gid
196
+ } ) )
197
+ }
198
+
184
199
module . exports . _exec = execGit
185
200
function execGit ( gitArgs , gitOpts , opts ) {
186
201
opts = optCheck ( opts )
187
- return checkGit ( opts ) . then ( gitPath => {
202
+ return BB . resolve ( cwdOwner ( gitOpts , opts ) . then ( ( ) => checkGit ( opts ) . then ( gitPath => {
188
203
return promiseRetry ( ( retry , number ) => {
189
204
if ( number !== 1 ) {
190
205
opts . log . silly ( 'pacote' , 'Retrying git command: ' + gitArgs . join ( ' ' ) + ' attempt # ' + number )
@@ -202,13 +217,13 @@ function execGit (gitArgs, gitOpts, opts) {
202
217
maxTimeout : opts [ 'fetch-retry-maxtimeout' ] ,
203
218
minTimeout : opts [ 'fetch-retry-mintimeout' ]
204
219
} )
205
- } )
220
+ } ) ) )
206
221
}
207
222
208
223
module . exports . _spawn = spawnGit
209
224
function spawnGit ( gitArgs , gitOpts , opts ) {
210
225
opts = optCheck ( opts )
211
- return checkGit ( opts ) . then ( gitPath => {
226
+ return BB . resolve ( cwdOwner ( gitOpts , opts ) . then ( ( ) => checkGit ( opts ) . then ( gitPath => {
212
227
return promiseRetry ( ( retry , number ) => {
213
228
if ( number !== 1 ) {
214
229
opts . log . silly ( 'pacote' , 'Retrying git command: ' + gitArgs . join ( ' ' ) + ' attempt # ' + number )
@@ -231,7 +246,7 @@ function spawnGit (gitArgs, gitOpts, opts) {
231
246
return stdout
232
247
} )
233
248
} , opts . retry )
234
- } )
249
+ } ) ) )
235
250
}
236
251
237
252
module . exports . _mkOpts = mkOpts
0 commit comments