<commit_before>package cmd

import (
	"fmt"
	"log"

	"github.com/spf13/cobra"
	"github.com/xanzy/go-gitlab"
	"github.com/zaquestion/lab/internal/git"
	lab "github.com/zaquestion/lab/internal/gitlab"
)

// listCmd represents the list command
var checkoutCmd = &cobra.Command{
	Use:   "checkout",
	Short: "Checkout an open merge request",
	Long:  ``,
	Args:  cobra.ExactArgs(1),
	Run: func(cmd *cobra.Command, args []string) {
		rn, mrID, err := parseArgs(args)
		if err != nil {
			log.Fatal(err)
		}

		mrs, err := lab.MRList(rn, &gitlab.ListProjectMergeRequestsOptions{
			IIDs: []int{int(mrID)},
		})
		if err != nil {
			log.Fatal(err)
		}
		if len(mrs) < 1 {
			fmt.Printf("MR #%d not found\n", mrID)
			return
		}
		// https://docs.gitlab.com/ee/user/project/merge_requests/#checkout-merge-requests-locally
		branch := mrs[0].SourceBranch
		mr := fmt.Sprintf("refs/merge-requests/%d/head", mrID)
		gitf := git.New("fetch", forkedFromRemote, fmt.Sprintf("%s:%s", mr, branch))
		err = gitf.Run()
		if err != nil {
			log.Fatal(err)
		}

		gitc := git.New("checkout", branch)
		err = gitc.Run()
		if err != nil {
			log.Fatal(err)
		}
	},
}

func init() {
	mrCmd.AddCommand(checkoutCmd)
}
<commit_msg>Add lab mr checkout <mrID> -b for naming branch.<commit_after>package cmd

import (
	"fmt"
	"log"

	"github.com/spf13/cobra"
	"github.com/xanzy/go-gitlab"
	"github.com/zaquestion/lab/internal/git"
	lab "github.com/zaquestion/lab/internal/gitlab"
)

var (
	branch string
)

// listCmd represents the list command
var checkoutCmd = &cobra.Command{
	Use:   "checkout",
	Short: "Checkout an open merge request",
	Long:  ``,
	Args:  cobra.ExactArgs(1),
	Run: func(cmd *cobra.Command, args []string) {
		rn, mrID, err := parseArgs(args)
		if err != nil {
			log.Fatal(err)
		}

		mrs, err := lab.MRList(rn, &gitlab.ListProjectMergeRequestsOptions{
			IIDs: []int{int(mrID)},
		})
		if err != nil {
			log.Fatal(err)
		}
		if len(mrs) < 1 {
			fmt.Printf("MR #%d not found\n", mrID)
			return
		}
		// https://docs.gitlab.com/ee/user/project/merge_requests/#checkout-merge-requests-locally
		if branch == "" {
			branch = mrs[0].SourceBranch
		}
		mr := fmt.Sprintf("refs/merge-requests/%d/head", mrID)
		gitf := git.New("fetch", forkedFromRemote, fmt.Sprintf("%s:%s", mr, branch))
		err = gitf.Run()
		if err != nil {
			log.Fatal(err)
		}

		gitc := git.New("checkout", branch)
		err = gitc.Run()
		if err != nil {
			log.Fatal(err)
		}
	},
}

func init() {
	checkoutCmd.Flags().StringVarP(&branch, "branch", "b", "", "checkout merge request with <branch> name")
	mrCmd.AddCommand(checkoutCmd)
}

{'fraction_non_alphanumeric': 0.17579028527370855, 'fraction_numerical': 0.0038550501156515036, 'mean_word_length': 3.5209059233449476, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6492442', 'n_tokens_mistral': 1135, 'n_tokens_neox': 1047, 'n_words': 204}