summaryrefslogtreecommitdiffstats
path: root/pkg/commands/git_commands/config.go
diff options
context:
space:
mode:
authorJesse Duffield <jessedduffield@gmail.com>2022-01-08 14:00:36 +1100
committerJesse Duffield <jessedduffield@gmail.com>2022-01-09 14:09:53 +1100
commitc9a0cc6b30dca6ff6c520268c10afff4e99a68e9 (patch)
tree74a03be28aafb5fba5c8391ca611aeb25ca89445 /pkg/commands/git_commands/config.go
parent3621854dc79baf2064b00b561ebea0ecc8fcb5df (diff)
refactor
Diffstat (limited to 'pkg/commands/git_commands/config.go')
-rw-r--r--pkg/commands/git_commands/config.go99
1 files changed, 99 insertions, 0 deletions
diff --git a/pkg/commands/git_commands/config.go b/pkg/commands/git_commands/config.go
new file mode 100644
index 000000000..1818edc2d
--- /dev/null
+++ b/pkg/commands/git_commands/config.go
@@ -0,0 +1,99 @@
+package git_commands
+
+import (
+ "os"
+ "strconv"
+ "strings"
+
+ gogit "github.com/jesseduffield/go-git/v5"
+ "github.com/jesseduffield/go-git/v5/config"
+ "github.com/jesseduffield/lazygit/pkg/commands/git_config"
+ "github.com/jesseduffield/lazygit/pkg/common"
+ "github.com/jesseduffield/lazygit/pkg/utils"
+)
+
+type ConfigCommands struct {
+ *common.Common
+
+ gitConfig git_config.IGitConfig
+ repo *gogit.Repository
+}
+
+func NewConfigCommands(
+ common *common.Common,
+ gitConfig git_config.IGitConfig,
+) *ConfigCommands {
+ return &ConfigCommands{
+ Common: common,
+ gitConfig: gitConfig,
+ }
+}
+
+func (self *ConfigCommands) ConfiguredPager() string {
+ if os.Getenv("GIT_PAGER") != "" {
+ return os.Getenv("GIT_PAGER")
+ }
+ if os.Getenv("PAGER") != "" {
+ return os.Getenv("PAGER")
+ }
+ output := self.gitConfig.Get("core.pager")
+ return strings.Split(output, "\n")[0]
+}
+
+func (self *ConfigCommands) GetPager(width int) string {
+ useConfig := self.UserConfig.Git.Paging.UseConfig
+ if useConfig {
+ pager := self.ConfiguredPager()
+ return strings.Split(pager, "| less")[0]
+ }
+
+ templateValues := map[string]string{
+ "columnWidth": strconv.Itoa(width/2 - 6),
+ }
+
+ pagerTemplate := self.UserConfig.Git.Paging.Pager
+ return utils.ResolvePlaceholderString(pagerTemplate, templateValues)
+}
+
+// UsingGpg tells us whether the user has gpg enabled so that we can know
+// whether we need to run a subprocess to allow them to enter their password
+func (self *ConfigCommands) UsingGpg() bool {
+ overrideGpg := self.UserConfig.Git.OverrideGpg
+ if overrideGpg {
+ return false
+ }
+
+ return self.gitConfig.GetBool("commit.gpgsign")
+}
+
+func (self *ConfigCommands) GetCoreEditor() string {
+ return self.gitConfig.Get("core.editor")
+}
+
+// GetRemoteURL returns current repo remote url
+func (self *ConfigCommands) GetRemoteURL() string {
+ return self.gitConfig.Get("remote.origin.url")
+}
+
+func (self *ConfigCommands) GetShowUntrackedFiles() string {
+ return self.gitConfig.Get("status.showUntrackedFiles")
+}
+
+// this determines whether the user has configured to push to the remote branch of the same name as the current or not
+func (self *ConfigCommands) GetPushToCurrent() bool {
+ return self.gitConfig.Get("push.default") == "current"
+}
+
+// returns the repo's branches as specified in the git config
+func (self *ConfigCommands) Branches() (map[string]*config.Branch, error) {
+ conf, err := self.repo.Config()
+ if err != nil {
+ return nil, err
+ }
+
+ return conf.Branches, nil
+}
+
+func (self *ConfigCommands) GetGitFlowPrefixes() string {
+ return self.gitConfig.GetGeneral("--local --get-regexp gitflow.prefix")
+}