1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-07-17 01:42:37 +02:00

fix(aur): allow to have multiple AUR configs pointing to the same repo (#4712)

- using the sha256 of the git url as repo name so we avoid race
conditions
- actually made the git client have a global lock so it doesn't step
over itself

closes #4679

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>
This commit is contained in:
Carlos Alexandro Becker
2024-03-19 23:53:08 -03:00
committed by GitHub
parent 6e0f426339
commit 08851dce61
3 changed files with 98 additions and 35 deletions

View File

@ -19,14 +19,11 @@ import (
"golang.org/x/crypto/ssh"
)
var gil sync.Mutex
// DefaulGitSSHCommand used for git over SSH.
const DefaulGitSSHCommand = `ssh -i "{{ .KeyPath }}" -o StrictHostKeyChecking=accept-new -F /dev/null`
var cloneLock = cloneGlobalLock{
l: sync.Mutex{},
repos: map[string]bool{},
}
type gitClient struct {
branch string
}
@ -46,6 +43,9 @@ func (g *gitClient) CreateFiles(
message string,
files []RepoFile,
) (err error) {
gil.Lock()
defer gil.Unlock()
url, err := tmpl.New(ctx).Apply(repo.GitURL)
if err != nil {
return fmt.Errorf("git: failed to template git url: %w", err)
@ -79,7 +79,8 @@ func (g *gitClient) CreateFiles(
cwd := filepath.Join(parent, name)
env := []string{fmt.Sprintf("GIT_SSH_COMMAND=%s", sshcmd)}
if err := cloneLock.clone(url, func() error {
if _, err := os.Stat(cwd); errors.Is(err, os.ErrNotExist) {
log.Infof("cloning %s %s", name, cwd)
if err := os.MkdirAll(parent, 0o755); err != nil {
return fmt.Errorf("git: failed to create parent: %w", err)
}
@ -98,21 +99,17 @@ func (g *gitClient) CreateFiles(
}); err != nil {
return fmt.Errorf("git: failed to setup local repository: %w", err)
}
if g.branch == "" {
return nil
}
if err := runGitCmds(ctx, cwd, env, [][]string{
{"checkout", g.branch},
}); err != nil {
if g.branch != "" {
if err := runGitCmds(ctx, cwd, env, [][]string{
{"checkout", "-b", g.branch},
{"checkout", g.branch},
}); err != nil {
return fmt.Errorf("git: could not checkout branch %s: %w", g.branch, err)
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
}
for _, file := range files {
@ -216,20 +213,3 @@ func runGitCmds(ctx *context.Context, cwd string, env []string, cmds [][]string)
func nameFromURL(url string) string {
return strings.TrimSuffix(url[strings.LastIndex(url, "/")+1:], ".git")
}
type cloneGlobalLock struct {
l sync.Mutex
repos map[string]bool
}
func (c *cloneGlobalLock) clone(url string, fn func() error) error {
c.l.Lock()
defer c.l.Unlock()
if c.repos[url] {
return nil
}
c.repos[url] = true
return fn()
}