Open a browser at the selected line in the diff of the current branch's PR

This commit is contained in:
Stefan Haller 2026-05-31 15:06:39 +02:00
parent 42d8f9697c
commit e092a7b054
10 changed files with 114 additions and 0 deletions

View file

@ -235,6 +235,7 @@ _This file is auto-generated. To update, make the changes in the pkg/i18n direct
| `` N `` | Go to previous file | |
| `` n `` | Go to next file | |
| `` f `` | Jump to file | |
| `` G `` | Open pull request for selected line | Open a browser at the selected line in the diff of the current branch's pull request, so that you can comment on it. Only works for local branches that have a pull request on GitHub. |
| `` <esc> `` | Exit back to side panel | |
| `` c `` | Commit | Commit staged changes. |
| `` w `` | Commit changes without pre-commit hook | |
@ -318,6 +319,7 @@ _This file is auto-generated. To update, make the changes in the pkg/i18n direct
| `` N `` | Go to previous file | |
| `` n `` | Go to next file | |
| `` f `` | Jump to file | |
| `` G `` | Open pull request for selected line | Open a browser at the selected line in the diff of the current branch's pull request, so that you can comment on it. Only works for local branches that have a pull request on GitHub. |
| `` <esc> `` | Exit back to side panel | |
| `` c `` | Commit | Commit staged changes. |
| `` w `` | Commit changes without pre-commit hook | |

View file

@ -204,6 +204,7 @@ _This file is auto-generated. To update, make the changes in the pkg/i18n direct
| `` N `` | Go to previous file | |
| `` n `` | Go to next file | |
| `` f `` | Jump to file | |
| `` G `` | Open pull request for selected line | Open a browser at the selected line in the diff of the current branch's pull request, so that you can comment on it. Only works for local branches that have a pull request on GitHub. |
| `` <esc> `` | サイドパネルに戻る | |
| `` c `` | コミット | ステージされた変更をコミットします。 |
| `` w `` | pre-commitフックなしで変更をコミット | |
@ -295,6 +296,7 @@ _This file is auto-generated. To update, make the changes in the pkg/i18n direct
| `` N `` | Go to previous file | |
| `` n `` | Go to next file | |
| `` f `` | Jump to file | |
| `` G `` | Open pull request for selected line | Open a browser at the selected line in the diff of the current branch's pull request, so that you can comment on it. Only works for local branches that have a pull request on GitHub. |
| `` <esc> `` | サイドパネルに戻る | |
| `` c `` | コミット | ステージされた変更をコミットします。 |
| `` w `` | pre-commitフックなしで変更をコミット | |

View file

@ -96,6 +96,7 @@ _This file is auto-generated. To update, make the changes in the pkg/i18n direct
| `` N `` | Go to previous file | |
| `` n `` | Go to next file | |
| `` f `` | Jump to file | |
| `` G `` | Open pull request for selected line | Open a browser at the selected line in the diff of the current branch's pull request, so that you can comment on it. Only works for local branches that have a pull request on GitHub. |
| `` <esc> `` | Exit back to side panel | |
| `` c `` | 커밋 변경내용 | 스테이징된 변경 사항 커밋. |
| `` w `` | Commit changes without pre-commit hook | |
@ -190,6 +191,7 @@ _This file is auto-generated. To update, make the changes in the pkg/i18n direct
| `` N `` | Go to previous file | |
| `` n `` | Go to next file | |
| `` f `` | Jump to file | |
| `` G `` | Open pull request for selected line | Open a browser at the selected line in the diff of the current branch's pull request, so that you can comment on it. Only works for local branches that have a pull request on GitHub. |
| `` <esc> `` | Exit back to side panel | |
| `` c `` | 커밋 변경내용 | 스테이징된 변경 사항 커밋. |
| `` w `` | Commit changes without pre-commit hook | |

View file

