WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit 7a61d11

Browse files
committed
Fixes for SHA vs branch vs HEAD checkout of templates
Signed-off-by: Alex Ellis (OpenFaaS Ltd) <[email protected]>
1 parent bfcc076 commit 7a61d11

File tree

4 files changed

+48
-34
lines changed

4 files changed

+48
-34
lines changed

commands/fetch_templates.go

Lines changed: 39 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -37,25 +37,28 @@ func fetchTemplates(templateURL, refName, templateName string, overwriteTemplate
3737

3838
log.Printf("Fetching templates from %s%s", templateURL, refMsg)
3939

40-
targetDir, err := os.MkdirTemp("", "openfaas-templates-*")
40+
extractedPath, err := os.MkdirTemp("", "openfaas-templates-*")
4141
if err != nil {
4242
return fmt.Errorf("unable to create temporary directory: %s", err)
4343
}
4444

4545
if !pullDebug {
46-
defer os.RemoveAll(targetDir)
46+
defer os.RemoveAll(extractedPath)
4747
}
4848

49-
pullDebugPrint(fmt.Sprintf("Temp files in %s", targetDir))
49+
pullDebugPrint(fmt.Sprintf("Temp files in %s", extractedPath))
5050

51-
args := map[string]string{"dir": targetDir, "repo": templateURL}
51+
args := map[string]string{"dir": extractedPath, "repo": templateURL}
5252
cmd := versioncontrol.GitCloneDefault
5353

5454
if len(refName) > 0 {
5555
if strings.HasPrefix(refName, "sha-") {
5656
cmd = versioncontrol.GitCloneFullDepth
5757
} else {
5858
args["refname"] = refName
59+
60+
cmd = versioncontrol.GitCloneBranch
61+
args["refname"] = refName
5962
}
6063
}
6164

@@ -64,12 +67,12 @@ func fetchTemplates(templateURL, refName, templateName string, overwriteTemplate
6467
}
6568

6669
if len(refName) > 0 && strings.HasPrefix(refName, "sha-") {
70+
6771
targetCommit := strings.TrimPrefix(refName, "sha-")
68-
checkoutArgs := []string{"-C", targetDir, "checkout", targetCommit}
6972

7073
t := execute.ExecTask{
7174
Command: "git",
72-
Args: checkoutArgs,
75+
Args: []string{"-C", extractedPath, "checkout", targetCommit},
7376
}
7477
res, err := t.Execute(context.Background())
7578
if err != nil {
@@ -79,16 +82,35 @@ func fetchTemplates(templateURL, refName, templateName string, overwriteTemplate
7982
out := res.Stdout + " " + res.Stderr
8083
return fmt.Errorf("error checking out ref %s: %s", targetCommit, out)
8184
}
82-
8385
}
8486

87+
task := execute.ExecTask{
88+
Command: "git",
89+
Args: []string{"-C", extractedPath, "log", "-1"},
90+
}
91+
res, err := task.Execute(context.Background())
92+
if err != nil {
93+
return fmt.Errorf("error getting git log: %w", err)
94+
}
95+
log.Printf("Git log: %s", res.Stdout)
8596
// Get the long SHA digest from the clone repository.
86-
sha, err := versioncontrol.GetGitSHAFor(targetDir, false)
97+
sha, err := versioncontrol.GetGitSHAFor(extractedPath, false)
8798
if err != nil {
8899
return err
89100
}
90101

91-
protectedLanguages, fetchedLanguages, err := moveTemplates(targetDir, templateName, overwriteTemplates, templateURL, refName, sha)
102+
cwd, err := os.Getwd()
103+
if err != nil {
104+
return fmt.Errorf("can't get current working directory: %s", err)
105+
}
106+
localTemplatesDir := filepath.Join(cwd, TemplateDirectory)
107+
if _, err := os.Stat(localTemplatesDir); err != nil && os.IsNotExist(err) {
108+
if err := os.MkdirAll(localTemplatesDir, 0755); err != nil {
109+
return fmt.Errorf("can't create template directory: %s", err)
110+
}
111+
}
112+
113+
protectedLanguages, fetchedLanguages, err := moveTemplates(localTemplatesDir, extractedPath, templateName, overwriteTemplates, templateURL, refName, sha)
92114
if err != nil {
93115
return err
94116
}
@@ -116,7 +138,7 @@ func canWriteLanguage(existingLanguages []string, language string, overwriteTemp
116138
// moveTemplates moves the templates from the repository to the template directory
117139
// It returns the existing languages and the fetched languages
118140
// It also returns an error if the templates cannot be read
119-
func moveTemplates(repoPath, templateName string, overwriteTemplate bool, repository string, refName string, sha string) ([]string, []string, error) {
141+
func moveTemplates(localTemplatesDir, extractedPath, templateName string, overwriteTemplate bool, repository string, refName string, sha string) ([]string, []string, error) {
120142

121143
var (
122144
existingLanguages []string
@@ -125,21 +147,9 @@ func moveTemplates(repoPath, templateName string, overwriteTemplate bool, reposi
125147
err error
126148
)
127149

128-
cwd, err := os.Getwd()
129-
if err != nil {
130-
return nil, nil, fmt.Errorf("can't get current working directory: %s", err)
131-
}
132-
133-
destTemplatePath := filepath.Join(cwd, TemplateDirectory)
134-
if _, err := os.Stat(destTemplatePath); err != nil && os.IsNotExist(err) {
135-
if err := os.MkdirAll(destTemplatePath, 0755); err != nil {
136-
return nil, nil, fmt.Errorf("can't create template directory: %s", err)
137-
}
138-
}
139-
140-
templateEntries, err := os.ReadDir(destTemplatePath)
150+
templateEntries, err := os.ReadDir(localTemplatesDir)
141151
if err != nil {
142-
return nil, nil, fmt.Errorf("unable to read directory: %s", destTemplatePath)
152+
return nil, nil, fmt.Errorf("unable to read directory: %s", localTemplatesDir)
143153
}
144154

145155
// OK if nothing exists yet
@@ -148,17 +158,17 @@ func moveTemplates(repoPath, templateName string, overwriteTemplate bool, reposi
148158
continue
149159
}
150160

151-
templateFile := filepath.Join(destTemplatePath, entry.Name(), "template.yml")
161+
templateFile := filepath.Join(localTemplatesDir, entry.Name(), "template.yml")
152162
if _, err := os.Stat(templateFile); err != nil && !os.IsNotExist(err) {
153163
return nil, nil, fmt.Errorf("can't find template.yml in: %s", templateFile)
154164
}
155165

156166
existingLanguages = append(existingLanguages, entry.Name())
157167
}
158168

159-
extractedTemplates, err := os.ReadDir(filepath.Join(repoPath, TemplateDirectory))
169+
extractedTemplates, err := os.ReadDir(filepath.Join(extractedPath, TemplateDirectory))
160170
if err != nil {
161-
return nil, nil, fmt.Errorf("can't find templates in: %s", filepath.Join(repoPath, TemplateDirectory))
171+
return nil, nil, fmt.Errorf("can't find templates in: %s", filepath.Join(extractedPath, TemplateDirectory))
162172
}
163173

164174
for _, entry := range extractedTemplates {
@@ -173,8 +183,8 @@ func moveTemplates(repoPath, templateName string, overwriteTemplate bool, reposi
173183

174184
if canWriteLanguage(existingLanguages, language, overwriteTemplate) {
175185
// Do cp here
176-
languageSrc := filepath.Join(repoPath, TemplateDirectory, language)
177-
languageDest := filepath.Join(destTemplatePath, language)
186+
languageSrc := filepath.Join(extractedPath, TemplateDirectory, language)
187+
languageDest := filepath.Join(localTemplatesDir, language)
178188
langName := language
179189
if refName != "" {
180190
languageDest += "@" + refName

commands/template_store_pull.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ func runTemplateStorePull(cmd *cobra.Command, args []string) error {
7272
break
7373
}
7474
}
75+
7576
if !found {
7677
return fmt.Errorf("template with name: `%s` does not exist in the repo", templateName)
7778
}

versioncontrol/core.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package versioncontrol
55
import (
66
"bytes"
77
"fmt"
8+
"log"
89
"os"
910
"os/exec"
1011
"strings"
@@ -48,6 +49,7 @@ func (v *vcsCmd) run(dir string, cmdline string, keyval map[string]string, verbo
4849
return nil, err
4950
}
5051

52+
log.Printf("[git] Running command: %s %s", v.cmd, strings.Join(args, " "))
5153
cmd := exec.Command(v.cmd, args...)
5254
cmd.Dir = dir
5355
cmd.Env = envWithPWD(cmd.Dir)

versioncontrol/git.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,32 @@ import (
77
"github.com/openfaas/faas-cli/exec"
88
)
99

10-
// GitClone defines the command to clone a repo into a directory
11-
var GitClone = &vcsCmd{
10+
// GitCloneBranch clones a specific branch of a repo into a directory
11+
var GitCloneBranch = &vcsCmd{
1212
name: "Git",
1313
cmd: "git",
1414
cmds: []string{"clone {repo} {dir} --depth=1 --config core.autocrlf=false -b {refname}"},
1515
scheme: []string{"git", "https", "http", "git+ssh", "ssh"},
1616
}
1717

18-
// GitCloneFullDepth defines the command to clone a repo into a directory with full depth
18+
// GitCloneFullDepth clones a repo into a directory with full depth so that a
19+
// SHA can be checked out after
1920
var GitCloneFullDepth = &vcsCmd{
2021
name: "Git",
2122
cmd: "git",
2223
cmds: []string{"clone {repo} {dir} --config core.autocrlf=false"},
2324
scheme: []string{"git", "https", "http", "git+ssh", "ssh"},
2425
}
2526

26-
// GitClone defines the command to clone the default branch of a repo into a directory
27+
// GitCloneDefault clones the default branch of a repo into a directory
2728
var GitCloneDefault = &vcsCmd{
2829
name: "Git",
2930
cmd: "git",
3031
cmds: []string{"clone {repo} {dir} --depth=1 --config core.autocrlf=false"},
3132
scheme: []string{"git", "https", "http", "git+ssh", "ssh"},
3233
}
3334

34-
// GitCheckout defines the command to clone a specific REF of repo into a directory
35+
// GitCheckout checks out a specific REF of a repo into a directory
3536
var GitCheckout = &vcsCmd{
3637
name: "Git",
3738
cmd: "git",

0 commit comments

Comments
 (0)