Repository: jesseduffield/lazygit
File: pkg/gui/controllers/helpers/refs_helper.go (Go)

Code before:
		LabelColumns: []string{fmt.Sprintf("%s %s", self.c.Tr.CheckoutCommit, self.c.Tr.AsDetachedHead), style.FgRed.Sprint(ref[:8])},
		OnPress: func() error {
			self.c.Confirm(types.ConfirmOpts{
				Title:  self.c.Tr.CheckoutCommit,
				Prompt: self.c.Tr.SureCheckoutThisCommit,
				HandleConfirm: func() error {
					self.c.LogAction(self.c.Tr.Actions.CheckoutCommit)
					return self.CheckoutRef(ref, types.CheckoutRefOptions{})
				},
			})
			return nil
		},
		Tooltip: self.c.Tr.CheckoutCommitTooltip,
		Key:     'd',
	})

	if len(branches) > 0 {
		menuItems = append(menuItems, lo.Map(branches, func(branch *models.Branch, index int) *types.MenuItem {
			return &types.MenuItem{
				LabelColumns: []string{self.c.Tr.Actions.CheckoutBranch, style.FgBlue.Sprint(branch.Name)},
				OnPress: func() error {
					self.c.LogAction(self.c.Tr.Actions.CheckoutBranch)
					return self.CheckoutRef(branch.RefName(), types.CheckoutRefOptions{})
				},
				Tooltip: self.c.Tr.CheckoutBranchTooltip,
				Key:     rune(index + 1 + '0'), // Convert 1-based index to key
			}
		})...)
	} else {
		menuItems = append(menuItems, &types.MenuItem{
			LabelColumns:   []string{self.c.Tr.Actions.CheckoutBranch},
			OnPress:        func() error { return nil },
			DisabledReason: &types.DisabledReason{Text: self.c.Tr.NoBranchesFoundAtCommitTooltip},
		})
	}

	return self.c.Menu(types.CreateMenuOptions{
		Title: self.c.Tr.Actions.CheckoutBranchOrCommit,
		Items: menuItems,
	})
}

func (self *RefsHelper) NewBranch(from string, fromFormattedName string, suggestedBranchName string) error {
	message := utils.ResolvePlaceholderString(
		self.c.Tr.NewBranchNameBranchOff,
		map[string]string{
			"branchName": fromFormattedName,
		},
	)

	if suggestedBranchName == "" {

Review comment (suggestion):
This probably doesn't work very well when there are more than 9 branches. Maybe we don't need to worry about that because it will never happen for anybody in practice; but I still wonder if we should write the extra code to stop assigning Keys after the 9th branch.

Code after:
		menuItems = append(menuItems, lo.Map(branches, func(branch *models.Branch, index int) *types.MenuItem {
			var key types.Key
			if index < 9 {
				key = rune(index + 1 + '0') // Convert 1-based index to key
			}
			return &types.MenuItem{
				LabelColumns: []string{fmt.Sprintf(self.c.Tr.Actions.CheckoutBranchAtCommit, branch.Name)},
				OnPress: func() error {
					self.c.LogAction(self.c.Tr.Actions.CheckoutBranch)
					return self.CheckoutRef(branch.RefName(), types.CheckoutRefOptions{})
				},
				Key: key,
			}
		})...)
	} else {
		menuItems = append(menuItems, &types.MenuItem{
			LabelColumns:   []string{self.c.Tr.Actions.CheckoutBranch},
			OnPress:        func() error { return nil },
			DisabledReason: &types.DisabledReason{Text: self.c.Tr.NoBranchesFoundAtCommitTooltip},
			Key:            '1',
		})
	}

	menuItems = append(menuItems, &types.MenuItem{
		LabelColumns: []string{fmt.Sprintf(self.c.Tr.Actions.CheckoutCommitAsDetachedHead, utils.ShortHash(hash))},
		OnPress: func() error {
			self.c.LogAction(self.c.Tr.Actions.CheckoutCommit)
			return self.CheckoutRef(hash, types.CheckoutRefOptions{})
		},
		Key: 'd',
	})

	return self.c.Menu(types.CreateMenuOptions{
		Title: self.c.Tr.Actions.CheckoutBranchOrCommit,
		Items: menuItems,
	})
}

func (self *RefsHelper) NewBranch(from string, fromFormattedName string, suggestedBranchName string) error {
	message := utils.ResolvePlaceholderString(
		self.c.Tr.NewBranchNameBranchOff,
		map[string]string{
			"branchName": fromFormattedName,
		},
	)

	if suggestedBranchName == "" {
		suggestedBranchName = self.c.UserConfig().Git.BranchPrefix
	}

	refresh := func() error {