@ -243,6 +243,7 @@ _This file is auto-generated. To update, make the changes in the pkg/i18n direct
| `` N `` | Go to previous file | |
| `` n `` | Go to next file | |
| `` f `` | Jump to file | |
| `` G `` | Open pull request for selected line | Open a browser at the selected line in the diff of the current branch's pull request, so that you can comment on it. Only works for local branches that have a pull request on GitHub. |
| `` <esc> `` | Exit back to side panel | |
| `` c `` | Commit veranderingen | Commit gestagede wijzigingen. |
| `` w `` | Commit veranderingen zonder pre-commit hook | |
@ -318,6 +319,7 @@ _This file is auto-generated. To update, make the changes in the pkg/i18n direct
| `` N `` | Go to previous file | |
| `` n `` | Go to next file | |
| `` f `` | Jump to file | |
| `` G `` | Open pull request for selected line | Open a browser at the selected line in the diff of the current branch's pull request, so that you can comment on it. Only works for local branches that have a pull request on GitHub. |
| `` <esc> `` | Exit back to side panel | |
| `` c `` | Commit veranderingen | Commit gestagede wijzigingen. |
| `` w `` | Commit veranderingen zonder pre-commit hook | |

View file

@ -111,6 +111,7 @@ _This file is auto-generated. To update, make the changes in the pkg/i18n direct
| `` N `` | Go to previous file | |
| `` n `` | Go to next file | |
| `` f `` | Jump to file | |
| `` G `` | Open pull request for selected line | Open a browser at the selected line in the diff of the current branch's pull request, so that you can comment on it. Only works for local branches that have a pull request on GitHub. |
| `` <esc> `` | Exit back to side panel | |
| `` c `` | Commit | Zatwierdź zmiany zatwierdzone. |
| `` w `` | Zatwierdź zmiany bez hooka pre-commit | |
@ -213,6 +214,7 @@ _This file is auto-generated. To update, make the changes in the pkg/i18n direct
| `` N `` | Go to previous file | |
| `` n `` | Go to next file | |
| `` f `` | Jump to file | |
| `` G `` | Open pull request for selected line | Open a browser at the selected line in the diff of the current branch's pull request, so that you can comment on it. Only works for local branches that have a pull request on GitHub. |
| `` <esc> `` | Exit back to side panel | |
| `` c `` | Commit | Zatwierdź zmiany zatwierdzone. |
| `` w `` | Zatwierdź zmiany bez hooka pre-commit | |

View file

@ -247,6 +247,7 @@ _This file is auto-generated. To update, make the changes in the pkg/i18n direct
| `` N `` | Go to previous file | |
| `` n `` | Go to next file | |
| `` f `` | Jump to file | |
| `` G `` | Open pull request for selected line | Open a browser at the selected line in the diff of the current branch's pull request, so that you can comment on it. Only works for local branches that have a pull request on GitHub. |
| `` <esc> `` | Exit back to side panel | |
| `` c `` | Commit | Submeter mudanças em staging |
| `` w `` | Fazer commit de alterações sem pré-commit | |
@ -327,6 +328,7 @@ _This file is auto-generated. To update, make the changes in the pkg/i18n direct
| `` N `` | Go to previous file | |
| `` n `` | Go to next file | |
| `` f `` | Jump to file | |
| `` G `` | Open pull request for selected line | Open a browser at the selected line in the diff of the current branch's pull request, so that you can comment on it. Only works for local branches that have a pull request on GitHub. |
| `` <esc> `` | Exit back to side panel | |
| `` c `` | Commit | Submeter mudanças em staging |
| `` w `` | Fazer commit de alterações sem pré-commit | |

View file

