diff options
Diffstat (limited to 'pkg/commands/submodules.go')
-rw-r--r-- | pkg/commands/submodules.go | 92 |
1 files changed, 54 insertions, 38 deletions
diff --git a/pkg/commands/submodules.go b/pkg/commands/submodules.go index 29a9ab700..fba676a07 100644 --- a/pkg/commands/submodules.go +++ b/pkg/commands/submodules.go @@ -10,6 +10,7 @@ import ( "github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/commands/oscommands" + "github.com/jesseduffield/lazygit/pkg/common" ) // .gitmodules looks like this: @@ -17,7 +18,22 @@ import ( // path = blah/mysubmodule // url = git@github.com:subbo.git -func (c *GitCommand) GetSubmoduleConfigs() ([]*models.SubmoduleConfig, error) { +type SubmoduleCommands struct { + *common.Common + + cmd oscommands.ICmdObjBuilder + dotGitDir string +} + +func NewSubmoduleCommands(common *common.Common, cmd oscommands.ICmdObjBuilder, dotGitDir string) SubmoduleCommands { + return SubmoduleCommands{ + Common: common, + cmd: cmd, + dotGitDir: dotGitDir, + } +} + +func (self *SubmoduleCommands) GetConfigs() ([]*models.SubmoduleConfig, error) { file, err := os.Open(".gitmodules") if err != nil { if os.IsNotExist(err) { @@ -63,36 +79,36 @@ func (c *GitCommand) GetSubmoduleConfigs() ([]*models.SubmoduleConfig, error) { return configs, nil } -func (c *GitCommand) SubmoduleStash(submodule *models.SubmoduleConfig) error { +func (self *SubmoduleCommands) Stash(submodule *models.SubmoduleConfig) error { // if the path does not exist then it hasn't yet been initialized so we'll swallow the error // because the intention here is to have no dirty worktree state if _, err := os.Stat(submodule.Path); os.IsNotExist(err) { - c.Log.Infof("submodule path %s does not exist, returning", submodule.Path) + self.Log.Infof("submodule path %s does not exist, returning", submodule.Path) return nil } - return c.Cmd.New("git -C " + c.Cmd.Quote(submodule.Path) + " stash --include-untracked").Run() + return self.cmd.New("git -C " + self.cmd.Quote(submodule.Path) + " stash --include-untracked").Run() } -func (c *GitCommand) SubmoduleReset(submodule *models.SubmoduleConfig) error { - return c.Cmd.New("git submodule update --init --force -- " + c.Cmd.Quote(submodule.Path)).Run() +func (self *SubmoduleCommands) Reset(submodule *models.SubmoduleConfig) error { + return self.cmd.New("git submodule update --init --force -- " + self.cmd.Quote(submodule.Path)).Run() } -func (c *GitCommand) SubmoduleUpdateAll() error { +func (self *SubmoduleCommands) UpdateAll() error { // not doing an --init here because the user probably doesn't want that - return c.Cmd.New("git submodule update --force").Run() + return self.cmd.New("git submodule update --force").Run() } -func (c *GitCommand) SubmoduleDelete(submodule *models.SubmoduleConfig) error { +func (self *SubmoduleCommands) Delete(submodule *models.SubmoduleConfig) error { // based on https://gist.github.com/myusuf3/7f645819ded92bda6677 - if err := c.Cmd.New("git submodule deinit --force -- " + c.Cmd.Quote(submodule.Path)).Run(); err != nil { + if err := self.cmd.New("git submodule deinit --force -- " + self.cmd.Quote(submodule.Path)).Run(); err != nil { if strings.Contains(err.Error(), "did not match any file(s) known to git") { - if err := c.Cmd.New("git config --file .gitmodules --remove-section submodule." + c.Cmd.Quote(submodule.Name)).Run(); err != nil { + if err := self.cmd.New("git config --file .gitmodules --remove-section submodule." + self.cmd.Quote(submodule.Name)).Run(); err != nil { return err } - if err := c.Cmd.New("git config --remove-section submodule." + c.Cmd.Quote(submodule.Name)).Run(); err != nil { + if err := self.cmd.New("git config --remove-section submodule." + self.cmd.Quote(submodule.Name)).Run(); err != nil { return err } @@ -102,69 +118,69 @@ func (c *GitCommand) SubmoduleDelete(submodule *models.SubmoduleConfig) error { } } - if err := c.Cmd.New("git rm --force -r " + submodule.Path).Run(); err != nil { + if err := self.cmd.New("git rm --force -r " + submodule.Path).Run(); err != nil { // if the directory isn't there then that's fine - c.Log.Error(err) + self.Log.Error(err) } - return os.RemoveAll(filepath.Join(c.DotGitDir, "modules", submodule.Path)) + return os.RemoveAll(filepath.Join(self.dotGitDir, "modules", submodule.Path)) } -func (c *GitCommand) SubmoduleAdd(name string, path string, url string) error { - return c.Cmd. +func (self *SubmoduleCommands) Add(name string, path string, url string) error { + return self.cmd. New( fmt.Sprintf( "git submodule add --force --name %s -- %s %s ", - c.Cmd.Quote(name), - c.Cmd.Quote(url), - c.Cmd.Quote(path), + self.cmd.Quote(name), + self.cmd.Quote(url), + self.cmd.Quote(path), )). Run() } -func (c *GitCommand) SubmoduleUpdateUrl(name string, path string, newUrl string) error { +func (self *SubmoduleCommands) UpdateUrl(name string, path string, newUrl string) error { // the set-url command is only for later git versions so we're doing it manually here - if err := c.Cmd.New("git config --file .gitmodules submodule." + c.Cmd.Quote(name) + ".url " + c.Cmd.Quote(newUrl)).Run(); err != nil { + if err := self.cmd.New("git config --file .gitmodules submodule." + self.cmd.Quote(name) + ".url " + self.cmd.Quote(newUrl)).Run(); err != nil { return err } - if err := c.Cmd.New("git submodule sync -- " + c.Cmd.Quote(path)).Run(); err != nil { + if err := self.cmd.New("git submodule sync -- " + self.cmd.Quote(path)).Run(); err != nil { return err } return nil } -func (c *GitCommand) SubmoduleInit(path string) error { - return c.Cmd.New("git submodule init -- " + c.Cmd.Quote(path)).Run() +func (self *SubmoduleCommands) Init(path string) error { + return self.cmd.New("git submodule init -- " + self.cmd.Quote(path)).Run() } -func (c *GitCommand) SubmoduleUpdate(path string) error { - return c.Cmd.New("git submodule update --init -- " + c.Cmd.Quote(path)).Run() +func (self *SubmoduleCommands) Update(path string) error { + return self.cmd.New("git submodule update --init -- " + self.cmd.Quote(path)).Run() } -func (c *GitCommand) SubmoduleBulkInitCmdObj() oscommands.ICmdObj { - return c.Cmd.New("git submodule init") +func (self *SubmoduleCommands) BulkInitCmdObj() oscommands.ICmdObj { + return self.cmd.New("git submodule init") } -func (c *GitCommand) SubmoduleBulkUpdateCmdObj() oscommands.ICmdObj { - return c.Cmd.New("git submodule update") +func (self *SubmoduleCommands) BulkUpdateCmdObj() oscommands.ICmdObj { + return self.cmd.New("git submodule update") } -func (c *GitCommand) SubmoduleForceBulkUpdateCmdObj() oscommands.ICmdObj { - return c.Cmd.New("git submodule update --force") +func (self *SubmoduleCommands) ForceBulkUpdateCmdObj() oscommands.ICmdObj { + return self.cmd.New("git submodule update --force") } -func (c *GitCommand) SubmoduleBulkDeinitCmdObj() oscommands.ICmdObj { - return c.Cmd.New("git submodule deinit --all --force") +func (self *SubmoduleCommands) BulkDeinitCmdObj() oscommands.ICmdObj { + return self.cmd.New("git submodule deinit --all --force") } -func (c *GitCommand) ResetSubmodules(submodules []*models.SubmoduleConfig) error { +func (self *SubmoduleCommands) ResetSubmodules(submodules []*models.SubmoduleConfig) error { for _, submodule := range submodules { - if err := c.SubmoduleStash(submodule); err != nil { + if err := self.Stash(submodule); err != nil { return err } } - return c.SubmoduleUpdateAll() + return self.UpdateAll() } |