Skip to content

Commit 1e537eb

Browse files
committed
Show PR information in main view above the branch log when a branch is selected
1 parent 8aded7f commit 1e537eb

3 files changed

Lines changed: 38 additions & 1 deletion

File tree

pkg/commands/git_commands/github.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ func (self *GitHubCommands) FetchRecentPRsAux(repoOwner string, repoName string,
211211
pr := &models.GithubPullRequest{
212212
HeadRefName: node.HeadRefName,
213213
Number: node.Number,
214+
Title: node.Title,
214215
State: node.State,
215216
Url: node.Url,
216217
HeadRepositoryOwner: models.GithubRepositoryOwner{

pkg/commands/models/github.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package models
44
type GithubPullRequest struct {
55
HeadRefName string `json:"headRefName"`
66
Number int `json:"number"`
7+
Title string `json:"title"`
78
State string `json:"state"` // "MERGED", "OPEN", "CLOSED"
89
Url string `json:"url"`
910
HeadRepositoryOwner GithubRepositoryOwner `json:"headRepositoryOwner"`

pkg/gui/controllers/branches_controller.go

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ package controllers
33
import (
44
"errors"
55
"fmt"
6+
"strconv"
67

78
"github.com/jesseduffield/gocui"
89
"github.com/jesseduffield/lazygit/pkg/commands/git_commands"
910
"github.com/jesseduffield/lazygit/pkg/commands/models"
1011
"github.com/jesseduffield/lazygit/pkg/gui/context"
1112
"github.com/jesseduffield/lazygit/pkg/gui/controllers/helpers"
13+
"github.com/jesseduffield/lazygit/pkg/gui/style"
1214
"github.com/jesseduffield/lazygit/pkg/gui/types"
1315
"github.com/jesseduffield/lazygit/pkg/utils"
1416
"github.com/samber/lo"
@@ -192,7 +194,23 @@ func (self *BranchesController) GetOnRenderToMain() func() {
192194
} else {
193195
cmdObj := self.c.Git().Branch.GetGraphCmdObj(branch.FullRefName())
194196

195-
task = types.NewRunPtyTask(cmdObj.GetCmd())
197+
ptyTask := types.NewRunPtyTask(cmdObj.GetCmd())
198+
task = ptyTask
199+
200+
// Shouldn't we hold on to the map for longer instead of generating it every time?
201+
// It is also generated every time we render the branches list.
202+
prs := git_commands.GenerateGithubPullRequestMap(
203+
self.c.Model().PullRequests,
204+
self.c.Model().Branches,
205+
self.c.Model().Remotes,
206+
)
207+
208+
if pr, ok := prs[branch.Name]; ok {
209+
ptyTask.Prefix = fmt.Sprintf("%s %s (%s)\n\n",
210+
coloredPrNumber(pr),
211+
style.FgYellow.Sprint(style.PrintHyperlink(pr.Title, pr.Url)),
212+
pr.State)
213+
}
196214
}
197215

198216
self.c.RenderToMainViews(types.RefreshMainOpts{
@@ -206,6 +224,23 @@ func (self *BranchesController) GetOnRenderToMain() func() {
206224
}
207225
}
208226

227+
func coloredPrNumber(pr *models.GithubPullRequest) string {
228+
return prColor(pr.State).Sprint("#" + strconv.Itoa(pr.Number))
229+
}
230+
231+
func prColor(state string) style.TextStyle {
232+
switch state {
233+
case "OPEN":
234+
return style.FgGreen
235+
case "CLOSED":
236+
return style.FgRed
237+
case "MERGED":
238+
return style.FgMagenta
239+
default:
240+
return style.FgDefault
241+
}
242+
}
243+
209244
func (self *BranchesController) viewUpstreamOptions(selectedBranch *models.Branch) error {
210245
upstream := lo.Ternary(selectedBranch.RemoteBranchStoredLocally(),
211246
selectedBranch.ShortUpstreamRefName(),

0 commit comments

Comments
 (0)