mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-04-21 12:16:54 +02:00
Minor refactor
This commit is contained in:
parent
3802b563b0
commit
58ddbae4d1
@ -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
|
||||
}
|
||||
|
@ -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")
|
||||
|
@ -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
|
||||
|
@ -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()
|
||||
|
||||
|
@ -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)
|
||||
},
|
||||
}, &menuItem{
|
||||
displayStrings: []string{
|
||||
fmt.Sprintf("%s → %s", checkedOutBranch.Name, selectedBranch.Name),
|
||||
},
|
||||
onPress: func() error {
|
||||
return createPullRequest(checkedOutBranch, selectedBranch, gui)
|
||||
},
|
||||
})
|
||||
fromToDisplayStrings := func(from string, to string) []string {
|
||||
return []string{fmt.Sprintf("%s → %s", from, to)}
|
||||
}
|
||||
|
||||
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)
|
||||
}},
|
||||
)
|
||||
},
|
||||
})
|
||||
menuItemsForBranch := func(branch *models.Branch) []*menuItem {
|
||||
return []*menuItem{
|
||||
{
|
||||
displayStrings: fromToDisplayStrings(branch.Name, gui.Tr.DefaultBranch),
|
||||
onPress: func() error {
|
||||
return gui.createPullRequest(branch.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)
|
||||
}},
|
||||
)
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
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})
|
||||
}
|
||||
|
||||
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 {
|
||||
|
@ -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",
|
||||
|
Loading…
x
Reference in New Issue
Block a user