diff options
-rw-r--r-- | pkg/commands/git.go | 10 | ||||
-rw-r--r-- | pkg/commands/os.go | 15 | ||||
-rw-r--r-- | pkg/commands/os_default_platform.go | 1 | ||||
-rw-r--r-- | pkg/commands/pull_request.go | 14 | ||||
-rw-r--r-- | pkg/commands/pull_request_test.go | 2 | ||||
-rw-r--r-- | pkg/config/config_default_platform.go | 3 | ||||
-rw-r--r-- | pkg/config/config_linux.go | 3 | ||||
-rw-r--r-- | pkg/config/config_windows.go | 3 | ||||
-rw-r--r-- | pkg/i18n/dutch.go | 3 | ||||
-rw-r--r-- | pkg/i18n/english.go | 3 | ||||
-rw-r--r-- | pkg/i18n/polish.go | 3 |
11 files changed, 49 insertions, 11 deletions
diff --git a/pkg/commands/git.go b/pkg/commands/git.go index 9764d75d7..bb4f28b75 100644 --- a/pkg/commands/git.go +++ b/pkg/commands/git.go @@ -561,6 +561,16 @@ func (c *GitCommand) GetRemoteURL() string { return utils.TrimTrailingNewline(url) } +// CheckRemoteBranchExists Returns remote branch +func (c *GitCommand) CheckRemoteBranchExists(branch *Branch) bool { + _, err := c.OSCommand.RunCommandWithOutput(fmt.Sprintf( + "git show-ref --verify -- refs/remotes/origin/%s", + branch.Name, + )) + + return err == nil +} + // Diff returns the diff of a file func (c *GitCommand) Diff(file *File) string { cachedArg := "" diff --git a/pkg/commands/os.go b/pkg/commands/os.go index c8ca40f29..2caedf07d 100644 --- a/pkg/commands/os.go +++ b/pkg/commands/os.go @@ -8,9 +8,7 @@ import ( "github.com/jesseduffield/lazygit/pkg/config" "github.com/jesseduffield/lazygit/pkg/utils" - "github.com/mgutz/str" - "github.com/sirupsen/logrus" gitconfig "github.com/tcnksm/go-gitconfig" ) @@ -22,6 +20,7 @@ type Platform struct { shellArg string escapedQuote string openCommand string + openLinkCommand string fallbackEscapedQuote string } @@ -110,6 +109,18 @@ func (c *OSCommand) OpenFile(filename string) error { return err } +// OpenFile opens a file with the given +func (c *OSCommand) OpenLink(link string) error { + commandTemplate := c.Config.GetUserConfig().GetString("os.openLinkCommand") + templateValues := map[string]string{ + "link": c.Quote(link), + } + + command := utils.ResolvePlaceholderString(commandTemplate, templateValues) + err := c.RunCommand(command) + return err +} + // EditFile opens a file in a subprocess using whatever editor is available, // falling back to core.editor, VISUAL, EDITOR, then vi func (c *OSCommand) EditFile(filename string) (*exec.Cmd, error) { diff --git a/pkg/commands/os_default_platform.go b/pkg/commands/os_default_platform.go index 7b063417b..73e453b6b 100644 --- a/pkg/commands/os_default_platform.go +++ b/pkg/commands/os_default_platform.go @@ -13,6 +13,7 @@ func getPlatform() *Platform { shellArg: "-c", escapedQuote: "'", openCommand: "open {{filename}}", + openLinkCommand: "open {{link}}", fallbackEscapedQuote: "\"", } } diff --git a/pkg/commands/pull_request.go b/pkg/commands/pull_request.go index bd076d877..043a3c07d 100644 --- a/pkg/commands/pull_request.go +++ b/pkg/commands/pull_request.go @@ -3,7 +3,6 @@ package commands import ( "errors" "fmt" - "regexp" "strings" ) @@ -53,6 +52,12 @@ func NewPullRequest(gitCommand *GitCommand) *PullRequest { // Create opens link to new pull request in browser func (pr *PullRequest) Create(branch *Branch) error { + branchExistsOnRemote := pr.GitCommand.CheckRemoteBranchExists(branch) + + if !branchExistsOnRemote { + return errors.New(pr.GitCommand.Tr.SLocalize("NoBranchOnRemote")) + } + repoURL := pr.GitCommand.GetRemoteURL() var gitService *Service @@ -69,19 +74,18 @@ func (pr *PullRequest) Create(branch *Branch) error { repoInfo := getRepoInfoFromURL(repoURL) - return pr.GitCommand.OSCommand.OpenFile(fmt.Sprintf( + return pr.GitCommand.OSCommand.OpenLink(fmt.Sprintf( gitService.PullRequestURL, repoInfo.Owner, repoInfo.Repository, branch.Name, )) } func getRepoInfoFromURL(url string) *RepoInformation { isHTTP := strings.HasPrefix(url, "http") - removeGitExtension := regexp.MustCompile(`\.git$`) if isHTTP { splits := strings.Split(url, "/") owner := splits[len(splits)-2] - repo := removeGitExtension.ReplaceAllString(splits[len(splits)-1], "") + repo := strings.TrimSuffix(splits[len(splits)-1], ".git") return &RepoInformation{ Owner: owner, @@ -92,7 +96,7 @@ func getRepoInfoFromURL(url string) *RepoInformation { tmpSplit := strings.Split(url, ":") splits := strings.Split(tmpSplit[1], "/") owner := splits[0] - repo := removeGitExtension.ReplaceAllString(splits[1], "") + repo := strings.TrimSuffix(splits[1], ".git") return &RepoInformation{ Owner: owner, diff --git a/pkg/commands/pull_request_test.go b/pkg/commands/pull_request_test.go index 111845e6d..a551ee081 100644 --- a/pkg/commands/pull_request_test.go +++ b/pkg/commands/pull_request_test.go @@ -144,7 +144,7 @@ func TestCreatePullRequest(t *testing.T) { t.Run(s.testName, func(t *testing.T) { gitCommand := newDummyGitCommand() gitCommand.OSCommand.command = s.command - gitCommand.OSCommand.Config.GetUserConfig().Set("os.openCommand", "open {{filename}}") + gitCommand.OSCommand.Config.GetUserConfig().Set("os.openLinkCommand", "open {{link}}") dummyPullRequest := NewPullRequest(gitCommand) s.test(dummyPullRequest.Create(s.branch)) }) diff --git a/pkg/config/config_default_platform.go b/pkg/config/config_default_platform.go index f3c1a36e5..df205c0d7 100644 --- a/pkg/config/config_default_platform.go +++ b/pkg/config/config_default_platform.go @@ -6,5 +6,6 @@ package config func GetPlatformDefaultConfig() []byte { return []byte( `os: - openCommand: 'open {{filename}}'`) + openCommand: 'open {{filename}}' + openLinkCommand: 'open {{link}}'`) } diff --git a/pkg/config/config_linux.go b/pkg/config/config_linux.go index 90e922416..2dfbdb1c6 100644 --- a/pkg/config/config_linux.go +++ b/pkg/config/config_linux.go @@ -4,5 +4,6 @@ package config func GetPlatformDefaultConfig() []byte { return []byte( `os: - openCommand: 'sh -c "xdg-open {{filename}} >/dev/null"'`) + openCommand: 'sh -c "xdg-open {{filename}} >/dev/null"' + openLinkCommand: 'sh -c "xdg-open {{link}} >/dev/null"'`) } diff --git a/pkg/config/config_windows.go b/pkg/config/config_windows.go index b81a5fdb5..6f6560316 100644 --- a/pkg/config/config_windows.go +++ b/pkg/config/config_windows.go @@ -4,5 +4,6 @@ package config func GetPlatformDefaultConfig() []byte { return []byte( `os: - openCommand: 'cmd /c "start "" {{filename}}"'`) + openCommand: 'cmd /c "start "" {{filename}}"' + openLinkCommand: 'cmd /c "start "" {{link}}"'`) } diff --git a/pkg/i18n/dutch.go b/pkg/i18n/dutch.go index c95b3a99c..74b230ab4 100644 --- a/pkg/i18n/dutch.go +++ b/pkg/i18n/dutch.go @@ -385,6 +385,9 @@ func addDutch(i18nObject *i18n.Bundle) error { }, &i18n.Message{ ID: "createPullRequest", Other: `maak een pull-aanvraag`, + }, &i18n.Message{ + ID: "NoBranchOnRemote", + Other: `Deze tak bestaat niet op de afstandsbediening. U moet eerst op de afstandsbediening drukken.`, }, ) } diff --git a/pkg/i18n/english.go b/pkg/i18n/english.go index a549c3d58..96c2031bf 100644 --- a/pkg/i18n/english.go +++ b/pkg/i18n/english.go @@ -408,6 +408,9 @@ func addEnglish(i18nObject *i18n.Bundle) error { }, &i18n.Message{ ID: "createPullRequest", Other: `create pull request`, + }, &i18n.Message{ + ID: "NoBranchOnRemote", + Other: `This branch doesn't exist on remote. You need to push it to remote first.`, }, ) } diff --git a/pkg/i18n/polish.go b/pkg/i18n/polish.go index e7003ca36..975035771 100644 --- a/pkg/i18n/polish.go +++ b/pkg/i18n/polish.go @@ -383,6 +383,9 @@ func addPolish(i18nObject *i18n.Bundle) error { }, &i18n.Message{ ID: "createPullRequest", Other: `utwórz żądanie wyciągnięcia`, + }, &i18n.Message{ + ID: "NoBranchOnRemote", + Other: `Ta gałąź nie istnieje na zdalnym. Najpierw musisz go odepchnąć na odległość.`, }, ) } |