fix based on the reviews

This commit is contained in:
Yuki Osaki 2021-11-09 23:51:09 +09:00
parent 27a12fe2ea
commit bf355bc0b5
13 changed files with 90 additions and 52 deletions

View file

@ -143,6 +143,12 @@ func NewApp(config config.AppConfigurer, filterPath string) (*App, error) {
return app, err
}
if app.Gui.Config.GetUserConfig().Git.EnableGhCommand {
if err := app.validateGhVersion(); err != nil {
return nil, err
}
}
return app, nil
}
@ -159,7 +165,6 @@ func (app *App) validateGhVersion() error {
}
return minVersionError
}
func (app *App) validateGitVersion() error {
@ -276,10 +281,6 @@ func (app *App) setupRepo() (bool, error) {
}
}
if err := app.validateGhVersion(); err != nil {
return false, err
}
return false, nil
}

View file

@ -64,6 +64,15 @@ func TestIsValidGhVersion(t *testing.T) {
https://github.com/cli/cli/releases/tag/v2.0.0`,
true,
},
{
`gh version 1.1.0 (2021-10-14)
https://github.com/cli/cli/releases/tag/v1.1.0
A new release of gh is available: 1.1.0 v2.2.0
To upgrade, run: brew update && brew upgrade gh
https://github.com/cli/cli/releases/tag/v2.2.0`,
false,
},
}
for _, s := range scenarios {

View file

@ -21,16 +21,17 @@ func (c *GitCommand) GithubMostRecentPRs() ([]*models.GithubPullRequest, error)
return prs, nil
}
func (c *GitCommand) GenerateGithubPullRequestMap(prs []*models.GithubPullRequest, branches []*models.Branch) map[*models.Branch]*models.GithubPullRequest {
func (c *GitCommand) GenerateGithubPullRequestMap(prs []*models.GithubPullRequest, branches []*models.Branch, remotes []*models.Remote) (map[*models.Branch]*models.GithubPullRequest, error) {
res := map[*models.Branch]*models.GithubPullRequest{}
if len(prs) == 0 {
return res
return res, nil
}
remotesToOwnersMap, _ := c.GetRemotesToOwnersMap()
remotesToOwnersMap, err := c.GetRemotesToOwnersMap(remotes)
if len(remotesToOwnersMap) == 0 {
return res
return res, err
}
prWithStringKey := map[string]models.GithubPullRequest{}
@ -57,5 +58,5 @@ func (c *GitCommand) GenerateGithubPullRequestMap(prs []*models.GithubPullReques
res[branch] = &pr
}
return res
return res, nil
}

View file

@ -11,14 +11,14 @@ func TestGetRepoInfoFromURL(t *testing.T) {
type scenario struct {
testName string
repoURL string
test func(*RepoInformation)
test func(RepoInformation)
}
scenarios := []scenario{
{
"Returns repository information for git remote url",
"git@github.com:petersmith/super_calculator",
func(repoInfo *RepoInformation) {
func(repoInfo RepoInformation) {
assert.EqualValues(t, repoInfo.Owner, "petersmith")
assert.EqualValues(t, repoInfo.Repository, "super_calculator")
},
@ -26,7 +26,7 @@ func TestGetRepoInfoFromURL(t *testing.T) {
{
"Returns repository information for http remote url",
"https://my_username@bitbucket.org/johndoe/social_network.git",
func(repoInfo *RepoInformation) {
func(repoInfo RepoInformation) {
assert.EqualValues(t, repoInfo.Owner, "johndoe")
assert.EqualValues(t, repoInfo.Repository, "social_network")
},

View file

@ -4,6 +4,7 @@ import (
"fmt"
"strings"
"github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
)
@ -48,7 +49,7 @@ func (c *GitCommand) GetRemoteURL() string {
return c.GitConfig.Get("remote.origin.url")
}
func GetRepoInfoFromURL(url string) *RepoInformation {
func GetRepoInfoFromURL(url string) RepoInformation {
isHTTP := strings.HasPrefix(url, "http")
if isHTTP {
@ -56,7 +57,7 @@ func GetRepoInfoFromURL(url string) *RepoInformation {
owner := strings.Join(splits[3:len(splits)-1], "/")
repo := strings.TrimSuffix(splits[len(splits)-1], ".git")
return &RepoInformation{
return RepoInformation{
Owner: owner,
Repository: repo,
}
@ -67,33 +68,31 @@ func GetRepoInfoFromURL(url string) *RepoInformation {
owner := strings.Join(splits[0:len(splits)-1], "/")
repo := strings.TrimSuffix(splits[len(splits)-1], ".git")
return &RepoInformation{
return RepoInformation{
Owner: owner,
Repository: repo,
}
}
func (c *GitCommand) GetRemotesToOwnersMap() (map[string]string, error) {
remotes, err := c.GetRemotes()
if err != nil {
return nil, err
}
func (c *GitCommand) GetRemotesToOwnersMap(remotes []*models.Remote) (map[string]string, error) {
res := map[string]string{}
for _, remote := range remotes {
if len(remote.Urls) == 0 {
continue
}
res[remote.Name] = GetRepoInfoFromURL(remote.Urls[0]).Owner
}
return res, nil
}
func (c *GitCommand) GetRemotesToRepositoryMap() (map[string]string, error) {
remotes, err := c.GetRemotes()
if err != nil {
return nil, err
}
func (c *GitCommand) GetRemotesToRepositoryMap(remotes []*models.Remote) (map[string]string, error) {
res := map[string]string{}
for _, remote := range remotes {
if len(remote.Urls) == 0 {
continue
}
info := GetRepoInfoFromURL(remote.Urls[0])
res[info.Owner] = info.Repository
}

View file

@ -77,7 +77,6 @@ func (gui *Gui) refreshBranches() {
func (gui *Gui) refreshGithubPullRequests() {
_, err := gui.GitCommand.RunCommandWithOutput("git config --local --get-regexp .gh-resolved$")
if err == nil {
_ = gui.setGithubPullRequests()
return
@ -86,20 +85,18 @@ func (gui *Gui) refreshGithubPullRequests() {
// when config not exits
_ = gui.refreshRemotes()
_ = gui.prompt(promptOpts{
title: "Select remote Repository",
title: gui.Tr.SelectRemoteRepository,
initialContent: "",
findSuggestionsFunc: gui.getRemoteUrlSuggestionsFunc(),
findSuggestionsFunc: gui.getRemoteRepoSuggestionsFunc(),
handleConfirm: func(repository string) error {
return gui.WithWaitingStatus(gui.Tr.SelectRemoteRepository, func() error {
return gui.WithWaitingStatus(gui.Tr.LcSelectingRemote, func() error {
// ex git config --local --add "remote.origin.gh-resolved" "jesseduffield/lazygit"
_, err := gui.GitCommand.RunCommandWithOutput(fmt.Sprintf("git config --local --add \"remote.origin.gh-resolved\" \"%s\"", repository))
if err != nil {
return err
}
err = gui.setGithubPullRequests()
if err != nil {
return err
}
@ -134,7 +131,10 @@ func (gui *Gui) handleBranchPress() error {
func (gui *Gui) handleCreateOrShowPullRequestPress() error {
branch := gui.getSelectedBranch()
pr, hasPr := gui.GetPr(branch)
pr, hasPr, err := gui.GetPr(branch)
if err != nil {
return err
}
if hasPr {
return gui.OSCommand.OpenLink(pr.Url)

View file

@ -4,6 +4,7 @@ import (
"fmt"
"os"
"github.com/jesseduffield/lazygit/pkg/commands"
"github.com/jesseduffield/lazygit/pkg/gui/presentation"
"github.com/jesseduffield/lazygit/pkg/gui/types"
"github.com/jesseduffield/lazygit/pkg/utils"
@ -46,15 +47,24 @@ func (gui *Gui) getRemoteSuggestionsFunc() func(string) []*types.Suggestion {
return fuzzySearchFunc(remoteNames)
}
func (gui *Gui) getRemoteUrlSuggestionsFunc() func(string) []*types.Suggestion {
remotesToOwnersMap, _ := gui.GitCommand.GetRemotesToRepositoryMap()
result := make([]string, len(remotesToOwnersMap))
i := 0
for owner, repository := range remotesToOwnersMap {
result[i] = owner + "/" + repository
i++
func (gui *Gui) getRemoteRepoSuggestionsFunc() func(string) []*types.Suggestion {
remotesNames := gui.getRemoteRepoNames()
return fuzzySearchFunc(remotesNames)
}
func (gui *Gui) getRemoteRepoNames() []string {
remotes := gui.State.Remotes
result := make([]string, 0, len(remotes))
for _, remote := range remotes {
if len(remote.Urls) == 0 {
continue
}
info := commands.GetRepoInfoFromURL(remote.Urls[0])
result = append(result, fmt.Sprintf("%s/%s", info.Owner, info.Repository))
}
return fuzzySearchFunc(result)
return result
}
func (gui *Gui) getBranchNames() []string {

View file

@ -746,9 +746,17 @@ func (gui *Gui) setColorScheme() error {
return nil
}
func (gui *Gui) GetPr(branch *models.Branch) (*models.GithubPullRequest, bool) {
prs := gui.GitCommand.GenerateGithubPullRequestMap(gui.State.GithubState.RecentPRs, []*models.Branch{branch})
func (gui *Gui) GetPr(branch *models.Branch) (*models.GithubPullRequest, bool, error) {
prs, err := gui.GitCommand.GenerateGithubPullRequestMap(
gui.State.GithubState.RecentPRs,
[]*models.Branch{branch},
gui.State.Remotes,
)
if err != nil {
return nil, false, err
}
pr, hasPr := prs[branch]
return pr, hasPr
return pr, hasPr, nil
}

View file

@ -68,7 +68,11 @@ func (gui *Gui) branchesListContext() IListContext {
OnFocus: gui.handleBranchSelect,
Gui: gui,
GetDisplayStrings: func(startIdx int, length int) [][]string {
prs := gui.GitCommand.GenerateGithubPullRequestMap(gui.State.GithubState.RecentPRs, gui.State.Branches)
prs, err := gui.GitCommand.GenerateGithubPullRequestMap(gui.State.GithubState.RecentPRs, gui.State.Branches, gui.State.Remotes)
if err != nil {
panic(err)
}
return presentation.GetBranchListDisplayStrings(gui.State.Branches, prs, gui.State.ScreenMode != SCREEN_NORMAL, gui.State.Modes.Diffing.Ref)
},
SelectedItem: func() (ListItem, bool) {

View file

@ -50,12 +50,12 @@ func getBranchDisplayStrings(
recencyColor = style.FgGreen
}
res := []string{recencyColor.Sprint(b.Recency), coloredName}
res := []string{recencyColor.Sprint(b.Recency)}
pr, hasPr := prs[b]
res = append(res, coloredPrNumber(pr, hasPr))
res = append(res, coloredPrNumber(pr, hasPr), coloredName)
if fullDescription {
return append(res, style.FgYellow.Sprint(b.UpstreamName))
res = append(res, style.FgYellow.Sprint(b.UpstreamName))
}
return res
}

View file

@ -39,7 +39,10 @@ func (gui *Gui) createOrOpenPullRequestMenu(selectedBranch *models.Branch, check
}
}
pr, hasPr := gui.GetPr(selectedBranch)
pr, hasPr, err := gui.GetPr(selectedBranch)
if err != nil {
return err
}
if hasPr {
menuItems = append(menuItems, &menuItem{

View file

@ -434,6 +434,7 @@ func chineseTranslationSet() TranslationSet {
CommandLogHeader: "您可以通过按 '%s' 隐藏或集中显示该面板,或使用 `gui.showCommandLog: false`\n将其永久隐藏在您的配置中",
RandomTip: "随机提示",
SelectRemoteRepository: "选择存储库",
LcSelectingRemote: "选择遥控器",
SelectParentCommitForMerge: "选择父提交进行合并",
ToggleWhitespaceInDiffView: "切换是否在差异视图中显示空白更改",
IgnoringWhitespaceInDiffView: "差异视图中的空格将被忽略",

View file

@ -445,6 +445,7 @@ type TranslationSet struct {
AbortTitle string
AbortPrompt string
SelectRemoteRepository string
LcSelectingRemote string
LcOpenLogMenu string
LogMenuTitle string
ToggleShowGitGraphAll string
@ -956,7 +957,8 @@ func englishTranslationSet() TranslationSet {
SuggestionsTitle: "Suggestions (press %s to focus)",
ExtrasTitle: "Extras",
PushingTagStatus: "pushing tag",
SelectRemoteRepository: "Select Remote Repository",
SelectRemoteRepository: "select base remote repository",
LcSelectingRemote: "selecting remote",
PullRequestURLCopiedToClipboard: "Pull request URL copied to clipboard",
CommitMessageCopiedToClipboard: "Commit message copied to clipboard",
LcCopiedToClipboard: "copied to clipboard",