diff --git a/docs-master/keybindings/Keybindings_en.md b/docs-master/keybindings/Keybindings_en.md index cd9bed28d..392c6199f 100644 --- a/docs-master/keybindings/Keybindings_en.md +++ b/docs-master/keybindings/Keybindings_en.md @@ -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. | | `` `` | 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. | | `` `` | Exit back to side panel | | | `` c `` | Commit | Commit staged changes. | | `` w `` | Commit changes without pre-commit hook | | diff --git a/docs-master/keybindings/Keybindings_ja.md b/docs-master/keybindings/Keybindings_ja.md index 49ae515f5..11a2e4a18 100644 --- a/docs-master/keybindings/Keybindings_ja.md +++ b/docs-master/keybindings/Keybindings_ja.md @@ -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. | | `` `` | サイドパネルに戻る | | | `` 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. | | `` `` | サイドパネルに戻る | | | `` c `` | コミット | ステージされた変更をコミットします。 | | `` w `` | pre-commitフックなしで変更をコミット | | diff --git a/docs-master/keybindings/Keybindings_ko.md b/docs-master/keybindings/Keybindings_ko.md index d4fc3c81d..439a6bf07 100644 --- a/docs-master/keybindings/Keybindings_ko.md +++ b/docs-master/keybindings/Keybindings_ko.md @@ -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. | | `` `` | 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. | | `` `` | Exit back to side panel | | | `` c `` | 커밋 변경내용 | 스테이징된 변경 사항 커밋. | | `` w `` | Commit changes without pre-commit hook | | diff --git a/docs-master/keybindings/Keybindings_nl.md b/docs-master/keybindings/Keybindings_nl.md index e74e96b96..0c5c492c5 100644 --- a/docs-master/keybindings/Keybindings_nl.md +++ b/docs-master/keybindings/Keybindings_nl.md @@ -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. | | `` `` | 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. | | `` `` | Exit back to side panel | | | `` c `` | Commit veranderingen | Commit gestagede wijzigingen. | | `` w `` | Commit veranderingen zonder pre-commit hook | | diff --git a/docs-master/keybindings/Keybindings_pl.md b/docs-master/keybindings/Keybindings_pl.md index 98b797916..c8a526d55 100644 --- a/docs-master/keybindings/Keybindings_pl.md +++ b/docs-master/keybindings/Keybindings_pl.md @@ -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. | | `` `` | 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. | | `` `` | Exit back to side panel | | | `` c `` | Commit | Zatwierdź zmiany zatwierdzone. | | `` w `` | Zatwierdź zmiany bez hooka pre-commit | | diff --git a/docs-master/keybindings/Keybindings_pt.md b/docs-master/keybindings/Keybindings_pt.md index 1320c8fe3..5a31ee72f 100644 --- a/docs-master/keybindings/Keybindings_pt.md +++ b/docs-master/keybindings/Keybindings_pt.md @@ -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. | | `` `` | 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. | | `` `` | Exit back to side panel | | | `` c `` | Commit | Submeter mudanças em staging | | `` w `` | Fazer commit de alterações sem pré-commit | | diff --git a/docs-master/keybindings/Keybindings_ru.md b/docs-master/keybindings/Keybindings_ru.md index 4ced54140..2079e0cef 100644 --- a/docs-master/keybindings/Keybindings_ru.md +++ b/docs-master/keybindings/Keybindings_ru.md @@ -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. | | `` `` | 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. | | `` `` | Exit back to side panel | | | `` c `` | Сохранить изменения | Commit staged changes. | | `` w `` | Закоммитить изменения без предварительного хука коммита | | diff --git a/docs-master/keybindings/Keybindings_zh-CN.md b/docs-master/keybindings/Keybindings_zh-CN.md index 95f66f13c..9a3e78777 100644 --- a/docs-master/keybindings/Keybindings_zh-CN.md +++ b/docs-master/keybindings/Keybindings_zh-CN.md @@ -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. | | `` `` | 退出回到侧边面板 | | | `` 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. | | `` `` | 退出回到侧边面板 | | | `` c `` | 提交变更 | 提交暂存文件 | | `` w `` | 提交变更而无需预先提交钩子 | | diff --git a/docs-master/keybindings/Keybindings_zh-TW.md b/docs-master/keybindings/Keybindings_zh-TW.md index f1a75f60f..321ad0663 100644 --- a/docs-master/keybindings/Keybindings_zh-TW.md +++ b/docs-master/keybindings/Keybindings_zh-TW.md @@ -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. | | `` `` | 退出回到側邊面板 | | | `` 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. | | `` `` | 退出回到側邊面板 | | | `` c `` | 提交變更 | 提交暫存區變更 | | `` w `` | 沒有預提交 hook 就提交更改 | | diff --git a/pkg/gui/controllers/main_view_controller.go b/pkg/gui/controllers/main_view_controller.go index 32d9acabd..1bd67c5cb 100644 --- a/pkg/gui/controllers/main_view_controller.go +++ b/pkg/gui/controllers/main_view_controller.go @@ -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 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() {