@@ -12,7 +12,7 @@ const _ = require('./util/protected.js')
1212const addGitSha = require ( './util/add-git-sha.js' )
1313const npm = require ( './util/npm.js' )
1414
15- const hashre = / ^ [ a - f 0 - 9 ] { 40 } $ /
15+ const hashre = / ^ [ a - f 0 - 9 ] { 40 , 64 } $ /
1616
1717// get the repository url.
1818// prefer https if there's auth, since ssh will drop that.
@@ -25,6 +25,14 @@ const repoUrl = (h, opts) =>
2525// add git+ to the url, but only one time.
2626const addGitPlus = url => url && `git+${ url } ` . replace ( / ^ ( g i t \+ ) + / , 'git+' )
2727
28+ const checkoutError = ( expected , found ) => {
29+ const err = new Error ( `Commit mismatch: expected SHA ${ expected } and cloned HEAD ${ found } ` )
30+ err . code = 'EGITCHECKOUT'
31+ err . sha = expected
32+ err . head = found
33+ return err
34+ }
35+
2836class GitFetcher extends Fetcher {
2937 constructor ( spec , opts ) {
3038 super ( spec , opts )
@@ -245,7 +253,7 @@ class GitFetcher extends Fetcher {
245253 pkgid : `git:${ nameat } ${ this . resolved } ` ,
246254 resolved : this . resolved ,
247255 integrity : null , // it'll always be different, if we have one
248- } ) . extract ( tmp ) . then ( ( ) => handler ( tmp ) , er => {
256+ } ) . extract ( tmp ) . then ( ( ) => handler ( ` ${ tmp } ${ this . spec . gitSubdir || '' } ` ) , er => {
249257 // fall back to ssh download if tarball fails
250258 if ( er . constructor . name . match ( / ^ H t t p / ) ) {
251259 return this . #clone( handler , false )
@@ -259,11 +267,15 @@ class GitFetcher extends Fetcher {
259267 h ? this . #cloneHosted( ref , tmp )
260268 : this . #cloneRepo( this . spec . fetchSpec , ref , tmp )
261269 )
270+ // if we already have a resolved sha ensure it doesn't change
271+ if ( this . resolvedSha && this . resolvedSha !== sha ) {
272+ throw checkoutError ( this . resolvedSha , sha )
273+ }
262274 this . resolvedSha = sha
263275 if ( ! this . resolved ) {
264276 await this . #addGitSha( sha )
265277 }
266- return handler ( tmp )
278+ return handler ( ` ${ tmp } ${ this . spec . gitSubdir || '' } ` )
267279 } )
268280 }
269281
0 commit comments