update the logic

This commit is contained in:
Yuki Osaki 2021-10-31 13:58:07 +09:00
parent 61d08dc4af
commit 502c23dff7
6 changed files with 45 additions and 54 deletions

View file

@ -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
}

View file

@ -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, "")

View file

@ -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()
})

View file

@ -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,

View file

@ -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
}

View file

@ -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 {