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

Code before:

	fromToLabelColumns := func(from string, to string) []string {
		return []string{fmt.Sprintf("%s → %s", from, to)}
	}

	menuItemsForBranch := func(branch *models.Branch) []*types.MenuItem {
		return []*types.MenuItem{
			{
				LabelColumns: fromToLabelColumns(branch.Name, self.c.Tr.DefaultBranch),
				OnPress: func() error {
					if len(branch.UpstreamBranch) == 0 {
						return self.c.ErrorMsg(self.c.Tr.PullRequestNoUpstream)
					}
					return self.createPullRequest(branch.Name, "")
				},
			},
			{
				LabelColumns: fromToLabelColumns(branch.Name, self.c.Tr.SelectBranch),
				OnPress: func() error {
					return self.c.Prompt(types.PromptOpts{
						Title:               branch.Name + " →",
						FindSuggestionsFunc: self.c.Helpers().Suggestions.GetRemoteBranchesSuggestionsFunc("/"),
						HandleConfirm: func(targetBranchName string) error {
							upstreamExists := false
							for _, remoteBranch := range self.c.Model().RemoteBranches {
								upstreamExists = upstreamExists || remoteBranch.Name == targetBranchName
							}
							if !upstreamExists {
								return self.c.ErrorMsg(self.c.Tr.PullRequestNoUpstream)
							}
							return self.createPullRequest(branch.Name, targetBranchName)
						},
					})
				},
			},
		}
	}

	if selectedBranch != checkedOutBranch {
		menuItems = append(menuItems,
			&types.MenuItem{
				LabelColumns: fromToLabelColumns(checkedOutBranch.Name, selectedBranch.Name),
				OnPress: func() error {
					if len(checkedOutBranch.UpstreamBranch) == 0 || len(selectedBranch.UpstreamBranch) == 0 {
						return self.c.ErrorMsg(self.c.Tr.PullRequestNoUpstream)
					}
					return self.createPullRequest(checkedOutBranch.Name, selectedBranch.Name)
				},
			},
		)
		menuItems = append(menuItems, menuItemsForBranch(checkedOutBranch)...)

Review comment (suggestion):
I think we should just skip the validation in this case: it's possible that the remote branch actually does exist but we just haven't fetched recently. The error we get back if the remote branch really doesn't exist isn't bad anyway.

Code after:
		self.c.Tr.ForceDeleteBranchMessage,
		map[string]string{
			"selectedBranchName": branch.Name,
		},
	)

	return self.c.Confirm(types.ConfirmOpts{
		Title:  title,
		Prompt: message,
		HandleConfirm: func() error {
			if err := self.c.Git().Branch.LocalDelete(branch.Name, true); err != nil {
				return self.c.ErrorMsg(err.Error())
			}
			return self.c.Refresh(types.RefreshOptions{Mode: types.ASYNC, Scope: []types.RefreshableView{types.BRANCHES}})
		},
	})
}

func (self *BranchesController) delete(branch *models.Branch) error {
	menuItems := []*types.MenuItem{}
	checkedOutBranch := self.c.Helpers().Refs.GetCheckedOutRef()

	localDeleteItem := &types.MenuItem{
		Label: self.c.Tr.DeleteLocalBranch,
		Key:   'c',
		OnPress: func() error {
			return self.localDelete(branch)
		},
	}
	if checkedOutBranch.Name == branch.Name {
		localDeleteItem = &types.MenuItem{
			Label:   self.c.Tr.DeleteLocalBranch,
			Key:     'c',
			Tooltip: self.c.Tr.CantDeleteCheckOutBranch,
			OnPress: func() error {
				return self.c.ErrorMsg(self.c.Tr.CantDeleteCheckOutBranch)
			},
		}
	}
	menuItems = append(menuItems, localDeleteItem)

	if branch.IsTrackingRemote() && !branch.UpstreamGone {
		menuItems = append(menuItems, &types.MenuItem{
			Label: self.c.Tr.DeleteRemoteBranch,
			Key:   'r',
			OnPress: func() error {
				return self.remoteDelete(branch)
			},
		})
	}