unique org_repo remote names
This commit is contained in:
@@ -86,13 +86,12 @@ type GitHandlerGenerator interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type gitHandlerGeneratorImpl struct {
|
type gitHandlerGeneratorImpl struct {
|
||||||
// TODO: add mutex to lock paths so only one workflow per path and others wait
|
|
||||||
path string
|
path string
|
||||||
git_author string
|
git_author string
|
||||||
email string
|
email string
|
||||||
|
|
||||||
lock_lock sync.Mutex
|
lock_lock sync.Mutex
|
||||||
lock map[string]sync.Mutex
|
lock map[string]*sync.Mutex // per org
|
||||||
}
|
}
|
||||||
|
|
||||||
func AllocateGitWorkTree(basePath, gitAuthor, email string) (GitHandlerGenerator, error) {
|
func AllocateGitWorkTree(basePath, gitAuthor, email string) (GitHandlerGenerator, error) {
|
||||||
@@ -117,7 +116,7 @@ func AllocateGitWorkTree(basePath, gitAuthor, email string) (GitHandlerGenerator
|
|||||||
git_author: gitAuthor,
|
git_author: gitAuthor,
|
||||||
email: email,
|
email: email,
|
||||||
|
|
||||||
lock: make(map[string]sync.Mutex),
|
lock: make(map[string]*sync.Mutex),
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -193,7 +192,11 @@ func (e *GitHandlerImpl) GitClone(repo, branch, remoteUrl string) error {
|
|||||||
return fmt.Errorf("Clone location not a directory or Stat error: %w", err)
|
return fmt.Errorf("Clone location not a directory or Stat error: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
remoteName := ""
|
remoteUrlComp, err := ParseGitRemoteUrl(remoteUrl)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("Cannot parse remote URL: %w", err)
|
||||||
|
}
|
||||||
|
remoteName := remoteUrlComp.RemoteName()
|
||||||
clonedRemote := strings.TrimSpace(e.GitExecWithOutputOrPanic(repo, "remote", "get-url", remoteName))
|
clonedRemote := strings.TrimSpace(e.GitExecWithOutputOrPanic(repo, "remote", "get-url", remoteName))
|
||||||
if clonedRemote != remoteUrl {
|
if clonedRemote != remoteUrl {
|
||||||
e.GitExecOrPanic(repo, "remote", "set-url", remoteName, remoteUrl)
|
e.GitExecOrPanic(repo, "remote", "set-url", remoteName, remoteUrl)
|
||||||
|
|||||||
@@ -111,3 +111,11 @@ func ParseGitRemoteUrl(urlString string) (*GitUrl, error) {
|
|||||||
|
|
||||||
return &u, nil
|
return &u, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (giturl *GitUrl) RemoteName() string {
|
||||||
|
if giturl == nil || len(giturl.Org) == 0 || len(giturl.Repo) == 0 {
|
||||||
|
return "origin"
|
||||||
|
}
|
||||||
|
|
||||||
|
return strings.ToLower(giturl.Org) + "_" + strings.ToLower(giturl.Repo)
|
||||||
|
}
|
||||||
|
|||||||
@@ -112,3 +112,56 @@ func TestGitUrlParse(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestRemoteName(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
giturl *common.GitUrl
|
||||||
|
remotename string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "empty",
|
||||||
|
remotename: "origin",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "regular repo",
|
||||||
|
giturl: &common.GitUrl{
|
||||||
|
Org: "org1",
|
||||||
|
Repo: "repo2",
|
||||||
|
Commit: "main",
|
||||||
|
},
|
||||||
|
remotename: "org1_repo2",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "regular repo with upper case",
|
||||||
|
giturl: &common.GitUrl{
|
||||||
|
Org: "Org1",
|
||||||
|
Repo: "REPO2",
|
||||||
|
},
|
||||||
|
remotename: "org1_repo2",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "no org",
|
||||||
|
giturl: &common.GitUrl{
|
||||||
|
Repo: "REPO2",
|
||||||
|
},
|
||||||
|
remotename: "origin",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "no repo",
|
||||||
|
giturl: &common.GitUrl{
|
||||||
|
Org: "ORG2",
|
||||||
|
},
|
||||||
|
remotename: "origin",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
t.Run(test.name, func(t *testing.T) {
|
||||||
|
n := test.giturl.RemoteName()
|
||||||
|
if n != test.remotename {
|
||||||
|
t.Errorf("Expected '%s' but received '%s'", test.remotename, n)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user