Skip to content

Commit 07e2dce

Browse files
authored
Merge pull request moby#5072 from tonistiigi/git-commit-tags-fetch
git: fix pulling commit SHA only referenced from a tag
2 parents edcf130 + dff03a2 commit 07e2dce

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

source/git/source.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,7 @@ func (gs *gitSourceHandler) Snapshot(ctx context.Context, g session.Group) (out
470470
if !isCommitSHA(ref) { // TODO: find a branch from ls-remote?
471471
args = append(args, "--depth=1", "--no-tags")
472472
} else {
473+
args = append(args, "--tags")
473474
if _, err := os.Lstat(filepath.Join(gitDir, "shallow")); err == nil {
474475
args = append(args, "--unshallow")
475476
}

source/git/source_test.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,72 @@ func testFetchBySHA(t *testing.T, keepGitDir bool) {
217217
require.Equal(t, "subcontents\n", string(dt))
218218
}
219219

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+
220286
func TestFetchByTag(t *testing.T) {
221287
testFetchByTag(t, "lightweight-tag", "third", false, true, false)
222288
}
@@ -610,6 +676,13 @@ func setupGitRepo(t *testing.T) gitRepoFixture {
610676
"git add subfile",
611677
"git commit -m initial",
612678
)
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
613686
runShell(t, fixture.mainPath,
614687
"git -c init.defaultBranch=master init",
615688
"git config --local user.email test",
@@ -622,6 +695,12 @@ func setupGitRepo(t *testing.T) gitRepoFixture {
622695
"git add def",
623696
"git commit -m second",
624697
"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",
625704
"echo sbb > foo13",
626705
"git add foo13",
627706
"git commit -m third",
@@ -635,6 +714,7 @@ func setupGitRepo(t *testing.T) gitRepoFixture {
635714
"git add -A",
636715
"git commit -m withsub",
637716
"git checkout master",
717+
// "git log --oneline --graph --decorate=full --all",
638718
)
639719
return fixture
640720
}

0 commit comments

Comments
 (0)