1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-06 03:13:48 +02:00

fix: git client should respect specified branch (#4324)

refs https://github.com/orgs/goreleaser/discussions/4321
This commit is contained in:
Carlos Alexandro Becker 2023-09-24 17:07:10 -03:00 committed by GitHub
parent 530223c2ac
commit 82200491bd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 74 additions and 8 deletions

View File

@ -38,7 +38,13 @@ func NewGitUploadClient(branch string) FilesCreator {
}
// CreateFiles implements FilesCreator.
func (g *gitClient) CreateFiles(ctx *context.Context, commitAuthor config.CommitAuthor, repo Repo, message string, files []RepoFile) (err error) {
func (g *gitClient) CreateFiles(
ctx *context.Context,
commitAuthor config.CommitAuthor,
repo Repo,
message string,
files []RepoFile,
) (err error) {
url, err := tmpl.New(ctx).Apply(repo.GitURL)
if err != nil {
return fmt.Errorf("git: failed to template git url: %w", err)
@ -68,7 +74,8 @@ func (g *gitClient) CreateFiles(ctx *context.Context, commitAuthor config.Commit
}
parent := filepath.Join(ctx.Config.Dist, "git")
cwd := filepath.Join(parent, repo.Name)
name := repo.Name + "-" + g.branch
cwd := filepath.Join(parent, name)
env := []string{fmt.Sprintf("GIT_SSH_COMMAND=%s", sshcmd)}
if err := cloneLock.clone(url, func() error {
@ -77,7 +84,7 @@ func (g *gitClient) CreateFiles(ctx *context.Context, commitAuthor config.Commit
}
if err := runGitCmds(ctx, parent, env, [][]string{
{"clone", url, repo.Name},
{"clone", url, name},
}); err != nil {
return fmt.Errorf("git: failed to clone local repository: %w", err)
}
@ -90,6 +97,15 @@ func (g *gitClient) CreateFiles(ctx *context.Context, commitAuthor config.Commit
}); err != nil {
return fmt.Errorf("git: failed to setup local repository: %w", err)
}
if err := runGitCmds(ctx, cwd, env, [][]string{
{"checkout", g.branch},
}); err != nil {
if err := runGitCmds(ctx, cwd, env, [][]string{
{"checkout", "-b", g.branch},
}); err != nil {
return fmt.Errorf("git: could not checkout branch %s: %w", g.branch, err)
}
}
return nil
}); err != nil {
return err

View File

@ -52,6 +52,42 @@ func TestGitClient(t *testing.T) {
require.Equal(t, "fake content updated", string(testlib.CatFileFromBareRepository(t, url, "fake.txt")))
require.Equal(t, "fake2 content", string(testlib.CatFileFromBareRepository(t, url, "fake2.txt")))
})
t.Run("with new branch", func(t *testing.T) {
cli := NewGitUploadClient("new-branch")
url := testlib.GitMakeBareRepository(t)
ctx := testctx.NewWithCfg(config.Project{
Dist: t.TempDir(),
})
repo := Repo{
GitURL: url,
PrivateKey: testlib.MakeNewSSHKey(t, keygen.Ed25519, ""),
Name: "test1",
}
require.NoError(t, cli.CreateFiles(
ctx,
author,
repo,
"hey test",
[]RepoFile{
{
Content: []byte("fake content"),
Path: "fake.txt",
},
{
Content: []byte("fake2 content"),
Path: "fake2.txt",
},
{
Content: []byte("fake content updated"),
Path: "fake.txt",
},
},
))
require.Equal(t, "fake content updated", string(testlib.CatFileFromBareRepositoryOnBranch(t, url, "new-branch", "fake.txt")))
require.Equal(t, "fake2 content", string(testlib.CatFileFromBareRepositoryOnBranch(t, url, "new-branch", "fake2.txt")))
})
t.Run("no repo name", func(t *testing.T) {
url := testlib.GitMakeBareRepository(t)
ctx := testctx.NewWithCfg(config.Project{

View File

@ -410,7 +410,11 @@ func TestFullPipe(t *testing.T) {
if url := ctx.Config.Brews[0].Repository.Git.URL; url == "" {
require.True(t, client.CreatedFile, "should have created a file")
} else {
content = testlib.CatFileFromBareRepository(t, url, name+".rb")
content = testlib.CatFileFromBareRepositoryOnBranch(
t, url,
ctx.Config.Brews[0].Repository.Branch,
name+".rb",
)
}
golden.RequireEqualRb(t, content)

View File

@ -334,7 +334,11 @@ func TestFullPipe(t *testing.T) {
if url := ctx.Config.Krews[0].Repository.Git.URL; url == "" {
require.True(t, client.CreatedFile, "should have created a file")
} else {
content = testlib.CatFileFromBareRepository(t, url, "plugins/"+name+".yaml")
content = testlib.CatFileFromBareRepositoryOnBranch(
t, url,
ctx.Config.Krews[0].Repository.Branch,
"plugins/"+name+".yaml",
)
}
golden.RequireEqualYaml(t, content)

View File

@ -261,9 +261,10 @@ func Test_doRun(t *testing.T) {
shouldNotErr,
func(tb testing.TB, a args) {
tb.Helper()
content := testlib.CatFileFromBareRepository(
content := testlib.CatFileFromBareRepositoryOnBranch(
tb,
a.ctx.Config.Scoop.Repository.Git.URL,
a.ctx.Config.Scoop.Repository.Branch,
"scoops/git-run-pipe.json",
)
golden.RequireEqualJSON(tb, content)

View File

@ -130,13 +130,18 @@ func MakeNewSSHKey(tb testing.TB, algo keygen.KeyType, pass string) string {
func CatFileFromBareRepository(tb testing.TB, url, name string) []byte {
tb.Helper()
return CatFileFromBareRepositoryOnBranch(tb, url, "master", name)
}
func CatFileFromBareRepositoryOnBranch(tb testing.TB, url, branch, name string) []byte {
tb.Helper()
out, err := exec.Command(
"git",
"-C", url,
"show",
"master:"+name,
branch+":"+name,
).CombinedOutput()
require.NoError(tb, err, "could not cat file "+name+" in repository")
require.NoError(tb, err, "could not cat file "+name+" in repository on branch "+branch)
return out
}