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 <raliev@mirantis.com>
This commit is contained in:
Ruslan Aliev 2021-01-12 10:09:41 -06:00
parent 78e0e81bc0
commit 0e44b580f0
6 changed files with 19 additions and 3 deletions

View File

@ -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,
}
}

View File

@ -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/

View File

@ -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/

View File

@ -1,4 +1,5 @@
branch: ""
commitHash: ""
force: false
localBranch: false
tag: v1.0.1

View File

@ -5,5 +5,6 @@ checkout:
branch: ""
commitHash: ""
force: false
localBranch: false
tag: v1.0.1
url: http://dummy.url.com/manifests.git

View File

@ -73,6 +73,7 @@ func TestPull(t *testing.T) {
name: "TestCloneRepositoriesValidOpts",
checkoutOpts: &config.RepoCheckout{
Branch: "master",
LocalBranch: true,
ForceCheckout: false,
},
error: nil,