Allow using any default branch name for template repos

**What**
- Use the git server's default remote branch when the template repo
  refName is empty. This allows us to support _any_ default branch
  instead of the previously hard coded `master`

Signed-off-by: Lucas Roesler <roesler.lucas@gmail.com>
This commit is contained in:
Lucas Roesler
2020-11-28 14:42:16 +01:00
committed by Alex Ellis
parent 81b166aaa1
commit 004f20be4d
5 changed files with 43 additions and 12 deletions

View File

@@ -35,8 +35,15 @@ func fetchTemplates(templateURL string, refName string, overwrite bool) error {
log.Printf("Attempting to expand templates from %s\n", templateURL)
pullDebugPrint(fmt.Sprintf("Temp files in %s", dir))
args := map[string]string{"dir": dir, "repo": templateURL, "refname": refName}
if err := versioncontrol.GitClone.Invoke(".", args); err != nil {
args := map[string]string{"dir": dir, "repo": templateURL}
cmd := versioncontrol.GitCloneDefault
if refName != "" {
args["refname"] = refName
cmd = versioncontrol.GitClone
}
if err := cmd.Invoke(".", args); err != nil {
return err
}
@@ -126,11 +133,14 @@ func pullTemplate(repository string) error {
repository, refName := versioncontrol.ParsePinnedRemote(repository)
if err := versioncontrol.GitCheckRefName.Invoke("", map[string]string{"refname": refName}); err != nil {
fmt.Printf("Invalid tag or branch name `%s`\n", refName)
fmt.Println("See https://git-scm.com/docs/git-check-ref-format for more details of the rules Git enforces on branch and reference names.")
if refName != "" {
err := versioncontrol.GitCheckRefName.Invoke("", map[string]string{"refname": refName})
if err != nil {
fmt.Printf("Invalid tag or branch name `%s`\n", refName)
fmt.Println("See https://git-scm.com/docs/git-check-ref-format for more details of the rules Git enforces on branch and reference names.")
return err
return err
}
}
fmt.Printf("Fetch templates from repository: %s at %s\n", repository, refName)

View File

@@ -33,6 +33,16 @@ func Test_PullTemplates(t *testing.T) {
}
})
t.Run("fetchTemplates with default ref", func(t *testing.T) {
defer tearDownFetchTemplates(t)
err := fetchTemplates(localTemplateRepository, "", false)
if err != nil {
t.Error(err)
}
})
}
// setupLocalTemplateRepo will create a local copy of the core OpenFaaS templates, this

View File

@@ -14,7 +14,15 @@ var GitClone = &vcsCmd{
scheme: []string{"git", "https", "http", "git+ssh", "ssh"},
}
// GitCheckout defines the command to clone a repo into a directory
// GitClone defines the command to clone the default branch of a repo into a directory
var GitCloneDefault = &vcsCmd{
name: "Git",
cmd: "git",
cmds: []string{"clone {repo} {dir} --depth=1 --config core.autocrlf=false"},
scheme: []string{"git", "https", "http", "git+ssh", "ssh"},
}
// GitCheckout defines the command to clone a specific REF of repo into a directory
var GitCheckout = &vcsCmd{
name: "Git",
cmd: "git",

View File

@@ -35,7 +35,10 @@ func IsPinnedGitRemote(repoURL string) bool {
// ParsePinnedRemote returns the remote url and contraint value from repository url
func ParsePinnedRemote(repoURL string) (remoteURL, refName string) {
refName = "master"
// default refName is empty
// the template fetcher can detect this and will pull the default when
// the ref is empty
refName = ""
remoteURL = repoURL
// If using a Regexp in multiple goroutines,
@@ -54,7 +57,7 @@ func ParsePinnedRemote(repoURL string) (remoteURL, refName string) {
}
if !IsGitRemote(remoteURL) {
return repoURL, "master"
return remoteURL, refName
}
return remoteURL, refName

View File

@@ -144,14 +144,14 @@ func Test_ParsePinnedRemote(t *testing.T) {
})
t.Run(fmt.Sprintf("can parse default refname from url with %s", scenario.name), func(t *testing.T) {
t.Run(fmt.Sprintf("can parse default empty refname from url with %s", scenario.name), func(t *testing.T) {
remote, refName := ParsePinnedRemote(scenario.url)
if remote != scenario.url {
t.Errorf("expected remote url: %s, got: %s", scenario.url, remote)
}
if refName != "master" {
t.Errorf("expected refName: master, got: %s", refName)
if refName != "" {
t.Errorf("expected emtpy refName, got: %s", refName)
}
})