@@ -16,6 +16,7 @@ const promiseRetry = require('promise-retry')
1616const uniqueFilename = require ( 'unique-filename' )
1717const which = BB . promisify ( require ( 'which' ) )
1818const semver = require ( 'semver' )
19+ const inferOwner = require ( 'infer-owner' )
1920
2021const GOOD_ENV_VARS = new Set ( [
2122 'GIT_ASKPASS' ,
@@ -181,10 +182,24 @@ function revs (repo, opts) {
181182 } )
182183}
183184
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+
184199module . exports . _exec = execGit
185200function execGit ( gitArgs , gitOpts , opts ) {
186201 opts = optCheck ( opts )
187- return checkGit ( opts ) . then ( gitPath => {
202+ return BB . resolve ( cwdOwner ( gitOpts , opts ) . then ( ( ) => checkGit ( opts ) . then ( gitPath => {
188203 return promiseRetry ( ( retry , number ) => {
189204 if ( number !== 1 ) {
190205 opts . log . silly ( 'pacote' , 'Retrying git command: ' + gitArgs . join ( ' ' ) + ' attempt # ' + number )
@@ -202,13 +217,13 @@ function execGit (gitArgs, gitOpts, opts) {
202217 maxTimeout : opts [ 'fetch-retry-maxtimeout' ] ,
203218 minTimeout : opts [ 'fetch-retry-mintimeout' ]
204219 } )
205- } )
220+ } ) ) )
206221}
207222
208223module . exports . _spawn = spawnGit
209224function spawnGit ( gitArgs , gitOpts , opts ) {
210225 opts = optCheck ( opts )
211- return checkGit ( opts ) . then ( gitPath => {
226+ return BB . resolve ( cwdOwner ( gitOpts , opts ) . then ( ( ) => checkGit ( opts ) . then ( gitPath => {
212227 return promiseRetry ( ( retry , number ) => {
213228 if ( number !== 1 ) {
214229 opts . log . silly ( 'pacote' , 'Retrying git command: ' + gitArgs . join ( ' ' ) + ' attempt # ' + number )
@@ -231,7 +246,7 @@ function spawnGit (gitArgs, gitOpts, opts) {
231246 return stdout
232247 } )
233248 } , opts . retry )
234- } )
249+ } ) ) )
235250}
236251
237252module . exports . _mkOpts = mkOpts
0 commit comments