Skip to content

Commit

Permalink
cmd/mr_checkout: check existing remotes before creating one
Browse files Browse the repository at this point in the history
In case tacking is enabled `lab mr checkout -t`, a new remote is created
with the name of the MR author's name without checking if the already
available remotes matches the one from which the MR was checked-out.
A particular case where it happens constantly is in the branching
development model: developers use a single repo to push their work as
branches.

This patch change the code behavor by first checking if any of the
existent remotes has the same path as the source MR project before
creating a new remote. If no remote is found, then create a new one with
the MR author's name.

Signed-off-by: Bruno Meneguele <[email protected]>
  • Loading branch information
bmeneg committed Feb 4, 2022
1 parent 7368ada commit 862a246
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
36 changes: 27 additions & 9 deletions cmd/mr_checkout.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ package cmd

import (
"fmt"
"github.com/MakeNowJust/heredoc/v2"
"os"

"github.com/MakeNowJust/heredoc/v2"
"github.com/rsteube/carapace"
"github.com/spf13/cobra"
gitconfig "github.com/tcnksm/go-gitconfig"
gitlab "github.com/xanzy/go-gitlab"
"github.com/zaquestion/lab/internal/action"
"github.com/zaquestion/lab/internal/git"
Expand Down Expand Up @@ -71,18 +70,37 @@ var checkoutCmd = &cobra.Command{
// the fetchToRef to the mr author/sourceBranch
if mrCheckoutCfg.track {
// Check if remote already exists
if _, err := gitconfig.Local("remote." + mr.Author.Username + ".url"); err != nil {
// Find and create remote
mrProject, err := lab.GetProject(mr.SourceProjectID)
project, err := lab.GetProject(mr.SourceProjectID)
if err != nil {
log.Fatal(err)
}

remotes, err := git.Remotes()
if err != nil {
log.Fatal(err)
}

remoteName := ""
for _, remote := range remotes {
path, err := git.PathWithNamespace(remote)
if err != nil {
log.Fatal(err)
continue
}
urlToRepo := labURLToRepo(mrProject)
if err := git.RemoteAdd(mr.Author.Username, urlToRepo, "."); err != nil {
if path == project.PathWithNamespace {
remoteName = remote
}
}

if remoteName == "" {
remoteName = mr.Author.Username
urlToRepo := labURLToRepo(project)
err := git.RemoteAdd(remoteName, urlToRepo, ".")
if err != nil {
log.Fatal(err)
}
}
fetchToRef = fmt.Sprintf("refs/remotes/%s/%s", mr.Author.Username, mr.SourceBranch)

fetchToRef = fmt.Sprintf("refs/remotes/%s/%s", remoteName, mr.SourceBranch)
}

if err := git.New("show-ref", "--verify", "--quiet", "refs/heads/"+fetchToRef).Run(); err == nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/mr_checkout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func Test_mrCheckoutCmd_track(t *testing.T) {
t.Fatal(err)
}
remotes := string(gitOut)
require.Contains(t, remotes, "zaquestion [email protected]:zaquestion/test.git")
require.Contains(t, remotes, "origin [email protected]:zaquestion/test.git")
}

func Test_mrCheckoutCmdRunWithDifferentName(t *testing.T) {
Expand Down

0 comments on commit 862a246

Please sign in to comment.