diff --git a/pkg/commands/github.go b/pkg/commands/github.go index 389883276..c34fce824 100644 --- a/pkg/commands/github.go +++ b/pkg/commands/github.go @@ -2,9 +2,9 @@ package commands import ( "encoding/json" + "strings" "github.com/jesseduffield/lazygit/pkg/commands/models" - "github.com/jesseduffield/lazygit/pkg/gui/presentation" ) func (c *GitCommand) GithubMostRecentPRs() (map[string]models.GithubPullRequest, error) { @@ -26,25 +26,40 @@ func (c *GitCommand) GithubMostRecentPRs() (map[string]models.GithubPullRequest, return res, nil } -func (c *GitCommand) FoundBranchWithGithubPullRequest(prs map[string]models.GithubPullRequest, branches []*models.Branch) bool { +func (c *GitCommand) GenerateGithubPullRequestMap(prs map[string]models.GithubPullRequest, branches []*models.Branch) (map[*models.Branch]*models.GithubPullRequest, bool) { + res := map[*models.Branch]*models.GithubPullRequest{} + if len(prs) == 0 { - return false + return res, false } remotesToOwnersMap, _ := c.GetRemotesToOwnersMap() if len(remotesToOwnersMap) == 0 { - return false + return res, false } foundBranchWithGithubPullRequest := false for _, branch := range branches { - _, has_pr := presentation.GetPr(branch, remotesToOwnersMap, prs) - - if has_pr { - foundBranchWithGithubPullRequest = true + if branch.UpstreamName == "" { + continue } + + remoteAndName := strings.SplitN(branch.UpstreamName, "/", 2) + owner, foundRemoteOwner := remotesToOwnersMap[remoteAndName[0]] + if len(remoteAndName) != 2 || !foundRemoteOwner { + continue + } + + pr, hasPr := prs[owner+":"+remoteAndName[1]] + if !hasPr { + continue + } + + foundBranchWithGithubPullRequest = true + + res[branch] = &pr } - return foundBranchWithGithubPullRequest + return res, foundBranchWithGithubPullRequest } diff --git a/pkg/gui/branches_panel.go b/pkg/gui/branches_panel.go index 2ce963ae9..4c70457f1 100644 --- a/pkg/gui/branches_panel.go +++ b/pkg/gui/branches_panel.go @@ -7,7 +7,6 @@ import ( "github.com/jesseduffield/lazygit/pkg/commands" "github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/commands/oscommands" - "github.com/jesseduffield/lazygit/pkg/gui/presentation" "github.com/jesseduffield/lazygit/pkg/utils" ) @@ -68,7 +67,9 @@ func (gui *Gui) refreshBranches() { _ = gui.surfaceError(err) } gui.State.Branches = builder.Build() - gui.State.BranchesWithGithubPullRequests = builder.GitCommand.FoundBranchWithGithubPullRequest(gui.State.GithubRecentPRs, gui.State.Branches) + _, branchesWithGithubPullRequests := builder.GitCommand.GenerateGithubPullRequestMap(gui.State.GithubRecentPRs, gui.State.Branches) + gui.State.BranchesWithGithubPullRequests = branchesWithGithubPullRequests + if err := gui.postRefreshUpdate(gui.State.Contexts.Branches); err != nil { gui.Log.Error(err) } @@ -102,12 +103,11 @@ func (gui *Gui) handleBranchPress() error { func (gui *Gui) handleCreateOrShowPullRequestPress() error { branch := gui.getSelectedBranch() - remotesToOwnersMap, _ := gui.GitCommand.GetRemotesToOwnersMap() - prs := gui.State.GithubRecentPRs - pr, has_pr := presentation.GetPr(branch, remotesToOwnersMap, prs) + prs, _ := gui.GitCommand.GenerateGithubPullRequestMap(gui.State.GithubRecentPRs, []*models.Branch{branch}) + pr, hasPr := prs[branch] - if has_pr { + if hasPr { return gui.OSCommand.OpenLink(pr.Url) } return gui.createPullRequest(branch.Name, "") diff --git a/pkg/gui/commits_panel.go b/pkg/gui/commits_panel.go index 2ac5d588a..aa14203c8 100644 --- a/pkg/gui/commits_panel.go +++ b/pkg/gui/commits_panel.go @@ -72,12 +72,11 @@ func (gui *Gui) refreshReflogCommitsConsideringStartup() { }) go utils.Safe(func() { // The github cli can be quite slow so we load the github PRs sparately - gui.State.BranchesWithGithubPullRequests = gui.GitCommand.FoundBranchWithGithubPullRequest(gui.State.GithubRecentPRs, gui.State.Branches) - if gui.State.BranchesWithGithubPullRequests { - gui.refreshGithubPullRequests() - } - + gui.refreshGithubPullRequests() wg.Wait() + _, branchesWithGithubPullRequests := gui.GitCommand.GenerateGithubPullRequestMap(gui.State.GithubRecentPRs, gui.State.Branches) + gui.State.BranchesWithGithubPullRequests = branchesWithGithubPullRequests + _ = gui.postRefreshUpdate(gui.State.Contexts.Branches) gui.refreshStatus() }) diff --git a/pkg/gui/list_context_config.go b/pkg/gui/list_context_config.go index a12158a62..0361655ce 100644 --- a/pkg/gui/list_context_config.go +++ b/pkg/gui/list_context_config.go @@ -66,12 +66,12 @@ func (gui *Gui) branchesListContext() *ListContext { OnFocus: gui.handleBranchSelect, Gui: gui, GetDisplayStrings: func() [][]string { - remotesToOwnersMap, _ := gui.GitCommand.GetRemotesToOwnersMap() + prs, branchesWithGithubPullRequests := gui.GitCommand.GenerateGithubPullRequestMap(gui.State.GithubRecentPRs, gui.State.Branches) + gui.State.BranchesWithGithubPullRequests = branchesWithGithubPullRequests return presentation.GetBranchListDisplayStrings( gui.State.Branches, - gui.State.GithubRecentPRs, - remotesToOwnersMap, + prs, gui.State.ScreenMode != SCREEN_NORMAL, gui.State.Modes.Diffing.Ref, gui.State.BranchesWithGithubPullRequests, diff --git a/pkg/gui/presentation/branches.go b/pkg/gui/presentation/branches.go index 68c36ce7d..d3da172f7 100644 --- a/pkg/gui/presentation/branches.go +++ b/pkg/gui/presentation/branches.go @@ -12,8 +12,7 @@ import ( func GetBranchListDisplayStrings( branches []*models.Branch, - prs map[string]models.GithubPullRequest, - remotesToOwnersMap map[string]string, + prs map[*models.Branch]*models.GithubPullRequest, fullDescription bool, diffName string, showGithub bool) [][]string { @@ -21,7 +20,7 @@ func GetBranchListDisplayStrings( for i := range branches { diffed := branches[i].Name == diffName - lines[i] = getBranchDisplayStrings(branches[i], prs, remotesToOwnersMap, fullDescription, diffed, showGithub) + lines[i] = getBranchDisplayStrings(branches[i], prs, fullDescription, diffed, showGithub) } return lines @@ -30,8 +29,7 @@ func GetBranchListDisplayStrings( // getBranchDisplayStrings returns the display string of branch func getBranchDisplayStrings( b *models.Branch, - prs map[string]models.GithubPullRequest, - remotesToOwnersMap map[string]string, + prs map[*models.Branch]*models.GithubPullRequest, fullDescription bool, diffed, showGithub bool) []string { @@ -56,8 +54,8 @@ func getBranchDisplayStrings( res := []string{recencyColor.Sprint(b.Recency), coloredName} if showGithub { - pr, has_pr := GetPr(b, remotesToOwnersMap, prs) - if has_pr { + pr, hasPr := prs[b] + if hasPr { colour := style.FgMagenta // = state MERGED switch pr.State { case "OPEN": @@ -107,22 +105,3 @@ func ColoredBranchStatus(branch *models.Branch) string { func BranchStatus(branch *models.Branch) string { return fmt.Sprintf("↑%s↓%s", branch.Pushables, branch.Pullables) } - -func GetPr(branch *models.Branch, remotesToOwnersMap map[string]string, prs map[string]models.GithubPullRequest) (*models.GithubPullRequest, bool) { - if len(prs) == 0 { - return nil, false - } - - if len(remotesToOwnersMap) == 0 { - return nil, false - } - - remoteAndName := strings.SplitN(branch.UpstreamName, "/", 2) - owner, foundRemoteOwner := remotesToOwnersMap[remoteAndName[0]] - if len(remoteAndName) != 2 || !foundRemoteOwner { - return nil, false - } - pr, hasPr := prs[owner+":"+remoteAndName[1]] - - return &pr, hasPr -} diff --git a/pkg/gui/pull_request_menu_panel.go b/pkg/gui/pull_request_menu_panel.go index 28d4bfcab..22da1347b 100644 --- a/pkg/gui/pull_request_menu_panel.go +++ b/pkg/gui/pull_request_menu_panel.go @@ -7,7 +7,6 @@ import ( "github.com/jesseduffield/lazygit/pkg/commands" "github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/commands/oscommands" - "github.com/jesseduffield/lazygit/pkg/gui/presentation" ) func (gui *Gui) createOrOpenPullRequestMenu(selectedBranch *models.Branch, checkedOutBranch *models.Branch) error { @@ -40,11 +39,10 @@ func (gui *Gui) createOrOpenPullRequestMenu(selectedBranch *models.Branch, check } } - remotesToOwnersMap, _ := gui.GitCommand.GetRemotesToOwnersMap() - prs := gui.State.GithubRecentPRs - pr, has_pr := presentation.GetPr(selectedBranch, remotesToOwnersMap, prs) + prs, _ := gui.GitCommand.GenerateGithubPullRequestMap(gui.State.GithubRecentPRs, []*models.Branch{selectedBranch}) + pr, hasPr := prs[selectedBranch] - if has_pr { + if hasPr { menuItems = append(menuItems, &menuItem{ displayString: "open #" + strconv.Itoa(pr.Number), onPress: func() error {