Skip to content

Commit 004f20b

Browse files
LucasRoesleralexellis
authored andcommitted
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 <[email protected]>
1 parent 81b166a commit 004f20b

File tree

5 files changed

+43
-12
lines changed

5 files changed

+43
-12
lines changed

commands/fetch_templates.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,15 @@ func fetchTemplates(templateURL string, refName string, overwrite bool) error {
3535

3636
log.Printf("Attempting to expand templates from %s\n", templateURL)
3737
pullDebugPrint(fmt.Sprintf("Temp files in %s", dir))
38-
args := map[string]string{"dir": dir, "repo": templateURL, "refname": refName}
39-
if err := versioncontrol.GitClone.Invoke(".", args); err != nil {
38+
args := map[string]string{"dir": dir, "repo": templateURL}
39+
cmd := versioncontrol.GitCloneDefault
40+
41+
if refName != "" {
42+
args["refname"] = refName
43+
cmd = versioncontrol.GitClone
44+
}
45+
46+
if err := cmd.Invoke(".", args); err != nil {
4047
return err
4148
}
4249

@@ -126,11 +133,14 @@ func pullTemplate(repository string) error {
126133

127134
repository, refName := versioncontrol.ParsePinnedRemote(repository)
128135

129-
if err := versioncontrol.GitCheckRefName.Invoke("", map[string]string{"refname": refName}); err != nil {
130-
fmt.Printf("Invalid tag or branch name `%s`\n", refName)
131-
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.")
136+
if refName != "" {
137+
err := versioncontrol.GitCheckRefName.Invoke("", map[string]string{"refname": refName})
138+
if err != nil {
139+
fmt.Printf("Invalid tag or branch name `%s`\n", refName)
140+
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.")
132141

133-
return err
142+
return err
143+
}
134144
}
135145

136146
fmt.Printf("Fetch templates from repository: %s at %s\n", repository, refName)

commands/fetch_templates_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@ func Test_PullTemplates(t *testing.T) {
3333
}
3434

3535
})
36+
37+
t.Run("fetchTemplates with default ref", func(t *testing.T) {
38+
defer tearDownFetchTemplates(t)
39+
40+
err := fetchTemplates(localTemplateRepository, "", false)
41+
if err != nil {
42+
t.Error(err)
43+
}
44+
45+
})
3646
}
3747

3848
// setupLocalTemplateRepo will create a local copy of the core OpenFaaS templates, this

versioncontrol/git.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,15 @@ var GitClone = &vcsCmd{
1414
scheme: []string{"git", "https", "http", "git+ssh", "ssh"},
1515
}
1616

17-
// GitCheckout defines the command to clone a repo into a directory
17+
// GitClone defines the command to clone the default branch of a repo into a directory
18+
var GitCloneDefault = &vcsCmd{
19+
name: "Git",
20+
cmd: "git",
21+
cmds: []string{"clone {repo} {dir} --depth=1 --config core.autocrlf=false"},
22+
scheme: []string{"git", "https", "http", "git+ssh", "ssh"},
23+
}
24+
25+
// GitCheckout defines the command to clone a specific REF of repo into a directory
1826
var GitCheckout = &vcsCmd{
1927
name: "Git",
2028
cmd: "git",

versioncontrol/parse.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ func IsPinnedGitRemote(repoURL string) bool {
3535

3636
// ParsePinnedRemote returns the remote url and contraint value from repository url
3737
func ParsePinnedRemote(repoURL string) (remoteURL, refName string) {
38-
refName = "master"
38+
// default refName is empty
39+
// the template fetcher can detect this and will pull the default when
40+
// the ref is empty
41+
refName = ""
3942
remoteURL = repoURL
4043

4144
// If using a Regexp in multiple goroutines,
@@ -54,7 +57,7 @@ func ParsePinnedRemote(repoURL string) (remoteURL, refName string) {
5457
}
5558

5659
if !IsGitRemote(remoteURL) {
57-
return repoURL, "master"
60+
return remoteURL, refName
5861
}
5962

6063
return remoteURL, refName

versioncontrol/parse_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,14 +144,14 @@ func Test_ParsePinnedRemote(t *testing.T) {
144144

145145
})
146146

147-
t.Run(fmt.Sprintf("can parse default refname from url with %s", scenario.name), func(t *testing.T) {
147+
t.Run(fmt.Sprintf("can parse default empty refname from url with %s", scenario.name), func(t *testing.T) {
148148
remote, refName := ParsePinnedRemote(scenario.url)
149149
if remote != scenario.url {
150150
t.Errorf("expected remote url: %s, got: %s", scenario.url, remote)
151151
}
152152

153-
if refName != "master" {
154-
t.Errorf("expected refName: master, got: %s", refName)
153+
if refName != "" {
154+
t.Errorf("expected emtpy refName, got: %s", refName)
155155
}
156156

157157
})

0 commit comments

Comments
 (0)