diff --git a/pkg/commands/models/branch.go b/pkg/commands/models/branch.go index 9199a3611..3b8268bff 100644 --- a/pkg/commands/models/branch.go +++ b/pkg/commands/models/branch.go @@ -11,7 +11,6 @@ type Branch struct { Pullables string UpstreamName string Head bool - PR *GithubPullRequest } func (b *Branch) RefName() string { diff --git a/pkg/gui/branches_panel.go b/pkg/gui/branches_panel.go index 9e1e8a5fe..2ce963ae9 100644 --- a/pkg/gui/branches_panel.go +++ b/pkg/gui/branches_panel.go @@ -7,6 +7,7 @@ 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" ) @@ -67,7 +68,7 @@ func (gui *Gui) refreshBranches() { _ = gui.surfaceError(err) } gui.State.Branches = builder.Build() - gui.State.BranchesWithGithubPullRequests = builder.GitCommand.InjectGithubPullRequests(gui.State.GithubRecentPRs, gui.State.Branches) + gui.State.BranchesWithGithubPullRequests = builder.GitCommand.FoundBranchWithGithubPullRequest(gui.State.GithubRecentPRs, gui.State.Branches) if err := gui.postRefreshUpdate(gui.State.Contexts.Branches); err != nil { gui.Log.Error(err) } @@ -76,7 +77,11 @@ func (gui *Gui) refreshBranches() { } func (gui *Gui) refreshGithubPullRequests() { - prs := gui.GitCommand.GithubMostRecentPRs() + prs, err := gui.GitCommand.GithubMostRecentPRs() + if err != nil { + gui.Log.Error(err) + } + if len(prs) > 0 { gui.State.GithubRecentPRs = prs } @@ -97,8 +102,13 @@ func (gui *Gui) handleBranchPress() error { func (gui *Gui) handleCreateOrShowPullRequestPress() error { branch := gui.getSelectedBranch() - if branch.PR != nil { - return gui.OSCommand.OpenLink(branch.PR.Url) + remotesToOwnersMap, _ := gui.GitCommand.GetRemotesToOwnersMap() + prs := gui.State.GithubRecentPRs + + pr, has_pr := presentation.GetPr(branch, remotesToOwnersMap, prs) + + if has_pr { + 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 70d0efb89..80bf285ae 100644 --- a/pkg/gui/commits_panel.go +++ b/pkg/gui/commits_panel.go @@ -74,7 +74,7 @@ func (gui *Gui) refreshReflogCommitsConsideringStartup() { // The github cli can be quite slow so we load the github PRs sparately gui.refreshGithubPullRequests() wg.Wait() - gui.State.BranchesWithGithubPullRequests = gui.GitCommand.InjectGithubPullRequests(gui.State.GithubRecentPRs, gui.State.Branches) + gui.State.BranchesWithGithubPullRequests = gui.GitCommand.FoundBranchWithGithubPullRequest(gui.State.GithubRecentPRs, gui.State.Branches) _ = gui.postRefreshUpdate(gui.State.Contexts.Branches) gui.refreshStatus() }) diff --git a/pkg/gui/gui.go b/pkg/gui/gui.go index 6276ae6e5..673f3464e 100644 --- a/pkg/gui/gui.go +++ b/pkg/gui/gui.go @@ -286,11 +286,11 @@ type guiMutexes struct { type guiState struct { // the file panels (files and commit files) can render as a tree, so we have // managers for them which handle rendering a flat list of files in tree form - FileManager *filetree.FileManager - CommitFileManager *filetree.CommitFileManager - Submodules []*models.SubmoduleConfig - Branches []*models.Branch - GithubRecentPRs map[string]models.GithubPullRequest + FileManager *filetree.FileManager + CommitFileManager *filetree.CommitFileManager + Submodules []*models.SubmoduleConfig + Branches []*models.Branch + GithubRecentPRs map[string]models.GithubPullRequest BranchesWithGithubPullRequests bool Commits []*models.Commit StashEntries []*models.StashEntry diff --git a/pkg/gui/list_context_config.go b/pkg/gui/list_context_config.go index 8b8dd5d2b..a12158a62 100644 --- a/pkg/gui/list_context_config.go +++ b/pkg/gui/list_context_config.go @@ -66,8 +66,12 @@ func (gui *Gui) branchesListContext() *ListContext { OnFocus: gui.handleBranchSelect, Gui: gui, GetDisplayStrings: func() [][]string { + remotesToOwnersMap, _ := gui.GitCommand.GetRemotesToOwnersMap() + return presentation.GetBranchListDisplayStrings( gui.State.Branches, + gui.State.GithubRecentPRs, + remotesToOwnersMap, 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 c6b92d3e6..68c36ce7d 100644 --- a/pkg/gui/presentation/branches.go +++ b/pkg/gui/presentation/branches.go @@ -10,19 +10,31 @@ import ( "github.com/jesseduffield/lazygit/pkg/theme" ) -func GetBranchListDisplayStrings(branches []*models.Branch, fullDescription bool, diffName string, showGithub bool) [][]string { +func GetBranchListDisplayStrings( + branches []*models.Branch, + prs map[string]models.GithubPullRequest, + remotesToOwnersMap map[string]string, + fullDescription bool, + diffName string, + showGithub bool) [][]string { lines := make([][]string, len(branches)) for i := range branches { diffed := branches[i].Name == diffName - lines[i] = getBranchDisplayStrings(branches[i], fullDescription, diffed, showGithub) + lines[i] = getBranchDisplayStrings(branches[i], prs, remotesToOwnersMap, fullDescription, diffed, showGithub) } return lines } // getBranchDisplayStrings returns the display string of branch -func getBranchDisplayStrings(b *models.Branch, fullDescription bool, diffed, showGithub bool) []string { +func getBranchDisplayStrings( + b *models.Branch, + prs map[string]models.GithubPullRequest, + remotesToOwnersMap map[string]string, + fullDescription bool, + diffed, + showGithub bool) []string { displayName := b.Name if b.DisplayName != "" { displayName = b.DisplayName @@ -44,15 +56,16 @@ func getBranchDisplayStrings(b *models.Branch, fullDescription bool, diffed, sho res := []string{recencyColor.Sprint(b.Recency), coloredName} if showGithub { - if b.PR != nil { + pr, has_pr := GetPr(b, remotesToOwnersMap, prs) + if has_pr { colour := style.FgMagenta // = state MERGED - switch b.PR.State { + switch pr.State { case "OPEN": colour = style.FgGreen case "CLOSED": colour = style.FgRed } - res = append(res, colour.Sprint("#"+strconv.Itoa(b.PR.Number))) + res = append(res, colour.Sprint("#"+strconv.Itoa(pr.Number))) } else { res = append(res, "") } @@ -94,3 +107,22 @@ 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 85888a58e..28d4bfcab 100644 --- a/pkg/gui/pull_request_menu_panel.go +++ b/pkg/gui/pull_request_menu_panel.go @@ -7,6 +7,7 @@ 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 { @@ -39,11 +40,15 @@ func (gui *Gui) createOrOpenPullRequestMenu(selectedBranch *models.Branch, check } } - if selectedBranch.PR != nil { + remotesToOwnersMap, _ := gui.GitCommand.GetRemotesToOwnersMap() + prs := gui.State.GithubRecentPRs + pr, has_pr := presentation.GetPr(selectedBranch, remotesToOwnersMap, prs) + + if has_pr { menuItems = append(menuItems, &menuItem{ - displayString: "open #" + strconv.Itoa(selectedBranch.PR.Number), + displayString: "open #" + strconv.Itoa(pr.Number), onPress: func() error { - return gui.OSCommand.OpenLink(selectedBranch.PR.Url) + return gui.OSCommand.OpenLink(pr.Url) }, }) }