From 66159b0c8eeee7a202e37f3865e77d9039123d16 Mon Sep 17 00:00:00 2001 From: Binbin Ye Date: Thu, 7 Nov 2024 16:00:00 +0900 Subject: [PATCH 1/4] fix(checkout): do not override existing branch remote and use origin as remote if possible --- commands/mr/checkout/mr_checkout.go | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/commands/mr/checkout/mr_checkout.go b/commands/mr/checkout/mr_checkout.go index 897e31c8b..96b935389 100644 --- a/commands/mr/checkout/mr_checkout.go +++ b/commands/mr/checkout/mr_checkout.go @@ -2,6 +2,7 @@ package checkout import ( "fmt" + "log" "strings" "github.com/MakeNowJust/heredoc/v2" @@ -10,6 +11,7 @@ import ( "gitlab.com/gitlab-org/cli/api" "gitlab.com/gitlab-org/cli/commands/cmdutils" "gitlab.com/gitlab-org/cli/commands/mr/mrutils" + "gitlab.com/gitlab-org/cli/internal/run" "gitlab.com/gitlab-org/cli/pkg/git" ) @@ -100,9 +102,28 @@ func NewCmdCheckout(f *cmdutils.Factory) *cobra.Command { // .remote is needed for `git pull` to work // .pushRemote is needed for `git push` to work, if user has set `remote.pushDefault`. // see https://git-scm.com/docs/git-config#Documentation/git-config.txt-branchltnamegtremote - if err := git.RunCmd([]string{"config", fmt.Sprintf("branch.%s.remote", mrCheckoutCfg.branch), mrProject.SSHURLToRepo}); err != nil { - return err + if err := git.RunCmd([]string{"config", fmt.Sprintf("branch.%s.remote", mrCheckoutCfg.branch)}); err != nil { + // It is not configured + branchRemoteURL := mrProject.SSHURLToRepo + + remoteUrlCmd := git.GitCommand("remote", "get-url", git.DefaultRemote) + originRemoteURLByte, err := run.PrepareCmd(remoteUrlCmd).Output() + if err == nil { + // in SSHURLToRepo we trust + url1, _ := git.ParseURL(mrProject.SSHURLToRepo) + url2, err := git.ParseURL(strings.TrimSpace(string(originRemoteURLByte))) + if err == nil && strings.Contains(url1.String(), url2.String()) { + branchRemoteURL = git.DefaultRemote + } + + } + + if err := git.RunCmd([]string{"config", fmt.Sprintf("branch.%s.remote", mrCheckoutCfg.branch), branchRemoteURL}); err != nil { + return err + } + } + if mr.AllowCollaboration { if err := git.RunCmd([]string{"config", fmt.Sprintf("branch.%s.pushRemote", mrCheckoutCfg.branch), mrProject.SSHURLToRepo}); err != nil { return err -- GitLab From e9c6517a86860f7873b6224a038acc832ad84802 Mon Sep 17 00:00:00 2001 From: Binbin Ye Date: Thu, 7 Nov 2024 16:06:19 +0900 Subject: [PATCH 2/4] remove unused import --- commands/mr/checkout/mr_checkout.go | 1 - 1 file changed, 1 deletion(-) diff --git a/commands/mr/checkout/mr_checkout.go b/commands/mr/checkout/mr_checkout.go index 96b935389..09978c9ce 100644 --- a/commands/mr/checkout/mr_checkout.go +++ b/commands/mr/checkout/mr_checkout.go @@ -2,7 +2,6 @@ package checkout import ( "fmt" - "log" "strings" "github.com/MakeNowJust/heredoc/v2" -- GitLab From 3c9f5bd948280f4572d9e523cc6975be9a86512c Mon Sep 17 00:00:00 2001 From: Binbin Ye Date: Tue, 19 Nov 2024 11:03:52 +0900 Subject: [PATCH 3/4] compare all the remotes --- commands/mr/checkout/mr_checkout.go | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/commands/mr/checkout/mr_checkout.go b/commands/mr/checkout/mr_checkout.go index 09978c9ce..551359a8f 100644 --- a/commands/mr/checkout/mr_checkout.go +++ b/commands/mr/checkout/mr_checkout.go @@ -105,16 +105,27 @@ func NewCmdCheckout(f *cmdutils.Factory) *cobra.Command { // It is not configured branchRemoteURL := mrProject.SSHURLToRepo - remoteUrlCmd := git.GitCommand("remote", "get-url", git.DefaultRemote) - originRemoteURLByte, err := run.PrepareCmd(remoteUrlCmd).Output() + listRemoteCmd := git.GitCommand("remote") + + listRemoteByte, err := run.PrepareCmd(listRemoteCmd).Output() + if err == nil { - // in SSHURLToRepo we trust - url1, _ := git.ParseURL(mrProject.SSHURLToRepo) - url2, err := git.ParseURL(strings.TrimSpace(string(originRemoteURLByte))) - if err == nil && strings.Contains(url1.String(), url2.String()) { - branchRemoteURL = git.DefaultRemote + remotes := strings.Split(string(listRemoteByte), "\n") + + for _, remote := range remotes { + remoteUrlCmd := git.GitCommand("remote", "get-url", remote) + remoteURLByte, err := run.PrepareCmd(remoteUrlCmd).Output() + if err == nil { + // in SSHURLToRepo we trust + url1, _ := git.ParseURL(mrProject.SSHURLToRepo) + url2, err := git.ParseURL(strings.TrimSpace(string(remoteURLByte))) + if err == nil && strings.Contains(url1.String(), url2.String()) { + branchRemoteURL = remote + break + } + + } } - } if err := git.RunCmd([]string{"config", fmt.Sprintf("branch.%s.remote", mrCheckoutCfg.branch), branchRemoteURL}); err != nil { -- GitLab From 100e59fb68e0d1df0ecac33a40c8f1074cff63c5 Mon Sep 17 00:00:00 2001 From: Binbin Ye Date: Tue, 19 Nov 2024 11:25:49 +0900 Subject: [PATCH 4/4] handle pushRemote as well --- commands/mr/checkout/mr_checkout.go | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/commands/mr/checkout/mr_checkout.go b/commands/mr/checkout/mr_checkout.go index 551359a8f..e2d8d1e71 100644 --- a/commands/mr/checkout/mr_checkout.go +++ b/commands/mr/checkout/mr_checkout.go @@ -132,13 +132,14 @@ func NewCmdCheckout(f *cmdutils.Factory) *cobra.Command { return err } - } - - if mr.AllowCollaboration { - if err := git.RunCmd([]string{"config", fmt.Sprintf("branch.%s.pushRemote", mrCheckoutCfg.branch), mrProject.SSHURLToRepo}); err != nil { - return err + if mr.AllowCollaboration { + if err := git.RunCmd([]string{"config", fmt.Sprintf("branch.%s.pushRemote", mrCheckoutCfg.branch), branchRemoteURL}); err != nil { + return err + } } + } + if err := git.RunCmd([]string{"config", fmt.Sprintf("branch.%s.merge", mrCheckoutCfg.branch), mrRef}); err != nil { return err } -- GitLab