Skip to content

Commit bfcc076

Browse files
committed
Fix checking out of a SHA by using a different git command
The SHA-based workflow which was tested appeared to fail to checkout the specific SHA due to the depth being 1 in the default command, and the reference name being added and not available. Signed-off-by: Alex Ellis (OpenFaaS Ltd) <[email protected]>
1 parent 2baa2cd commit bfcc076

File tree

2 files changed

+38
-10
lines changed

2 files changed

+38
-10
lines changed

commands/fetch_templates.go

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,17 @@
44
package commands
55

66
import (
7+
"context"
78
"encoding/json"
89
"fmt"
910
"log"
1011
"os"
11-
"os/exec"
1212
"path/filepath"
1313
"slices"
1414
"strings"
1515
"time"
1616

17+
execute "github.com/alexellis/go-execute/v2"
1718
"github.com/openfaas/faas-cli/builder"
1819
"github.com/openfaas/faas-cli/versioncontrol"
1920
)
@@ -29,11 +30,16 @@ func fetchTemplates(templateURL, refName, templateName string, overwriteTemplate
2930
return fmt.Errorf("pass valid templateURL")
3031
}
3132

32-
log.Printf("Fetching templates from %s", templateURL)
33+
refMsg := ""
34+
if len(refName) > 0 {
35+
refMsg = " [" + refName + "]"
36+
}
37+
38+
log.Printf("Fetching templates from %s%s", templateURL, refMsg)
3339

3440
targetDir, err := os.MkdirTemp("", "openfaas-templates-*")
3541
if err != nil {
36-
return err
42+
return fmt.Errorf("unable to create temporary directory: %s", err)
3743
}
3844

3945
if !pullDebug {
@@ -45,21 +51,35 @@ func fetchTemplates(templateURL, refName, templateName string, overwriteTemplate
4551
args := map[string]string{"dir": targetDir, "repo": templateURL}
4652
cmd := versioncontrol.GitCloneDefault
4753

48-
if len(refName) > 0 && !strings.HasPrefix(refName, "sha-") {
49-
args["refname"] = refName
50-
cmd = versioncontrol.GitClone
54+
if len(refName) > 0 {
55+
if strings.HasPrefix(refName, "sha-") {
56+
cmd = versioncontrol.GitCloneFullDepth
57+
} else {
58+
args["refname"] = refName
59+
}
5160
}
5261

5362
if err := cmd.Invoke(".", args); err != nil {
54-
return err
63+
return fmt.Errorf("error invoking git clone: %w", err)
5564
}
5665

5766
if len(refName) > 0 && strings.HasPrefix(refName, "sha-") {
58-
checkout := exec.Command("git", "-C", targetDir, "checkout", strings.TrimPrefix(refName, "sha-"))
67+
targetCommit := strings.TrimPrefix(refName, "sha-")
68+
checkoutArgs := []string{"-C", targetDir, "checkout", targetCommit}
5969

60-
if err := checkout.Run(); err != nil {
61-
return err
70+
t := execute.ExecTask{
71+
Command: "git",
72+
Args: checkoutArgs,
73+
}
74+
res, err := t.Execute(context.Background())
75+
if err != nil {
76+
return fmt.Errorf("error checking out ref %s: %w", targetCommit, err)
6277
}
78+
if res.ExitCode != 0 {
79+
out := res.Stdout + " " + res.Stderr
80+
return fmt.Errorf("error checking out ref %s: %s", targetCommit, out)
81+
}
82+
6383
}
6484

6585
// Get the long SHA digest from the clone repository.

versioncontrol/git.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ var GitClone = &vcsCmd{
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
19+
var GitCloneFullDepth = &vcsCmd{
20+
name: "Git",
21+
cmd: "git",
22+
cmds: []string{"clone {repo} {dir} --config core.autocrlf=false"},
23+
scheme: []string{"git", "https", "http", "git+ssh", "ssh"},
24+
}
25+
1826
// GitClone defines the command to clone the default branch of a repo into a directory
1927
var GitCloneDefault = &vcsCmd{
2028
name: "Git",

0 commit comments

Comments
 (0)