From bf355bc0b5f819886e04cb6cc4f3d311713696e7 Mon Sep 17 00:00:00 2001 From: Yuki Osaki Date: Tue, 9 Nov 2021 23:51:09 +0900 Subject: [PATCH] fix based on the reviews --- pkg/app/app.go | 11 ++++++----- pkg/app/app_test.go | 9 +++++++++ pkg/commands/github.go | 11 ++++++----- pkg/commands/pull_request_test.go | 6 +++--- pkg/commands/remotes.go | 29 ++++++++++++++--------------- pkg/gui/branches_panel.go | 14 +++++++------- pkg/gui/find_suggestions.go | 26 ++++++++++++++++++-------- pkg/gui/gui.go | 14 +++++++++++--- pkg/gui/list_context_config.go | 6 +++++- pkg/gui/presentation/branches.go | 6 +++--- pkg/gui/pull_request_menu_panel.go | 5 ++++- pkg/i18n/chinese.go | 1 + pkg/i18n/english.go | 4 +++- 13 files changed, 90 insertions(+), 52 deletions(-) diff --git a/pkg/app/app.go b/pkg/app/app.go index 442a50839..c3e4042c5 100644 --- a/pkg/app/app.go +++ b/pkg/app/app.go @@ -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 } diff --git a/pkg/app/app_test.go b/pkg/app/app_test.go index e3c4228fe..2008e0a80 100644 --- a/pkg/app/app_test.go +++ b/pkg/app/app_test.go @@ -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 { diff --git a/pkg/commands/github.go b/pkg/commands/github.go index f9e8e0e32..65489143f 100644 --- a/pkg/commands/github.go +++ b/pkg/commands/github.go @@ -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 } diff --git a/pkg/commands/pull_request_test.go b/pkg/commands/pull_request_test.go index 66fd06716..f1283447b 100644 --- a/pkg/commands/pull_request_test.go +++ b/pkg/commands/pull_request_test.go @@ -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") }, diff --git a/pkg/commands/remotes.go b/pkg/commands/remotes.go index 327586e2d..d25798d32 100644 --- a/pkg/commands/remotes.go +++ b/pkg/commands/remotes.go @@ -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 } diff --git a/pkg/gui/branches_panel.go b/pkg/gui/branches_panel.go index 3a9ac67c9..74f1ed485 100644 --- a/pkg/gui/branches_panel.go +++ b/pkg/gui/branches_panel.go @@ -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) diff --git a/pkg/gui/find_suggestions.go b/pkg/gui/find_suggestions.go index 73d14904c..5864eb178 100644 --- a/pkg/gui/find_suggestions.go +++ b/pkg/gui/find_suggestions.go @@ -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 { diff --git a/pkg/gui/gui.go b/pkg/gui/gui.go index 1d0a98180..726a159c8 100644 --- a/pkg/gui/gui.go +++ b/pkg/gui/gui.go @@ -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 } diff --git a/pkg/gui/list_context_config.go b/pkg/gui/list_context_config.go index fc1b5d66b..818ae45df 100644 --- a/pkg/gui/list_context_config.go +++ b/pkg/gui/list_context_config.go @@ -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) { diff --git a/pkg/gui/presentation/branches.go b/pkg/gui/presentation/branches.go index 45f31c7c4..3fff6a4d5 100644 --- a/pkg/gui/presentation/branches.go +++ b/pkg/gui/presentation/branches.go @@ -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 } diff --git a/pkg/gui/pull_request_menu_panel.go b/pkg/gui/pull_request_menu_panel.go index 023acf1f8..dcc78f893 100644 --- a/pkg/gui/pull_request_menu_panel.go +++ b/pkg/gui/pull_request_menu_panel.go @@ -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{ diff --git a/pkg/i18n/chinese.go b/pkg/i18n/chinese.go index cad10c4ce..ebd0c2744 100644 --- a/pkg/i18n/chinese.go +++ b/pkg/i18n/chinese.go @@ -434,6 +434,7 @@ func chineseTranslationSet() TranslationSet { CommandLogHeader: "您可以通过按 '%s' 隐藏或集中显示该面板,或使用 `gui.showCommandLog: false`\n将其永久隐藏在您的配置中", RandomTip: "随机提示", SelectRemoteRepository: "选择存储库", + LcSelectingRemote: "选择遥控器", SelectParentCommitForMerge: "选择父提交进行合并", ToggleWhitespaceInDiffView: "切换是否在差异视图中显示空白更改", IgnoringWhitespaceInDiffView: "差异视图中的空格将被忽略", diff --git a/pkg/i18n/english.go b/pkg/i18n/english.go index 32b246ad0..85a4d8edb 100644 --- a/pkg/i18n/english.go +++ b/pkg/i18n/english.go @@ -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",