Skip to content

Commit c990659

Browse files
authored
feat: check and warn on local to remote branches mismatch (knative#2884)
1 parent a384d6e commit c990659

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

cmd/deploy.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"errors"
55
"fmt"
66
"io"
7+
"os"
8+
"os/exec"
79
"strconv"
810
"strings"
911

@@ -777,6 +779,36 @@ func printDeployMessages(out io.Writer, f fn.Function) {
777779
if !f.Local.Remote && (f.Build.Git.URL != "" || f.Build.Git.Revision != "" || f.Build.Git.ContextDir != "") {
778780
fmt.Fprintf(out, "Warning: git settings are only applicable when running with --remote. Local source code will be used.")
779781
}
782+
783+
// Git Branch Mismatch
784+
// -------------------
785+
// When doing a remote build with --git-branch, warn if the local branch
786+
// doesn't match, as this can lead to confusion about which func.yaml is used.
787+
if f.Local.Remote && f.Build.Git.URL != "" && f.Build.Git.Revision != "" {
788+
// Doing a remote build, specified a git repository to pull from, and
789+
// specified a reference within that remote.
790+
currentBranch, err := getCurrentGitBranch()
791+
if err != nil {
792+
fmt.Fprintf(out, "Warning: unable to verify local and remote references match. %v\n", err)
793+
} else if currentBranch != f.Build.Git.Revision {
794+
fmt.Fprintf(out, "Warning: Local git branch '%s' does not match --git-branch '%s'. The local func.yaml will be used for function metadata (name, runtime, etc). Ensure your local branch matches the remote branch to avoid deployment issues.\n", currentBranch, f.Build.Git.Revision)
795+
}
796+
}
797+
}
798+
799+
// getCurrentGitBranch returns the current git branch name
800+
func getCurrentGitBranch() (string, error) {
801+
gitCmd := os.Getenv("FUNC_GIT")
802+
if gitCmd == "" {
803+
gitCmd = "git"
804+
}
805+
806+
cmd := exec.Command(gitCmd, "rev-parse", "--abbrev-ref", "HEAD")
807+
output, err := cmd.Output()
808+
if err != nil {
809+
return "", err
810+
}
811+
return strings.TrimSpace(string(output)), nil
780812
}
781813

782814
// isDigested checks that the given image reference has a digest. Invalid

0 commit comments

Comments
 (0)