From 9ad1a0e9d89c56b81a98943b615616d0ef07d3ff Mon Sep 17 00:00:00 2001 From: phanium Date: Fri, 13 Dec 2024 12:27:12 +0800 Subject: [PATCH] Add user config `git.preferRemotes` to `OpenInBrowser` on specified remotes --- docs/Config.md | 4 ++++ pkg/commands/git_commands/remote.go | 7 ++++++- pkg/config/user_config.go | 3 +++ pkg/gui/controllers/helpers/host_helper.go | 15 ++++++++++----- schema/config.json | 11 +++++++++++ 5 files changed, 34 insertions(+), 6 deletions(-) diff --git a/docs/Config.md b/docs/Config.md index e791c257955..37d7ee2e930 100644 --- a/docs/Config.md +++ b/docs/Config.md @@ -305,6 +305,10 @@ git: - master - main + # Prefer to specified remote repositories, E.g. when `OpenInBrowser` + preferRemotes: + - origin + # Prefix to use when skipping hooks. E.g. if set to 'WIP', then pre-commit hooks will be skipped when the commit message starts with 'WIP' skipHookPrefix: WIP diff --git a/pkg/commands/git_commands/remote.go b/pkg/commands/git_commands/remote.go index ca361067976..7f37caad9e0 100644 --- a/pkg/commands/git_commands/remote.go +++ b/pkg/commands/git_commands/remote.go @@ -1,6 +1,7 @@ package git_commands import ( + "errors" "fmt" "strings" @@ -85,5 +86,9 @@ func (self *RemoteCommands) GetRemoteURL(remoteName string) (string, error) { ToArgv() url, err := self.cmd.New(cmdArgs).RunWithOutput() - return strings.TrimSpace(url), err + url = strings.TrimSpace(url) + if url == remoteName { + return "", errors.New("Remote not found") + } + return url, err } diff --git a/pkg/config/user_config.go b/pkg/config/user_config.go index 1148bb94721..84b3a88e6c4 100644 --- a/pkg/config/user_config.go +++ b/pkg/config/user_config.go @@ -224,6 +224,8 @@ type GitConfig struct { Merging MergingConfig `yaml:"merging"` // list of branches that are considered 'main' branches, used when displaying commits MainBranches []string `yaml:"mainBranches" jsonschema:"uniqueItems=true"` + // Prefer to specified remote repositories, E.g. when `OpenInBrowser` + PreferRemotes []string `yaml:"preferRemotes" jsonschema:"uniqueItems=true"` // Prefix to use when skipping hooks. E.g. if set to 'WIP', then pre-commit hooks will be skipped when the commit message starts with 'WIP' SkipHookPrefix string `yaml:"skipHookPrefix"` // If true, periodically fetch from remote @@ -765,6 +767,7 @@ func GetDefaultConfig() *UserConfig { ShowGraph: "always", ShowWholeGraph: false, }, + PreferRemotes: []string{"origin"}, SkipHookPrefix: "WIP", MainBranches: []string{"master", "main"}, AutoFetch: true, diff --git a/pkg/gui/controllers/helpers/host_helper.go b/pkg/gui/controllers/helpers/host_helper.go index ab8631d3618..06f4bed3086 100644 --- a/pkg/gui/controllers/helpers/host_helper.go +++ b/pkg/gui/controllers/helpers/host_helper.go @@ -42,10 +42,15 @@ func (self *HostHelper) GetCommitURL(commitHash string) (string, error) { // getting this on every request rather than storing it in state in case our remoteURL changes // from one invocation to the next. func (self *HostHelper) getHostingServiceMgr() (*hosting_service.HostingServiceMgr, error) { - remoteUrl, err := self.c.Git().Remote.GetRemoteURL("origin") - if err != nil { - return nil, err + remotes := self.c.UserConfig().Git.PreferRemotes + var err error + var remoteUrl string + for _, remote := range remotes { + remoteUrl, err = self.c.Git().Remote.GetRemoteURL(remote) + if err == nil { + configServices := self.c.UserConfig().Services + return hosting_service.NewHostingServiceMgr(self.c.Log, self.c.Tr, remoteUrl, configServices), nil + } } - configServices := self.c.UserConfig().Services - return hosting_service.NewHostingServiceMgr(self.c.Log, self.c.Tr, remoteUrl, configServices), nil + return nil, err } diff --git a/schema/config.json b/schema/config.json index ee5726740c1..1f2688e98c0 100644 --- a/schema/config.json +++ b/schema/config.json @@ -573,6 +573,17 @@ "main" ] }, + "preferRemotes": { + "items": { + "type": "string" + }, + "type": "array", + "uniqueItems": true, + "description": "Prefer to specified remote repositories, E.g. when `OpenInBrowser`", + "default": [ + "origin" + ] + }, "skipHookPrefix": { "type": "string", "description": "Prefix to use when skipping hooks. E.g. if set to 'WIP', then pre-commit hooks will be skipped when the commit message starts with 'WIP'",