diff --git a/pkg/kpt/clone.go b/pkg/kpt/clone.go index 43076b87..c1039e60 100644 --- a/pkg/kpt/clone.go +++ b/pkg/kpt/clone.go @@ -31,6 +31,10 @@ func UpdateUpstream(kptfileContents string, name string, upstream kptfilev1.Upst return "", fmt.Errorf("cannot parse Kptfile: %w", err) } + // Normalize the repository URL and directory path + normalizeGitFields(&upstream) + normalizeGitLockFields(&lock) // Use separate function for lock + // populate the cloneFrom values so we know where the package came from kptfile.UpstreamLock = &lock kptfile.Upstream = &upstream @@ -46,6 +50,31 @@ func UpdateUpstream(kptfileContents string, name string, upstream kptfilev1.Upst return string(b), nil } +// normalizeGitFields ensures consistent formatting of git repository URLs and directory paths +func normalizeGitFields(u *kptfilev1.Upstream) { + if u.Git != nil { + // Ensure .git suffix is present + if !strings.HasSuffix(u.Git.Repo, ".git") { + u.Git.Repo = u.Git.Repo + ".git" + } + + // Ensure directory doesn't start with a slash + u.Git.Directory = strings.TrimPrefix(u.Git.Directory, "/") + } +} + +// normalizeGitLockFields ensures consistent formatting for UpstreamLock git fields +func normalizeGitLockFields(l *kptfilev1.UpstreamLock) { + if l.Git != nil { + // Ensure .git suffix is present + if !strings.HasSuffix(l.Git.Repo, ".git") { + l.Git.Repo = l.Git.Repo + ".git" + } + + // Ensure directory doesn't start with a slash + l.Git.Directory = strings.TrimPrefix(l.Git.Directory, "/") + } +} func UpdateName(kptfileContents string, name string) (string, error) { kptfile, err := internalpkg.DecodeKptfile(strings.NewReader(kptfileContents)) if err != nil {