1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-04-21 12:16:54 +02:00

Minor refactor

This commit is contained in:
Jesse Duffield 2021-07-27 20:02:52 +10:00
parent 3802b563b0
commit 58ddbae4d1
6 changed files with 98 additions and 143 deletions

View File

@ -5,7 +5,6 @@ import (
"strings" "strings"
"github.com/go-errors/errors" "github.com/go-errors/errors"
"github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/config" "github.com/jesseduffield/lazygit/pkg/config"
) )
@ -16,19 +15,6 @@ type Service struct {
pullRequestURLIntoTargetBranch func(owner string, repository string, from string, to 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
}
// NewService builds a Service based on the host type // NewService builds a Service based on the host type
func NewService(typeName string, repositoryDomain string, siteDomain string) *Service { func NewService(typeName string, repositoryDomain string, siteDomain string) *Service {
var service *Service var service *Service
@ -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 { func getServices(config config.AppConfigurer) []*Service {
services := []*Service{ services := []*Service{
NewService("github", "github.com", "github.com"), NewService("github", "github.com", "github.com"),
@ -114,7 +113,7 @@ func NewPullRequest(gitCommand *GitCommand) *PullRequest {
} }
// Create opens link to new pull request in browser // 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) pullRequestURL, err := pr.getPullRequestURL(from, to)
if err != nil { if err != nil {
return "", err 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 // 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) pullRequestURL, err := pr.getPullRequestURL(from, to)
if err != nil { if err != nil {
return "", err return "", err
@ -133,7 +132,7 @@ func (pr *PullRequest) CopyURL(from *models.Branch, to *models.Branch) (string,
return pullRequestURL, pr.GitCommand.OSCommand.CopyToClipboard(pullRequestURL) 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) branchExistsOnRemote := pr.GitCommand.CheckRemoteBranchExists(from)
if !branchExistsOnRemote { if !branchExistsOnRemote {
@ -155,11 +154,8 @@ func (pr *PullRequest) getPullRequestURL(from *models.Branch, to *models.Branch)
} }
repoInfo := getRepoInfoFromURL(repoURL) repoInfo := getRepoInfoFromURL(repoURL)
toBranchName := ""
if to != nil { pullRequestURL := gitService.PullRequestURL(repoInfo.Owner, repoInfo.Repository, from, to)
toBranchName = to.Name
}
pullRequestURL := gitService.PullRequestURL(repoInfo.Owner, repoInfo.Repository, from.Name, toBranchName)
return pullRequestURL, nil return pullRequestURL, nil
} }

View File

@ -5,7 +5,6 @@ import (
"strings" "strings"
"testing" "testing"
"github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/secureexec" "github.com/jesseduffield/lazygit/pkg/secureexec"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
@ -48,8 +47,8 @@ func TestGetRepoInfoFromURL(t *testing.T) {
func TestCreatePullRequest(t *testing.T) { func TestCreatePullRequest(t *testing.T) {
type scenario struct { type scenario struct {
testName string testName string
from *models.Branch from string
to *models.Branch to string
remoteUrl string remoteUrl string
command func(string, ...string) *exec.Cmd command func(string, ...string) *exec.Cmd
test func(url string, err error) test func(url string, err error)
@ -58,9 +57,7 @@ func TestCreatePullRequest(t *testing.T) {
scenarios := []scenario{ scenarios := []scenario{
{ {
testName: "Opens a link to new pull request on bitbucket", testName: "Opens a link to new pull request on bitbucket",
from: &models.Branch{ from: "feature/profile-page",
Name: "feature/profile-page",
},
remoteUrl: "git@bitbucket.org:johndoe/social_network.git", remoteUrl: "git@bitbucket.org:johndoe/social_network.git",
command: func(cmd string, args ...string) *exec.Cmd { command: func(cmd string, args ...string) *exec.Cmd {
// Handle git remote url call // Handle git remote url call
@ -79,9 +76,7 @@ func TestCreatePullRequest(t *testing.T) {
}, },
{ {
testName: "Opens a link to new pull request on bitbucket with http remote url", testName: "Opens a link to new pull request on bitbucket with http remote url",
from: &models.Branch{ from: "feature/events",
Name: "feature/events",
},
remoteUrl: "https://my_username@bitbucket.org/johndoe/social_network.git", remoteUrl: "https://my_username@bitbucket.org/johndoe/social_network.git",
command: func(cmd string, args ...string) *exec.Cmd { command: func(cmd string, args ...string) *exec.Cmd {
// Handle git remote url call // Handle git remote url call
@ -100,9 +95,7 @@ func TestCreatePullRequest(t *testing.T) {
}, },
{ {
testName: "Opens a link to new pull request on github", testName: "Opens a link to new pull request on github",
from: &models.Branch{ from: "feature/sum-operation",
Name: "feature/sum-operation",
},
remoteUrl: "git@github.com:peter/calculator.git", remoteUrl: "git@github.com:peter/calculator.git",
command: func(cmd string, args ...string) *exec.Cmd { command: func(cmd string, args ...string) *exec.Cmd {
// Handle git remote url call // Handle git remote url call
@ -121,12 +114,8 @@ func TestCreatePullRequest(t *testing.T) {
}, },
{ {
testName: "Opens a link to new pull request on bitbucket with specific target branch", testName: "Opens a link to new pull request on bitbucket with specific target branch",
from: &models.Branch{ from: "feature/profile-page/avatar",
Name: "feature/profile-page/avatar", to: "feature/profile-page",
},
to: &models.Branch{
Name: "feature/profile-page",
},
remoteUrl: "git@bitbucket.org:johndoe/social_network.git", remoteUrl: "git@bitbucket.org:johndoe/social_network.git",
command: func(cmd string, args ...string) *exec.Cmd { command: func(cmd string, args ...string) *exec.Cmd {
// Handle git remote url call // Handle git remote url call
@ -145,12 +134,8 @@ func TestCreatePullRequest(t *testing.T) {
}, },
{ {
testName: "Opens a link to new pull request on bitbucket with http remote url with specified target branch", testName: "Opens a link to new pull request on bitbucket with http remote url with specified target branch",
from: &models.Branch{ from: "feature/remote-events",
Name: "feature/remote-events", to: "feature/events",
},
to: &models.Branch{
Name: "feature/events",
},
remoteUrl: "https://my_username@bitbucket.org/johndoe/social_network.git", remoteUrl: "https://my_username@bitbucket.org/johndoe/social_network.git",
command: func(cmd string, args ...string) *exec.Cmd { command: func(cmd string, args ...string) *exec.Cmd {
// Handle git remote url call // Handle git remote url call
@ -169,12 +154,8 @@ func TestCreatePullRequest(t *testing.T) {
}, },
{ {
testName: "Opens a link to new pull request on github with specific target branch", testName: "Opens a link to new pull request on github with specific target branch",
from: &models.Branch{ from: "feature/sum-operation",
Name: "feature/sum-operation", to: "feature/operations",
},
to: &models.Branch{
Name: "feature/operations",
},
remoteUrl: "git@github.com:peter/calculator.git", remoteUrl: "git@github.com:peter/calculator.git",
command: func(cmd string, args ...string) *exec.Cmd { command: func(cmd string, args ...string) *exec.Cmd {
// Handle git remote url call // Handle git remote url call
@ -193,9 +174,7 @@ func TestCreatePullRequest(t *testing.T) {
}, },
{ {
testName: "Opens a link to new pull request on gitlab", testName: "Opens a link to new pull request on gitlab",
from: &models.Branch{ from: "feature/ui",
Name: "feature/ui",
},
remoteUrl: "git@gitlab.com:peter/calculator.git", remoteUrl: "git@gitlab.com:peter/calculator.git",
command: func(cmd string, args ...string) *exec.Cmd { command: func(cmd string, args ...string) *exec.Cmd {
// Handle git remote url call // Handle git remote url call
@ -214,9 +193,7 @@ func TestCreatePullRequest(t *testing.T) {
}, },
{ {
testName: "Opens a link to new pull request on gitlab in nested groups", testName: "Opens a link to new pull request on gitlab in nested groups",
from: &models.Branch{ from: "feature/ui",
Name: "feature/ui",
},
remoteUrl: "git@gitlab.com:peter/public/calculator.git", remoteUrl: "git@gitlab.com:peter/public/calculator.git",
command: func(cmd string, args ...string) *exec.Cmd { command: func(cmd string, args ...string) *exec.Cmd {
// Handle git remote url call // Handle git remote url call
@ -235,12 +212,8 @@ func TestCreatePullRequest(t *testing.T) {
}, },
{ {
testName: "Opens a link to new pull request on gitlab with specific target branch", testName: "Opens a link to new pull request on gitlab with specific target branch",
from: &models.Branch{ from: "feature/commit-ui",
Name: "feature/commit-ui", to: "epic/ui",
},
to: &models.Branch{
Name: "epic/ui",
},
remoteUrl: "git@gitlab.com:peter/calculator.git", remoteUrl: "git@gitlab.com:peter/calculator.git",
command: func(cmd string, args ...string) *exec.Cmd { command: func(cmd string, args ...string) *exec.Cmd {
// Handle git remote url call // Handle git remote url call
@ -259,12 +232,8 @@ func TestCreatePullRequest(t *testing.T) {
}, },
{ {
testName: "Opens a link to new pull request on gitlab with specific target branch in nested groups", testName: "Opens a link to new pull request on gitlab with specific target branch in nested groups",
from: &models.Branch{ from: "feature/commit-ui",
Name: "feature/commit-ui", to: "epic/ui",
},
to: &models.Branch{
Name: "epic/ui",
},
remoteUrl: "git@gitlab.com:peter/public/calculator.git", remoteUrl: "git@gitlab.com:peter/public/calculator.git",
command: func(cmd string, args ...string) *exec.Cmd { command: func(cmd string, args ...string) *exec.Cmd {
// Handle git remote url call // Handle git remote url call
@ -283,9 +252,7 @@ func TestCreatePullRequest(t *testing.T) {
}, },
{ {
testName: "Throws an error if git service is unsupported", testName: "Throws an error if git service is unsupported",
from: &models.Branch{ from: "feature/divide-operation",
Name: "feature/divide-operation",
},
remoteUrl: "git@something.com:peter/calculator.git", remoteUrl: "git@something.com:peter/calculator.git",
command: func(cmd string, args ...string) *exec.Cmd { command: func(cmd string, args ...string) *exec.Cmd {
return secureexec.Command("echo") return secureexec.Command("echo")

View File

@ -2,8 +2,6 @@ package commands
import ( import (
"fmt" "fmt"
"github.com/jesseduffield/lazygit/pkg/commands/models"
) )
func (c *GitCommand) AddRemote(name string, url string) error { 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 // CheckRemoteBranchExists Returns remote branch
func (c *GitCommand) CheckRemoteBranchExists(branch *models.Branch) bool { func (c *GitCommand) CheckRemoteBranchExists(branchName string) bool {
_, err := c.OSCommand.RunCommandWithOutput( _, err := c.OSCommand.RunCommandWithOutput(
"git show-ref --verify -- refs/remotes/origin/%s", "git show-ref --verify -- refs/remotes/origin/%s",
branch.Name, branchName,
) )
return err == nil return err == nil

View File

@ -92,7 +92,7 @@ func (gui *Gui) handleBranchPress() error {
func (gui *Gui) handleCreatePullRequestPress() error { func (gui *Gui) handleCreatePullRequestPress() error {
branch := gui.getSelectedBranch() branch := gui.getSelectedBranch()
return createPullRequest(branch, nil, gui) return gui.createPullRequest(branch.Name, "")
} }
func (gui *Gui) handleCreatePullRequestMenu() error { func (gui *Gui) handleCreatePullRequestMenu() error {
@ -109,7 +109,7 @@ func (gui *Gui) handleCopyPullRequestURLPress() error {
pullRequest := commands.NewPullRequest(gui.GitCommand) pullRequest := commands.NewPullRequest(gui.GitCommand)
branch := gui.getSelectedBranch() branch := gui.getSelectedBranch()
url, err := pullRequest.CopyURL(branch, nil) url, err := pullRequest.CopyURL(branch.Name, "")
if err != nil { if err != nil {
return gui.surfaceError(err) return gui.surfaceError(err)
} }
@ -545,16 +545,6 @@ func (gui *Gui) getBranchNames() []string {
return result 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 { func (gui *Gui) findBranchNameSuggestions(input string) []*types.Suggestion {
branchNames := gui.getBranchNames() branchNames := gui.getBranchNames()

View File

@ -2,6 +2,7 @@ package gui
import ( import (
"fmt" "fmt"
"github.com/jesseduffield/lazygit/pkg/commands" "github.com/jesseduffield/lazygit/pkg/commands"
"github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/commands/oscommands" "github.com/jesseduffield/lazygit/pkg/commands/oscommands"
@ -10,54 +11,51 @@ import (
func (gui *Gui) createPullRequestMenu(selectedBranch *models.Branch, checkedOutBranch *models.Branch) error { func (gui *Gui) createPullRequestMenu(selectedBranch *models.Branch, checkedOutBranch *models.Branch) error {
menuItems := make([]*menuItem, 0, 4) menuItems := make([]*menuItem, 0, 4)
if selectedBranch != checkedOutBranch { fromToDisplayStrings := func(from string, to string) []string {
menuItems = append(menuItems, &menuItem{ return []string{fmt.Sprintf("%s → %s", from, to)}
displayStrings: []string{
fmt.Sprintf("%s → default branch", selectedBranch.Name),
},
onPress: func() error {
return createPullRequest(selectedBranch, nil, gui)
},
}, &menuItem{
displayStrings: []string{
fmt.Sprintf("%s → %s", checkedOutBranch.Name, selectedBranch.Name),
},
onPress: func() error {
return createPullRequest(checkedOutBranch, selectedBranch, gui)
},
})
} }
menuItems = append(menuItems, &menuItem{ menuItemsForBranch := func(branch *models.Branch) []*menuItem {
displayStrings: []string{ return []*menuItem{
fmt.Sprintf("%s → default branch", checkedOutBranch.Name), {
}, displayStrings: fromToDisplayStrings(branch.Name, gui.Tr.DefaultBranch),
onPress: func() error { onPress: func() error {
return createPullRequest(checkedOutBranch, nil, gui) return gui.createPullRequest(branch.Name, "")
}, },
}, &menuItem{
displayStrings: []string{
fmt.Sprintf("%s → select branch", checkedOutBranch.Name),
}, },
{
displayStrings: fromToDisplayStrings(branch.Name, gui.Tr.SelectBranch),
onPress: func() error { onPress: func() error {
return gui.prompt(promptOpts{ return gui.prompt(promptOpts{
title: checkedOutBranch.Name + " →", title: branch.Name + " →",
findSuggestionsFunc: gui.findBranchNameSuggestions, findSuggestionsFunc: gui.findBranchNameSuggestions,
handleConfirm: func(response string) error { handleConfirm: func(targetBranchName string) error {
targetBranch := gui.getBranchByName(response) return gui.createPullRequest(branch.Name, targetBranchName)
if targetBranch == nil {
return gui.createErrorPanel(gui.Tr.BranchNotFoundTitle)
}
return createPullRequest(checkedOutBranch, targetBranch, 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, menuItemsForBranch(selectedBranch)...)
return gui.createMenu(fmt.Sprintf(gui.Tr.CreatePullRequestOptions), menuItems, createMenuOptions{showCancel: true}) 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) pullRequest := commands.NewPullRequest(gui.GitCommand)
url, err := pullRequest.Create(from, to) url, err := pullRequest.Create(from, to)
if err != nil { if err != nil {

View File

@ -457,6 +457,9 @@ type TranslationSet struct {
CreatePullRequest string CreatePullRequest string
CreatePullRequestOptions string CreatePullRequestOptions string
LcCreatePullRequestOptions string LcCreatePullRequestOptions string
SelectParentCommitForMerge string
DefaultBranch string
SelectBranch string
Spans Spans Spans Spans
} }
@ -1007,6 +1010,9 @@ func englishTranslationSet() TranslationSet {
CreatePullRequest: "Create pull request", CreatePullRequest: "Create pull request",
CreatePullRequestOptions: "Create pull request options", CreatePullRequestOptions: "Create pull request options",
LcCreatePullRequestOptions: "create pull request options", LcCreatePullRequestOptions: "create pull request options",
SelectParentCommitForMerge: "Select parent commit for merge",
DefaultBranch: "default branch",
SelectBranch: "select branch",
Spans: Spans{ Spans: Spans{
// TODO: combine this with the original keybinding descriptions (those are all in lowercase atm) // TODO: combine this with the original keybinding descriptions (those are all in lowercase atm)
CheckoutCommit: "Checkout commit", CheckoutCommit: "Checkout commit",