@@ -217,6 +217,72 @@ func testFetchBySHA(t *testing.T, keepGitDir bool) {
217
217
require .Equal (t , "subcontents\n " , string (dt ))
218
218
}
219
219
220
+ func TestFetchUnreferencedTagSha (t * testing.T ) {
221
+ testFetchUnreferencedTagSha (t , false )
222
+ }
223
+
224
+ func TestFetchUnreferencedTagShaKeepGitDir (t * testing.T ) {
225
+ testFetchUnreferencedTagSha (t , true )
226
+ }
227
+
228
+ // testFetchUnreferencedTagSha tests fetching a SHA that points to a tag that is not reachable from any branch.
229
+ func testFetchUnreferencedTagSha (t * testing.T , keepGitDir bool ) {
230
+ if runtime .GOOS == "windows" {
231
+ t .Skip ("Depends on unimplemented containerd bind-mount support on Windows" )
232
+ }
233
+
234
+ t .Parallel ()
235
+ ctx := namespaces .WithNamespace (context .Background (), "buildkit-test" )
236
+ ctx = logProgressStreams (ctx , t )
237
+
238
+ gs := setupGitSource (t , t .TempDir ())
239
+
240
+ repo := setupGitRepo (t )
241
+
242
+ cmd := exec .Command ("git" , "rev-parse" , "v1.2.3-special" )
243
+ cmd .Dir = repo .mainPath
244
+
245
+ out , err := cmd .Output ()
246
+ require .NoError (t , err )
247
+
248
+ sha := strings .TrimSpace (string (out ))
249
+ require .Equal (t , 40 , len (sha ))
250
+
251
+ id := & GitIdentifier {Remote : repo .mainURL , Ref : sha , KeepGitDir : keepGitDir }
252
+
253
+ g , err := gs .Resolve (ctx , id , nil , nil )
254
+ require .NoError (t , err )
255
+
256
+ key1 , pin1 , _ , done , err := g .CacheKey (ctx , nil , 0 )
257
+ require .NoError (t , err )
258
+ require .True (t , done )
259
+
260
+ expLen := 40
261
+ if keepGitDir {
262
+ expLen += 4
263
+ }
264
+
265
+ require .Equal (t , expLen , len (key1 ))
266
+ require .Equal (t , 40 , len (pin1 ))
267
+
268
+ ref1 , err := g .Snapshot (ctx , nil )
269
+ require .NoError (t , err )
270
+ defer ref1 .Release (context .TODO ())
271
+
272
+ mount , err := ref1 .Mount (ctx , true , nil )
273
+ require .NoError (t , err )
274
+
275
+ lm := snapshot .LocalMounter (mount )
276
+ dir , err := lm .Mount ()
277
+ require .NoError (t , err )
278
+ defer lm .Unmount ()
279
+
280
+ dt , err := os .ReadFile (filepath .Join (dir , "bar" ))
281
+ require .NoError (t , err )
282
+
283
+ require .Equal (t , "foo\n " , string (dt ))
284
+ }
285
+
220
286
func TestFetchByTag (t * testing.T ) {
221
287
testFetchByTag (t , "lightweight-tag" , "third" , false , true , false )
222
288
}
@@ -610,6 +676,13 @@ func setupGitRepo(t *testing.T) gitRepoFixture {
610
676
"git add subfile" ,
611
677
"git commit -m initial" ,
612
678
)
679
+ // * (refs/heads/feature) withsub
680
+ // * feature
681
+ // * (HEAD -> refs/heads/master, tag: refs/tags/lightweight-tag) third
682
+ // | * (tag: refs/tags/v1.2.3-special) tagonly-leaf
683
+ // |/
684
+ // * (tag: refs/tags/v1.2.3) second
685
+ // * (tag: refs/tags/a/v1.2.3) initial
613
686
runShell (t , fixture .mainPath ,
614
687
"git -c init.defaultBranch=master init" ,
615
688
"git config --local user.email test" ,
@@ -622,6 +695,12 @@ func setupGitRepo(t *testing.T) gitRepoFixture {
622
695
"git add def" ,
623
696
"git commit -m second" ,
624
697
"git tag -a -m \" this is an annotated tag\" v1.2.3" ,
698
+ "echo foo > bar" ,
699
+ "git add bar" ,
700
+ "git commit -m tagonly-leaf" ,
701
+ "git tag --no-sign v1.2.3-special" ,
702
+ // switch master back to v1.2.3
703
+ "git checkout -B master v1.2.3" ,
625
704
"echo sbb > foo13" ,
626
705
"git add foo13" ,
627
706
"git commit -m third" ,
@@ -635,6 +714,7 @@ func setupGitRepo(t *testing.T) gitRepoFixture {
635
714
"git add -A" ,
636
715
"git commit -m withsub" ,
637
716
"git checkout master" ,
717
+ // "git log --oneline --graph --decorate=full --all",
638
718
)
639
719
return fixture
640
720
}
0 commit comments