Skip to content

Commit

Permalink
mr checkout: Add --force option
Browse files Browse the repository at this point in the history
Users have reported a minor annoyance with 'lab mr checkout':

- reviewer checkouts out MR for review
lab mr checkout 3
- reviewer requests changes
- author makes changes
- reviewer checkouts out MR for re-review
lab mr checkout 3

and is returned a "ERROR: mr 3 branch branchname already
exists."

In most cases that I can think of the person checking out the MR
wants the latest version of the the code in the MR, not old code.  It
would be convenient to have a --force option that would delete and
overwrite the existing MR branch.

Add 'lab mr checkout --force' option that overwrites an existing MR branch
checkout.

Signed-off-by: Prarit Bhargava <[email protected]>
  • Loading branch information
prarit committed Mar 15, 2021
1 parent 52a9daf commit c81e575
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
12 changes: 10 additions & 2 deletions cmd/mr_checkout.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
// mrCheckoutConfig holds configuration values for calls to lab mr checkout
type mrCheckoutConfig struct {
branch string
force bool
track bool
}

Expand Down Expand Up @@ -80,8 +81,14 @@ var checkoutCmd = &cobra.Command{
}

if err := git.New("show-ref", "--verify", "--quiet", "refs/heads/"+fetchToRef).Run(); err == nil {
fmt.Println("ERROR: mr", mrID, "branch", fetchToRef, "already exists.")
os.Exit(1)
if mrCheckoutCfg.force {
if err := git.New("branch", "-D", mrCheckoutCfg.branch).Run(); err != nil {
log.Fatal(err)
}
} else {
fmt.Println("ERROR: mr", mrID, "branch", fetchToRef, "already exists.")
os.Exit(1)
}
}

// https://docs.gitlab.com/ce/user/project/merge_requests/#checkout-merge-requests-locally
Expand Down Expand Up @@ -111,6 +118,7 @@ func init() {
checkoutCmd.Flags().BoolVarP(&mrCheckoutCfg.track, "track", "t", false, "set checked out branch to track mr author remote branch, adds remote if needed")
// useHTTP is defined in "project_create.go"
checkoutCmd.Flags().BoolVar(&useHTTP, "http", false, "checkout using HTTP protocol instead of SSH")
checkoutCmd.Flags().BoolVarP(&mrCheckoutCfg.force, "force", "f", false, "force branch checkout and override existing branch")
mrCmd.AddCommand(checkoutCmd)
carapace.Gen(checkoutCmd).PositionalCompletion(
carapace.ActionCallback(func(c carapace.Context) carapace.Action {
Expand Down
37 changes: 36 additions & 1 deletion cmd/mr_checkout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func Test_mrCheckoutCmdRunWithDifferentName(t *testing.T) {
require.Contains(t, eLog, "54fd49a2ac60aeeef5ddc75efecd49f85f7ba9b0")
}

func Test_mrDoubleCheckoutCmdRun(t *testing.T) {
func Test_mrDoubleCheckoutFailCmdRun(t *testing.T) {
repo := copyTestRepo(t)

// make sure the branch does not exist
Expand All @@ -153,3 +153,38 @@ func Test_mrDoubleCheckoutCmdRun(t *testing.T) {
}
require.Contains(t, eLog, "ERROR: mr 1 branch mrtest already exists")
}

func Test_mrDoubleCheckoutForceRun(t *testing.T) {
repo := copyTestRepo(t)

// make sure the branch does not exist
cmd := exec.Command("git", "branch", "-D", "mrtest")
cmd.Dir = repo
cmd.CombinedOutput()

first := exec.Command(labBinaryPath, "mr", "checkout", "1")
first.Dir = repo
b, err := first.CombinedOutput()
if err != nil {
t.Log(string(b))
t.Fatal(err)
}

changeBranch := exec.Command("git", "checkout", "master")
changeBranch.Dir = repo
c, err := changeBranch.CombinedOutput()
if err != nil {
t.Log(string(c))
t.Fatal(err)
}

second := exec.Command(labBinaryPath, "mr", "checkout", "1", "--force")
second.Dir = repo
log, err := second.CombinedOutput()
eLog := string(log)
if err != nil {
t.Log(eLog)
t.Fatal(err)
}
require.Contains(t, eLog, "Deleted branch mrtest")
}

0 comments on commit c81e575

Please sign in to comment.