diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2021-07-27 20:02:52 +1000 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2021-07-27 21:30:08 +1000 |
commit | 58ddbae4d12d397a1409eaf961cfdc80f42f9168 (patch) | |
tree | bdf90e87a0fb46cb212a39966c7c20add8e431c7 /pkg | |
parent | 3802b563b03171607965b8d4771ea8f9d206553b (diff) |
Minor refactor
Diffstat (limited to 'pkg')
-rw-r--r-- | pkg/commands/pull_request.go | 42 | ||||
-rw-r--r-- | pkg/commands/pull_request_test.go | 91 | ||||
-rw-r--r-- | pkg/commands/remotes.go | 6 | ||||
-rw-r--r-- | pkg/gui/branches_panel.go | 14 | ||||
-rw-r--r-- | pkg/gui/pull_request_menu_panel.go | 76 | ||||
-rw-r--r-- | pkg/i18n/english.go | 6 |
6 files changed, 95 insertions, 140 deletions
diff --git a/pkg/commands/pull_request.go b/pkg/commands/pull_request.go index a8c96e328..46dcbfd68 100644 --- a/pkg/commands/pull_request.go +++ b/pkg/commands/pull_request.go @@ -5,7 +5,6 @@ import ( "strings" "github.com/go-errors/errors" - "github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/config" ) @@ -13,20 +12,7 @@ import ( type Service struct { Name string pullRequestURLIntoDefaultBranch func(owner string, repository string, from string) string - pullRequestURLIntoTargetBranch func(owner string, repository string, from string, to string) string -} - -// PullRequest opens a link in browser to create new pull request -// with selected branch -type PullRequest struct { - GitServices []*Service - GitCommand *GitCommand -} - -// RepoInformation holds some basic information about the repo -type RepoInformation struct { - Owner string - Repository string + pullRequestURLIntoTargetBranch func(owner string, repository string, from string, to string) string } // NewService builds a Service based on the host type @@ -77,6 +63,19 @@ func (s *Service) PullRequestURL(owner string, repository string, from string, t } } +// PullRequest opens a link in browser to create new pull request +// with selected branch +type PullRequest struct { + GitServices []*Service + GitCommand *GitCommand +} + +// RepoInformation holds some basic information about the repo +type RepoInformation struct { + Owner string + Repository string +} + func getServices(config config.AppConfigurer) []*Service { services := []*Service{ NewService("github", "github.com", "github.com"), @@ -114,7 +113,7 @@ func NewPullRequest(gitCommand *GitCommand) *PullRequest { } // Create opens link to new pull request in browser -func (pr *PullRequest) Create(from *models.Branch, to *models.Branch) (string, error) { +func (pr *PullRequest) Create(from string, to string) (string, error) { pullRequestURL, err := pr.getPullRequestURL(from, to) if err != nil { return "", err @@ -124,7 +123,7 @@ func (pr *PullRequest) Create(from *models.Branch, to *models.Branch) (string, e } // CopyURL copies the pull request URL to the clipboard -func (pr *PullRequest) CopyURL(from *models.Branch, to *models.Branch) (string, error) { +func (pr *PullRequest) CopyURL(from string, to string) (string, error) { pullRequestURL, err := pr.getPullRequestURL(from, to) if err != nil { return "", err @@ -133,7 +132,7 @@ func (pr *PullRequest) CopyURL(from *models.Branch, to *models.Branch) (string, return pullRequestURL, pr.GitCommand.OSCommand.CopyToClipboard(pullRequestURL) } -func (pr *PullRequest) getPullRequestURL(from *models.Branch, to *models.Branch) (string, error) { +func (pr *PullRequest) getPullRequestURL(from string, to string) (string, error) { branchExistsOnRemote := pr.GitCommand.CheckRemoteBranchExists(from) if !branchExistsOnRemote { @@ -155,11 +154,8 @@ func (pr *PullRequest) getPullRequestURL(from *models.Branch, to *models.Branch) } repoInfo := getRepoInfoFromURL(repoURL) - toBranchName := "" - if to != nil { - toBranchName = to.Name - } - pullRequestURL := gitService.PullRequestURL(repoInfo.Owner, repoInfo.Repository, from.Name, toBranchName) + + pullRequestURL := gitService.PullRequestURL(repoInfo.Owner, repoInfo.Repository, from, to) return pullRequestURL, nil } diff --git a/pkg/commands/pull_request_test.go b/pkg/commands/pull_request_test.go index 1a9137be5..2db5b8ade 100644 --- a/pkg/commands/pull_request_test.go +++ b/pkg/commands/pull_request_test.go @@ -5,7 +5,6 @@ import ( "strings" "testing" - "github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/secureexec" "github.com/stretchr/testify/assert" ) @@ -48,8 +47,8 @@ func TestGetRepoInfoFromURL(t *testing.T) { func TestCreatePullRequest(t *testing.T) { type scenario struct { testName string - from *models.Branch - to *models.Branch + from string + to string remoteUrl string command func(string, ...string) *exec.Cmd test func(url string, err error) @@ -57,10 +56,8 @@ func TestCreatePullRequest(t *testing.T) { scenarios := []scenario{ { - testName: "Opens a link to new pull request on bitbucket", - from: &models.Branch{ - Name: "feature/profile-page", - }, + testName: "Opens a link to new pull request on bitbucket", + from: "feature/profile-page", remoteUrl: "git@bitbucket.org:johndoe/social_network.git", command: func(cmd string, args ...string) *exec.Cmd { // Handle git remote url call @@ -78,10 +75,8 @@ func TestCreatePullRequest(t *testing.T) { }, }, { - testName: "Opens a link to new pull request on bitbucket with http remote url", - from: &models.Branch{ - Name: "feature/events", - }, + testName: "Opens a link to new pull request on bitbucket with http remote url", + from: "feature/events", remoteUrl: "https://my_username@bitbucket.org/johndoe/social_network.git", command: func(cmd string, args ...string) *exec.Cmd { // Handle git remote url call @@ -99,10 +94,8 @@ func TestCreatePullRequest(t *testing.T) { }, }, { - testName: "Opens a link to new pull request on github", - from: &models.Branch{ - Name: "feature/sum-operation", - }, + testName: "Opens a link to new pull request on github", + from: "feature/sum-operation", remoteUrl: "git@github.com:peter/calculator.git", command: func(cmd string, args ...string) *exec.Cmd { // Handle git remote url call @@ -120,13 +113,9 @@ func TestCreatePullRequest(t *testing.T) { }, }, { - testName: "Opens a link to new pull request on bitbucket with specific target branch", - from: &models.Branch{ - Name: "feature/profile-page/avatar", - }, - to: &models.Branch{ - Name: "feature/profile-page", - }, + testName: "Opens a link to new pull request on bitbucket with specific target branch", + from: "feature/profile-page/avatar", + to: "feature/profile-page", remoteUrl: "git@bitbucket.org:johndoe/social_network.git", command: func(cmd string, args ...string) *exec.Cmd { // Handle git remote url call @@ -144,13 +133,9 @@ func TestCreatePullRequest(t *testing.T) { }, }, { - testName: "Opens a link to new pull request on bitbucket with http remote url with specified target branch", - from: &models.Branch{ - Name: "feature/remote-events", - }, - to: &models.Branch{ - Name: "feature/events", - }, + testName: "Opens a link to new pull request on bitbucket with http remote url with specified target branch", + from: "feature/remote-events", + to: "feature/events", remoteUrl: "https://my_username@bitbucket.org/johndoe/social_network.git", command: func(cmd string, args ...string) *exec.Cmd { // Handle git remote url call @@ -168,13 +153,9 @@ func TestCreatePullRequest(t *testing.T) { }, }, { - testName: "Opens a link to new pull request on github with specific target branch", - from: &models.Branch{ - Name: "feature/sum-operation", - }, - to: &models.Branch{ - Name: "feature/operations", - }, + testName: "Opens a link to new pull request on github with specific target branch", + from: "feature/sum-operation", + to: "feature/operations", remoteUrl: "git@github.com:peter/calculator.git", command: func(cmd string, args ...string) *exec.Cmd { // Handle git remote url call @@ -192,10 +173,8 @@ func TestCreatePullRequest(t *testing.T) { }, }, { - testName: "Opens a link to new pull request on gitlab", - from: &models.Branch{ - Name: "feature/ui", - }, + testName: "Opens a link to new pull request on gitlab", + from: "feature/ui", remoteUrl: "git@gitlab.com:peter/calculator.git", command: func(cmd string, args ...string) *exec.Cmd { // Handle git remote url call @@ -213,10 +192,8 @@ func TestCreatePullRequest(t *testing.T) { }, }, { - testName: "Opens a link to new pull request on gitlab in nested groups", - from: &models.Branch{ - Name: "feature/ui", - }, + testName: "Opens a link to new pull request on gitlab in nested groups", + from: "feature/ui", remoteUrl: "git@gitlab.com:peter/public/calculator.git", command: func(cmd string, args ...string) *exec.Cmd { // Handle git remote url call @@ -234,13 +211,9 @@ func TestCreatePullRequest(t *testing.T) { }, }, { - testName: "Opens a link to new pull request on gitlab with specific target branch", - from: &models.Branch{ - Name: "feature/commit-ui", - }, - to: &models.Branch{ - Name: "epic/ui", - }, + testName: "Opens a link to new pull request on gitlab with specific target branch", + from: "feature/commit-ui", + to: "epic/ui", remoteUrl: "git@gitlab.com:peter/calculator.git", command: func(cmd string, args ...string) *exec.Cmd { // Handle git remote url call @@ -258,13 +231,9 @@ func TestCreatePullRequest(t *testing.T) { }, }, { - testName: "Opens a link to new pull request on gitlab with specific target branch in nested groups", - from: &models.Branch{ - Name: "feature/commit-ui", - }, - to: &models.Branch{ - Name: "epic/ui", - }, + testName: "Opens a link to new pull request on gitlab with specific target branch in nested groups", + from: "feature/commit-ui", + to: "epic/ui", remoteUrl: "git@gitlab.com:peter/public/calculator.git", command: func(cmd string, args ...string) *exec.Cmd { // Handle git remote url call @@ -282,10 +251,8 @@ func TestCreatePullRequest(t *testing.T) { }, }, { - testName: "Throws an error if git service is unsupported", - from: &models.Branch{ - Name: "feature/divide-operation", - }, + testName: "Throws an error if git service is unsupported", + from: "feature/divide-operation", remoteUrl: "git@something.com:peter/calculator.git", command: func(cmd string, args ...string) *exec.Cmd { return secureexec.Command("echo") diff --git a/pkg/commands/remotes.go b/pkg/commands/remotes.go index 535149c1a..75dee0b46 100644 --- a/pkg/commands/remotes.go +++ b/pkg/commands/remotes.go @@ -2,8 +2,6 @@ package commands import ( "fmt" - - "github.com/jesseduffield/lazygit/pkg/commands/models" ) func (c *GitCommand) AddRemote(name string, url string) error { @@ -28,10 +26,10 @@ func (c *GitCommand) DeleteRemoteBranch(remoteName string, branchName string, pr } // CheckRemoteBranchExists Returns remote branch -func (c *GitCommand) CheckRemoteBranchExists(branch *models.Branch) bool { +func (c *GitCommand) CheckRemoteBranchExists(branchName string) bool { _, err := c.OSCommand.RunCommandWithOutput( "git show-ref --verify -- refs/remotes/origin/%s", - branch.Name, + branchName, ) return err == nil diff --git a/pkg/gui/branches_panel.go b/pkg/gui/branches_panel.go index 867566a24..9e44a8a8c 100644 --- a/pkg/gui/branches_panel.go +++ b/pkg/gui/branches_panel.go @@ -92,7 +92,7 @@ func (gui *Gui) handleBranchPress() error { func (gui *Gui) handleCreatePullRequestPress() error { branch := gui.getSelectedBranch() - return createPullRequest(branch, nil, gui) + return gui.createPullRequest(branch.Name, "") } func (gui *Gui) handleCreatePullRequestMenu() error { @@ -109,7 +109,7 @@ func (gui *Gui) handleCopyPullRequestURLPress() error { pullRequest := commands.NewPullRequest(gui.GitCommand) branch := gui.getSelectedBranch() - url, err := pullRequest.CopyURL(branch, nil) + url, err := pullRequest.CopyURL(branch.Name, "") if err != nil { return gui.surfaceError(err) } @@ -545,16 +545,6 @@ func (gui *Gui) getBranchNames() []string { return result } -func (gui *Gui) getBranchByName(name string) *models.Branch { - for _, branch := range gui.State.Branches { - if branch.Name == name { - return branch - } - } - - return nil -} - func (gui *Gui) findBranchNameSuggestions(input string) []*types.Suggestion { branchNames := gui.getBranchNames() diff --git a/pkg/gui/pull_request_menu_panel.go b/pkg/gui/pull_request_menu_panel.go index b0f1ec796..7cdd7dcbd 100644 --- a/pkg/gui/pull_request_menu_panel.go +++ b/pkg/gui/pull_request_menu_panel.go @@ -2,6 +2,7 @@ package gui import ( "fmt" + "github.com/jesseduffield/lazygit/pkg/commands" "github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/commands/oscommands" @@ -10,54 +11,51 @@ import ( func (gui *Gui) createPullRequestMenu(selectedBranch *models.Branch, checkedOutBranch *models.Branch) error { menuItems := make([]*menuItem, 0, 4) - if selectedBranch != checkedOutBranch { - menuItems = append(menuItems, &menuItem{ - displayStrings: []string{ - fmt.Sprintf("%s → default branch", selectedBranch.Name), - }, - onPress: func() error { - return createPullRequest(selectedBranch, nil, gui) + fromToDisplayStrings := func(from string, to string) []string { + return []string{fmt.Sprintf("%s → %s", from, to)} + } + + menuItemsForBranch := func(branch *models.Branch) []*menuItem { + return []*menuItem{ + { + displayStrings: fromToDisplayStrings(branch.Name, gui.Tr.DefaultBranch), + onPress: func() error { + return gui.createPullRequest(branch.Name, "") + }, }, - }, &menuItem{ - displayStrings: []string{ - fmt.Sprintf("%s → %s", checkedOutBranch.Name, selectedBranch.Name), + { + displayStrings: fromToDisplayStrings(branch.Name, gui.Tr.SelectBranch), + onPress: func() error { + return gui.prompt(promptOpts{ + title: branch.Name + " →", + findSuggestionsFunc: gui.findBranchNameSuggestions, + handleConfirm: func(targetBranchName string) error { + return gui.createPullRequest(branch.Name, targetBranchName) + }}, + ) + }, }, - onPress: func() error { - return createPullRequest(checkedOutBranch, selectedBranch, gui) + } + } + + if selectedBranch != checkedOutBranch { + menuItems = append(menuItems, + &menuItem{ + displayStrings: fromToDisplayStrings(checkedOutBranch.Name, selectedBranch.Name), + onPress: func() error { + return gui.createPullRequest(checkedOutBranch.Name, selectedBranch.Name) + }, }, - }) + ) + menuItems = append(menuItems, menuItemsForBranch(checkedOutBranch)...) } - menuItems = append(menuItems, &menuItem{ - displayStrings: []string{ - fmt.Sprintf("%s → default branch", checkedOutBranch.Name), - }, - onPress: func() error { - return createPullRequest(checkedOutBranch, nil, gui) - }, - }, &menuItem{ - displayStrings: []string{ - fmt.Sprintf("%s → select branch", checkedOutBranch.Name), - }, - onPress: func() error { - return gui.prompt(promptOpts{ - title: checkedOutBranch.Name + " →", - findSuggestionsFunc: gui.findBranchNameSuggestions, - handleConfirm: func(response string) error { - targetBranch := gui.getBranchByName(response) - if targetBranch == nil { - return gui.createErrorPanel(gui.Tr.BranchNotFoundTitle) - } - return createPullRequest(checkedOutBranch, targetBranch, gui) - }}, - ) - }, - }) + menuItems = append(menuItems, menuItemsForBranch(selectedBranch)...) return gui.createMenu(fmt.Sprintf(gui.Tr.CreatePullRequestOptions), menuItems, createMenuOptions{showCancel: true}) } -func createPullRequest(from *models.Branch, to *models.Branch, gui *Gui) error { +func (gui *Gui) createPullRequest(from string, to string) error { pullRequest := commands.NewPullRequest(gui.GitCommand) url, err := pullRequest.Create(from, to) if err != nil { diff --git a/pkg/i18n/english.go b/pkg/i18n/english.go index 7d1c8b337..da49e46ce 100644 --- a/pkg/i18n/english.go +++ b/pkg/i18n/english.go @@ -457,6 +457,9 @@ type TranslationSet struct { CreatePullRequest string CreatePullRequestOptions string LcCreatePullRequestOptions string + SelectParentCommitForMerge string + DefaultBranch string + SelectBranch string Spans Spans } @@ -1007,6 +1010,9 @@ func englishTranslationSet() TranslationSet { CreatePullRequest: "Create pull request", CreatePullRequestOptions: "Create pull request options", LcCreatePullRequestOptions: "create pull request options", + SelectParentCommitForMerge: "Select parent commit for merge", + DefaultBranch: "default branch", + SelectBranch: "select branch", Spans: Spans{ // TODO: combine this with the original keybinding descriptions (those are all in lowercase atm) CheckoutCommit: "Checkout commit", |