@ -86,6 +86,7 @@ _This file is auto-generated. To update, make the changes in the pkg/i18n direct
| `` N `` | Go to previous file | |
| `` n `` | Go to next file | |
| `` f `` | Jump to file | |
| `` G `` | Open pull request for selected line | Open a browser at the selected line in the diff of the current branch's pull request, so that you can comment on it. Only works for local branches that have a pull request on GitHub. |
| `` <esc> `` | Exit back to side panel | |
| `` c `` | Сохранить изменения | Commit staged changes. |
| `` w `` | Закоммитить изменения без предварительного хука коммита | |
@ -112,6 +113,7 @@ _This file is auto-generated. To update, make the changes in the pkg/i18n direct
| `` N `` | Go to previous file | |
| `` n `` | Go to next file | |
| `` f `` | Jump to file | |
| `` G `` | Open pull request for selected line | Open a browser at the selected line in the diff of the current branch's pull request, so that you can comment on it. Only works for local branches that have a pull request on GitHub. |
| `` <esc> `` | Exit back to side panel | |
| `` c `` | Сохранить изменения | Commit staged changes. |
| `` w `` | Закоммитить изменения без предварительного хука коммита | |

View file

@ -282,6 +282,7 @@ _This file is auto-generated. To update, make the changes in the pkg/i18n direct
| `` N `` | Go to previous file | |
| `` n `` | Go to next file | |
| `` f `` | Jump to file | |
| `` G `` | Open pull request for selected line | Open a browser at the selected line in the diff of the current branch's pull request, so that you can comment on it. Only works for local branches that have a pull request on GitHub. |
| `` <esc> `` | 退出回到侧边面板 | |
| `` c `` | 提交变更 | 提交暂存文件 |
| `` w `` | 提交变更而无需预先提交钩子 | |
@ -324,6 +325,7 @@ _This file is auto-generated. To update, make the changes in the pkg/i18n direct
| `` N `` | Go to previous file | |
| `` n `` | Go to next file | |
| `` f `` | Jump to file | |
| `` G `` | Open pull request for selected line | Open a browser at the selected line in the diff of the current branch's pull request, so that you can comment on it. Only works for local branches that have a pull request on GitHub. |
| `` <esc> `` | 退出回到侧边面板 | |
| `` c `` | 提交变更 | 提交暂存文件 |
| `` w `` | 提交变更而无需预先提交钩子 | |

View file

@ -71,6 +71,7 @@ _This file is auto-generated. To update, make the changes in the pkg/i18n direct
| `` N `` | Go to previous file | |
| `` n `` | Go to next file | |
| `` f `` | Jump to file | |
| `` G `` | Open pull request for selected line | Open a browser at the selected line in the diff of the current branch's pull request, so that you can comment on it. Only works for local branches that have a pull request on GitHub. |
| `` <esc> `` | 退出回到側邊面板 | |
| `` c `` | 提交變更 | 提交暫存區變更 |
| `` w `` | 沒有預提交 hook 就提交更改 | |
@ -346,6 +347,7 @@ _This file is auto-generated. To update, make the changes in the pkg/i18n direct
| `` N `` | Go to previous file | |
| `` n `` | Go to next file | |
| `` f `` | Jump to file | |
| `` G `` | Open pull request for selected line | Open a browser at the selected line in the diff of the current branch's pull request, so that you can comment on it. Only works for local branches that have a pull request on GitHub. |
| `` <esc> `` | 退出回到側邊面板 | |
| `` c `` | 提交變更 | 提交暫存區變更 |
| `` w `` | 沒有預提交 hook 就提交更改 | |

View file

