Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

git: fix pulling commit SHA only referenced from a tag #5072

Merged
merged 1 commit into from
Jun 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a leftover debug print?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line can be used to update the comment showing the git structure in line 686 if updates are made

)
return fixture
}
Expand Down
Loading