Skip to content

Commit

Permalink
Merge pull request #5072 from tonistiigi/git-commit-tags-fetch
Browse files Browse the repository at this point in the history
git: fix pulling commit SHA only referenced from a tag
  • Loading branch information
AkihiroSuda authored Jun 23, 2024
2 parents edcf130 + dff03a2 commit 07e2dce
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
1 change: 1 addition & 0 deletions source/git/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,7 @@ func (gs *gitSourceHandler) Snapshot(ctx context.Context, g session.Group) (out
if !isCommitSHA(ref) { // TODO: find a branch from ls-remote?
args = append(args, "--depth=1", "--no-tags")
} else {
args = append(args, "--tags")
if _, err := os.Lstat(filepath.Join(gitDir, "shallow")); err == nil {
args = append(args, "--unshallow")
}
Expand Down
80 changes: 80 additions & 0 deletions source/git/source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,72 @@ func testFetchBySHA(t *testing.T, keepGitDir bool) {
require.Equal(t, "subcontents\n", string(dt))
}

func TestFetchUnreferencedTagSha(t *testing.T) {
testFetchUnreferencedTagSha(t, false)
}

func TestFetchUnreferencedTagShaKeepGitDir(t *testing.T) {
testFetchUnreferencedTagSha(t, true)
}

// testFetchUnreferencedTagSha tests fetching a SHA that points to a tag that is not reachable from any branch.
func testFetchUnreferencedTagSha(t *testing.T, keepGitDir bool) {
if runtime.GOOS == "windows" {
t.Skip("Depends on unimplemented containerd bind-mount support on Windows")
}

t.Parallel()
ctx := namespaces.WithNamespace(context.Background(), "buildkit-test")
ctx = logProgressStreams(ctx, t)

gs := setupGitSource(t, t.TempDir())

repo := setupGitRepo(t)

cmd := exec.Command("git", "rev-parse", "v1.2.3-special")
cmd.Dir = repo.mainPath

out, err := cmd.Output()
require.NoError(t, err)

sha := strings.TrimSpace(string(out))
require.Equal(t, 40, len(sha))

id := &GitIdentifier{Remote: repo.mainURL, Ref: sha, KeepGitDir: keepGitDir}

g, err := gs.Resolve(ctx, id, nil, nil)
require.NoError(t, err)

key1, pin1, _, done, err := g.CacheKey(ctx, nil, 0)
require.NoError(t, err)
require.True(t, done)

expLen := 40
if keepGitDir {
expLen += 4
}

require.Equal(t, expLen, len(key1))
require.Equal(t, 40, len(pin1))

ref1, err := g.Snapshot(ctx, nil)
require.NoError(t, err)
defer ref1.Release(context.TODO())

mount, err := ref1.Mount(ctx, true, nil)
require.NoError(t, err)

lm := snapshot.LocalMounter(mount)
dir, err := lm.Mount()
require.NoError(t, err)
defer lm.Unmount()

dt, err := os.ReadFile(filepath.Join(dir, "bar"))
require.NoError(t, err)

require.Equal(t, "foo\n", string(dt))
}

func TestFetchByTag(t *testing.T) {
testFetchByTag(t, "lightweight-tag", "third", false, true, false)
}
Expand Down Expand Up @@ -610,6 +676,13 @@ func setupGitRepo(t *testing.T) gitRepoFixture {
"git add subfile",
"git commit -m initial",
)
// * (refs/heads/feature) withsub
// * feature
// * (HEAD -> refs/heads/master, tag: refs/tags/lightweight-tag) third
// | * (tag: refs/tags/v1.2.3-special) tagonly-leaf
// |/
// * (tag: refs/tags/v1.2.3) second
// * (tag: refs/tags/a/v1.2.3) initial
runShell(t, fixture.mainPath,
"git -c init.defaultBranch=master init",
"git config --local user.email test",
Expand All @@ -622,6 +695,12 @@ func setupGitRepo(t *testing.T) gitRepoFixture {
"git add def",
"git commit -m second",
"git tag -a -m \"this is an annotated tag\" v1.2.3",
"echo foo > bar",
"git add bar",
"git commit -m tagonly-leaf",
"git tag --no-sign v1.2.3-special",
// switch master back to v1.2.3
"git checkout -B master v1.2.3",
"echo sbb > foo13",
"git add foo13",
"git commit -m third",
Expand All @@ -635,6 +714,7 @@ func setupGitRepo(t *testing.T) gitRepoFixture {
"git add -A",
"git commit -m withsub",
"git checkout master",
// "git log --oneline --graph --decorate=full --all",
)
return fixture
}
Expand Down

0 comments on commit 07e2dce

Please sign in to comment.