@ -1,6 +1,10 @@
package controllers
import (
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
"path/filepath"
"time"
@ -163,6 +167,14 @@ func (self *MainViewController) GetKeybindings(opts types.KeybindingsOpts) []*ty
DescriptionFunc: self.diffSelectionDescriptionText("Jump to file"),
GetDisabledReason: self.diffSelectionDisabledReason,
},
{
Keys: opts.GetKeys(opts.Config.Commits.OpenPullRequestInBrowser),
Handler: self.openPullRequestForSelectedLine,
Description: "Open pull request for selected line",
DescriptionFunc: self.diffSelectionDescriptionText("Open pull request for selected line"),
Tooltip: "Open a browser at the selected line in the diff of the current branch's pull request, so that you can comment on it. Only works for local branches that have a pull request on GitHub.",
GetDisabledReason: self.diffSelectionDisabledReason,
},
{
Keys: opts.GetKeys(opts.Config.Universal.Return),
Handler: self.escape,
@ -1043,6 +1055,90 @@ func (self *MainViewController) editDiffLine(viewLine int, beforeEdit func()) er
return self.c.Helpers().Files.EditFileAtLine(info.Path, lineNumber)
}
func (self *MainViewController) openPullRequestForSelectedLine() error {
sidePanelContext := self.c.Context().NextInStack(self.context)
if sidePanelContext == nil {
return nil
}
// The branch whose PR to open depends on where we navigated from: the
// checked-out branch when looking at its own commits, but the branch we
// drilled into when in the sub-commits or commit-files panels.
branchName, ok := self.branchForPullRequest(sidePanelContext)
if !ok {
return nil
}
pr, ok := self.c.Model().PullRequestsMap[branchName]
if !ok {
return errors.New(self.c.Tr.NoPullRequestForBranch)
}
// The diff shown is the diff of a particular commit, so we deep-link into
// that commit's view of the PR; its right-side line numbers match what we're
// showing, so (unlike editLine) no line-number adjustment is needed.
diffableContext, ok := sidePanelContext.(types.DiffableContext)
if !ok {
return nil
}
commitSha := diffableContext.RefForAdjustingLineNumberInDiff()
if commitSha == "" {
return nil
}
info, ok := self.c.Helpers().DiffLine.GetDiffLineInfo(self.context.GetView(), self.context.GetView().SelectedLineIdx())
if !ok {
return nil
}
relativePath, err := filepath.Rel(self.c.Git().RepoPaths.WorktreePath(), info.Path)
if err != nil {
return err
}
self.c.LogAction(self.c.Tr.Actions.OpenPullRequest)
return self.c.OS().OpenLink(
githubPullRequestLineURL(pr.Url, commitSha, filepath.ToSlash(relativePath), info.NewLine))
}
// branchForPullRequest returns the local branch whose pull request applies to
// the diff currently shown in the focused main view, given the side panel
// beneath it. It returns false for contexts that don't map to a local branch
// (e.g. the working-tree files panel, stashes, tags, or remote branches).
func (self *MainViewController) branchForPullRequest(sidePanelContext types.Context) (string, bool) {
switch sidePanelContext.GetKey() {
case context.LOCAL_COMMITS_CONTEXT_KEY:
return self.c.Model().CheckedOutBranch, true
case context.SUB_COMMITS_CONTEXT_KEY:
ref := self.c.Contexts().SubCommits.GetRef()
if ref == nil {
return "", false
}
return ref.RefName(), true
case context.COMMIT_FILES_CONTEXT_KEY:
// The commit files panel doesn't itself know which branch it belongs to;
// that's determined by the panel we entered it from.
parent := self.c.Contexts().CommitFiles.GetParentContext()
if parent == nil {
return "", false
}
return self.branchForPullRequest(parent)
default:
return "", false
}
}
// githubPullRequestLineURL builds a URL that opens the given line of a file in
// the diff of a specific commit within a GitHub pull request. The file is
// identified by the SHA-256 of its repo-relative path, and R<line> targets the
// right (new) side of the diff. See
// https://github.com/orgs/community/discussions/55764.
func githubPullRequestLineURL(prURL string, commitSha string, relativePath string, lineNumber int) string {
pathHash := sha256.Sum256([]byte(relativePath))
anchor := fmt.Sprintf("diff-%sR%d", hex.EncodeToString(pathHash[:]), lineNumber)
return fmt.Sprintf("%s/changes/%s#%s", prURL, commitSha, anchor)
}
func (self *MainViewController) openSearch() error {
if manager := self.c.GetViewBufferManagerForView(self.context.GetView()); manager != nil {
manager.ReadToEnd(func() {