2022-01-08 05:00:36 +02:00
|
|
|
package git_commands
|
2020-09-29 12:03:39 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2022-01-07 06:07:35 +02:00
|
|
|
gogit "github.com/jesseduffield/go-git/v5"
|
|
|
|
"github.com/jesseduffield/go-git/v5/config"
|
2022-01-02 01:34:33 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/commands/git_config"
|
|
|
|
"github.com/jesseduffield/lazygit/pkg/common"
|
2020-09-29 12:03:39 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/utils"
|
|
|
|
)
|
|
|
|
|
2022-01-02 01:34:33 +02:00
|
|
|
type ConfigCommands struct {
|
|
|
|
*common.Common
|
|
|
|
|
|
|
|
gitConfig git_config.IGitConfig
|
2022-01-07 06:07:35 +02:00
|
|
|
repo *gogit.Repository
|
2022-01-02 01:34:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewConfigCommands(
|
|
|
|
common *common.Common,
|
|
|
|
gitConfig git_config.IGitConfig,
|
2022-01-08 05:44:07 +02:00
|
|
|
repo *gogit.Repository,
|
2022-01-02 01:34:33 +02:00
|
|
|
) *ConfigCommands {
|
|
|
|
return &ConfigCommands{
|
|
|
|
Common: common,
|
|
|
|
gitConfig: gitConfig,
|
2022-01-08 06:51:15 +02:00
|
|
|
repo: repo,
|
2022-01-02 01:34:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *ConfigCommands) ConfiguredPager() string {
|
2020-09-29 12:03:39 +02:00
|
|
|
if os.Getenv("GIT_PAGER") != "" {
|
|
|
|
return os.Getenv("GIT_PAGER")
|
|
|
|
}
|
|
|
|
if os.Getenv("PAGER") != "" {
|
|
|
|
return os.Getenv("PAGER")
|
|
|
|
}
|
2022-01-02 01:34:33 +02:00
|
|
|
output := self.gitConfig.Get("core.pager")
|
2021-10-23 00:52:19 +02:00
|
|
|
return strings.Split(output, "\n")[0]
|
2020-09-29 12:03:39 +02:00
|
|
|
}
|
|
|
|
|
2022-01-02 01:34:33 +02:00
|
|
|
func (self *ConfigCommands) GetPager(width int) string {
|
|
|
|
useConfig := self.UserConfig.Git.Paging.UseConfig
|
2020-09-29 12:03:39 +02:00
|
|
|
if useConfig {
|
2022-01-02 01:34:33 +02:00
|
|
|
pager := self.ConfiguredPager()
|
2020-09-29 12:03:39 +02:00
|
|
|
return strings.Split(pager, "| less")[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
templateValues := map[string]string{
|
|
|
|
"columnWidth": strconv.Itoa(width/2 - 6),
|
|
|
|
}
|
|
|
|
|
2022-01-02 01:34:33 +02:00
|
|
|
pagerTemplate := self.UserConfig.Git.Paging.Pager
|
2020-09-29 12:03:39 +02:00
|
|
|
return utils.ResolvePlaceholderString(pagerTemplate, templateValues)
|
|
|
|
}
|
|
|
|
|
2021-04-10 03:40:42 +02:00
|
|
|
// 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
|
2022-01-02 01:34:33 +02:00
|
|
|
func (self *ConfigCommands) UsingGpg() bool {
|
|
|
|
overrideGpg := self.UserConfig.Git.OverrideGpg
|
2021-04-10 03:40:42 +02:00
|
|
|
if overrideGpg {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-01-02 01:34:33 +02:00
|
|
|
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"
|
2021-04-10 03:40:42 +02:00
|
|
|
}
|
2022-01-07 06:07:35 +02:00
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
2022-01-07 10:56:33 +02:00
|
|
|
|
|
|
|
func (self *ConfigCommands) GetGitFlowPrefixes() string {
|
2022-01-07 11:17:23 +02:00
|
|
|
return self.gitConfig.GetGeneral("--local --get-regexp gitflow.prefix")
|
2022-01-07 10:56:33 +02:00
|
|
|
}
|
2023-07-02 06:03:16 +02:00
|
|
|
|
|
|
|
func (self *ConfigCommands) GetCoreCommentChar() byte {
|
|
|
|
if commentCharStr := self.gitConfig.Get("core.commentChar"); len(commentCharStr) == 1 {
|
|
|
|
return commentCharStr[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
return '#'
|
|
|
|
}
|
2023-07-29 18:15:10 +02:00
|
|
|
|
|
|
|
func (self *ConfigCommands) GetRebaseUpdateRefs() bool {
|
|
|
|
return self.gitConfig.GetBool("rebase.updateRefs")
|
|
|
|
}
|