From 6456eb19aed6b2e46886c45c867b718d7ca8cc68 Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Sun, 4 Feb 2024 22:42:58 +0100 Subject: [PATCH] WIP recursive submodules Entering submodules works, but other commands fail, so this is not good enough yet. --- pkg/commands/git_commands/submodule.go | 21 ++++++++++++++----- pkg/commands/git_commands/working_tree.go | 2 +- pkg/gui/controllers/helpers/refresh_helper.go | 2 +- 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/pkg/commands/git_commands/submodule.go b/pkg/commands/git_commands/submodule.go index d9d1ccd20b6..637104daec1 100644 --- a/pkg/commands/git_commands/submodule.go +++ b/pkg/commands/git_commands/submodule.go @@ -26,8 +26,8 @@ func NewSubmoduleCommands(gitCommon *GitCommon) *SubmoduleCommands { } } -func (self *SubmoduleCommands) GetConfigs() ([]*models.SubmoduleConfig, error) { - file, err := os.Open(".gitmodules") +func (self *SubmoduleCommands) GetConfigs(parentName string, parentPath string) ([]*models.SubmoduleConfig, error) { + file, err := os.Open(filepath.Join(parentPath, ".gitmodules")) if err != nil { if os.IsNotExist(err) { return nil, nil @@ -55,17 +55,28 @@ func (self *SubmoduleCommands) GetConfigs() ([]*models.SubmoduleConfig, error) { line := scanner.Text() if name, ok := firstMatch(line, `\[submodule "(.*)"\]`); ok { + if parentName != "" { + name = parentName + "/" + name + } configs = append(configs, &models.SubmoduleConfig{Name: name}) continue } if len(configs) > 0 { - lastConfig := configs[len(configs)-1] + lastConfigIdx := len(configs) - 1 if path, ok := firstMatch(line, `\s*path\s*=\s*(.*)\s*`); ok { - lastConfig.Path = path + if parentPath != "" { + configs[lastConfigIdx].Path = parentPath + "/" + path + } else { + configs[lastConfigIdx].Path = path + } + nestedConfigs, err := self.GetConfigs(configs[lastConfigIdx].Name, configs[lastConfigIdx].Path) + if err == nil { + configs = append(configs, nestedConfigs...) + } } else if url, ok := firstMatch(line, `\s*url\s*=\s*(.*)\s*`); ok { - lastConfig.Url = url + configs[lastConfigIdx].Url = url } } } diff --git a/pkg/commands/git_commands/working_tree.go b/pkg/commands/git_commands/working_tree.go index 054a272d4b9..481010f23bc 100644 --- a/pkg/commands/git_commands/working_tree.go +++ b/pkg/commands/git_commands/working_tree.go @@ -342,7 +342,7 @@ func (self *WorkingTreeCommands) RemoveUntrackedFiles() error { // ResetAndClean removes all unstaged changes and removes all untracked files func (self *WorkingTreeCommands) ResetAndClean() error { - submoduleConfigs, err := self.submodule.GetConfigs() + submoduleConfigs, err := self.submodule.GetConfigs("", "") if err != nil { return err } diff --git a/pkg/gui/controllers/helpers/refresh_helper.go b/pkg/gui/controllers/helpers/refresh_helper.go index a8f32d1162d..6fb38416ea0 100644 --- a/pkg/gui/controllers/helpers/refresh_helper.go +++ b/pkg/gui/controllers/helpers/refresh_helper.go @@ -413,7 +413,7 @@ func (self *RefreshHelper) refreshTags() error { } func (self *RefreshHelper) refreshStateSubmoduleConfigs() error { - configs, err := self.c.Git().Submodule.GetConfigs() + configs, err := self.c.Git().Submodule.GetConfigs("", "") if err != nil { return err }