From eaa1551ec36be94d62b4fd90355145a4a937817e Mon Sep 17 00:00:00 2001 From: mansoora Date: Tue, 24 Dec 2024 12:27:35 +0530 Subject: [PATCH 1/2] fix for #631 where slight differences seen in Kptfile upstream formatting with kpt pkg get and kpt alpha rpkg clone --- pkg/kpt/clone.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/pkg/kpt/clone.go b/pkg/kpt/clone.go index 43076b87..b5190a7f 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,26 @@ 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 { + // Remove .git suffix if present + u.Git.Repo = strings.TrimSuffix(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 { + // Remove .git suffix if present + l.Git.Repo = strings.TrimSuffix(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 { From 3ee4fae7fb35657644342b32d96f8c4786a5d912 Mon Sep 17 00:00:00 2001 From: mansoora Date: Tue, 24 Dec 2024 13:12:21 +0530 Subject: [PATCH 2/2] fix for #631 where slight differences seen in Kptfile upstream formatting with kpt pkg get and kpt alpha rpkg clone --- pkg/kpt/clone.go | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/pkg/kpt/clone.go b/pkg/kpt/clone.go index b5190a7f..c1039e60 100644 --- a/pkg/kpt/clone.go +++ b/pkg/kpt/clone.go @@ -53,8 +53,11 @@ func UpdateUpstream(kptfileContents string, name string, upstream kptfilev1.Upst // normalizeGitFields ensures consistent formatting of git repository URLs and directory paths func normalizeGitFields(u *kptfilev1.Upstream) { if u.Git != nil { - // Remove .git suffix if present - u.Git.Repo = strings.TrimSuffix(u.Git.Repo, ".git") + // 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, "/") } @@ -63,13 +66,15 @@ func normalizeGitFields(u *kptfilev1.Upstream) { // normalizeGitLockFields ensures consistent formatting for UpstreamLock git fields func normalizeGitLockFields(l *kptfilev1.UpstreamLock) { if l.Git != nil { - // Remove .git suffix if present - l.Git.Repo = strings.TrimSuffix(l.Git.Repo, ".git") + // 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 {