@@ -304,54 +304,70 @@ func testFetchUnreferencedRefSha(t *testing.T, ref string, keepGitDir bool) {
304304}
305305
306306func TestFetchByTag (t * testing.T ) {
307- testFetchByTag (t , "lightweight-tag" , "third" , false , true , false )
307+ testFetchByTag (t , "lightweight-tag" , "third" , false , true , false , testCommitHashModeNone )
308308}
309309
310310func TestFetchByTagKeepGitDir (t * testing.T ) {
311- testFetchByTag (t , "lightweight-tag" , "third" , false , true , true )
311+ testFetchByTag (t , "lightweight-tag" , "third" , false , true , true , testCommitHashModeNone )
312312}
313313
314314func TestFetchByTagFull (t * testing.T ) {
315- testFetchByTag (t , "refs/tags/lightweight-tag" , "third" , false , true , true )
315+ testFetchByTag (t , "refs/tags/lightweight-tag" , "third" , false , true , true , testCommitHashModeNone )
316316}
317317
318318func TestFetchByAnnotatedTag (t * testing.T ) {
319- testFetchByTag (t , "v1.2.3" , "second" , true , false , false )
319+ testFetchByTag (t , "v1.2.3" , "second" , true , false , false , testCommitHashModeNone )
320320}
321321
322322func TestFetchByAnnotatedTagKeepGitDir (t * testing.T ) {
323- testFetchByTag (t , "v1.2.3" , "second" , true , false , true )
323+ testFetchByTag (t , "v1.2.3" , "second" , true , false , true , testCommitHashModeNone )
324324}
325325
326326func TestFetchByAnnotatedTagFull (t * testing.T ) {
327- testFetchByTag (t , "refs/tags/v1.2.3" , "second" , true , false , true )
327+ testFetchByTag (t , "refs/tags/v1.2.3" , "second" , true , false , true , testCommitHashModeNone )
328328}
329329
330330func TestFetchByBranch (t * testing.T ) {
331- testFetchByTag (t , "feature" , "withsub" , false , true , false )
331+ testFetchByTag (t , "feature" , "withsub" , false , true , false , testCommitHashModeNone )
332332}
333333
334334func TestFetchByBranchKeepGitDir (t * testing.T ) {
335- testFetchByTag (t , "feature" , "withsub" , false , true , true )
335+ testFetchByTag (t , "feature" , "withsub" , false , true , true , testCommitHashModeNone )
336336}
337337
338338func TestFetchByBranchFull (t * testing.T ) {
339- testFetchByTag (t , "refs/heads/feature" , "withsub" , false , true , true )
339+ testFetchByTag (t , "refs/heads/feature" , "withsub" , false , true , true , testCommitHashModeNone )
340340}
341341
342342func TestFetchByRef (t * testing.T ) {
343- testFetchByTag (t , "test" , "feature" , false , true , false )
343+ testFetchByTag (t , "test" , "feature" , false , true , false , testCommitHashModeNone )
344344}
345345
346346func TestFetchByRefKeepGitDir (t * testing.T ) {
347- testFetchByTag (t , "test" , "feature" , false , true , true )
347+ testFetchByTag (t , "test" , "feature" , false , true , true , testCommitHashModeNone )
348348}
349349
350350func TestFetchByRefFull (t * testing.T ) {
351- testFetchByTag (t , "refs/test" , "feature" , false , true , true )
351+ testFetchByTag (t , "refs/test" , "feature" , false , true , true , testCommitHashModeNone )
352352}
353353
354- func testFetchByTag (t * testing.T , tag , expectedCommitSubject string , isAnnotatedTag , hasFoo13File , keepGitDir bool ) {
354+ func TestFetchByTagWithCommitHash (t * testing.T ) {
355+ testFetchByTag (t , "lightweight-tag" , "third" , false , true , false , testCommitHashModeValid )
356+ }
357+
358+ func TestFetchByTagWithCommitHashInvalid (t * testing.T ) {
359+ testFetchByTag (t , "lightweight-tag" , "third" , false , true , false , testCommitHashModeInvalid )
360+ }
361+
362+ type testCommitHashMode int
363+
364+ const (
365+ testCommitHashModeNone testCommitHashMode = iota
366+ testCommitHashModeValid
367+ testCommitHashModeInvalid
368+ )
369+
370+ func testFetchByTag (t * testing.T , tag , expectedCommitSubject string , isAnnotatedTag , hasFoo13File , keepGitDir bool , commitHashMode testCommitHashMode ) {
355371 if runtime .GOOS == "windows" {
356372 t .Skip ("Depends on unimplemented containerd bind-mount support on Windows" )
357373 }
@@ -366,6 +382,24 @@ func testFetchByTag(t *testing.T, tag, expectedCommitSubject string, isAnnotated
366382
367383 id := & GitIdentifier {Remote : repo .mainURL , Ref : tag , KeepGitDir : keepGitDir }
368384
385+ if commitHashMode != testCommitHashModeNone {
386+ cmd := exec .Command ("git" , "rev-parse" , tag )
387+ cmd .Dir = repo .mainPath
388+
389+ out , err := cmd .Output ()
390+ require .NoError (t , err )
391+
392+ sha := strings .TrimSpace (string (out ))
393+ require .Equal (t , 40 , len (sha ))
394+
395+ id .CommitHash = sha
396+
397+ if commitHashMode == testCommitHashModeInvalid {
398+ // Make the hash value invalid
399+ id .CommitHash = "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef"
400+ }
401+ }
402+
369403 g , err := gs .Resolve (ctx , id , nil , nil )
370404 require .NoError (t , err )
371405
@@ -383,6 +417,10 @@ func testFetchByTag(t *testing.T, tag, expectedCommitSubject string, isAnnotated
383417 require .Equal (t , 40 , len (pin1 ))
384418
385419 ref1 , err := g .Snapshot (ctx , nil )
420+ if commitHashMode == testCommitHashModeInvalid {
421+ require .ErrorContains (t , err , "expected commit hash " + id .CommitHash )
422+ return
423+ }
386424 require .NoError (t , err )
387425 defer ref1 .Release (context .TODO ())
388426
0 commit comments