From 0e44b580f02ec25a3d6897fef412ba52f314eb21 Mon Sep 17 00:00:00 2001 From: Ruslan Aliev Date: Tue, 12 Jan 2021 10:09:41 -0600 Subject: [PATCH] Fix pulling docs from remote branch Currently airshipctl is not able to checkout remote branch due to using simple branch reference name instead of remote one. This patch fixes appropriate problem and allows user to specify what kind of branch to use - local or remote one (since reference name will be different in these scenarios). Change-Id: I1fea29f84097b9e7160597003d0e8961f4d3aca6 Signed-off-by: Ruslan Aliev --- pkg/config/repo.go | 17 ++++++++++++++--- pkg/config/testdata/config-string.yaml | 1 + pkg/config/testdata/manifest-string.yaml | 1 + pkg/config/testdata/repo-checkout-string.yaml | 1 + pkg/config/testdata/repository-string.yaml | 1 + pkg/document/pull/pull_test.go | 1 + 6 files changed, 19 insertions(+), 3 deletions(-) diff --git a/pkg/config/repo.go b/pkg/config/repo.go index 632301831..6cd0e9658 100644 --- a/pkg/config/repo.go +++ b/pkg/config/repo.go @@ -35,6 +35,10 @@ const ( HTTPBasic = "http-basic" ) +// remoteName is a remote name that airshipctl work with during document pull +// TODO (raliev) consider make this variable configurable via repoCheckout options +var remoteName = git.DefaultRemoteName + // Repository struct holds the information for the remote sources of manifest yaml documents. // Information such as location, authentication info, // as well as details of what to get such as branch, tag, commit it, etc. @@ -80,6 +84,8 @@ type RepoCheckout struct { RemoteRef string `json:"remoteRef,omitempty"` // ForceCheckout is a boolean to indicate whether to use the `--force` option when checking out ForceCheckout bool `json:"force"` + // LocalBranch is a boolean to indicate whether the Branch is local one. False by default + LocalBranch bool `json:"localBranch"` } // RepoCheckout methods @@ -223,7 +229,11 @@ func (repo *Repository) ToCheckoutOptions() *git.CheckoutOptions { co.Force = repo.CheckoutOptions.ForceCheckout switch { case repo.CheckoutOptions.Branch != "": - co.Branch = plumbing.NewBranchReferenceName(repo.CheckoutOptions.Branch) + if repo.CheckoutOptions.LocalBranch { + co.Branch = plumbing.NewBranchReferenceName(repo.CheckoutOptions.Branch) + } else { + co.Branch = plumbing.NewRemoteReferenceName(remoteName, repo.CheckoutOptions.Branch) + } case repo.CheckoutOptions.Tag != "": co.Branch = plumbing.NewTagReferenceName(repo.CheckoutOptions.Tag) case repo.CheckoutOptions.CommitHash != "": @@ -238,8 +248,9 @@ func (repo *Repository) ToCheckoutOptions() *git.CheckoutOptions { // CloneOptions describes how a clone should be performed func (repo *Repository) ToCloneOptions(auth transport.AuthMethod) *git.CloneOptions { return &git.CloneOptions{ - Auth: auth, - URL: repo.URLString, + Auth: auth, + URL: repo.URLString, + RemoteName: remoteName, } } diff --git a/pkg/config/testdata/config-string.yaml b/pkg/config/testdata/config-string.yaml index c8eebdf41..70891e9f3 100644 --- a/pkg/config/testdata/config-string.yaml +++ b/pkg/config/testdata/config-string.yaml @@ -28,6 +28,7 @@ manifests: branch: "" commitHash: "" force: false + localBranch: false tag: v1.0.1 url: http://dummy.url.com/manifests.git targetPath: /var/tmp/ diff --git a/pkg/config/testdata/manifest-string.yaml b/pkg/config/testdata/manifest-string.yaml index cb67f9432..e13a4c670 100644 --- a/pkg/config/testdata/manifest-string.yaml +++ b/pkg/config/testdata/manifest-string.yaml @@ -9,6 +9,7 @@ repositories: branch: "" commitHash: "" force: false + localBranch: false tag: v1.0.1 url: http://dummy.url.com/manifests.git targetPath: /var/tmp/ diff --git a/pkg/config/testdata/repo-checkout-string.yaml b/pkg/config/testdata/repo-checkout-string.yaml index 221fd041d..15f5d639e 100644 --- a/pkg/config/testdata/repo-checkout-string.yaml +++ b/pkg/config/testdata/repo-checkout-string.yaml @@ -1,4 +1,5 @@ branch: "" commitHash: "" force: false +localBranch: false tag: v1.0.1 diff --git a/pkg/config/testdata/repository-string.yaml b/pkg/config/testdata/repository-string.yaml index eebb784ef..ed2a85dd8 100644 --- a/pkg/config/testdata/repository-string.yaml +++ b/pkg/config/testdata/repository-string.yaml @@ -5,5 +5,6 @@ checkout: branch: "" commitHash: "" force: false + localBranch: false tag: v1.0.1 url: http://dummy.url.com/manifests.git diff --git a/pkg/document/pull/pull_test.go b/pkg/document/pull/pull_test.go index aeb5fbf4b..a8660e609 100644 --- a/pkg/document/pull/pull_test.go +++ b/pkg/document/pull/pull_test.go @@ -73,6 +73,7 @@ func TestPull(t *testing.T) { name: "TestCloneRepositoriesValidOpts", checkoutOpts: &config.RepoCheckout{ Branch: "master", + LocalBranch: true, ForceCheckout: false, }, error: nil,